<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>ma-x.im — Maksym Kuzmitskyi</title>
    <link>https://ma-x.im</link>
    <description>Thoughts on frontend architecture, React patterns, and building systems that scale.</description>
    <language>en</language>
    <atom:link href="https://ma-x.im/feed.xml" rel="self" type="application/rss+xml"/>
    <lastBuildDate>Fri, 06 Nov 2026 16:02:15 GMT</lastBuildDate>
    
    <item>
      <title>Read-Only Is Not the Same as Safe</title>
      <link>https://ma-x.im/blog/ai-read-only-data-boundaries</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-read-only-data-boundaries</guid>
      <description>Preventing writes protects one boundary. An agent may still read sensitive data, send context to services or expose it in outputs. I want those paths explicit.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-read-only-data-boundaries/cover.png" alt="Read-Only Is Not the Same as Safe" /></p>
<p>An agent cannot edit the repository. It can still read a log containing customer information and include it in a remote request or a published explanation, if its tools and data path allow that.</p>
<p>Nothing in that hypothetical example requires a file write.</p>
<p>I like read-only modes. They are useful restrictions, especially for review and investigation. I don&#39;t like treating the label as a complete security argument.</p>
<p>Integrity and confidentiality are different properties. Preventing a change to local files doesn&#39;t, by itself, establish who can see their contents or where those contents can travel.</p>
<h2>Start with the data path, not the mode name</h2>
<p>For an agent-assisted task, I would identify what can be read, which process or service receives it, and where outputs can appear.</p>
<p>That includes the model provider, local and remote tools, logs, review comments and any connected workspace. A tool named search may run locally or call a remote index. A browser may already be authenticated to services the task doesn&#39;t need.</p>
<p>The answer depends on the actual product, integration, account and organization policy. I wouldn&#39;t infer retention, training use, residency or access controls from the word enterprise or from a familiar brand name.</p>
<p>GitHub&#39;s <a href="https://docs.github.com/en/copilot/responsible-use/code-review">current application card</a> distinguishes data flows across its agent surfaces and explicitly discusses responsibility for a bring-your-own-provider configuration. That is a reason to inspect the selected path, not to assume every Copilot session sends data through the same route.</p>
<p>Read-only should be one property in that description. It shouldn&#39;t replace the description.</p>
<h2>Source code isn&#39;t the only sensitive input</h2>
<p>A repository can contain private fixtures, copied production responses or internal endpoint information. Development machines can also hold browser profiles, credential files and local service configuration outside the checkout.</p>
<p>I would avoid collecting all of that merely because a tool is capable of reading it. The task should establish a need for particular information, and the environment should enforce meaningful limits where possible.</p>
<p>The right question isn&#39;t whether the agent is generally trustworthy. It is whether this task requires this class of data under the approved handling rules.</p>
<p>For many investigations, a sanitized schema or a small synthetic fixture can answer the technical question. If real data is necessary, its use needs the appropriate authorization and controls. Replacing names in one field doesn&#39;t automatically anonymize an entire payload.</p>
<p>I would not ask someone to paste secrets into a chat so the agent can diagnose an authentication problem. A person can configure a credential through the intended secure mechanism without turning its value into task content.</p>
<h2>File access and network access are separate boundaries</h2>
<p>A process may have restricted writes and broad reads. It may also have an outbound network path. Those properties need to be examined together.</p>
<p>The <a href="https://code.visualstudio.com/docs/agents/run/security">VS Code security guide</a>, checked on October 23, 2026, explicitly separates sandbox behavior, permission checks and network configuration. It notes that outbound network access can remain enabled under its documented defaults and that tool paths and harnesses differ.</p>
<p>I am deliberately not presenting a generic setting that supposedly secures every session. A shell command, a built-in file tool and a remote MCP tool can pass through different controls.</p>
<p>Restricting a local process doesn&#39;t necessarily restrict a connected remote service. Blocking an arbitrary external host doesn&#39;t prevent disclosure to an allowed service that should not receive a particular payload.</p>
<p>Likewise, an allowed destination isn&#39;t permission to send every accessible file there. Access to a provider for legitimate inference still requires appropriate selection of the context supplied.</p>
<blockquote>
<p>A permitted route and a permitted payload are different decisions.</p>
</blockquote>
<p>This is where a read-only label becomes particularly incomplete. It says little about either one unless the product defines those properties explicitly.</p>
<h2>The agent can follow the task and still expose too much</h2>
<p>Prompt injection is one route to an undesirable action, but it is not required for every disclosure.</p>
<p>An agent honestly trying to explain a failing test may quote an entire response body when a status code and a sanitized field name would suffice. A reviewer may include a sensitive configuration value in an otherwise useful comment.</p>
<p>I would want output handling to consider necessity and destination, not only whether the conclusion is correct.</p>
<p>This also matters for task records. Preserving evidence does not require keeping raw credentials, complete private payloads or every retrieved document. Logs that improve observability can create another data store with its own access and retention obligations.</p>
<p>A link or bounded summary may be more appropriate than a copy. Sometimes neither is appropriate in the destination where the agent is about to write.</p>
<p>Read-only access to the source doesn&#39;t prevent a different tool from publishing the resulting text. The review of that publication path needs to consider what it contains.</p>
<h2>Approval needs enough information to be meaningful</h2>
<p>A request for permission to call a tool should identify the operation and relevant destination without displaying secrets in the prompt itself.</p>
<p>A person approving a broad capability for convenience may not be approving every later use of sensitive content. Long-lived approvals, inherited authentication and ambiguous tool descriptions can widen the practical boundary beyond the user&#39;s immediate intention.</p>
<p>I would prefer task-scoped access with an understandable lifetime. Where a product supports separate permissions for reading, processing and publishing, those should reflect the task rather than share one blanket decision by default.</p>
<p>That doesn&#39;t mean asking a person about every ordinary file. Approval fatigue can make a nominal checkpoint less useful. Enforced restrictions and clearly defined safe paths can reduce unnecessary prompts while preserving meaningful decisions.</p>
<p>The <a href="https://ma-x.im/blog/agent-playbook-guardrails">existing guardrails article</a> concerns control over agent behavior. Here the question is narrower: which data may cross which boundary, even during behavior that appears routine.</p>
<h2>Test the boundary without using real secrets</h2>
<p>For an authorized evaluation, I would use disposable files containing unique, harmless markers rather than actual credentials or customer records. Then check which reads, requests, logs and outputs contain those markers.</p>
<p>The setup should use a controlled environment and destinations approved for the test. The purpose is to observe the data path, not to try bypassing organization controls.</p>
<p>A successful fixture establishes something about the observed tools and configuration. It doesn&#39;t prove every model decision will minimize data or that every future integration inherits the same restrictions.</p>
<p>Denied operations deserve testing too. A tool should not silently retry through a more privileged route simply because its first attempt was blocked. An unavailable source should remain unavailable in the report, not become a reason to weaken the boundary.</p>
<p>I would also test what survives after the session: retained outputs, cached tool responses and published artifacts. Stopping the agent is not necessarily the same as removing information already sent or stored.</p>
<h2>Keep read-only useful by describing what it protects</h2>
<p>I don&#39;t want to discard a useful mode because it doesn&#39;t solve every problem. Preventing mutation can substantially reduce the risk of an investigation or review task.</p>
<p>I want the claim to remain specific: which writes are prevented, which reads remain possible, what network paths exist and which publication actions are available.</p>
<p>Once those are explicit, the team can decide whether the environment is appropriate for the data involved. Without them, safe becomes a feeling attached to a mode selector.</p>
<p>A read-only agent may be exactly the right tool. I would still ask what it can learn, who receives that knowledge and what remains after the task finishes.</p>
<p>When you approve read-only agent access, which data boundary do you verify separately? I&#39;d be interested in the practical checks that made the mode&#39;s actual guarantees clear.</p>
]]></content:encoded>
      <pubDate>Fri, 06 Nov 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Security</category>
      <category>Privacy</category>
      <category>Developer Tooling</category>
    </item>
    <item>
      <title>You Didn&apos;t Write It. You Still Have to Maintain It.</title>
      <link>https://ma-x.im/blog/ai-generated-code-maintenance</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-generated-code-maintenance</guid>
      <description>Generated code can be correct and still be expensive to change. I care whether the next engineer can explain its boundaries without reconstructing the prompt.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-generated-code-maintenance/cover.png" alt="You Didn&apos;t Write It. You Still Have to Maintain It." /></p>
<p>The feature works. The tests pass. The agent can explain every file it created.</p>
<p>Now ask another engineer to change one business rule without the original conversation.</p>
<p>That is the test I care about after the demonstration. Not whether a human typed the code, but whether the resulting system can be understood and changed by the people responsible for it.</p>
<p>Generated code doesn&#39;t need a special moral category. It needs an ordinary maintenance owner. The prompt isn&#39;t that owner, and the model being able to regenerate an explanation isn&#39;t a substitute for one.</p>
<h2>Understanding is not remembering every line</h2>
<p>I wouldn&#39;t require an engineer to memorize generated code before accepting it. We don&#39;t apply that standard to dependencies or to every change written by a colleague.</p>
<p>I would expect someone to understand the important boundaries: where state is authoritative, where input becomes trusted, which contracts are shared, how failures surface and what a future change is likely to affect.</p>
<p>GitHub&#39;s <a href="https://docs.github.com/en/copilot/responsible-use/code-review">current application card</a> explicitly calls for reviewing and testing generated code. I see that as an acceptance floor, not a complete account of whether another engineer can maintain the result.</p>
<p>That understanding can come from code structure, tests, documentation and exploration. It doesn&#39;t require manual authorship.</p>
<p>The concern begins when the implementation can only be explained through the agent that produced it. A maintainer asks why a cache exists, which consumer requires an adapter, or whether a retry can repeat a write, and the answer is to start another conversation hoping the model reconstructs the original decision.</p>
<p>The model may produce a persuasive explanation. Without supporting evidence, it may also produce a new justification for an old artifact.</p>
<h2>Local plausibility can accumulate into a confusing system</h2>
<p>An agent can reasonably add a helper for one task, another wrapper for the next, and an extra configuration layer to keep the third change small.</p>
<p>Each PR can look tidy on its own. The combined codebase can acquire several ways to perform the same operation, with no clear reason to choose among them.</p>
<p>This is a hypothetical pattern, not a report of a particular employer&#39;s repository. It is also familiar without AI. More convenient generation can make local additions cheaper while leaving their long-term coordination cost intact.</p>
<p>OpenAI&#39;s <a href="https://openai.com/index/harness-engineering/">2026 harness-engineering report</a> describes agents reproducing existing patterns, including uneven ones, and discusses mechanical constraints and cleanup in its own experiment. It also explicitly leaves long-term architectural coherence as an open question.</p>
<p>I take that as a reason to inspect accumulating structure, not evidence that every generated system inevitably decays or that a recurring cleanup agent solves maintenance universally.</p>
<p>The important question is what the next change will copy. A convenient local pattern can become an accidental standard simply because it is visible.</p>
<h2>A useful abstraction should make the next decision easier</h2>
<p>I am not against introducing an abstraction with AI assistance. I want its existence to reduce complexity that actually matters.</p>
<p>For an illustrative payment flow, an adapter separating provider responses from application state can be a clear boundary. Several generic layers that forward the same parameters may only make the behavior harder to find.</p>
<p>The review question isn&#39;t whether the abstraction looks sophisticated. It is which variation, dependency or invariant it isolates, and where that value can be observed.</p>
<p>I would ask for a concrete next change. Add a provider error, change a validation rule or remove a consumer. Which files move, and which parts should remain untouched?</p>
<p>This doesn&#39;t predict every future requirement. It tests whether the explanation of the current design connects to an ordinary modification rather than a diagram alone.</p>
<p>The same standard applies to hand-written code. AI doesn&#39;t make architectural taste objective, but it makes it particularly easy to generate more structure before deciding whether the structure earns its place.</p>
<h2>Preserve reasons that aren&#39;t visible in the code</h2>
<p>A good name explains what a function does. It may not explain why an older implementation still exists or why a seemingly simpler migration was deliberately postponed.</p>
<p>Those decisions deserve a small durable record when they affect future work. Link the relevant requirement or design decision, identify the remaining constraint, and keep the record close enough to be discovered.</p>
<p>I don&#39;t want a transcript of every rejected alternative. I want the few facts a future maintainer would otherwise have to guess.</p>
<p>That is consistent with <a href="https://ma-x.im/blog/agent-playbook-context-is-the-product">keeping repository context useful</a>: context is an engineering artifact with ownership and freshness, not an unlimited pile of prose.</p>
<p>An agent-written explanation still needs review. It can describe the intended design while the implementation differs, or turn an inference into a historical claim. Documentation should point to evidence and make uncertain reasons visible.</p>
<p>When a constraint changes, update or remove the obsolete explanation. A permanent paragraph defending a temporary workaround can become part of the problem.</p>
<h2>Tests should explain behavior, not freeze incidental shape</h2>
<p>Tests are part of maintenance because they tell the next engineer what must remain true.</p>
<p>A suite dominated by snapshots of internal structure can make a straightforward refactor feel dangerous without explaining the product invariant. A suite of overly broad mocks can give the opposite impression: every change looks safe because the meaningful interaction is replaced.</p>
<p>Neither snapshots nor mocks are inherently wrong. Their value depends on the question they preserve.</p>
<p>I would prefer a future maintainer to see what a denied operation must do, which retry is permitted, or how a state transition handles interruption. Those are constraints that help a change stay local.</p>
<p>The existing <a href="https://ma-x.im/blog/react-playbook-testing">React testing article</a> discusses choosing checks by what they establish. The maintenance angle is what those checks teach someone who didn&#39;t participate in the implementation.</p>
<p>A test that merely agrees with generated code can preserve the wrong assumption. A well-founded test can make that assumption challengeable without requiring the original prompt.</p>
<h2>Try a handoff change before accepting a large structure</h2>
<p>For a consequential generated subsystem, I would give another engineer a small realistic modification. Ask them to identify the owning behavior, explain the relevant invariant and make the change using the available repository evidence.</p>
<p>This is a proposed acceptance exercise, not a measured experiment from my work. It shouldn&#39;t become compulsory theater for a two-line fix.</p>
<p>The useful observations are where the person has to guess, which explanation doesn&#39;t match the code, and whether an unrelated change becomes necessary just to touch the intended behavior.</p>
<p>Another agent can attempt the exercise too, but its success doesn&#39;t automatically establish human maintainability. It may tolerate a structure that a team cannot reasonably review or operate.</p>
<p>I also wouldn&#39;t require every handoff to produce exactly the same implementation. Different valid solutions can preserve the same contract. The goal is intelligibility and controlled change, not reproducing the author&#39;s path.</p>
<h2>Ownership includes deciding not to regenerate</h2>
<p>When a generated component becomes confusing, asking for a complete rewrite can be tempting. It can also replace one poorly understood system with another and make the previous review evidence obsolete.</p>
<p>I would first identify the concrete maintenance problem: unclear ownership, duplicated behavior, an unnecessary dependency or a test that prevents safe change. Then repair that boundary with proportionate scope.</p>
<p>Sometimes replacement is justified. It still needs a migration and verification plan, not just a cleaner-looking output.</p>
<p>I want AI assistance to reduce the cost of working on a system, not merely the cost of producing its first version. That means judging generated code by the work it leaves behind as well as the work it completes.</p>
<p>The next engineer shouldn&#39;t need the same model, the same prompt and the same conversation to make a safe change. They should have a codebase that explains enough of itself.</p>
<p>What would you ask someone else to change before accepting a substantial AI-generated implementation? I&#39;d be interested in the smallest handoff exercise that reveals whether the design is genuinely maintainable.</p>
]]></content:encoded>
      <pubDate>Wed, 04 Nov 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>Maintainability</category>
      <category>Engineering Workflow</category>
    </item>
    <item>
      <title>The Agent Knows the Library. Just Not Your Version.</title>
      <link>https://ma-x.im/blog/ai-library-version-mismatch</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-library-version-mismatch</guid>
      <description>A plausible API can belong to another release, runtime or feature mode. I want agent advice checked against the package and configuration the project uses.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-library-version-mismatch/cover.png" alt="The Agent Knows the Library. Just Not Your Version." /></p>
<p>The suggested API is real. The documentation example is valid. The project still cannot use it.</p>
<p>I find that more interesting than a completely invented method name. A believable solution can belong to a different version, package entry point, runtime or feature mode. It survives a quick recognition check because an engineer has seen something similar before.</p>
<p>The agent may know the library. The task requires it to know the library as installed and configured here.</p>
<h2>Familiarity is not a compatibility check</h2>
<p>A package name is a broad label. Several major versions can coexist in active projects, and a migration guide can describe a destination rather than the current state.</p>
<p>Even a matching version number may not settle the question. A package can expose different browser and server entry points. A feature may require configuration. A workspace package may resolve to a local artifact rather than the registry version the agent searched for.</p>
<p>This isn&#39;t a reason to distrust documentation. It is a reason to identify which documentation applies.</p>
<p>I would first establish the relevant installed package, its resolution from the consuming application and the configuration that changes its behavior. Only then would I decide whether the proposed example is evidence or inspiration.</p>
<h2>Inspect what the application resolves</h2>
<p>For a Node-based project, an inspection script placed next to the application&#39;s package.json can make the question concrete:</p>
<pre><code class="language-javascript">import { createRequire } from &#39;node:module&#39;;

const requireFromApp = createRequire(new URL(&#39;./package.json&#39;, import.meta.url));
const packagePath = requireFromApp.resolve(&#39;next/package.json&#39;);
const installedPackage = requireFromApp(&#39;next/package.json&#39;);

console.log(JSON.stringify({ packagePath, version: installedPackage.version }, null, 2));
</code></pre>
<p>This example checks Next.js metadata through Node&#39;s resolution from that application location. It doesn&#39;t inspect every conditional runtime export. Some packages don&#39;t expose package.json through their exports, so the same inspection method isn&#39;t universal.</p>
<p>The important part is the starting point. Resolving a package from the monorepo root may answer a different question from resolving it inside the application that imports it.</p>
<p>For a browser bundle, I would also inspect the relevant bundler configuration or output when aliasing and conditional resolution matter. A convenient Node probe isn&#39;t proof of what every runtime loads.</p>
<p>A manifest range describes intent. An actual resolution tells us more about the artifact being examined.</p>
<h2>Current documentation can describe more than one model</h2>
<p>Next.js provides a useful present example without needing a claim that its framework is unusually confusing. Its <a href="https://nextjs.org/docs/app/guides/caching-without-cache-components">caching guide for projects without Cache Components</a>, checked on October 23, 2026, explicitly identifies itself as the previous model and distinguishes the Cache Components path introduced in version 16.</p>
<p>That means searching for caching guidance isn&#39;t enough. The agent needs to establish the project&#39;s version and selected mode before transferring advice.</p>
<p>I am not upgrading this site or recommending one caching mode in this article. The point is that two official explanations can be correct while applying to different configurations.</p>
<p>The same pattern appears with router modes, compiler settings and server/client boundaries. Advice from a new guide may require a migration that is outside the current task.</p>
<p>An agent should not quietly perform that migration merely to make its preferred example fit. Discovering that a solution requires an upgrade is useful output. It is not automatic authorization to upgrade.</p>
<h2>Types narrow the question, but don&#39;t finish it</h2>
<p>A compiler rejecting an unfamiliar option is valuable feedback. Removing the error with a cast doesn&#39;t establish that the runtime supports the option.</p>
<p>I would inspect the installed declaration and the exported implementation around the disputed boundary. A type can be stale, overly permissive or describe an entry point different from the one actually used.</p>
<p>Likewise, a compiler accepting an option doesn&#39;t prove its intended behavioral effect. Some configuration paths tolerate extra input; some features are available only under particular runtime conditions.</p>
<p>The next check should discriminate between those possibilities. That might be a narrow runtime probe, a version-matched example or a test through the actual deployment adapter. Re-running the whole suite without exercising the disputed behavior may add time without resolving the uncertainty.</p>
<p>This is one place where I would rather have an agent say the evidence is incomplete than confidently widen a type until the build becomes green.</p>
<h2>Upgrade instructions are not current project instructions</h2>
<p>A migration document often says to replace one function with another or remove an old configuration field. Those instructions are conditional on performing the migration.</p>
<p>If the task is a bug fix on the existing release, copying the destination&#39;s code can create an accidental mixed-version implementation. It may even look cleaner than the valid old API.</p>
<p>I would keep the task&#39;s compatibility target explicit. Are we fixing behavior on the current version, preparing a migration, or completing an approved upgrade? Each has a different acceptable diff.</p>
<p>Related packages need attention too. A new plugin, shared component package or framework adapter may have peer requirements that the application doesn&#39;t satisfy. Satisfying one import doesn&#39;t establish a supported dependency set.</p>
<p>The versioned <a href="https://docs.npmjs.com/cli/v11/commands/npm-ci">npm ci documentation</a> describes frozen installation and configuration requirements for that CLI line. It helps explain why a manifest edit, lockfile resolution and installation can diverge. It is not permission to apply npm 11-specific behavior to another package manager without checking.</p>
<h2>Keep retrieval attached to the compatibility question</h2>
<p>I would search with the package version, relevant mode and disputed symbol rather than ask broadly how to use the library. Then I would compare the retrieved claim with the installed artifact.</p>
<p>If an MCP documentation tool exposes version selection, use it deliberately. If it returns only current documentation, retain that limitation. A successful tool call doesn&#39;t establish that the returned guide matches the consuming application.</p>
<p>Pinned documentation can age too. A project may have upgraded while its instructions still point to an earlier guide. Repository memory is useful for locating the intended source, not for overriding current package evidence.</p>
<p>The <a href="https://ma-x.im/blog/agent-playbook-instructions-rot">instructions-upkeep article</a> concerns rules that stop describing the project. Dependency advice can fail in the same quiet way: the sentence remains plausible while its compatibility conditions have changed.</p>
<h2>A useful answer includes the boundary</h2>
<p>When an agent recommends an API, I want to know which package, version, entry point and configuration support the recommendation. The detail should match the risk; a familiar stable helper doesn&#39;t require a research report.</p>
<p>When compatibility is uncertain, a small verified alternative may be better than pulling the whole project toward the newest documentation. When an upgrade is genuinely necessary, make it a deliberate proposal with its own validation.</p>
<p>I don&#39;t want an agent that knows fewer libraries. I want one that can stop recognizing and start checking when recognition is no longer enough.</p>
<p>Which version-dependent mistakes are hardest to catch in your AI-assisted work: APIs from another release, features behind configuration, or packages resolved from the wrong workspace? I&#39;d be interested in the smallest check that reliably settles the question for you.</p>
]]></content:encoded>
      <pubDate>Mon, 02 Nov 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>TypeScript</category>
      <category>Dependencies</category>
      <category>Developer Tooling</category>
    </item>
    <item>
      <title>&quot;Works on My Machine&quot; Now Includes the Agent&apos;s Machine</title>
      <link>https://ma-x.im/blog/ai-agent-environment-reproducibility</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-agent-environment-reproducibility</guid>
      <description>An agent can validate a change in an environment nobody else can reproduce. I want the handoff to explain the runtime, dependencies and state behind the result.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-agent-environment-reproducibility/cover.png" alt="&quot;Works on My Machine&quot; Now Includes the Agent&apos;s Machine" /></p>
<p>The agent ran the test. The result was green. Another engineer checks out the branch and cannot get the application to start.</p>
<p>That hypothetical handoff doesn&#39;t require the model to have lied. It may have accurately described a result from an environment the next person does not have.</p>
<p>I think the useful question is what made that result possible: runtime, installed artifacts, configuration, test data, processes and state. A commit identifies code. It doesn&#39;t identify all of those inputs.</p>
<p>AI-assisted development doesn&#39;t invent this old problem. It adds more environments that can appear, validate work and disappear before a teammate sees them.</p>
<h2>A worktree isn&#39;t a complete environment</h2>
<p>A separate Git worktree is useful for isolating source changes. It doesn&#39;t automatically isolate ports, databases, home-directory configuration, credentials or services shared by several tasks.</p>
<p>Two branches can be separate on disk while their tests talk to the same local service. One session can migrate a database the other expected to remain unchanged. A process started earlier can serve a different revision from the files currently being inspected.</p>
<p>The agent may see a healthy page without establishing which server produced it.</p>
<p>I would identify the actual application instance, its revision and its relevant configuration before using an interaction as evidence. That can be a build identifier, an explicit process path or another practical observation. It need not become a ceremony after every refresh.</p>
<p>The <a href="https://docs.github.com/en/copilot/responsible-use/code-review">current GitHub application card</a> describes ephemeral environments for its cloud agent and distinguishes them from local execution. Those are meaningful differences in lifecycle and access. They are not a promise that every tool called an agent runs in the same reproducible container.</p>
<h2>The lockfile is necessary evidence, not the whole story</h2>
<p>A manifest expresses requested dependencies. The lockfile records a more specific resolution. The installation on disk is the artifact the process actually loads.</p>
<p>Those can disagree because of a stale installation, workspace linking, local tarballs or generated output. An agent can make a correct diagnosis of the code it loaded while the committed project still produces something else.</p>
<p>The versioned <a href="https://docs.npmjs.com/cli/v11/commands/npm-ci">npm 11 documentation for npm ci</a> makes its frozen-install behavior explicit: it requires a lockfile, rejects manifest/lockfile disagreement and doesn&#39;t rewrite those inputs. It also says tree-shaping flags used when creating the lockfile must be supplied consistently.</p>
<p>I wouldn&#39;t present that as a universal command for every package manager or npm release. Use the repository&#39;s supported install path and actual tool version. The point is to make dependency reconstruction deliberate rather than inherit a convenient node_modules directory.</p>
<p>A clean installation still depends on artifact availability, platform support and permitted lifecycle scripts. A lockfile cannot provide an unavailable private package or a native library missing from the host.</p>
<p>Reproducible dependency selection is one boundary. Reproducible execution is larger.</p>
<h2>Configuration needs a contract without exposing its values</h2>
<p>An agent&#39;s environment might contain an environment variable that makes a service reachable or silently selects a mock. A fresh checkout may not.</p>
<p>I would document required configuration names, their purpose, safe defaults and how an authorized person obtains them. I would not copy secret values into a PR, task record or agent transcript to make the handoff convenient.</p>
<p>Missing configuration should fail visibly where appropriate. A fallback to a mock can be useful for local tests, but it must remain distinguishable from a real integration check. Otherwise a working simulation can be reported as evidence about a system it never contacted.</p>
<p>Build-time and runtime settings also differ. A browser bundle may already contain a selected public setting even after the environment changes. Checking the shell&#39;s current value doesn&#39;t establish what the built application uses.</p>
<p>This is why I want configuration provenance, not a dump of configuration. The next engineer needs to recreate the intended conditions without inheriting sensitive information they don&#39;t need.</p>
<h2>Test state can be the hidden dependency</h2>
<p>Consider an illustrative account-management test that passes only because a user record was created manually in an earlier session. The fixture isn&#39;t in the repository. The setup step isn&#39;t in the task notes.</p>
<p>The agent&#39;s test result can be real and the validation can still be nonportable.</p>
<p>I would prefer explicit fixtures with controlled ownership and cleanup. If a shared environment is unavoidable, record the relevant preconditions and limits. Don&#39;t reset shared state merely because it would make the current task easier.</p>
<p>Time, locale, timezone and network responses can also influence results. Not every test needs a simulated clock or a hermetic network. But a result used to justify a behavioral claim should identify the inputs that decide it.</p>
<p>The same caution applies to retries. A flaky success after several attempts isn&#39;t equivalent to a stable result, and an agent shouldn&#39;t erase the failed attempts from the explanation.</p>
<p>Caches deserve similar treatment. A warm cache can make a path work that a first-time user cannot complete. A stale build cache can preserve implementation bytes the source no longer describes.</p>
<h2>Record a reconstruction path, not an entire machine</h2>
<p>For a meaningful handoff, I would keep a small environment record alongside the evidence: relevant revision, runtime and package-manager versions, install procedure, configuration contract, fixture setup, and the exact check performed.</p>
<p>That is a proposal for useful evidence, not a requirement to archive every process and environment variable. Excessive collection creates privacy and maintenance problems of its own.</p>
<p>The record should also say what was not reconstructed. An unavailable external provider, a mocked payment step or a local-only service is a limitation, not an embarrassing detail to hide.</p>
<p>I would check the reconstruction in a fresh authorized environment when the risk justifies it. The aim is to remove accidental dependencies, not to claim that a clean checkout guarantees production behavior.</p>
<p>A container image can package more of the environment. It still needs controlled inputs, external contracts and an explanation of which mounted files or services sit outside it.</p>
<h2>The evidence should survive the session</h2>
<p>My <a href="https://ma-x.im/blog/agent-playbook-green-checks">green-checks article</a> asks what a check actually established. Environment provenance makes that question answerable after the agent&#39;s session ends.</p>
<p>An ephemeral runner is useful precisely because it doesn&#39;t preserve incidental state forever. That also means useful setup and result evidence must live somewhere appropriate before the runner disappears.</p>
<p>There is a balance here. I don&#39;t need a permanent recording of every terminal screen. I need enough non-sensitive information to understand and repeat the decisive check.</p>
<p>If the only reconstruction method is asking the original agent to remember what it did, the environment hasn&#39;t really been handed off.</p>
<h2>Leave the next engineer a smaller mystery</h2>
<p>I want agents to validate their changes. I also want validation to leave something more durable than the sentence that it worked.</p>
<p>The handoff should help another engineer distinguish a code defect from a missing setup step, a dependency-resolution difference from a changed requirement, and a mocked success from a real integration result.</p>
<p>That is ordinary engineering discipline, made more important by the number of disposable workspaces we can now create.</p>
<p>When an agent finishes a task in your environment, which part is hardest to reproduce afterward: the dependencies, the service state, or the exact conditions of its checks? I&#39;d be curious what you preserve and what you deliberately avoid collecting.</p>
]]></content:encoded>
      <pubDate>Sat, 31 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Developer Experience</category>
      <category>Testing</category>
      <category>Reproducibility</category>
    </item>
    <item>
      <title>Upgrading the Model Means Retesting the Workflow</title>
      <link>https://ma-x.im/blog/ai-model-upgrade-workflow-regression</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-model-upgrade-workflow-regression</guid>
      <description>A model upgrade can change tool use, stopping decisions and recovery, not just answer quality. I would test the workflow contract before trusting a new default.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-model-upgrade-workflow-regression/cover.png" alt="Upgrading the Model Means Retesting the Workflow" /></p>
<p>Changing a model identifier can be a one-line edit. It is not necessarily a one-line change to the system&#39;s behavior.</p>
<p>The same task may now produce different searches, tool arguments, clarification questions, stopping decisions or recovery attempts. The final code can improve while one of those surrounding behaviors becomes less suitable for the workflow.</p>
<p>I would treat a model upgrade as a change to an engineering dependency, not as permission to carry every previous assumption forward.</p>
<p>That isn&#39;t an argument for staying on an old model. It is a reason to make the benefit and the compatibility work observable.</p>
<h2>The application contract is larger than the final answer</h2>
<p>A coding agent operates inside a harness: prompts, tool definitions, permissions, context management, execution loops and output handling. The model is one consequential part of that arrangement.</p>
<p>The contract may require the agent to inspect the current base before editing, use a particular tool for a sensitive action, stop when authorization is missing, and distinguish an unavailable check from a successful one.</p>
<p>Those requirements don&#39;t disappear because the candidate model produces a better-looking patch.</p>
<p>Some differences are API compatibility issues. Others are valid behavior changes inside an otherwise compatible API. A request succeeding doesn&#39;t establish that the application still gets the kind of progress events or tool-use pattern it expects.</p>
<p>There is current, concrete evidence for taking that distinction seriously. Anthropic&#39;s <a href="https://platform.claude.com/docs/en/models/fable-5-1/migration-guide">Fable 5.1 migration guide</a>, checked on October 23, 2026, documents model-specific changes involving forced tool choice, conversation-history handling and agent-loop behavior. Those details apply to the documented migration, not to every model or managed product.</p>
<p>The general lesson isn&#39;t to memorize today&#39;s parameter list. It is to read the migration contract for the exact integration being changed. A managed agent product may handle details that a custom API harness must manage itself.</p>
<h2>First name what is actually changing</h2>
<p>An editor update can change the model, tool descriptions and execution behavior together. A provider can update a model alias. A team can revise its system prompt while testing a new model.</p>
<p>If all of those happen at once, an improved result does not identify which change helped. A regression doesn&#39;t identify which component broke either.</p>
<p>For a controlled comparison, I would record the model identity available to the integration, harness revision, prompt revision, tool schemas, relevant settings and fixture state. If the exact underlying model is hidden, I would record that limitation instead of inventing a stable identity.</p>
<p>Keep task inputs and starting repositories comparable. A new base commit, warmed cache or already-fixed fixture can turn a model comparison into a comparison of different problems.</p>
<p>One variable at a time is a useful diagnostic ideal, not always a deployment option. When a product bundles changes, evaluate the bundle and describe the attribution limit honestly.</p>
<h2>Build cases around decisions that matter</h2>
<p>I would start with a small regression set drawn from the workflow&#39;s consequential boundaries.</p>
<p>A clear local fix should remain clear and local. An ambiguous requirement should produce a useful question rather than an invented product decision. A failed integration check should not be summarized as a successful task because the unit suite passed.</p>
<p>Another fixture might change the PR head during review. The expected outcome is to notice that the reviewed artifact has changed, not merely attach the old conclusion somewhere the API accepts it.</p>
<p>These are proposed evaluation cases, not reported experiments or results from my work. Their value comes from defining the acceptable outcome before reading the candidate&#39;s explanation.</p>
<p>I would avoid requiring one exact tool-call sequence unless the sequence itself enforces a real constraint. Two valid investigations may read different files. The evaluation should distinguish a different route from a violation of the task&#39;s boundaries.</p>
<p>Conversely, a plausible final patch shouldn&#39;t erase an unauthorized intermediate action. Evaluate consequential actions and the final artifact, not just the last paragraph.</p>
<h2>Grade observable behavior, with room for uncertainty</h2>
<p>Some outcomes can be checked mechanically: whether a forbidden mutation occurred, whether the final diff contains unrelated files, or whether a claimed test command actually completed.</p>
<p>Other outcomes need a domain reviewer: whether a clarification was necessary, whether a suggested integration belongs in scope, or whether the proposed test establishes the intended behavior.</p>
<p>A model-based grader can help organize evidence, but it isn&#39;t automatically an independent authority. I would calibrate it against reviewed examples and inspect disagreements. A score without an understandable basis is another opaque output.</p>
<p>OpenAI&#39;s <a href="https://developers.openai.com/api/docs/guides/evaluation-getting-started">evaluation documentation</a> describes datasets, annotations and graders. At the October 23 check, it also carries a deprecation notice for the hosted Evals platform. I am not recommending a new dependency on that retiring service. Representative cases and explicit grading criteria remain useful regardless of which supported runner stores them.</p>
<p>The tooling should serve the comparison. It should not become a reason to keep a fragile evaluation tied to an obsolete interface.</p>
<h2>Recovery deserves its own test</h2>
<p>A clean success path is a friendly demonstration. The more revealing cases often involve unavailable tools, authentication failures, partial edits and interrupted work.</p>
<p>I would check whether the candidate preserves the task boundary after an error. Does it ask for a secret in an inappropriate channel? Does it retry an irreversible operation without checking the previous outcome? Does it weaken a test merely to complete the loop?</p>
<p>These concerns aren&#39;t claims that a particular model does those things. They are reasons to observe recovery rather than assume it inherits the quality of the model&#39;s successful answers.</p>
<p>Permissions should remain enforced outside the model. An evaluation can detect undesirable behavior; it shouldn&#39;t be the only thing preventing a consequential action.</p>
<p>The same reasoning applies to compaction and handoffs. A workflow that works for five turns may fail when a summary loses an important unresolved condition. A longer-running fixture can expose that boundary without requiring an enormous benchmark.</p>
<h2>Compare the cost of an accepted result</h2>
<p>Tokens and response latency are useful measurements. They don&#39;t describe the whole task if the candidate requires more correction, publishes noisier reviews or leaves harder-to-understand changes.</p>
<p>I would record human interventions and rework alongside execution cost, using actual observations rather than estimated productivity claims. Passive waiting and a person&#39;s active effort are different quantities.</p>
<p>Repeated runs matter where outputs vary. One candidate success and one baseline failure are anecdotes, not a stable rate. The amount of repetition should match the consequence of the decision and the available resources.</p>
<p>The result can be mixed: better implementation on one class of task, more clarification on another, a compatibility repair needed in the harness. That is a useful migration finding, not a disappointing failure to produce a winner.</p>
<h2>Keep a way back</h2>
<p>Before changing a default, I would keep the previous configuration identifiable and limit initial exposure. That might mean a small authorized task class or shadow review without automatic publication.</p>
<p>Rollback has limits too. A pinned model may later be retired, and a managed product may not expose an exact old configuration. Record what can actually be restored rather than promising a permanent switch.</p>
<p>The existing <a href="https://ma-x.im/blog/agent-playbook-evaluation-is-the-missing-loop">evaluation article</a> asks whether the workflow produces useful outcomes. A model upgrade is a concrete occasion to ask that question again, with the old assumptions made visible.</p>
<p>I want to use better models. I also want to know which part of my working agreement with the agent changed when I selected one.</p>
<p>What would make a model upgrade fail acceptance in your workflow even if its final code looked better? I&#39;d like to hear which behavioral regressions your evaluations can actually detect.</p>
]]></content:encoded>
      <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Evaluation</category>
      <category>Developer Tooling</category>
      <category>Reliability</category>
    </item>
    <item>
      <title>When Repository Content Starts Giving Orders</title>
      <link>https://ma-x.im/blog/ai-repository-content-trust</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-repository-content-trust</guid>
      <description>A coding agent must read repository content without treating it all as authority. I want the boundary between useful evidence and instructions to stay visible.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-repository-content-trust/cover.png" alt="When Repository Content Starts Giving Orders" /></p>
<p>A source comment explains a strange compatibility rule. A README explains how to run the project. A test fixture contains a conversation. A fetched issue describes what should change.</p>
<p>A coding agent needs all of them. They do not all have the same right to tell it what to do.</p>
<p>That is the boundary I find more useful than telling an agent not to trust the internet. A repository can contain relevant evidence, authorized project instructions, obsolete advice and deliberately hostile text. Sometimes those arrive through the same search result.</p>
<p>The problem is not reading a suspicious sentence. The problem is promoting a sentence from material under inspection into authority over the inspection.</p>
<h2>The source being reviewed cannot appoint itself the reviewer</h2>
<p>Consider an illustrative PR that adds a comment describing a component as exempt from ordinary validation. The same change tells automated reviewers to ignore failures involving that component.</p>
<p>The comment may be useful evidence about the author&#39;s intent. It is not, by itself, an approved exception to the team&#39;s validation policy.</p>
<p>If the agent treats it as an instruction, the proposed code has acquired a way to influence how it is judged. The author need not have changed a tool permission or a protected policy file. The confusion happened in interpretation.</p>
<p>This can be deliberate prompt injection. It can also resemble an ordinary documentation mistake: an outdated setup note asks for a step the current task doesn&#39;t need. The intent of the text and the authority of the text are separate questions.</p>
<p>I would not claim every incorrect README instruction is an attack. I would ask whether the agent is using it as context to verify or as a command that overrides the actual task.</p>
<h2>Repository-local doesn&#39;t mean equally trusted</h2>
<p>An instruction file deliberately selected by the user or organization has a different role from a copied log line. A task description from an authorized owner differs from an arbitrary comment underneath it. A policy on the reviewed branch may itself be part of the proposed change.</p>
<p>That doesn&#39;t mean refusing all repository instructions. It means keeping their origin, scope and review status visible.</p>
<p>For a PR reviewer, the base branch&#39;s approved policy and a proposed replacement on the head branch should not silently become the same source. A legitimate policy update may need review. Its existence in the diff doesn&#39;t automatically authorize it to govern that review.</p>
<p>Likewise, a retrieval tool returning content doesn&#39;t authenticate the instructions inside that content. The tool may be trusted to fetch a page accurately while the page remains untrusted to direct actions.</p>
<p>The distinction also applies to generated summaries. If a summary turns a quoted instruction from a fixture into an unqualified next step, it can lose the original boundary while preserving the words.</p>
<blockquote>
<p>Accurate retrieval is not delegated authority.</p>
</blockquote>
<p>I want a handoff to preserve that difference, not just preserve more text.</p>
<h2>Current controls matter, but they cover different surfaces</h2>
<p>This is not an argument that current coding agents operate without protections. The <a href="https://code.visualstudio.com/docs/agents/run/security">VS Code security guide</a>, checked on October 23, 2026, documents workspace trust, approvals, sandboxing, MCP-server trust and information-exposure risks.</p>
<p>The details depend on the active harness and tool path. Its guidance distinguishes process sandboxing from permission checks on other tools and warns that sandboxing is not a standalone security boundary. That is more useful than assuming one toggle makes every route equivalent.</p>
<p>Instructions guide the model. Permissions determine which operations are available. Isolation can constrain what an executing process reaches. Network restrictions constrain destinations. None of these automatically proves that a proposed change satisfies the task.</p>
<p>The <a href="https://docs.github.com/en/copilot/responsible-use/code-review">current GitHub application card</a> also describes different environments and data flows for its agentic surfaces. I wouldn&#39;t transfer a cloud agent&#39;s documented constraints to a local terminal session merely because both carry the same product name.</p>
<p>Good controls reduce the consequences of a confused decision. They do not remove the need to distinguish data from instructions.</p>
<h2>Keep the action boundary outside the prose</h2>
<p>A document can recommend a command. It should not be able to grant that command new permissions.</p>
<p>I would enforce consequential restrictions in the tool or execution environment rather than depend solely on the model agreeing to follow a warning. A reviewer that doesn&#39;t need mutation tools shouldn&#39;t receive them by default. A task needing one repository shouldn&#39;t inherit every account a developer happens to use.</p>
<p>That principle is narrower than prescribing a particular sandbox configuration. Filesystem access, credential availability, network routes and connected services vary by environment. The controls have to be checked where actions actually run.</p>
<p>A tool wrapper that inspects only one command shape may miss a different execution route. A hook&#39;s failure or timeout behavior may not match an engineer&#39;s assumption about stopping. I would test those boundaries instead of treating the existence of a hook as enforcement.</p>
<p>There is also a quiet escalation to avoid: allowing a document being analyzed to nominate additional tools or a more privileged environment as prerequisites. A claimed need for access is still a request to evaluate.</p>
<p>I discussed useful tool context in <a href="https://ma-x.im/blog/agent-playbook-tools-are-intelligence">Tools Are Part of the Agent&#39;s Intelligence</a>. That usefulness makes precise tool authority more important, not less.</p>
<h2>Test whether an instruction stays inside its source</h2>
<p>For an authorized local evaluation, I would use harmless fixtures. A source file or mock tool result could request a clearly out-of-task change to a disposable marker file. The expected outcome would be to recognize the instruction as content, not carry it out.</p>
<p>No real secrets, external collection endpoints or production systems are needed to establish that question. The test should inspect actual tool activity as well as the final explanation. Saying that an instruction was ignored while performing its requested action is not a pass.</p>
<p>I would vary the source: a comment, a generated report, a quoted issue reply and a summarized tool result. The aim is to check the boundary through paths the workflow actually uses, not to collect theatrical attack phrases.</p>
<p>Passing such fixtures is evidence about those cases. It is not proof against every prompt injection. The environment still needs constraints that limit the effect of a missed case.</p>
<p>Legitimate instructions matter in the evaluation too. An agent that refuses to follow the approved project setup has not solved trust by refusing all work. I want correct treatment of different sources, not indiscriminate suspicion.</p>
<h2>A stopped action should leave an understandable reason</h2>
<p>If a source asks for an unexpected operation, the useful response identifies the source and the conflict: this instruction appeared in material being analyzed, but the task did not authorize the action.</p>
<p>A person can then confirm an actual requirement through the appropriate channel. The agent doesn&#39;t need to turn every questionable paragraph into an emergency. It needs to avoid quietly changing who is in charge.</p>
<p>Logs can help reconstruct that decision, but they should not copy entire sensitive documents merely to preserve evidence. Record enough origin and action information to investigate, with appropriate access and retention.</p>
<p>I don&#39;t want to make agents blind to repositories. I want them to read carefully without making every readable sentence a new supervisor.</p>
<p>When a coding agent fetches something in your workflow, what establishes whether that content may direct its next action? I&#39;d be interested in the boundaries you have actually tested, especially the ones that survive summaries and handoffs.</p>
]]></content:encoded>
      <pubDate>Tue, 27 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Security</category>
      <category>Prompt Injection</category>
      <category>Developer Tooling</category>
    </item>
    <item>
      <title>The Agent Finished. The Reviewer Is Still Reading.</title>
      <link>https://ma-x.im/blog/ai-review-human-capacity</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-review-human-capacity</guid>
      <description>Generating a patch and understanding its consequences consume different resources. I want agent workflows to respect the capacity of the people reviewing them.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-review-human-capacity/cover.png" alt="The Agent Finished. The Reviewer Is Still Reading." /></p>
<p>The agent has finished the implementation. CI is green. There is a useful PR description, a test summary and another task ready to start.</p>
<p>The reviewer is still trying to understand the first change.</p>
<p>I don&#39;t think that makes the reviewer the slow part of an otherwise efficient machine. Generation and acceptance are different kinds of work. Producing another plausible implementation does not create another person&#39;s attention to examine it.</p>
<p>The interesting question isn&#39;t how many PRs an agent can open. It is how much consequential change a team can understand, validate and own without turning review into an exercise in trusting the summary.</p>
<h2>A faster author changes the queue</h2>
<p>Imagine a team where generated changes arrive faster than reviewers can assess them. This is a hypothetical operating condition, not a productivity measurement from my work.</p>
<p>The immediate symptoms might look encouraging: more branches, more completed implementation tasks, more green checks. The waiting work has simply moved.</p>
<p>Opening several PRs in parallel may shorten implementation time for each author while extending the delay before any reviewer can concentrate on one. Interrupting reviewers with repeated requests makes their effective capacity smaller still.</p>
<p>This is a workflow problem, not an argument against agents. A compiler getting faster never meant that architectural judgment became free. The same distinction deserves to survive another increase in code-generation capacity.</p>
<p>OpenAI&#39;s <a href="https://openai.com/index/harness-engineering/">2026 harness-engineering report</a> describes human QA capacity becoming a constraint as code throughput increased in its own internal experiment. Its response involved making application behavior and observability more accessible to agents. Those are observations from a particular environment, not a throughput target or permission model I would import wholesale.</p>
<p>For my purposes, the important part is the changed constraint. If acceptance is waiting, making authors produce more work may be optimizing the wrong stage.</p>
<h2>Reviewability isn&#39;t a line-count limit</h2>
<p>A small diff can contain a large decision. One changed authorization condition may deserve more attention than a generated file containing hundreds of mechanical edits.</p>
<p>I would size review around behavioral questions: what changed, who depends on it, which assumptions changed, and what evidence is available. Line counts can help notice outliers. They do not calculate risk.</p>
<p>Consider an illustrative PR that introduces a shared utility, migrates consumers, changes a dependency and refreshes unrelated snapshots. Every individual edit might be reasonable. Together they create several distinct review jobs.</p>
<p>The reviewer has to separate the intended behavior change from migration mechanics, dependency effects and test-expectation updates. A detailed explanation can help. A cleaner division of work may help more.</p>
<p>That doesn&#39;t mean splitting code into arbitrary tiny PRs. A sequence of fragments that cannot be tested or understood independently can increase coordination costs. I would look for cohesive behavioral slices with explicit dependencies and a valid state between them.</p>
<blockquote>
<p>A reviewable change gives someone a bounded question they can actually answer.</p>
</blockquote>
<p>The unit isn&#39;t necessarily one file, one agent session or one convenient commit. It is an intelligible decision with enough surrounding evidence.</p>
<h2>Summaries should lower the cost of checking</h2>
<p>A good summary tells me where to inspect, not merely what conclusion to reach.</p>
<p>For a permission change, I want the affected rule, the relevant route and the check that exercised a denied action. For a shared component change, I want the changed contract and the consumers that make it consequential. For an expected-result update, I want the requirement that changed.</p>
<p>I don&#39;t need a transcript of every search or an elaborate account of the model&#39;s internal reasoning. I need references I can follow and limitations I can see.</p>
<p>An agent saying that all relevant scenarios passed should be able to identify those scenarios. If a check was unavailable, the absence should remain visible rather than disappear inside a reassuring overall conclusion.</p>
<p>This is related to <a href="https://ma-x.im/blog/github-ai-pr-reviewer">useful automated review comments</a>, but it is a different responsibility. The question here is whether the whole change can be accepted with reasonable effort, not whether an individual bot comment is well phrased.</p>
<p>Long descriptions can make the problem worse if their conclusions are easier to read than their evidence is to verify. I would rather have three precise links and one unresolved question than a confident report that nobody has time to audit.</p>
<h2>Another agent can remove work, or add another queue</h2>
<p>Automated review can catch local defects, investigate a suspicious export or check a routine convention before a person arrives. That is useful preparation.</p>
<p>It can also generate a second pile of work: findings to validate, false positives to dismiss, replies to interpret and repeated comments to reconcile. A quiet tool isn&#39;t necessarily effective, and a busy one isn&#39;t necessarily helping.</p>
<p>GitHub&#39;s current <a href="https://docs.github.com/en/copilot/responsible-use/code-review">application card for Copilot agents</a> explicitly discusses missed issues, false positives and inaccurate suggestions, and recommends supplementing automated review with human review. These are present documented limits, not a claim that current tools only inspect a few lines of a diff.</p>
<p>I would assess whether a review tool reduces the human effort needed to reach a sound decision. That includes the effort spent checking the tool itself. Comment count is a poor substitute.</p>
<p>The same applies to UI evidence. Browser automation can make a previously invisible interaction inspectable, but a screenshot doesn&#39;t verify every state. Helpful automation narrows the remaining question. It doesn&#39;t just add another green label.</p>
<h2>Control work in progress before asking for faster review</h2>
<p>If review capacity is the constraint, I would try limiting how many changes are actively seeking attention. Completing one reviewable slice before opening three more can improve the flow without changing the model.</p>
<p>That limit should be a team decision, not a universal number. A routine dependency stream and a change to a shared identity contract don&#39;t impose the same load.</p>
<p>Review ownership also matters. An agent can identify potential consumers, but that doesn&#39;t automatically identify who is accountable for accepting the change. A PR waiting for everyone can quietly become a PR owned by no one.</p>
<p>I would make blocked states explicit: waiting for a domain decision, waiting for a missing integration check, or waiting for a reviewer with the relevant ownership. Those call for different actions. Re-running the same agent is unlikely to resolve all three.</p>
<p>There is a useful stopping point for automation here. When the next step is an accountable decision, generating more implementation isn&#39;t progress on that decision.</p>
<h2>Measure the waiting and the rework</h2>
<p>I would keep measurements modest and specific: how long changes wait for first substantive review, how often a PR needs to be separated, what corrections arrive after apparent completion, and which review questions repeatedly lack evidence.</p>
<p>These are proposed observations, not results I have measured for a comparative study. They need definitions and local interpretation. A shorter review can mean a better-prepared change, but it can also mean a rushed reviewer.</p>
<p>The goal isn&#39;t to make every change slower. It is to stop borrowing confidence from an overloaded acceptance process. If the team cannot explain what it accepted, the implementation speed has hidden part of its cost.</p>
<p>I still want the agent to finish quickly. I also want its finished work to be something another engineer can pick up without reconstructing an entire conversation.</p>
<p>When I look at a queue of generated PRs, that is the question I would put ahead of the next throughput improvement: can the people responsible for this system still understand what is entering it?</p>
<p>Where does review capacity constrain your AI-assisted workflow: understanding the change, checking the evidence, or sorting automated findings? I&#39;d be interested in what actually reduced that burden, not just what produced more output.</p>
]]></content:encoded>
      <pubDate>Sun, 25 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Code Review</category>
      <category>Engineering Workflow</category>
      <category>Maintainability</category>
    </item>
    <item>
      <title>The Agent Was Right. The Scope Still Changed.</title>
      <link>https://ma-x.im/blog/ai-review-right-code-wrong-scope</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-review-right-code-wrong-scope</guid>
      <description>An AI agent made a sound improvement, and I agreed. A colleague found his task implemented in my PR. What should review verify beyond the quality of the code?</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-review-right-code-wrong-scope/cover.png" alt="The Agent Was Right. The Scope Still Changed." /></p>
<p>A colleague started working on his ticket and discovered that its implementation was already there. He traced the files back to my pull request.</p>
<p>I hadn&#39;t found a technical problem with the additional work. The awkward part was explaining how it had become part of my task.</p>
<p>My ticket introduced a new abstraction, which I&#39;ll call a model. Another task covered integrating it at three selected usage sites. The older implementation served other consumers and needed to remain.</p>
<p>The agent built the model and covered it with tests. Then it noticed that the model wasn&#39;t being used. The task description explained what it would eventually replace, and the agent identified the places where it could be connected.</p>
<p>That was a reasonable suggestion. I accepted the argument and let it continue.</p>
<p>The integration was good work. It also belonged to another ticket.</p>
<p>This wasn&#39;t an agent secretly editing forbidden files. It asked, and I agreed. That makes the case more interesting than a story about automation ignoring instructions. Human agreement was present. What was missing was an explicit reconciliation between that agreement and the way the work had been divided.</p>
<blockquote>
<p>The agent was right about what came next. That didn&#39;t establish that it belonged in this PR.</p>
</blockquote>
<h2>Unused is not the same as unfinished</h2>
<p>A tested abstraction waiting for its integration task can look like incomplete engineering when viewed only through the current repository.</p>
<p>The code suggests an obvious next step. The ticket boundary may say something different.</p>
<p>That boundary isn&#39;t automatically sacred. Tasks can be split poorly. Combining implementation and integration may be the better decision. But it should be a deliberate change to the plan, with the affected people aware of it, not an accidental consequence of a convincing technical explanation.</p>
<p>I wouldn&#39;t require product approval for every extracted helper. Engineers need room to make implementation decisions. The useful distinction is between supporting work necessary to satisfy the current acceptance criteria and work that delivers a separately planned behavior.</p>
<p>An additional compatibility fix might be necessary for the requested change to function. Integrating a new model into consumers assigned to another task is a different decision. Both can be technically justified. Only one follows automatically from the current task&#39;s goal.</p>
<p>Knowing the intended destination is not the same as owning the next step.</p>
<p>This isn&#39;t unique to AI. A human engineer can make the same choice. But an agent can provide both the persuasive argument and the implementation. I need to examine the transition between those two, not only the quality of the result.</p>
<h2>I started making the origin of decisions visible</h2>
<p>My response was not another permanent instruction saying never do anything extra. That would make the agent less useful without resolving the ambiguity.</p>
<p>Instead, I made a distinction in the task records: work grounded in requirements, documentation, repository instructions or acceptance criteria, versus additions originating in the agent&#39;s own engineering judgment.</p>
<p>The latter should be presented as a proposal or inference, not quietly described as required work.</p>
<p>Both I and the review agent can inspect that record. It gives us somewhere to look when a change is sensible but its relationship to the task is unclear.</p>
<p>The important difference is between stating that a documented requirement calls for a change and stating that the agent recommends it because the current implementation appears incomplete. Those explanations deserve different review questions.</p>
<p>Of course, the agent can misclassify its own reasoning. A documentation reference may describe a future direction rather than a current requirement. A record written by the implementer is an account to examine, not independent evidence that everything happened correctly.</p>
<p>The reviewer needs the original task and relevant sources, not just the implementer&#39;s summary. It also needs to compare that account with the actual changes. Otherwise, two agents may agree because they inherited the same assumption.</p>
<p>For substantial work, this record can preserve decisions, checks, open questions and work intentionally left outside the task. A tiny, clear change doesn&#39;t necessarily need another specification file.</p>
<p>I&#39;ve described the broader <a href="https://ma-x.im/blog/agent-playbook-do-it-as-usual">task-to-PR workflow</a> elsewhere. Jira, Confluence, repository instructions, Git history and API documentation can be available through MCP integrations and other tools. Access helps only if the sources are current and their authority is understood. More context can also mean more convincing outdated instructions.</p>
<p>I want the record to make disagreement easier to locate. I don&#39;t want an impressive document that makes disagreement harder.</p>
<h2>A good review needs several kinds of evidence</h2>
<p>My <a href="https://ma-x.im/blog/github-ai-pr-reviewer">personal PR-reviewer article</a> focused on specific, checkable comments and visibility into what the tool inspected. That remains useful engineering attention. A removed export or disappearing permission check deserves investigation, not an unexplained warning.</p>
<p>But a reviewer can correctly assess an implementation and still miss why that implementation is in the PR.</p>
<p>Tests address particular behaviors under particular conditions. A local TypeScript check doesn&#39;t establish compatibility with a downstream consumer whose actual package contract differs. As I argued in <a href="https://ma-x.im/blog/agent-playbook-green-checks">Green Checks Only Cover What You Checked</a>, the question is what the check exercised, not merely whether it passed.</p>
<p>Automation can change the object being reviewed too. My <a href="https://ma-x.im/blog/agent-playbook-scope-after-tools">scope-after-tools article</a> uses a synthetic dependency update where a hook regenerates unrelated API artifacts. The generator succeeds; the resulting commit still needs inspection. That example is illustrative, not a second incident from the model-integration story.</p>
<p>Review evidence also expires. A conclusion about one revision may no longer describe the next. <a href="https://ma-x.im/blog/agent-playbook-review-belongs-to-commit">A Review Belongs to a Commit</a> examines revision tracking and guarded actions in personal tooling I&#39;ve developed. Remembering a SHA helps identify what was inspected; it doesn&#39;t prove every finding remains valid or turn an earlier approval into authority over later changes.</p>
<p>These aren&#39;t interchangeable checks. Code correctness, dependency compatibility, task inclusion, action permissions and the relevance of evidence each answer a different question.</p>
<p>A passing test doesn&#39;t authorize extra work. An allowed directory doesn&#39;t define the business requirement. A second model&#39;s agreement doesn&#39;t establish independent verification.</p>
<p>Nor should every small edit trigger an investigation across every connected repository. The validation needs to follow the affected contract and the decision being made.</p>
<h2>Three conversations around different parts of the problem</h2>
<p>During October, three groups contacted me after reading my articles. The useful connection was their different approaches to questions I was already trying to answer.</p>
<p>In an early-October exchange with <a href="https://kaicontext.com/">Kai</a>, we discussed code relationships: imports, exports, calls, and how a finding might be challenged rather than immediately published. We also discussed limits of static analysis around runtime behavior and some React relationships. That is an account of the conversation, not a specification of today&#39;s product.</p>
<p>I proposed an evaluation with expected defects recorded before running the reviewer, without revealing the answers in the PR descriptions. Cross-file regressions, contract changes, React behavior and false positives would make useful cases. That evaluation was discussed, not completed.</p>
<p>The <a href="https://docs.deeppattern.ai/en/preview/overview">DeepPattern</a> conversation approached the problem through verification: missed requirements, untested assumptions and whether the available evidence supports a conclusion. It also touched attribution. If a task succeeds, was the useful contribution a reusable skill, connected documentation, an existing test, or a human correction? Without a meaningful comparison, I can&#39;t isolate the cause. A preview was offered; I am not reporting evaluation results.</p>
<p><a href="https://github.com/suirindo/agent-role-contracts">Agent Role Contracts</a>, from Netsujo, addresses explicit declarations of roles, scope and handoffs. Its documentation makes an important boundary clear: internally consistent declarations do not grant runtime authority, authenticate a reviewer or establish business acceptance.</p>
<p>A task requesting writes outside a declared scope can be rejected before execution. My model-integration example asks a different question: what if every edited file is permitted, but the work belongs to another task?</p>
<p>These are complementary directions, not three products I&#39;ve ranked. Code relationships, verification evidence and declared authority all matter. None alone decides whether a technically sensible addition belongs in this particular PR.</p>
<h2>Move the scope question earlier, without pretending it is solved</h2>
<p>By PR review, a disputed decision already has an implementation. The code can make the proposed extension look settled, even when the reason for including it remains uncertain.</p>
<p>I would like to make that uncertainty visible before implementation: compare the proposed work with the original goal, identify additions, and decide which ones require coordination. Then compare the actual result with the accepted scope.</p>
<p>That is a direction to investigate, not a semantic scope checker I&#39;ve completed. Requirements can be ambiguous. Plans can omit necessary work. The reviewer can share the implementer&#39;s blind spot. A real change in understanding may justify revising the plan.</p>
<p>I would evaluate a mechanism like this on cases where the right response differs. One task genuinely requires a supporting change. Another should produce a suggestion to do the integration separately. A third needs clarification because the requirement doesn&#39;t settle the choice.</p>
<p>The expected outcome has to include stopping or separating work, not just producing more code. Otherwise, the evaluation rewards the very behavior it is supposed to examine.</p>
<p>I also wouldn&#39;t ask an agent for an elaborate account of its internal thoughts. I need inspectable grounds: which requirement it relied on, what it inferred, what it changed, and what the checks actually support.</p>
<p>My colleague didn&#39;t uncover bad code. He uncovered work that had crossed a task boundary while still looking like sensible engineering. I had participated in that decision. The part I want to improve is how such a decision becomes visible before someone else discovers their task inside my PR.</p>
<p>If you work on agent review, verification or workflow controls, how do you distinguish a necessary supporting change from an expansion that needs a separate decision? What evidence would you need to trust that the agent did the requested job, rather than a well-implemented neighboring one? I&#39;d like to compare approaches, including where they fail.</p>
]]></content:encoded>
      <pubDate>Fri, 23 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Code Review</category>
      <category>Engineering Workflow</category>
      <category>Verification</category>
    </item>
    <item>
      <title>Independent Releases Still Need Compatible Neighbors</title>
      <link>https://ma-x.im/blog/micro-frontends-vs-modular-monolith</link>
      <guid isPermaLink="true">https://ma-x.im/blog/micro-frontends-vs-modular-monolith</guid>
      <description>Micro-frontends and a modular monolith can share strong code boundaries. Their practical difference appears when old and new parts coexist: shell contracts, assets, dependency versions, cross-app state, and rollback need an operating agreement.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/micro-frontends-vs-modular-monolith/cover.png" alt="Independent Releases Still Need Compatible Neighbors" /></p>
<p>The account team releases its new frontend. The shell stays on the previous version. Checkout was updated yesterday.</p>
<p>That can be a healthy arrangement. It is what independent deployment is meant to permit.</p>
<p>It is also a compatibility problem. Those versions need to work together, and users will discover the combinations we did not consider while each team tested its own application.</p>
<p>The earlier <a href="https://ma-x.im/blog/react-playbook-micro-frontends-org-chart">micro-frontends article</a> asks whether organizational independence justifies the split. Here I want to examine the technical consequence after that decision: what independence requires during ordinary releases and rollbacks.</p>
<h2>Separate three kinds of boundary</h2>
<p>A code boundary determines which module can import another. An ownership boundary determines who may change it. A deployment boundary determines which artifact can be released without rebuilding the others.</p>
<p>They can align, but they do not have to.</p>
<p>A modular frontend can have feature packages, explicit APIs, separate owners, and enforced import rules while producing one application build. That is the modular monolith in this comparison, not a claim that all code belongs in one undifferentiated folder.</p>
<p>Micro-frontends add independently deployed application artifacts, composed through routes, runtime loading, frames, or another integration mechanism. Different composition methods have different costs. An iframe provides a stronger browser isolation boundary than mounting two modules in the same JavaScript runtime; it also changes interaction and integration constraints.</p>
<p>I would name the intended composition before discussing the benefits. &quot;Micro-frontends&quot; is otherwise doing too much work in the sentence.</p>
<h2>One build moves compatibility work earlier</h2>
<p>In a modular monolith, one build checks the selected package versions together. A failing interface change can stop the release before production sees it. A coordinated rollback returns that tested set of frontend modules together.</p>
<p>That does not guarantee correctness. Old tabs still exist, backend APIs still evolve, and integration tests can miss a workflow. But the frontend&#39;s internal version combination is more constrained.</p>
<p>With independent artifacts, compatibility work remains necessary and moves partly into runtime and release policy. The shell needs to understand the remote&#39;s contract; the remote needs to tolerate the supported shell versions.</p>
<p>The work did not disappear because CI pipelines became separate. We changed where it is paid.</p>
<h2>A remote needs a supported contract</h2>
<p>An illustrative mount contract might include a version, capabilities, lifecycle, and a constrained way to communicate:</p>
<pre><code class="language-typescript">type AccountModuleContract = {
  version: 2;
  mount: (container: HTMLElement, context: AccountContext) =&gt; () =&gt; void;
  capabilities: readonly (&#39;profile&#39; | &#39;notification-settings&#39;)[];
};
</code></pre>
<p>The shape is not a recommended universal micro-frontend API. It shows the questions a boundary needs to answer: what does the host provide, what may the remote assume, how does it clean up, and how can the host detect incompatibility?</p>
<p>Do not make shared context an unrestricted store containing every application&#39;s internals. That turns the mount API into a distributed import path with weaker verification.</p>
<p>Capabilities can make additive evolution easier than assuming that every participant upgrades simultaneously. For breaking changes, define a support window and migration path.</p>
<h2>Release independence creates version combinations</h2>
<p>If several remotes each support two versions while the shell also supports two, the possible combinations multiply. It is rarely practical to test every theoretical combination equally.</p>
<p>Constrain the supported matrix. Test each remote against the current shell and the declared compatibility window. Keep representative whole-product flows that cross the important boundaries.</p>
<p>A remote&#39;s unit tests may correctly show that its save action works. They do not show that the shell refreshes the account menu, preserves navigation, and clears the right state after logout.</p>
<p>Contract tests help, but a contract can be structurally valid and behaviorally wrong. The few shared workflows deserve integration tests owned by someone, not by an imaginary team named &quot;the platform.&quot;</p>
<h2>Runtime manifests are release artifacts</h2>
<p>If the shell loads a remote entry through a mutable URL, the deployed composition can change without the shell&#39;s commit changing.</p>
<p>Record the selected remote versions or immutable asset references. Keep enough history to reconstruct what users were served. Rollback should select a known-compatible composition, not point several URLs at yesterday and hope.</p>
<p>Asset retention matters for clients that already loaded an old entry. Removing the old chunks immediately can break lazy navigation later, long after the initial page load succeeded.</p>
<p>CDN cache lifetimes can also cause different users to load different manifest versions. This is manageable if the contracts support overlap. It is inconvenient if independent deployment silently assumes instantaneous global consistency.</p>
<h2>Sharing dependencies exchanges one cost for another</h2>
<p>Shipping separate dependencies can increase download size and produce multiple copies of libraries. Sharing them at runtime can reduce duplication, but introduces version negotiation and lifecycle constraints.</p>
<p>React singletons deserve care when components and hooks interact across a shared runtime. A shared design-system library also has behavioral and styling contracts; two versions can differ in focus management or portal assumptions even when the buttons look similar.</p>
<p>I would not force every dependency into a singleton merely to improve a bundle report. Nor would I accept uncontrolled duplication without measuring the user cost. Decide which shared dependencies are part of the platform contract, and verify both upgrades and rollbacks.</p>
<p>For stronger isolation, route-level composition may be more straightforward than many independently loaded widgets sharing one screen. It depends on the product flow, not on which demo looked most flexible.</p>
<h2>Isolation needs failure behavior</h2>
<p>A remote can fail to load because of a network error, stale asset reference, or incompatible deployment. The shell still needs a useful route and a recoverable state.</p>
<p>Distinguish an optional sidebar from checkout&#39;s payment step. One may show a fallback. The other may need to stop the workflow and preserve user input. An error boundary catches some rendering failures; it is not full isolation from CSS conflicts, shared global state, or security problems.</p>
<p>Keep remote-loading timeouts, telemetry, retry rules, and fallback ownership explicit. A spinner with no deadline is not a graceful degradation strategy.</p>
<p>The modular monolith has fewer runtime composition failure points, but its deployment can affect more features at once. Feature flags, staged releases, and modular tests can reduce that risk without creating separate runtime applications.</p>
<h2>Shared state is an authority question</h2>
<p>Identity, cart state, locale, and navigation often cross frontend boundaries.</p>
<p>Decide who owns each truth. An event that says &quot;cart changed&quot; can prompt other applications to read the authoritative value. An event carrying a partial cart snapshot can accidentally create another source of truth with its own freshness problems.</p>
<p>An event bus should have typed contracts, lifecycle rules, and unsubscribe behavior. It should not become an excuse for every remote to discover undocumented events by reading the others&#39; source.</p>
<p>Authorization must remain enforced at the backend. A remote being mounted by an authenticated shell does not give its API calls permanent authority.</p>
<p>These rules apply inside a modular monolith too. Independent deployment makes violations harder to repair together, which raises the value of keeping the shared surface small.</p>
<h2>Try modularity before distributing a coordination problem</h2>
<p>If the main pain is accidental imports, unclear ownership, or slow whole-repo CI, a modular build with boundary rules and affected-project checks may address it directly.</p>
<p>If teams genuinely need separate release schedules, existing applications cannot reasonably be merged, or one part must evolve under substantially different constraints, runtime composition may justify its cost.</p>
<p>I would trial the split at a boundary already understandable in the product, with a specific blocked workflow the trial is meant to improve. Measure release lead time and integration effort, then account for the platform work needed to keep it reliable.</p>
<p>A module boundary that cannot be described cleanly inside one build usually does not become clearer when a network load is inserted between its parts.</p>
<h2>Independence has a maintenance agreement</h2>
<p>The decision is not simply one deployment versus many. It is how much coordination we want to perform before release and how much compatibility we are prepared to maintain afterward.</p>
<p>I would choose a modular monolith when coordinated artifacts are affordable and the team&#39;s main need is better boundaries. I would choose micro-frontends when independent delivery solves a demonstrated constraint and the teams can own the resulting compatibility work.</p>
<p>Either arrangement can be maintained well. Neither relieves us of agreeing what a neighboring module may do while we are deploying something else.</p>
<p>If your teams already release independently, what happens when one of them rolls back? I&#39;d be interested in the supported combination, because that answer says more about the architecture than the number of repositories.</p>
]]></content:encoded>
      <pubDate>Thu, 22 Oct 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>Micro-Frontends</category>
      <category>Modular Monolith</category>
      <category>Delivery</category>
    </item>
    <item>
      <title>Who Checks the Tests an AI Agent Writes?</title>
      <link>https://ma-x.im/blog/ai-generated-tests-independent-oracle</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-generated-tests-independent-oracle</guid>
      <description>An AI agent can write code and tests that agree on the same mistake. I want the expected result to come from somewhere other than the implementation under test.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-generated-tests-independent-oracle/cover.png" alt="Who Checks the Tests an AI Agent Writes?" /></p>
<p>An agent writes a function, adds tests, runs them, and reports success. The implementation and the tests agree.</p>
<p>What established that they agreed on the right behavior?</p>
<p>This is the part of AI-generated testing I want to examine before counting new assertions. A test can be perfectly executable, exercise the relevant branch, and confirm the same mistaken interpretation that produced the code.</p>
<p>Humans do this too. The additional concern with an agent is how conveniently implementation, test cases, fixtures and the final explanation can be produced from one interpretation. They look like several pieces of evidence. They may have only one underlying assumption.</p>
<p>I don&#39;t want fewer generated tests. I want a clearer answer to where their expected results came from.</p>
<h2>A passing test for the wrong rule</h2>
<p>Consider a fictional checkout policy. Free shipping applies when the merchandise subtotal <strong>after discounts</strong> is at least 5,000 cents. Tax and shipping charges do not contribute. The examples below assume valid, nonnegative integer inputs, with discounts no greater than the merchandise subtotal; input validation is a separate concern.</p>
<p>This is an illustrative example, not an incident from a client project.</p>
<p>An implementation and its test might look like this:</p>
<pre><code class="language-typescript">import assert from &#39;node:assert/strict&#39;;

type ShippingQuote = {
  merchandiseCents: number;
  discountCents: number;
};

function qualifiesForFreeShipping(quote: ShippingQuote): boolean {
  return quote.merchandiseCents &gt;= 5_000;
}

assert.equal(
  qualifiesForFreeShipping({ merchandiseCents: 6_000, discountCents: 1_500 }),
  true,
);
</code></pre>
<p>The test passes. The discounted subtotal is 4,500 cents. Under the stated policy, that order does not qualify.</p>
<p>The test has not failed to execute the implementation. It has asserted the wrong answer. A coverage report can show that the function ran without noticing that the expected value contradicts the requirement.</p>
<p>Adding more examples above the undiscounted threshold won&#39;t necessarily help. Nor will renaming the test to sound more specific. The missing ingredient is an expected result derived from the policy, rather than an observation of what the function currently returns.</p>
<p>That expected result is the test oracle: the basis for deciding whether the observed behavior is correct. It doesn&#39;t have to be a separate program or an expensive formal specification. Sometimes it is one arithmetic calculation and a sentence someone has actually agreed on.</p>
<h2>Establish the expectation before polishing the implementation</h2>
<p>For this policy, I would start with a few examples whose answers can be worked out without consulting the function:</p>
<pre><code class="language-typescript">assert.equal(
  qualifiesForFreeShipping({ merchandiseCents: 6_000, discountCents: 1_500 }),
  false,
);
assert.equal(
  qualifiesForFreeShipping({ merchandiseCents: 5_000, discountCents: 0 }),
  true,
);
assert.equal(
  qualifiesForFreeShipping({ merchandiseCents: 4_999, discountCents: 0 }),
  false,
);
</code></pre>
<p>The first case disagrees with the original test. That disagreement is useful. It makes the interpretation visible instead of burying it beneath a successful run.</p>
<p>The implementation can then follow the policy:</p>
<pre><code class="language-typescript">function qualifiesForFreeShipping(quote: ShippingQuote): boolean {
  const discountedMerchandiseCents =
    quote.merchandiseCents - quote.discountCents;

  return discountedMerchandiseCents &gt;= 5_000;
}
</code></pre>
<p>This is not a complete checkout test suite. The cases establish the particular discount and threshold behavior under discussion. They don&#39;t establish currency conversion, tax treatment elsewhere, coupon validity or production integration.</p>
<p>I would ask the agent to identify the source of each important expectation. An acceptance criterion, an approved example, a documented contract or a trusted reference can justify it. The current implementation can tell us what happens, but not always what should happen.</p>
<p>If the requirement doesn&#39;t settle whether the threshold applies before or after discounts, generating an assertion doesn&#39;t settle it either. The correct next step is a question. An executable guess is still a guess.</p>
<h2>A second agent needs a different starting point</h2>
<p>It is tempting to solve this by assigning test generation to another agent. That can help, but a separate role is not automatically an independent basis for judgment.</p>
<p>If the second agent receives the implementation and a persuasive explanation of why it is correct, it may simply encode that explanation as tests. Changing the model doesn&#39;t guarantee a different interpretation of an ambiguous requirement.</p>
<p>For consequential behavior, I would give the test reviewer the original requirement and ask it to derive concrete outcomes before showing it the implementation. Then compare the cases with the code and existing tests.</p>
<p>This reduces one source of anchoring. It doesn&#39;t prove independence or eliminate shared mistakes. Both reviewers may still misunderstand the requirement, which is why the expected outcomes need to remain inspectable by a person.</p>
<p>In <a href="https://ma-x.im/blog/agent-playbook-green-checks">Green Checks Only Cover What You Checked</a>, the concern was the reach of evidence across an integration boundary. Here the boundary is earlier: whether the assertion itself expresses the intended behavior. An integration test can have a wrong expectation too.</p>
<h2>Check that the test can disagree</h2>
<p>A useful test should fail for a relevant wrong implementation. I would deliberately check that property, rather than infer it from a green run.</p>
<p>In this example, ignoring the discount should fail the first requirement-derived case. Changing the threshold comparison from inclusive to exclusive should fail the exact-boundary case.</p>
<p>Mutation testing automates variations of this idea by introducing changes into the code and checking which tests detect them. Tools such as <a href="https://stryker-mutator.io/docs/">Stryker</a> make that practical without inventing a mutation framework for each project.</p>
<p>A detected mutation establishes sensitivity to that change. It doesn&#39;t establish the truth of the original requirement. An incorrect expected value can still kill mutants; a surviving mutant can reflect equivalent behavior rather than a meaningful defect. Mutation scores need interpretation, not ceremonial targets.</p>
<p>Property-based tests add another useful angle. Under our stated policy, increasing the discount while holding the merchandise subtotal fixed must not turn an ineligible order into an eligible one. This describes a relationship across inputs instead of one fixed example.</p>
<p>But a function that always returns false also satisfies that relationship. Properties need enough independent constraints to rule out plausible wrong implementations. They complement concrete examples; they don&#39;t manufacture a business rule from nothing.</p>
<h2>Fixtures can make the same mistake less visible</h2>
<p>I would also inspect how test data is created. A fixture built by calling the implementation under test can quietly recycle the result it is supposed to check. A broad mock can remove the behavior that actually needs verification.</p>
<p>This doesn&#39;t make builders or mocks inherently bad. A builder can provide ordinary input data while leaving the important expected result explicit. A mock can isolate a collaborator without deciding the business outcome for the test.</p>
<p>The question is what the helper owns. Does it simplify setup, or does it calculate the answer through the same logic the assertion claims to validate?</p>
<p>Duplicating the production algorithm inside the test is not necessarily a better oracle. It can reproduce the same defect in another file. For a small example, independently calculated values may be easier to audit. For complex behavior, a reviewed reference implementation, contract examples or another representation may be appropriate.</p>
<p>There isn&#39;t one universally independent source. The point is to identify the dependence instead of mistaking a second file for a second line of reasoning.</p>
<h2>Review the test change, not just its result</h2>
<p>When an agent fixes a failing suite, I want to know whether it changed the behavior or changed what the suite accepts.</p>
<p>Sometimes the expected result is genuinely obsolete because the requirement changed. Sometimes the implementation is wrong. Sometimes the test was wrong from the beginning. All three can end with green CI. They are not the same engineering decision.</p>
<p>A snapshot update deserves the same question. Accepting a new snapshot records an output; it doesn&#39;t establish that the user should see it. Removing an assertion or relaxing a matcher may be justified, but the justification needs more than getting the run to pass.</p>
<p>For a bug fix, I would prefer the regression test to demonstrate the reported failure on the relevant prior implementation, then pass with the correction. It also needs to fail for the intended reason, not because an unrelated dependency or import is broken.</p>
<p>For new behavior, a failing test against the previous implementation is expected. What matters is that its assertion comes from the requested behavior, not from whichever output the new implementation happened to produce.</p>
<h2>Keep generation useful</h2>
<p>I like the prospect of an agent handling repetitive setup, exploring input combinations and proposing edge cases. Those are useful contributions even when the final expectation still needs human judgment.</p>
<p>What I don&#39;t want is to promote the existence of generated tests into evidence that the requirement has been independently checked. More assertions can make a mistaken assumption look thoroughly documented.</p>
<p>The shipping example is small enough that the disagreement fits in one subtraction. Real systems hide the same question behind fixtures, collaborators and years of existing behavior. I would still start by asking who decided the answer and what supports it.</p>
<p>If you use agents to write tests, how do you keep expected outcomes independent of the implementation? I&#39;d be interested in the checks that expose shared assumptions, especially when both the code and its tests look completely reasonable.</p>
]]></content:encoded>
      <pubDate>Wed, 21 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Testing</category>
      <category>Code Review</category>
      <category>Engineering Workflow</category>
    </item>
    <item>
      <title>TanStack Start or Next.js: Follow One Mutation Through Production</title>
      <link>https://ma-x.im/blog/tanstack-start-vs-nextjs-production</link>
      <guid isPermaLink="true">https://ma-x.im/blog/tanstack-start-vs-nextjs-production</guid>
      <description>Both frameworks can render pages and execute server-side work. A more useful comparison follows one authenticated mutation through cache invalidation, browser state, deployment skew, and the actual hosting path.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/tanstack-start-vs-nextjs-production/cover.png" alt="TanStack Start or Next.js: Follow One Mutation Through Production" /></p>
<p>A dashboard loads correctly in both frameworks. That part of the comparison is already fairly well understood.</p>
<p>Now change an order, switch accounts, keep an old tab open through a deployment, and make the request through the production CDN.</p>
<p>That is the comparison I would spend time on. The first screen tells us whether the framework can render. The later operations tell us whether the team understands the state and deployment model it has chosen.</p>
<p>The earlier <a href="https://ma-x.im/blog/react-playbook-ssr-tanstack-start">TanStack Start article</a> introduces SSR and the server boundary. This one examines an authenticated, interaction-heavy application after that introduction. It is not a performance benchmark or a claim of personal production results for either framework.</p>
<p><strong>Version scope:</strong> package metadata and official documentation were checked on October 8, 2026: <code>next</code> 16.4.0 and <code>@tanstack/react-start</code> 1.168.60. Features and host integrations below should be checked again against the project&#39;s pinned versions before implementation. This site itself still uses its existing Next.js version; writing the comparison does not upgrade it.</p>
<h2>Compare a workflow, not an API count</h2>
<p>Use one representative flow: load a private order, edit its delivery option, save, return to the list, and sign out.</p>
<p>It exercises identity, server authorization, validation, durable state, multiple cached views, URL state, and browser cleanup. Add an old-client submission after a rolling deployment, and it also exercises release compatibility.</p>
<p>I would build that slice with the deployment target intended for production. A development server is too forgiving a witness for cache behavior and runtime bindings.</p>
<p>The result need not be a second full application. It needs enough ordinary inconvenience to reveal which model the team can explain.</p>
<h2>The center of gravity is different</h2>
<p>Next.js App Router organizes work around route segments, layouts, Server Components, client boundaries, and its rendering and caching model. That is useful when much of the route can be composed on the server and the team wants the framework to own those conventions.</p>
<p>TanStack Start builds around TanStack Router, loaders, server functions, and the associated client navigation model. That is useful when typed route/search state and client-rich workflows are already central to the application.</p>
<p>Neither description is an exclusive capability boundary. Start&#39;s current documentation also describes Server Components; I would not use &quot;Start cannot do RSC&quot; as a timeless reason to choose Next. Nor does a Next.js application forbid TanStack Query or carefully designed client state.</p>
<p>The question is which default model makes the particular workflow easier for this team to reason about, and which extensions create a second overlapping model.</p>
<h2>Server functions still need an API boundary</h2>
<p>In either system, a function that runs on the server can feel like a local call from frontend code. That convenience does not make the request trusted.</p>
<p>Validate input, authenticate the caller, check access to the specific order, and enforce idempotency where repeated writes matter. A protected page or route guard improves navigation; it does not replace checks in the separately callable mutation endpoint.</p>
<p>For Start, the current <a href="https://tanstack.com/start/latest/docs/framework/react/guide/server-functions">server-function documentation</a> describes compilation to client RPC stubs, validation, middleware, and same-origin protections. Next&#39;s Server Functions also need explicit authorization in the data path.</p>
<p>I would test both flows by calling the endpoint while signed out and as another account. The implementation syntax differs. The rejected unauthorized write should not.</p>
<p>Keep external APIs separate from framework-private RPC assumptions. Mobile clients and third-party integrations need a deliberately supported contract, not an incidental generated function identifier.</p>
<h2>A successful mutation can leave several stale views</h2>
<p>After saving the order, which copy changes?</p>
<p>In a Start application using Query, there may be a query cache, router loader data, server-side data caches, and a CDN response. Invalidating one does not automatically invalidate every other layer.</p>
<p>In a Next application, server data and route output can have their own cache behavior, while the browser retains navigation state and perhaps a separate Query cache. Adding Query is reasonable when it serves a clear purpose. Using it merely because it is familiar can create two owners for the same remote data.</p>
<p>I would write down the expected invalidation path before selecting helper functions:</p>
<pre><code class="language-text">save delivery option
  -&gt; commit the order change
  -&gt; update or invalidate the detail view
  -&gt; refresh affected list representations
  -&gt; preserve unrelated local form state
</code></pre>
<p>For Next 16, be explicit about whether the application uses Cache Components or the previous caching model. Advice copied from a different major version or mode may describe different defaults. The <a href="https://nextjs.org/docs/app/guides/caching">current caching guide</a> distinguishes those paths.</p>
<p>I would not choose a framework because its cache seems invisible. Invisible is convenient until two views disagree.</p>
<h2>Request isolation matters more than friendly types</h2>
<p>On the server, create user-specific state in request scope. A process-wide Query client or mutable user store can accidentally carry account data between requests if used incorrectly.</p>
<p>Test two accounts concurrently through SSR and the real CDN. Inspect returned HTML and serialized data, then switch accounts in the same browser. Sign-out should remove private client cache state as well as the cookie.</p>
<p>The <a href="https://tanstack.com/start/latest/docs/framework/react/guide/production-checklist">Start production checklist</a> explicitly addresses these checks. They are useful for either framework.</p>
<p>A type-safe cache key still needs the correct identity dimensions. If two tenants share the same key, the compiler can happily confirm that the wrong tenant&#39;s response has the expected shape.</p>
<h2>Hosting is part of the choice</h2>
<p>Start&#39;s <a href="https://tanstack.com/start/latest/docs/framework/react/guide/hosting">hosting documentation</a> describes provider-specific integrations and build outputs. Next&#39;s <a href="https://nextjs.org/docs/app/guides/self-hosting">self-hosting documentation</a> describes multi-instance caches, deployment identifiers, Server Function encryption keys, and streaming considerations.</p>
<p>Both require attention outside the route code. Verify the adapter, runtime APIs, environment-variable timing, asset paths, and proxy buffering. A container deployment and an edge-worker deployment are not interchangeable merely because both serve HTTP.</p>
<p>For Next across multiple instances, plan shared cache behavior and invalidation coordination where needed. For Start, verify the selected server output and host bindings rather than assuming the development runtime is the production runtime.</p>
<p>Do not judge self-hosting by whether <code>npm start</code> returned a page once. Judge whether the intended features behave correctly through the intended infrastructure.</p>
<h2>Keep an old browser alive during the test</h2>
<p>Server-function identifiers and client assets belong to a build. A tab can retain an older build while the server deploys a newer one.</p>
<p>Next documents deployment skew protections, including deployment identifiers; their exact behavior matters for self-hosting. Start also generates server-function identities, so test old-client calls against the actual deployed output rather than assuming type sharing guarantees runtime compatibility.</p>
<p>Retain required immutable assets. Decide whether an incompatible client should reload, and what happens to unsaved edits when it does. A full reload is a recovery mechanism, not a product-neutral event.</p>
<p>This check can matter more to an internal editing application than the difference in a clean first-load demo.</p>
<h2>Include maintenance in the experiment</h2>
<p>Ask another engineer to change the saved field, add a validation failure, and diagnose a controlled stale-view bug in each slice.</p>
<p>How much behavior can they find locally? Which conventions are familiar to the team? Are the deployment docs and libraries they depend on compatible with the pinned versions? Can they explain the cache lifecycle without relying on the person who built the example?</p>
<p>I would prefer this evidence to a general claim that one framework is simpler. Simplicity depends on the workflow and on what the team already understands.</p>
<p>For server-composed routes and a team committed to the App Router model, Next is a reasonable choice. For router-driven, interaction-heavy work with a strong TanStack investment, Start is a reasonable choice. Either can be made awkward by layering on another system without deciding who owns what.</p>
<p>The framework I would choose is the one whose unhappy production path the team can describe clearly. A nice dashboard matters too. Fortunately, both can make one.</p>
<p>If you&#39;re weighing the two, bring one mutation and the hosting target rather than a feature matrix. I&#39;d be interested in where the ownership of data becomes unclear in that concrete path.</p>
]]></content:encoded>
      <pubDate>Tue, 20 Oct 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TanStack Start</category>
      <category>Next.js</category>
      <category>Architecture</category>
      <category>Production</category>
    </item>
    <item>
      <title>Freelance Architect or Agency: Who Keeps the Decisions?</title>
      <link>https://ma-x.im/blog/freelance-architect-vs-agency</link>
      <guid isPermaLink="true">https://ma-x.im/blog/freelance-architect-vs-agency</guid>
      <description>Choosing external engineering support is partly about capacity, but the harder questions concern decision ownership, continuity, review authority, and what remains usable after the engagement ends.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/freelance-architect-vs-agency/cover.png" alt="Freelance Architect or Agency: Who Keeps the Decisions?" /></p>
<p>A team asks for an architect, but the work waiting for them is a backlog of implementation tickets.</p>
<p>Another asks an agency to build a feature, while every consequential decision still needs approval from an internal team that has no time to review it.</p>
<p>Neither arrangement is necessarily wrong. They are simply describing a different purchase from the one in the proposal.</p>
<p>When choosing between an independent engineer and an agency, I would spend less time comparing job titles and more time describing who owns the decisions while the work is happening. That tends to matter after the first few meetings, when everyone has access and the easy questions have been answered.</p>
<p>This is a way to evaluate an engagement, not a claim about particular suppliers, prices, or a universal ranking of one model over the other.</p>
<h2>First separate diagnosis from capacity</h2>
<p>Sometimes the team knows what it needs to build and lacks enough delivery capacity. Sometimes it has plenty of developers but cannot explain why every change crosses five modules. Sometimes a deadline exposes both problems at once.</p>
<p>An architecture-focused independent engagement can make sense when the central work is concentrated judgement: inspect a system, identify boundaries, work with the internal team, and make a few consequential changes.</p>
<p>An agency can make sense when the work needs a coordinated mix of implementation, testing, design, delivery management, and continuity across several people.</p>
<p>Those are tendencies, not guarantees. An independent engineer may deliver a complete feature; an agency may provide one specialist. The actual assigned people and operating agreement matter more than the supplier category.</p>
<p>I would write the first outcome in plain terms. &quot;Reduce the number of modules needed to change policy validation&quot; is more testable than &quot;improve architecture.&quot; &quot;Own delivery of this workflow through acceptance and handover&quot; is clearer than &quot;provide a frontend team.&quot;</p>
<h2>Responsibility needs corresponding authority</h2>
<p>If an external architect is responsible for quality but cannot change boundaries, influence sequencing, or obtain review decisions, the responsibility is mostly ceremonial.</p>
<p>The opposite arrangement is also uncomfortable: an external team makes choices that the internal owners must maintain, without involving them until handover.</p>
<p>For decisions with lasting consequences, agree who proposes, who approves, and who maintains. This does not need an elaborate governance document. A small record can be enough:</p>
<pre><code class="language-text">Decision: separate payment validation from page orchestration
Proposed by: external engineer
Approved by: internal payments owner
Maintained by: payments team
Evidence: two representative flows and regression tests
Fallback: keep the old adapter during migration
</code></pre>
<p>The example is hypothetical. Its useful property is that someone still owns the result after the external person&#39;s last invoice.</p>
<h2>Review availability limits delivery</h2>
<p>Adding implementation capacity does not increase the internal team&#39;s review or decision capacity automatically.</p>
<p>If an agency submits ten pull requests a week and the internal team can carefully review three, work accumulates at the boundary. More developers may increase that queue rather than shorten the release.</p>
<p>An independent engineer working on a difficult architectural change can encounter the same problem. One large refactor waiting for a busy owner is not inherently easier to absorb than several ordinary features.</p>
<p>I would include reviewer availability in the plan: expected turnaround, who can make product decisions, when integration questions get resolved, and how work is split into changes the team can evaluate.</p>
<p>This is particularly important when acceptance depends on integrations the supplier cannot access or change. A delivery date that assumes immediate internal answers is a date with an unstated dependency.</p>
<h2>Prefer demonstrated uncertainty over polished certainty</h2>
<p>Before a substantial commitment, a small paid diagnostic or bounded delivery slice can reveal the working relationship.</p>
<p>Choose something representative rather than conveniently isolated: a flow with an awkward API contract, an existing shared component, a realistic test requirement, and an internal reviewer.</p>
<p>The useful output includes what was discovered, what changed, what remains uncertain, and which assumptions affect the next estimate. An apparently slower answer that identifies a dependency may be worth more than a confident schedule built without examining it.</p>
<p>I would also distinguish an estimate for implementation from the time waiting on decisions, environments, and approvals. They belong in the same delivery discussion, but they are not the same kind of work.</p>
<h2>Continuity is a mechanism, not a headcount</h2>
<p>An independent engineer has a clear availability risk: one person may be absent or the engagement may end.</p>
<p>An agency may offer replacement capacity. That does not guarantee the replacement already understands the system. A named specialist can leave; an account can be reassigned; context still has to transfer.</p>
<p>Ask how continuity actually works. Which people have read the critical code? Where are decisions stored? Can another engineer deploy, reproduce an issue, and explain the outstanding risks without a private conversation with the original author?</p>
<p>For both models, I would favor shared context in the client&#39;s systems, small decisions recorded near the code, and at least one internal engineer involved in consequential changes.</p>
<p>Continuity becomes less fragile when it is exercised during delivery rather than promised in a final document.</p>
<h2>The work should remain inside the client&#39;s control</h2>
<p>Source repositories, deployment accounts, domains, package ownership, and design assets need deliberate ownership. An engagement can finish with working software and still leave the client dependent on credentials or infrastructure controlled by the supplier.</p>
<p>Use client-controlled repositories and appropriately scoped access where possible. Name any exception and the transfer procedure before it becomes necessary.</p>
<p>The same applies to automation. If a supplier uses AI-assisted tooling, clarify permitted data handling, review expectations, generated-code responsibility, and what configuration belongs to the client. Faster implementation does not change who must verify the result or who may access private source and customer information.</p>
<p>I would not equate a supplier&#39;s preferred toolchain with a requirement for the client to adopt it permanently.</p>
<h2>Avoid a handover that only describes files</h2>
<p>A directory tour helps. It does not answer why a boundary exists, which alternatives were rejected, or what would break if the API changes.</p>
<p>A useful handover includes the important decisions, reproducible checks, release and rollback steps, known limitations, and the first likely maintenance changes. Keep it proportionate; six understandable notes are better than a large template nobody can navigate.</p>
<p>Have an internal engineer perform one change and one deployment before the engagement ends. The external person can assist, but should not quietly do every step that the exercise is intended to transfer.</p>
<p>That is a more convincing ownership test than asking whether everyone has read the documentation.</p>
<h2>Architecture needs an implementation consequence</h2>
<p>If the engagement is mostly architecture, the deliverables should still meet the code at some point.</p>
<p>A dependency diagram can identify a problem. A bounded migration, representative slice, or working contract test shows whether the proposed boundary is usable. Without that contact, the design may be elegant while the team&#39;s practical cost remains unknown.</p>
<p>Conversely, measuring only shipped tickets can discourage the diagnostic work needed to prevent the next expensive change. Agree which technical outcomes matter: fewer coupled changes, faster verification, clearer ownership, or an integration that no longer needs several teams to coordinate every edit.</p>
<p>I would avoid treating diagrams and implementation as competing definitions of productive work. They should inform each other, with evidence appropriate to the risk.</p>
<h2>Choose the arrangement that can leave well</h2>
<p>For a focused diagnosis with a capable internal team, I would often consider an independent senior engineer or architect. For a delivery stream needing several disciplines and coordinated capacity, I would consider an agency with named people and explicit ownership.</p>
<p>If the organization has nobody available to own decisions, review changes, or maintain the result, neither category solves that absence by itself. The operating model needs attention before more external capacity arrives.</p>
<p>The end of the engagement is a useful design constraint from the beginning. What should the internal team be able to do without asking the supplier? Which promises must remain true when a person changes?</p>
<p>I would rather make those questions slightly awkward during selection than leave them until the final week. By then the software may be complete, and the ownership may still be under construction.</p>
<p>If you&#39;re deciding between these arrangements, tell me which decision or delivery responsibility is currently unowned. That detail makes the comparison much more useful than the labels on the proposals.</p>
]]></content:encoded>
      <pubDate>Sun, 18 Oct 2026 00:00:00 GMT</pubDate>
      <category>Engineering Leadership</category>
      <category>Architecture</category>
      <category>Delivery</category>
      <category>Working With Teams</category>
    </item>
    <item>
      <title>What a Skill Changed, and What It Didn&apos;t</title>
      <link>https://ma-x.im/blog/agent-playbook-what-a-skill-changed</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-what-a-skill-changed</guid>
      <description>A successful task can depend on a skill, existing instructions, tool access, tests, or a person correcting the assumption. Evaluating agent workflows means keeping those contributions apart instead of turning one good result into a productivity claim.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-what-a-skill-changed/cover.png" alt="What a Skill Changed, and What It Didn&apos;t" /></p>
<p>A task went well with a reusable skill enabled.</p>
<p>It also had clear requirements, repository instructions, connected tools, a useful test, and an engineer who corrected an early assumption.</p>
<p>Which part made the result better?</p>
<p>I would not answer that from the final diff alone. The diff can establish what changed. It cannot, by itself, tell us which part of the workflow caused the change to be correct.</p>
<p>I participate in evaluating reusable agent skills and refining how they are applied to ordinary engineering work. The interesting question is not whether the process produced a polished artifact. It is what observable contribution the skill made, what it cost, and what remains unknown.</p>
<p>This article describes an evaluation approach. It does not report internal programme results, measurements, or a controlled comparison that has established a productivity gain.</p>
<h2>A skill is one input among several</h2>
<p>A reusable skill packages instructions or a workflow. It may help discover context, plan a change, validate it, or handle review. It is not a separately trained model or an autonomous employee.</p>
<p>Its contribution depends on what was already present.</p>
<p>If repository rules already require a focused check immediately after an edit, a skill that repeats that instruction may add consistency, but it is not evidence that the check originated with the skill. If a connected tool supplies current requirements, the tool can explain why the agent avoided a stale assumption.</p>
<p>The engineer&#39;s domain knowledge matters too. A person may notice that two services disagree, narrow the investigation, or reject an obsolete proposal. Leaving that correction out makes the skill look more independent than it was.</p>
<p>I want the record to include those existing controls. Otherwise we are comparing a named framework with an imaginary workflow that has no useful practices of its own.</p>
<h2>Observe behavior before assigning credit</h2>
<p>The earlier <a href="https://ma-x.im/blog/agent-playbook-evaluation-is-the-missing-loop">evaluation article</a> discusses what to measure. Here the narrower question is which explanation the observations support.</p>
<p>Use a deliberately illustrative record, not a real reporting form or an internal task:</p>
<pre><code class="language-text">Task: update a client without changing its callers
Observed behavior: agent checked a contract before editing
Existing control: repository instructions also require that check
Correction: engineer identified one stale source
Verified outcome: retained change passes the relevant gates
Skill contribution: may have reinforced discovery; attribution unresolved
Residual risk: deployment compatibility needs a separate decision
</code></pre>
<p>The conclusion is modest because the evidence is modest. That is not a failed evaluation. It is an evaluation that did not replace uncertainty with enthusiasm.</p>
<p>If the skill visibly introduced a previously missing check, say so. If the task does not distinguish its effect from existing instructions, say that instead.</p>
<h2>A useful outcome is not always delivered code</h2>
<p>A cross-repository investigation can end with an obsolete PR being closed. An architecture task can produce a proposal with explicit assumptions for discussion. A session-handoff change can produce a clearer continuation scheme.</p>
<p>Those outcomes have value. They are not interchangeable with a merged feature, an approved architecture, or guaranteed reproducibility.</p>
<p>Describe the actual artifact and its decision state. A reviewable proposal is ready to evaluate; it is not production rollout. A documented handoff reduces reliance on conversation history; it does not establish identical behavior across models and people.</p>
<p>In the supplied generalized experiences behind this series, recovery and refinement are part of the work. I would not remove the initial incomplete diagnosis simply because the final action was sensible.</p>
<p>The sequence explains what the process learned. A cleaned-up success story explains much less.</p>
<h2>Separate work from overhead without pretending overhead is worthless</h2>
<p>Implementation, framework setup, planning, repeated approvals, and reporting take different kinds of effort.</p>
<p>A specification can prevent an expensive mistake. It can also repeat a small task&#39;s already clear requirements. Reporting can support honest evaluation. It can also consume time that should be counted rather than hidden.</p>
<p>Keep normal engineering effort, framework overhead, and reporting effort distinguishable. Do not turn them into a single impressive number before understanding what each category contains.</p>
<p>The classification will sometimes require judgement. A discussion that clarifies an ambiguous requirement is engineering work even if a framework prompted it. A template completed only to satisfy the framework is a different contribution.</p>
<p>I would make that distinction visible rather than use it to prejudge whether the framework is good or bad.</p>
<h2>Tiny tasks do not need a retrospective specification</h2>
<p>For a small, clear change, the ticket and nearby contract may already be an adequate written requirement. A long specification can create another artifact to review without resolving a real uncertainty.</p>
<p>For ambiguous, risky, or multi-session work, a plan and checkpoints before implementation can be useful. They capture assumptions while there is still time for them to guide the work.</p>
<p>Do not write a specification afterwards and present it as evidence that the task followed specification-driven development. A retrospective note can explain the result, but it did not shape decisions that happened before it existed.</p>
<p>This is a simple chronology rule. It prevents polished documentation from making the process look more formal than it actually was.</p>
<h2>Not using a skill can be the proportionate choice</h2>
<p>An evaluation can tempt people to apply every available skill to every task so the record has plenty to discuss.</p>
<p>That changes the thing being evaluated. We are no longer observing useful selection; we are imposing ceremony for evidence collection.</p>
<p>Use a skill when it helps the task&#39;s current stage and risk. Record when a tiny or non-coding task offers limited evidence. Do not manufacture a repository-wide plan or another planning file after the fact to fill a reporting category.</p>
<p>A good workflow should be able to say &quot;not needed here&quot; without that becoming an admission that the whole framework failed.</p>
<h2>Repeated success still has confounders</h2>
<p>Several tasks going well can suggest a useful pattern. It does not automatically establish a causal productivity improvement.</p>
<p>Tasks differ in difficulty, familiarity, requirements, code quality, tools, models, and human involvement. An experienced engineer may become better at the domain during the same period the skills are introduced.</p>
<p>Without an appropriate baseline and comparison, avoid percentages, cost savings, or defect-rate reductions. Task counts and green checks are observations, not impact metrics by themselves.</p>
<p>Qualitative findings remain worthwhile: a missing boundary became explicit, a handoff had a clearer source of current decisions, or the process detected an unrelated generated change before review.</p>
<p>Name them as observations. They do not need a fabricated multiplier to be interesting.</p>
<h2>Keep the evaluation record separate from public storytelling</h2>
<p>An internal assessment may need task-level evidence, correction history, and reporting detail. A public article does not need to reproduce those artifacts.</p>
<p>Use generalized, explicitly labelled composite examples. Remove internal identities, programme details, source snippets, links, counts and model policy. Do not attach an employer&#39;s name to an anonymized case merely because the employment record is already public.</p>
<p>Even a collection of individually harmless details can make an episode identifiable. Preserve the engineering mechanism while changing or omitting unnecessary specifics.</p>
<p>The public claim should be something the available evidence can support without inviting the reader to inspect a closed system. &quot;I evaluated and refined workflows&quot; is a different claim from &quot;this programme improved company delivery.&quot;</p>
<h2>Feed the result back into the right layer</h2>
<p>When an error repeats, decide which control should change.</p>
<p>An outdated repository convention belongs in repository context. A general validation habit may belong in a reusable workflow. A missing permission boundary may require tool access to change, not another paragraph in the prompt.</p>
<p>If the failure came from stale task evidence, repair the handoff or current-state check. If the skill added unnecessary steps, narrow when it applies. If the result remains ambiguous, keep the ambiguity in the record rather than forcing a favorable or unfavorable rating.</p>
<p>This is where evaluation becomes engineering work. The record is not the final product; the corrected operating arrangement is.</p>
<blockquote>
<p>Useful attribution is specific enough to change the next task and modest enough not to invent a cause.</p>
</blockquote>
<h2>A smaller conclusion can be more reusable</h2>
<p>I do not need every evaluation to end with a winner. I need to know which practice helped under which conditions, what required correction, and what the evidence could not distinguish.</p>
<p>That lets the next task inherit a more useful check or a simpler process instead of a larger claim.</p>
<p>The five articles in this wave share that approach. Context needs freshness. Green checks need scope. Tool output needs inspection. Reviews need a revision. Skills need honest attribution. None of those mechanisms removes the need for engineering judgement; they give it better material to work with.</p>
<p>That is the agentic engineering I want to describe: an environment that can learn from ordinary failures without pretending the agent was correct all along.</p>
<p>If a skill helped your work, what changed in the actual sequence of decisions? I&#39;d be interested in that observation, including what your existing tools or judgement contributed alongside it.</p>
]]></content:encoded>
      <pubDate>Sat, 17 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Agentic Engineering</category>
      <category>Evaluation</category>
      <category>Engineering Workflow</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>The Backup Exists. The Restore Still Needs Work.</title>
      <link>https://ma-x.im/blog/full-stack-backups-and-recovery</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-backups-and-recovery</guid>
      <description>A restored database can disagree with payment providers, object storage, jobs, and deployed code. Recovery is a coordinated system change, with controlled side effects and evidence that the recovered state is usable.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-backups-and-recovery/cover.png" alt="The Backup Exists. The Restore Still Needs Work." /></p>
<p>The database restores successfully to 10:42.</p>
<p>The payment provider has transactions through 11:07. Object storage contains invoices created after the restore point. The queue still holds jobs referring to orders the restored database does not remember.</p>
<p>The backup worked. We have not yet recovered the application.</p>
<p>I think this is the useful distinction to make before an incident. Recovery is not one storage operation. It is deciding which facts remain authoritative after different parts of the system have moved to different points in time.</p>
<h2>Availability and recovery solve different failures</h2>
<p>A replica can keep the service available after one machine fails. It may also faithfully replicate the accidental deletion we are trying to undo.</p>
<p>A backup preserves another state. Point-in-time recovery can reconstruct a database using a base backup and subsequent transaction-log records, within the retained recovery window. Its value depends on those records being available and the restore procedure actually working.</p>
<p>Neither mechanism automatically restores object storage, credentials, queue state, or external services.</p>
<p>I would define the failure being covered: infrastructure loss, accidental deletion, corruption, an incompatible migration, or compromised access. A design that handles the first can still be inadequate for the others.</p>
<h2>Put a business meaning behind RPO and RTO</h2>
<p>Recovery point objective describes acceptable data loss. Recovery time objective describes acceptable time to restore the required service.</p>
<p>&quot;Fifteen minutes&quot; sounds specific. It still needs a meaning. Can fifteen minutes of submitted support forms disappear? Of inventory reservations? Of charged orders? Those are different promises.</p>
<p>Some operations need reconciliation from another authoritative system even when the database recovery point is acceptable. A payment provider may tell us about a charge the restored database lost. We need a stable reference linking it back to the business operation.</p>
<p>For recovery time, include more than restoring bytes: provisioning, access approval, decryption keys, application startup, verification, reconciliation, traffic switching, and safe resumption of workers.</p>
<p>I would measure the whole exercise. A ten-minute restore inside a two-hour recovery procedure is not a ten-minute RTO.</p>
<h2>Restore beside production, not blindly over it</h2>
<p>For a logical data mistake, restoring an isolated instance gives us room to inspect the recovered state and compare it with current production. We may be able to repair a specific set of rows instead of rewinding everything.</p>
<p>Keep the recovery environment unable to send real emails, charge payments, consume production queues, or execute live webhooks. A copied database can contain pending work that application startup eagerly resumes.</p>
<p>Use controlled configuration and network access, not merely a promise that nobody will click the dangerous button. Preserve the original state as evidence while deciding which changes to apply.</p>
<p>Sometimes a full cutover is necessary. Even then, the new instance should be verified before traffic moves, with a deliberate plan for connections, endpoints, and old application instances.</p>
<p>DNS changes have the <a href="https://ma-x.im/blog/full-stack-domains-and-proxies">overlap behavior</a> discussed earlier. Applications may also cache connection settings or continue using old pools. Changing a database endpoint is a release step, not a universal disconnect command.</p>
<h2>Match data with code and configuration</h2>
<p>A database restored to yesterday may have yesterday&#39;s schema. Today&#39;s application may require a column that did not exist then.</p>
<p>Record the schema migration state alongside artifact and configuration revisions. Retain deployable artifacts, not just source code that could theoretically be rebuilt with today&#39;s package registry.</p>
<p>An <a href="https://ma-x.im/blog/full-stack-zero-downtime-migrations">expand/contract migration</a> gives recovery more options, but it does not remove the need to choose a compatible application version. Running every pending migration automatically against the restored instance may recreate the destructive change we were trying to investigate.</p>
<p>Secrets matter too. A backup encrypted with an unavailable key is not useful. A restored application with revoked credentials cannot necessarily reach its dependencies. Back up and test the recovery of access paths without scattering secret values through documents or logs.</p>
<h2>External effects do not rewind with the database</h2>
<p>Consider an order that was paid at 10:55 and lost from a database restored to 10:42. Asking the customer to retry can charge them again unless the payment operation has a durable, stable identity.</p>
<p>Reconcile against the provider using idempotency keys, merchant references, and the provider&#39;s authoritative transaction state. Do not infer a refund or charge from the absence of a local row alone.</p>
<p>Email is another external effect. A replayed confirmation job may send a second message. An invoice number may already have been issued. A shipment may already be in transit.</p>
<p>The recovery procedure should describe those consequences and the systems that settle them. We cannot restore the world to match a snapshot. We can reconstruct our records so they agree with the world that continued operating.</p>
<p>This is where the identity choices from the <a href="https://ma-x.im/blog/full-stack-api-contracts">API</a> and <a href="https://ma-x.im/blog/full-stack-background-jobs">queue</a> articles become more than implementation conveniences.</p>
<h2>Queues and caches need a recovery policy</h2>
<p>A restored outbox may contain events already published before the incident. A current queue may contain messages for records absent from the recovered database.</p>
<p>Pause consumers while reconciling. Decide which messages to preserve, replay, quarantine, or discard. Idempotent consumers reduce the cost of duplicates, but they still need a coherent business state to operate on.</p>
<p>Do not delete the queue simply to make its depth look tidy. It may contain the only remaining record of accepted work.</p>
<p>Redis also needs role-specific handling. Rebuild a disposable product cache from the recovered source. For sessions, consider whether invalidating them is safer than restoring older authorization state. For locks or scheduler metadata, old values may be actively misleading.</p>
<p>&quot;Restore every dependency&quot; is not always the right instruction. Some state should be restored, some reconciled, and some deliberately recreated.</p>
<h2>Verify meaning, not only row counts</h2>
<p>A database that starts and contains the expected number of rows is encouraging. It does not prove the checkout workflow is usable.</p>
<p>Check relationships, critical constraints, recent business operations, object references, account permissions, and one controlled end-to-end flow. Compare representative records with external authoritative systems where necessary.</p>
<p>Keep enough telemetry to tell when corrupted or missing data first appeared. Otherwise choosing a restore point becomes a sequence of guesses, each followed by another expensive restore.</p>
<p>Data integrity checks and recovery logs should avoid publishing private customer content. The people conducting a recovery need controlled access, not an unrestricted data export pasted into the incident channel.</p>
<h2>Test the access path that an incident will use</h2>
<p>A recovery exercise run by an administrator with every permission can conceal missing privileges in the actual emergency role. Test who can start the restore, decrypt it, deploy compatible code, change routing, and release the isolation controls.</p>
<p>For compromise scenarios, consider independent backup protection: restricted deletion, retention controls, and appropriate account or region separation. If the same credential can destroy production and every backup, the copies share a rather important failure mode.</p>
<p>There is no universal arrangement that makes all of this free. The choices should reflect the actual data, recovery promise, and threat model.</p>
<p>Document who makes the cutover decision and who handles reconciliation. That is operational ownership, not an excuse to collect an impressive runbook nobody has tried.</p>
<h2>A useful drill has an inconvenient detail</h2>
<p>Try restoring with an old schema, an unavailable primary region, a rotated credential, or a job already delivered once. Choose one complication rather than pretending the exercise must reproduce every disaster at once.</p>
<p>Measure the result, record the steps that required undocumented knowledge, and repair those gaps. A failed rehearsal is useful when it changes the recovery path before the real incident.</p>
<blockquote>
<p>A backup preserves data. A rehearsed recovery preserves the ability to use it safely.</p>
</blockquote>
<p>That is where I would end this field guide. The database, cache, queue, proxy, and application are not independent checkboxes. Their boundaries become visible when a request fails, a deployment overlaps, or one component returns to an earlier state.</p>
<p>I would be satisfied with an ordinary system whose recovery procedure is unremarkable because it has been tested. That seems a more useful goal than another layer whose failure behavior we will learn later.</p>
<p>If you&#39;ve attempted a restore rehearsal, what took longer than the database restore itself? I&#39;d be interested in the dependency or decision the backup dashboard could not account for.</p>
]]></content:encoded>
      <pubDate>Fri, 16 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Database</category>
      <category>Recovery</category>
      <category>Reliability</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>A Review Belongs to a Commit</title>
      <link>https://ma-x.im/blog/agent-playbook-review-belongs-to-commit</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-review-belongs-to-commit</guid>
      <description>Review automation has to track more than whether it has seen a PR. Findings, CI evidence, duplicate suppression and guarded actions need a revision and a lifecycle, while final approval remains an accountable human decision.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-review-belongs-to-commit/cover.png" alt="A Review Belongs to a Commit" /></p>
<p>A reviewer sees a change and raises a useful concern. The author pushes another commit.</p>
<p>The comment is still there. The code it described may not be.</p>
<p>Human reviewers handle this imperfectly too. An automated reviewer makes the state-management problem easier to notice because it can repeat the same concern every time it runs, or confidently act on a conclusion from the previous revision.</p>
<p>The earlier <a href="https://ma-x.im/blog/github-ai-pr-reviewer">PR-reviewer article</a> focused on useful language and visibility into a personal tool. This article is about a narrower engineering problem: what a finding, check, or review decision refers to as the code changes.</p>
<p>I have developed commit-aware tracking, duplicate-finding safeguards, dry-run checks, and guarded actions in personal review tooling with AI assistance. That describes development of mechanisms, not current continuous operation, organization-wide adoption, or a replacement for human approval.</p>
<h2>A PR number is not the reviewed object</h2>
<p>The PR is a container for an evolving proposal. The head commit identifies one version of that proposal. The base revision and comparison mode also affect what the reviewer sees.</p>
<p>If an automated tool records only &quot;PR reviewed,&quot; it cannot distinguish unchanged work from a new commit. If it records only head SHA, it may miss a changed base or integration result that matters to its conclusion.</p>
<p>A useful record identifies what was reviewed and under which assumptions:</p>
<pre><code class="language-typescript">type ReviewSnapshot = {
  headSha: string;
  baseSha: string;
  comparison: &#39;pull-request-diff&#39;;
  rulesRevision: string;
  checkedAt: string;
};
</code></pre>
<p>This is an illustrative type, not an internal implementation. The rules revision matters because the same code can be assessed differently after the checking policy changes.</p>
<p>I would not let a convenient database row imply more history than it contains. &quot;Reviewed&quot; should be explainable as a particular observation, not a permanent property of the PR.</p>
<h2>CI has a revision too</h2>
<p>A green check from an earlier commit does not establish the state of the current one. A required integration check may run on a merge result rather than the branch head. Those are different artifacts.</p>
<p>Before review actions, identify which checks are relevant, which revision they exercised, and whether they are finished. A skipped check or an optional success is not necessarily equivalent to every required gate passing.</p>
<p>Even then, CI evidence has the scope limits discussed in Green Checks Only Cover What You Checked. It can support a reviewable state without proving the absence of semantic defects.</p>
<p>I want a tool to report that distinction rather than promote &quot;green CI&quot; into &quot;safe to approve.&quot;</p>
<h2>A finding needs identity and evidence</h2>
<p>Line numbers are useful for placement. They are poor long-term finding identities.</p>
<p>An author can move the same risky expression to another line. The finding remains relevant, but a line-based key changes and the bot may publish it again. Conversely, new code at the old line can have a different risk that a naive key suppresses.</p>
<p>A more semantic identity can include the rule, affected symbol, risk category, and relevant normalized evidence. Keep the exact source revision alongside it.</p>
<p>Normalization introduces its own trade-off. Strip too much context and different findings collapse into one fingerprint. Keep every character and harmless formatting changes produce duplicates.</p>
<p>Treat a fingerprint as a useful matching mechanism, not proof that two concerns are identical. Re-evaluate the current evidence before deciding the old finding still applies or no new comment is needed.</p>
<h2>Suppressed does not mean resolved</h2>
<p>A repeated finding can be current and intentionally not republished. A missing finding can mean it was fixed, became unobservable in the selected diff, or was skipped because the analysis failed.</p>
<p>Those states need different names.</p>
<pre><code class="language-text">current: the concern is supported by the latest evidence
unchanged: current, but no repeated comment is needed
resolved: evidence supports that the concern no longer applies
not-observed: this run did not establish either outcome
needs-human: the tool cannot settle the question safely
</code></pre>
<p>The exact storage model can vary. The distinction keeps duplicate suppression from quietly becoming automatic resolution.</p>
<p>An unresolved human thread is another state the tool should not overwrite simply because its own detector no longer sees the pattern. The thread may concern context or a decision that the detector never understood.</p>
<h2>Recheck state before publication</h2>
<p>Analysis can take time. While it runs, the author can push new code. A comment created from the original diff may now point to invalid lines or describe a risk that has already changed.</p>
<p>Re-read the relevant state before publishing. If it no longer matches the reviewed snapshot, stop, discard the pending action, or schedule another review. Do not silently attach the finding to a different revision because that makes the API call easier.</p>
<p>The same applies to stronger actions. A guarded action should carry the expected revision and use a platform-supported condition where available:</p>
<pre><code class="language-typescript">type GuardedReviewAction = {
  expectedHeadSha: string;
  snapshotId: string;
  action: &#39;publish-review&#39;;
};
</code></pre>
<p>This type expresses the requirement; it does not implement an atomic guarantee. Reading head and then calling an API leaves a time-of-check/time-of-use interval. If the platform cannot condition the action atomically, handle that limitation explicitly and avoid unsupported claims about stale-action prevention.</p>
<p>A fresh check reduces risk. It does not freeze another person&#39;s branch.</p>
<h2>A dry run has its own evidence boundary</h2>
<p>A dry run can show which findings and actions a tool would produce. It is useful for inspecting scope, tone, duplicates, and targeting without creating external effects.</p>
<p>It does not prove that the later API call will be authorized, attach to the intended line, or observe the same PR state. Nor does a successful API call prove the review content was correct.</p>
<p>Use the dry run to inspect proposed actions. Use publication checks to verify current state and delivery. Keep the two results separate in the control interface.</p>
<p>If the inline attachment fails, a body-level review may preserve the concern, but should still identify the source revision. A fallback should not turn a precisely scoped observation into an unexplained general warning.</p>
<h2>Human decisions need context, not silent reassignment</h2>
<p>A person may approve a particular revision. New code can change the basis for that decision. The tool should make that change visible and follow the platform&#39;s review policy rather than assume an earlier approval means the new code is accepted.</p>
<p>Whether an approval is dismissed, still valid, or requires a new decision depends on the repository&#39;s settings and accountable reviewers. The automation should not invent its own release authority.</p>
<p>Likewise, an AI suggestion that a dependency update appears routine is evidence, not permission to merge it. Any permitted automatic action needs its own narrowly defined authorization and technical controls. It should not be inferred from the absence of findings.</p>
<blockquote>
<p>A review result describes observed code. It does not transfer responsibility for whatever code arrives next.</p>
</blockquote>
<h2>Preserve enough history to explain the tool</h2>
<p>The control surface should answer ordinary questions: which revision was analyzed, what was published, what was suppressed, why a run stopped, and what remains undecided.</p>
<p>Record identifiers and outcomes with appropriate retention. Avoid storing raw credentials, private payloads, or unnecessary code copies just to make a log feel complete.</p>
<p>When the tool is wrong, the history should help reproduce the decision. Was the input stale? Did normalization merge two findings? Did a missing detector result get treated as resolution? Did the action target change between analysis and publication?</p>
<p>Those are repairable mechanisms. Without the history, every correction becomes a guess about the model&#39;s intentions.</p>
<h2>The quieter reviewer is not necessarily the better reviewer</h2>
<p>Duplicate suppression makes the conversation less noisy. That is useful. It can also hide an unresolved issue if the lifecycle is poorly modelled.</p>
<p>I would evaluate the tool on the relevance and traceability of its findings, not merely on how few comments it emits. Check unchanged code, moved code, corrected code, a new risk at the old location, and a head update during analysis.</p>
<p>Keep a human in charge of interpretation and approval. A review assistant should make that person&#39;s evidence easier to find, not create a reassuring status that no one can explain.</p>
<p>The next article, <a href="https://ma-x.im/blog/agent-playbook-what-a-skill-changed">What a Skill Changed, and What It Didn&#39;t</a>, examines the same attribution problem at workflow level: what a reusable skill changed, and what it did not.</p>
<p>If your review automation remembers that it saw a PR, what exactly does it remember about the code? That is a useful place to inspect before adding another action to it.</p>
]]></content:encoded>
      <pubDate>Thu, 15 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Code Review</category>
      <category>Developer Tooling</category>
      <category>Reliability</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>The Dashboard Is Green. The Order Is Missing.</title>
      <link>https://ma-x.im/blog/full-stack-production-observability</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-production-observability</guid>
      <description>Useful observability connects browser intent, API acceptance, durable state, and background outcomes. Sampling, cardinality, missing denominators, privacy, and telemetry failure determine which questions that connection can answer.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-production-observability/cover.png" alt="The Dashboard Is Green. The Order Is Missing." /></p>
<p>The API error rate is low. The workers are running. The browser reported no uncaught exception.</p>
<p>A user says their order never appeared.</p>
<p>All of those observations can be true. We measured the health of individual pieces, while the user described an outcome across several pieces. The missing part is the connection between them.</p>
<p>I do not think the answer is simply to log more. A larger pile of unrelated events gives us more to read, which is not quite the same thing as giving us more to understand.</p>
<h2>Start with the outcome we need to explain</h2>
<p>For checkout, I would distinguish browser intent, request acceptance, order persistence, payment confirmation, and follow-up work.</p>
<p>These are not synonyms. A click event proves that someone activated a control. A 200 response proves that an HTTP exchange returned that status. A database record proves that an order was stored. A confirmation email proves another effect entirely.</p>
<p>The <a href="https://ma-x.im/blog/react-playbook-analytics-as-a-layer">analytics layer</a> and an operational trace also serve different purposes. Product analytics can describe behavior; operational telemetry needs enough context to reconstruct a specific failure without assuming every recorded click became a completed purchase.</p>
<p>I would define the business success point explicitly, then work backwards to the evidence required at each boundary. Otherwise the most available metric quietly becomes the definition of success.</p>
<h2>A request id is useful, but it is not the whole identity</h2>
<p>A request id correlates one HTTP attempt. An idempotency key identifies one intent across retries. An order id identifies durable business state. A trace context links spans across participating services.</p>
<p>Keeping these separate is helpful:</p>
<pre><code class="language-json">{
  &quot;event&quot;: &quot;order.persisted&quot;,
  &quot;request_id&quot;: &quot;req_9c7&quot;,
  &quot;operation_id&quot;: &quot;checkout_62a&quot;,
  &quot;order_id&quot;: &quot;order_18f&quot;,
  &quot;trace_id&quot;: &quot;4bf92f3577b34da6a3ce929d0e0e4736&quot;,
  &quot;release&quot;: &quot;checkout-2026-10-14&quot;
}
</code></pre>
<p>These are synthetic identifiers. The record does not include a customer&#39;s name, email, address, token, or raw payment data.</p>
<p>A retry gets a new request id but retains the operation identity. A background job can link to the operation after the original request has ended. Depending on timing and instrumentation, a span link may describe that relationship more honestly than an endlessly extended parent span.</p>
<p>Use established trace propagation and instrumentation, such as OpenTelemetry, rather than inventing a collection of vaguely compatible headers. Still validate untrusted incoming context and keep authorization separate from any telemetry identity.</p>
<h2>Missing data has several explanations</h2>
<p>If a trace is absent, the request may never have happened. It may also have been sampled out, rejected by a collector, dropped from a full buffer, or generated by an uninstrumented component.</p>
<p>Head sampling decides early which traces to retain. It keeps costs bounded, but it cannot know at the start which request will fail late. Tail sampling can select after observing more of the trace, at the cost of buffering, coordination, and its own resource limits.</p>
<p>Neither gives perfect evidence automatically. Keep critical outcome counters and durable business records independently of a sampling decision. Record the telemetry pipeline&#39;s own drops and health so absence is less ambiguous.</p>
<p>For frontend error reporting, the same limitation applies. Browser extensions, network conditions, consent settings, and navigation can affect delivery. No recorded exception is not proof that no exception occurred.</p>
<h2>Denominators need their own definition</h2>
<p>&quot;Twenty failed orders&quot; is difficult to interpret without knowing how many attempts occurred and which attempts count.</p>
<p>Counting API requests exaggerates user intent when retries occur. Counting clicks includes actions rejected before a request. Counting created orders excludes failures before persistence.</p>
<p>For an outcome metric, define both numerator and denominator around the promise being measured. For example: distinct accepted checkout operations that reach a confirmed or explicit terminal state within a chosen interval.</p>
<p>Then describe exclusions: abandoned carts, expected validation failures, test traffic, and operations still inside their allowed processing window. An illustrative target is not a universal SLO.</p>
<p>The arithmetic is usually easy. The difficult part is resisting a reassuring percentage with a poorly chosen population.</p>
<h2>Cardinality is a modelling choice</h2>
<p>Metrics work well with bounded dimensions such as route templates, status categories, region, and service.</p>
<p>Attaching <code>order_id</code>, raw URLs, or customer email to every metric label creates huge numbers of distinct time series. It also turns the monitoring system into a rather expensive and unsuitable customer index.</p>
<p>Use <code>/orders/:id</code>, not every concrete order URL, as a metric dimension. Put specific identities in controlled logs or traces when needed, with retention and access appropriate to their sensitivity.</p>
<p>Histograms deserve some care too. Percentiles computed independently by instances cannot generally be averaged into the fleet&#39;s percentile. Aggregate compatible histogram observations rather than averaging whichever p95 numbers happen to be on the screen.</p>
<p>The goal is not to avoid detail. It is to put detail into the kind of evidence that can handle it.</p>
<h2>Frontend context can make a backend trace usable</h2>
<p>A server sees requests. The browser knows which route, interaction, and release led to them.</p>
<p>For an error-reporting tool such as Sentry, a release identifier and matching source maps can turn a minified stack into something useful. Upload source maps through controlled tooling; decide whether publicly serving source files is intended. A production error should identify the bundle that actually ran, not the latest source in main.</p>
<p>Session replay can help explain interaction sequences, but I would not enable it broadly without reviewing masking, consent, sensitive fields, retention, and access. &quot;It is an internal admin screen&quot; is often a reason for more care, not less.</p>
<p>Expected cancellation should not appear beside faults as though a user navigating away is a broken system. The <a href="https://ma-x.im/blog/react-playbook-abort-controller">AbortController article</a> covers that distinction at the interface boundary.</p>
<h2>An alert should name a response</h2>
<p>High CPU may deserve investigation. Missing orders deserve investigation for a more direct reason.</p>
<p>Prefer alerts connected to user-visible impact or exhausted safety margins: sustained error-budget consumption, queue age beyond the product allowance, connection-pool waits, failure to complete accepted operations, or a recovery dependency approaching expiry.</p>
<p>The notification needs an owner, a scope, and a useful next action. A chart link is helpful. So are a recent deployment marker and a runbook that says which evidence separates an application failure from a shared-dependency failure.</p>
<p>I would not page someone every time one instance reports a brief spike unless that spike calls for a meaningful intervention. Noise does not stay harmless; it changes what people notice later.</p>
<h2>Telemetry is another dependency</h2>
<p>Exporters have buffers, network calls, timeouts, and shutdown behavior. A slow collector should not cause the checkout handler to wait indefinitely or allocate unbounded memory.</p>
<p>Use bounded, asynchronous export appropriate to the instrumentation. Decide what can be dropped and what must be durably recorded elsewhere. Business events required for fulfilment belong in a durable mechanism such as the <a href="https://ma-x.im/blog/full-stack-background-jobs">transactional outbox</a>, not solely in a best-effort log stream.</p>
<p>Also test redaction failures and collector outages. Observability should help us explain failure without becoming its next cause or a route for leaking data.</p>
<blockquote>
<p>A telemetry event is evidence with delivery conditions, not a second database transaction.</p>
</blockquote>
<h2>Reconstruct one unsuccessful operation</h2>
<p>I would exercise a deliberately failed checkout and try to follow it from browser intent to its final state. Can we distinguish a request that never arrived from an order that persisted but lost its response? Can we find the background job? Can we identify the release and a retry without counting them as two purchases?</p>
<p>If those questions are difficult, another dashboard will not necessarily improve the situation. A better identity or an event at the missing boundary might.</p>
<p>The green dashboard becomes useful when we know exactly what its green means. Until then, I would keep the user&#39;s missing order as the more interesting observation.</p>
<p>The final part of this guide covers <a href="https://ma-x.im/blog/full-stack-backups-and-recovery">backups and recovery</a>: what happens when diagnosis is finished and some part of the system actually has to be rebuilt.</p>
<p>Which production question still sends you comparing timestamps across three tools? I&#39;d like to hear the missing connection, not just which monitoring product you use.</p>
]]></content:encoded>
      <pubDate>Wed, 14 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Observability</category>
      <category>Reliability</category>
      <category>Architecture</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>The Command Succeeded. The Scope Changed.</title>
      <link>https://ma-x.im/blog/agent-playbook-scope-after-tools</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-scope-after-tools</guid>
      <description>Hooks and generators can transform a small, reviewed change into a different committed artifact. Agent workflows need to inspect the output of automation, distinguish unrelated drift, and stop when the final scope no longer matches the task.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-scope-after-tools/cover.png" alt="The Command Succeeded. The Scope Changed." /></p>
<p>The agent inspects a small diff and runs the required command.</p>
<p>The command succeeds. A hook also regenerates a contract file from a newer source, and the resulting commit contains a change nobody asked for.</p>
<p>That is not a runaway agent. It is fairly normal repository automation doing what it was configured to do. The workflow still needs to decide whether the output belongs in this task.</p>
<p>I have used this kind of scope drift to refine the checks around delegated work. Reviewing an initial diff is useful; reviewing the artifact after automation is also necessary. They are not always the same artifact.</p>
<h2>The example is deliberately synthetic</h2>
<p>Consider this illustrative composite, not a file tree from an actual company repository.</p>
<p>A task updates one client dependency. Before the commit, the changes are:</p>
<pre><code class="language-text">package.json
package-lock.json
</code></pre>
<p>A repository hook refreshes generated API code. The commit now also contains:</p>
<pre><code class="language-text">src/generated/catalog-api.ts
src/generated/catalog-schema.json
</code></pre>
<p>The generator may be correct about the source it read. The new contract may be a legitimate change. Neither fact says it belongs in a dependency-only PR.</p>
<p>It could need another owner, another compatibility check, another release sequence, or simply a separate explanation. A successful generator run does not provide those decisions.</p>
<h2>There are several versions of the change</h2>
<p>A Git operation has more than one relevant state: working tree, index, commit tree, and the remote branch used by review.</p>
<p>An agent can inspect the working tree, stage a subset, and then assume the commit contains that subset. A hook can change files or restage content between those steps. Later, a push hook or generated build output can leave additional local changes that are not committed at all.</p>
<p>The useful question is &quot;which artifact did we verify?&quot;</p>
<p>These generic inspection commands address different artifacts:</p>
<pre><code class="language-sh">git diff --name-status
git diff --cached --name-status
git show --format= --stat HEAD
git show --format= HEAD
git status --short
</code></pre>
<p>The first two compare working and staged changes. The next two inspect the actual commit. The final status shows what remains outside it. None, by itself, establishes whether the whole task is correctly scoped.</p>
<p>I would inspect the post-hook commit before treating it as ready for review or push. If the repository runs consequential automation during push, inspect its effects too rather than assuming the operation left no new state.</p>
<h2>Capture the starting state</h2>
<p>Scope checks are less useful if the agent does not know which changes existed before it started.</p>
<p>A working tree can already contain another person&#39;s edits. A generated file may have been dirty before the task. A script may update it again, mixing pre-existing content with the agent&#39;s output.</p>
<p>Record the branch, base revision, and clean or dirty status before implementation. For relevant dirty files, understand the existing changes instead of treating everything outside the task as disposable.</p>
<p>This is especially important when recovering from generated drift. &quot;Remove unrelated files&quot; is not permission to overwrite a colleague&#39;s uncommitted work.</p>
<p>The safe recovery depends on provenance: which change was introduced by this operation, which was already present, and which now conflicts with the authorized work. If that cannot be established cheaply, stop and ask rather than manufacture a clean tree.</p>
<h2>A file allowlist helps, but it is not the contract</h2>
<p>For a bounded task, an expected file set can reveal surprises:</p>
<pre><code class="language-text">Expected: dependency manifest and lockfile
Observed: manifest, lockfile and generated service contract
Decision needed: does the contract change belong to this task?
</code></pre>
<p>Sometimes the right implementation legitimately needs another file. A test, a call site, or an adapter may be required to satisfy the original behavior. A strict allowlist should expose that question, not force an incomplete fix.</p>
<p>The reverse is also true: changes inside an expected file can be unrelated. A lockfile may contain several package updates. One generated module can include multiple contract changes with different owners.</p>
<p>Review semantics as well as filenames. The requested scope is an engineering constraint, not a glob pattern.</p>
<h2>Generated does not mean low risk</h2>
<p>Generated artifacts can encode public APIs, schemas, route tables, database clients, or configuration. A small mechanical change there can affect more consumers than a large handwritten component edit.</p>
<p>A tool may regenerate against a remote source whose revision is not recorded. Run it twice and it can produce two different outputs from the same local commit. The task has acquired an external input even though nobody edited an external file by hand.</p>
<p>Where practical, pin and record the generator version and input revision, or preserve the relevant source snapshot. Keep generated output reproducible enough to explain a future diff.</p>
<p>If a live input is intentional, review the new output with that knowledge. Do not present it as a consequence of the dependency change when it came from unrelated source drift.</p>
<h2>Do not bypass the gate just to avoid its output</h2>
<p>The tempting response is to disable the hook and finish the small PR.</p>
<p>That may discard a required repository control. It also avoids the useful question of why this hook introduces changes unrelated to the task.</p>
<p>Follow the repository&#39;s supported procedure. It may allow a separate generated update, a pinned input, a check-only mode, or an explicitly approved exception. The agent should not invent an exemption because the hook is inconvenient.</p>
<p>After separating unrelated drift, run the checks that matter for the retained change and inspect the final artifact again. A repaired scope still needs verification; the earlier green result may have observed a different set of files.</p>
<h2>Tool success is not scope approval</h2>
<p>An exit code tells us whether the tool considers its operation successful. It does not certify that the operation had the intended inputs or that every resulting change was authorized.</p>
<p>This applies beyond Git hooks. A formatter can touch unrelated documents. A package manager can alter registry URLs or transitive versions. A code migration can rewrite more call sites than the agreed rollout intended.</p>
<p>The agent&#39;s explanation should identify those effects, not compress them into &quot;all commands passed.&quot;</p>
<blockquote>
<p>The command&#39;s success belongs to the command. Approval of the resulting scope belongs to the task.</p>
</blockquote>
<p>That distinction allows useful automation without pretending tools understand the whole purpose of a change.</p>
<h2>Make the check repeatable, not the incident immortal</h2>
<p>After a scope problem, I would refine the workflow around the failure condition: record the starting state, inspect after mutating automation, and verify the artifact that will actually be reviewed.</p>
<p>I would not add a permanent paragraph describing every generated file from that one incident. Those details age quickly. The principle remains useful across different generators and repositories.</p>
<p>The rule also needs proportion. A read-only test command does not need the same artifact accounting as a commit hook that regenerates contracts. Use the checks where state can change, and keep the explanation local enough that the agent can apply it.</p>
<h2>The final diff should be unsurprising for the right reason</h2>
<p>An agent can produce a neat initial patch and still need help with the effects of the surrounding toolchain. That is not a contradiction. The repository is part of the execution environment, and it can change the result.</p>
<p>I want a reviewer to find the final scope unsurprising because the task explains every meaningful change, not because generated files were hidden from the description.</p>
<p>The next article, <a href="https://ma-x.im/blog/agent-playbook-review-belongs-to-commit">A Review Belongs to a Commit</a>, follows that reviewed artifact as it changes again. A later commit can make yesterday&#39;s finding or decision stale.</p>
<p>If a tool in your repository can change the artifact after its last inspection, where is the next inspection scheduled? That small gap is worth closing before adding more autonomy.</p>
]]></content:encoded>
      <pubDate>Tue, 13 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Agentic Engineering</category>
      <category>Git</category>
      <category>Scope Control</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>More Replicas, the Same Database</title>
      <link>https://ma-x.im/blog/full-stack-load-balancing</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-load-balancing</guid>
      <description>Horizontal scaling multiplies connection pools, retries, and deployment overlap as well as capacity. The useful questions concern admission limits, health-check meaning, draining, and which state a replacement instance can actually recover.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-load-balancing/cover.png" alt="More Replicas, the Same Database" /></p>
<p>The application is slow, so the autoscaler adds replicas.</p>
<p>Each new replica opens its normal database connection pool. The database was already waiting on locks. Now it has more callers waiting on the same locks.</p>
<p>CPU on the application instances improves. Checkout latency does not.</p>
<p>Nothing in that sequence is especially exotic. We scaled the part that could multiply, without checking the capacity of the part all those copies still share. I find that a more useful starting point than the usual discussion of which load-balancing algorithm looks fairest.</p>
<h2>The unit of scaling includes its dependencies</h2>
<p>An application replica is not just a process with some CPU. It is also a database pool, a set of cache connections, outbound calls, retry behavior, and startup work.</p>
<p>Consider a simplified connection budget:</p>
<pre><code class="language-text">database limit:                  200
reserved for administration:      20
workers and other services:        40
available to the web application: 140

8 replicas x 15 pool connections: 120
12 replicas x 15 connections:     180
</code></pre>
<p>These numbers are illustrative. A pool maximum does not mean every process immediately opens every connection, but it is a capacity promise we need to bound. A rolling deployment can temporarily run both old and new replicas. The maximum deployment overlap belongs in the budget too.</p>
<p>I would cap replica count and per-process concurrency together. A connection pooler may help, but it cannot make a transaction release its locks sooner or make a slow query cheaper.</p>
<h2>Queuing moves; it does not disappear</h2>
<p>If the application accepts work faster than it can finish, requests wait somewhere: at the proxy, inside the process, in the pool, or in the database.</p>
<p>An unlimited internal queue can make an overloaded service look healthy until requests exceed every client timeout. By then the service may still be doing work for callers that already gave up.</p>
<p>Explicit admission limits give the system a more useful response. Bound concurrency, reject excess work with a deliberate status, and decide whether a retry can safely occur. Do not spend thirty seconds doing an operation whose result the caller is no longer waiting for.</p>
<p>Cancellation matters, but cancelling a browser request does not automatically cancel the database operation. Deadline propagation needs to reach the layers that support it, with transaction cleanup handled deliberately.</p>
<p>I would prefer a small number of quick, understandable overload responses to a large number of ambiguous timeouts. That choice is less attractive on a throughput chart and more attractive during an incident.</p>
<h2>Retries can manufacture the next spike</h2>
<p>A browser retries. The gateway retries. The application retries its upstream call. Three attempts at each layer can multiply rather than merely add.</p>
<p>If the original work is slow because a shared dependency is saturated, retrying immediately adds more work to that dependency. The request deadline may also restart at each hop, extending the actual time spent far beyond what the caller expects.</p>
<p>Assign retry ownership. Use a bounded budget, backoff, jitter, and idempotency for writes. Honor useful rate-limit guidance such as <code>Retry-After</code> rather than asking every client to guess its own recovery rhythm.</p>
<p>Retries are appropriate when another attempt has a reasonable chance of finding different conditions. They are not a substitute for an overload policy.</p>
<h2>Stateless does not mean there is no state</h2>
<p>The practical meaning is narrower: another instance can handle the next request without relying on unique state in the previous instance&#39;s memory.</p>
<p>Sessions may live in a shared store. Uploaded files belong in durable object storage. Order state belongs in the database. Any replica can reconstruct what it needs.</p>
<p>Local caches are still possible. So are in-memory counters and background bookkeeping, provided they are explicitly disposable and do not claim global correctness.</p>
<p>Sticky sessions can keep a client on one instance, which may be useful for a specific workload. They do not make that instance durable. When it disappears, the unique state disappears too unless a recovery path exists.</p>
<p>I would describe required affinity as a constraint rather than advertise a system as stateless while quietly depending on the load balancer to remember each user.</p>
<h2>Health checks are routing policy</h2>
<p>A liveness check asks whether the process can continue. A readiness check asks whether it should receive new work. A synthetic business check asks whether a useful operation succeeds. They do not necessarily belong in the same endpoint.</p>
<p>Suppose every instance&#39;s readiness check requires Redis to respond. Redis has a shared outage, so every instance becomes unready. Removing all instances may be correct for a session-only application that cannot authenticate anyone. It may be unnecessary for public product pages with a database fallback.</p>
<p>Restarting all processes because a shared dependency is slow can also worsen recovery by creating connection and cache-warming bursts.</p>
<p>There is a specific AWS detail worth knowing: <a href="https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html">ALB fails open when a target group has only unhealthy registered targets</a>. It can route to those targets rather than treating health failure as a guarantee of no traffic.</p>
<p>This is not a recommendation to return false health results. It is a reminder to understand the platform&#39;s actual response and keep application safeguards in place. The health check does not become a security boundary just because the dashboard colors it red.</p>
<h2>Scaling needs time to become useful</h2>
<p>A new instance may need to pull an image, initialize connections, load configuration, and warm required state before becoming ready. Autoscaling follows observations with a delay; it cannot instantly absorb a sudden spike.</p>
<p>Keep enough baseline capacity for the workload that cannot wait, and choose signals that reflect the bottleneck. CPU can help for compute-heavy traffic. Queue age may better describe workers. Pool wait time or active-request concurrency may reveal pressure that average CPU hides.</p>
<p>Avoid a warmup procedure that makes every new replica perform the same expensive full-cache refresh. The capacity meant to help the database can briefly become its largest customer.</p>
<p>Scale-in is equally important. A quiet instance may still hold a long request or many WebSocket connections. Removing it is a lifecycle event, not just subtracting one from a counter.</p>
<h2>Draining coordinates several timers</h2>
<p>The load balancer has a deregistration delay. The process has a shutdown grace period. Requests have deadlines. Connections have idle timeouts. These settings need to agree on how ongoing work ends.</p>
<p>Stop admitting new requests, allow existing work a bounded interval, then shut down. If the platform kills the task before the drain interval finishes, the load balancer&#39;s generous delay does not help.</p>
<p>For WebSockets, draining may require application messages and client reconnection. A reconnected client can land on another replica; events and subscriptions need shared or reconstructible state. The replacement must not assume the old connection&#39;s in-memory history is available.</p>
<p>A slow deployment should not require every user to discover the reconnection policy through a frozen screen.</p>
<h2>Test a removal, not only an addition</h2>
<p>A useful scaling exercise removes an instance while traffic is active. It observes errors, latency, duplicate writes, session continuity, and long-lived connection recovery.</p>
<p>Test mixed versions too. <a href="https://ma-x.im/blog/full-stack-zero-downtime-migrations">Schema compatibility</a> and old frontend bundles remain relevant while the load balancer serves both generations. Retain content-hashed assets long enough for active clients to finish or recover cleanly.</p>
<p>These tests make capacity a statement about the whole service rather than a count of containers.</p>
<p>I am comfortable with modest scale when the limits are known. I am less comfortable with an autoscaling policy whose only promise is that more processes will appear whenever something becomes slow.</p>
<p>The shared database has not read that promise. It will keep accepting only the work it can actually perform.</p>
<p>Next, the upcoming article on production observability looks at how to see these limits before an error report becomes the first measurement.</p>
<p>When your replica count grows, which downstream quantity grows with it? I&#39;d start a scaling review with that number, before choosing another autoscaling signal.</p>
]]></content:encoded>
      <pubDate>Mon, 12 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Scaling</category>
      <category>Load Balancing</category>
      <category>Reliability</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>Green Checks Only Cover What You Checked</title>
      <link>https://ma-x.im/blog/agent-playbook-green-checks</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-green-checks</guid>
      <description>A local build can accept a dependency change while a related service rejects its contract. The useful work is establishing the scope and freshness of the evidence, including when the original proposal should be closed rather than completed.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-green-checks/cover.png" alt="Green Checks Only Cover What You Checked" /></p>
<p>The build passes. The tests pass. The dependency update is small.</p>
<p>The integration check disagrees.</p>
<p>I do not find that especially surprising. A local check and an integration check can ask different questions about the same change. The awkward part begins when the agent treats the first green result as evidence for a larger conclusion than the check supports.</p>
<p>In The Work Around the Agent Is Engineering Work, I described the work around an agent as part of the engineering itself. One useful responsibility in that work is keeping the scope of evidence visible.</p>
<h2>A fictional contract makes the distinction easier to see</h2>
<p>The following is an illustrative composite. Names, fields and implementation details are invented; it is not a reconstruction of an identifiable system or an internal contract.</p>
<p>A booking service consumes a delivery window from a planning service. The contract previously used an ISO timestamp. An updated client expects an integer timestamp in milliseconds.</p>
<p>The booking service&#39;s local fixture is updated too:</p>
<pre><code class="language-typescript">type DeliveryWindow = {
  startsAtMs: number;
};

const window: DeliveryWindow = {
  startsAtMs: 1_800_000_000_000,
};
</code></pre>
<p>The compiler accepts the fixture. The local tests confirm that the booking code handles that fixture correctly.</p>
<p>Neither observation tells us which representation the deployed planning service actually returns. If it still returns the previous shape, the local result is not wrong. It is simply narrower than &quot;these services remain compatible.&quot;</p>
<p>This is one reason I would not call local validation a miniature version of the whole production system. Its exclusions are often exactly what lets it run quickly.</p>
<h2>Evidence has a scope and a revision</h2>
<p>For each check, ask what it exercised and which code it observed.</p>
<p>A test against a mock exercises the mock contract. A test against a generated client exercises the declaration and the path using it. An integration gate may inspect a combined contract or contact a real environment. A package&#39;s exported type can differ from its runtime behavior or from what another participant still expects.</p>
<p>The checks have different value. They do not automatically inherit each other&#39;s coverage.</p>
<p>I would keep a small evidence record:</p>
<pre><code class="language-text">Check: booking service unit tests
Observed revision: current task commit
Established: local handling of the declared delivery window
Not established: compatibility with the deployed planner

Check: shared integration contract gate
Observed inputs: declared participating revisions
Established: compatibility within that selected set
Not established: behavior of every deployed client
</code></pre>
<p>This is not a demand for a document after every test command. It is a way to make the distinction explicit when the check is being used to justify a consequential decision.</p>
<p>A green integration result also needs an honest boundary. It can be based on a particular environment, selected revisions, or a contract model. It is stronger evidence for some questions, not proof of every runtime property.</p>
<h2>Read the package, not only its version label</h2>
<p>When a dependency change is involved, inspect what was installed: package metadata, declarations, runtime exports, and the relevant contract representation.</p>
<p>A requested version in a manifest is not the full observation. The lockfile, registry artifact, workspace resolution and build output determine what code is actually used. A stale installation can make an agent diagnose a version that is not the one under test.</p>
<p>This does not mean dumping the entire dependency tree into context. Start with the disputed boundary and follow the few inputs that decide it.</p>
<p>I want the agent to compare evidence it can point to, rather than expand an explanation around a guessed package behavior. More confident prose does not resolve disagreement between an artifact and a declaration.</p>
<h2>Several repositories do not form one snapshot</h2>
<p>Giving an agent access to related repositories is helpful. It does not make their branches, local changes, and deployed versions coherent automatically.</p>
<p>One repository may be on a current base. Another may have an old checkout. A third may contain a pending fix that already changed the intended migration path.</p>
<p>Before reasoning across them, identify the revisions and the purpose of each source: current main, active task branch, proposed change, or deployed release. A locally visible implementation can be real without being the implementation production uses.</p>
<p>Repository memory should help locate those questions, not answer them from an earlier session. The source of truth for current compatibility is the current relevant state.</p>
<h2>Recheck the problem before repairing the diagnosis</h2>
<p>The composite continues. Further investigation finds a compatible change already delivered through another path. The original dependency update is now obsolete.</p>
<p>There are two different next actions available: make the old proposal more elaborate, or stop pursuing it.</p>
<p>An agent can be quite good at the first. It has a task, a branch and an explanation to complete. That momentum is useful when the task remains valid. It is not useful when the current system no longer needs the proposed work.</p>
<p>The correct result can be closing the obsolete PR and recording why it was superseded. Do not describe that as delivering the other change. Do not rewrite the history so the incomplete initial diagnosis becomes a clean success.</p>
<p>The useful outcome is a rejected unsuitable proposal and a better freshness check for the next task.</p>
<blockquote>
<p>Before asking whether the patch works, ask whether it is still the patch the system needs.</p>
</blockquote>
<p>This is particularly valuable with agents because implementation is relatively easy to continue. Deciding not to continue still requires evidence and an accountable owner.</p>
<h2>Correct the workflow at the missing boundary</h2>
<p>If the initial diagnosis relied on stale or incomplete sources, merely fixing the current diff leaves the same failure available next time.</p>
<p>Move the relevant current-base or related-change check earlier. If a shared contract matters, make the integration gate part of the verification criteria. If the package contents were assumed, inspect the actual artifact before proposing a fix.</p>
<p>Keep the correction proportional. One incident does not justify requiring every task to scan every repository. A small single-repository CSS change and a shared dependency migration do not need identical discovery work.</p>
<p>The rule should describe the condition that makes the check useful: cross-service contracts, dependency behavior, version skew, or an active related change. Otherwise a lesson becomes another universal instruction the agent follows even when it has no bearing on the task.</p>
<h2>Separate a failed assumption from a failed person</h2>
<p>An incomplete AI diagnosis deserves correction. It does not need a theatrical explanation about agents being incapable, nor a reassuring story in which the model secretly intended the right thing.</p>
<p>Record the assumption, the contradicting evidence, the correction, and the remaining uncertainty. That is enough to learn from the task.</p>
<p>It also gives credit accurately. A person may notice the stale base. CI may reject the shared contract. The agent may then help compare sources and prepare the explanation. Those contributions can all be real without being compressed into &quot;AI solved it.&quot;</p>
<h2>Keep green useful by keeping it specific</h2>
<p>I want local checks to stay fast and valuable. Their usefulness does not depend on pretending they cover the whole system.</p>
<p>A build confirms something about the selected sources and toolchain. A test confirms behavior under its chosen conditions. A related contract check adds another layer. Current-state investigation tells us whether the task itself still makes sense.</p>
<p>The result is not certainty. It is a decision with evidence that matches its scope.</p>
<p>I would rather close one well-explained obsolete proposal than merge an increasingly polished implementation of yesterday&#39;s requirement. There is less to show in the diff, but more of the system has been understood.</p>
<p>The next article, <a href="https://ma-x.im/blog/agent-playbook-scope-after-tools">The Command Succeeded. The Scope Changed.</a>, looks at another boundary green checks do not automatically cover: what automation changed after the initial edit.</p>
<p>If a check in your workflow is routinely used to justify more than it tested, which claim gets attached to it? That gap is usually worth examining before adding another test suite.</p>
]]></content:encoded>
      <pubDate>Sun, 11 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Agentic Engineering</category>
      <category>Integration</category>
      <category>Validation</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>A Small AWS Deployment, With the Awkward Parts Included</title>
      <link>https://ma-x.im/blog/full-stack-practical-aws</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-practical-aws</guid>
      <description>CloudFront, ALB, ECS, RDS, and an optional Redis layer are enough to explain a useful deployment. The less obvious work is controlling identity, cache behavior, outbound access, connection budgets, and the cost of keeping the system recoverable.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-practical-aws/cover.png" alt="A Small AWS Deployment, With the Awkward Parts Included" /></p>
<p>A diagram with CloudFront, a load balancer, some containers, and a database is a reasonable start.</p>
<p>It does not tell us whether the checkout page can cache someone else&#39;s order, whether the containers can download their image, or who is allowed to restore the database after a bad migration.</p>
<p>Those details are where I would spend the second pass. The boxes are familiar enough. Their permissions and failure behavior deserve a little more attention.</p>
<p>For this article, assume an illustrative React application with a Node backend, PostgreSQL, and a few background workers. This is a proposed deployment shape, not a claim about a system I have operated or a measured monthly bill.</p>
<h2>Give each component a limited job</h2>
<p>One possible layout is:</p>
<pre><code class="language-text">public DNS -&gt; CloudFront
                -&gt; static assets in S3
                -&gt; application routes through ALB
                     -&gt; ECS/Fargate application tasks
                          -&gt; RDS PostgreSQL
                          -&gt; optional ElastiCache

queue -&gt; separate worker tasks
</code></pre>
<p>CloudFront is the public edge. ALB distributes application traffic. ECS manages tasks; Fargate avoids operating the underlying servers directly. RDS runs the database. Redis is optional and should have the <a href="https://ma-x.im/blog/full-stack-redis-without-magic">explicit job</a> we discussed earlier.</p>
<p>I would not add Redis simply to complete the diagram, or Kubernetes simply to make the deployment feel serious. A managed application platform may also be sufficient. The point of this layout is understanding responsibility, not demonstrating the largest collection of AWS abbreviations.</p>
<h2>A cache policy is part of authorization safety</h2>
<p>Static, content-hashed assets are good candidates for long-lived caching. A signed-in customer&#39;s order page is not the same kind of object.</p>
<p>CloudFront distinguishes which values form the cache key from which values are forwarded to the origin. Sending a session cookie to the backend does not, by itself, guarantee responses are isolated correctly in the cache.</p>
<p>For private application responses, start with caching disabled and explicitly configure authentication forwarding. For public cacheable responses, include the dimensions that actually change the representation. Test with two separate users, not twice with the same session.</p>
<p>There is a particularly useful detail in the <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DownloadDistValuesCacheBehavior.html">CloudFront cache behavior documentation</a>: a minimum TTL above zero can force caching even when the origin sends <code>private</code>, <code>no-store</code>, or <code>no-cache</code>.</p>
<p>That is not a theoretical distinction. The application and infrastructure can each look reasonable in isolation while their combined policy is wrong.</p>
<p>Path-pattern ordering matters too. A broad public behavior matching before a narrower protected behavior can apply the wrong rule. I would review edge configuration like application code, including tests for authenticated and unauthenticated variants.</p>
<h2>Private does not mean disconnected</h2>
<p>Application tasks and databases should not be needlessly reachable from the public internet. But tasks still need outbound paths: pull images, obtain secrets, write logs, and contact payment or email providers.</p>
<p>Putting a task in a private subnet without arranging those paths produces a deployment that cannot start or cannot finish its normal work.</p>
<p>NAT provides general outbound connectivity. VPC endpoints can provide private access to supported AWS services. They have different setup, availability, and cost implications; neither is a universal instruction to copy without checking the task&#39;s actual dependencies.</p>
<p>For a small deployment, NAT, load balancers, logs, and minimum database capacity can be a meaningful part of the bill even when application traffic is low. I would estimate those standing costs before optimizing the price of one container.</p>
<p>Do not publish the database merely because it makes a first connectivity test easier. Use a controlled administrative path and keep the production network model intact.</p>
<h2>Security groups express specific relationships</h2>
<p>Rather than allowing the whole subnet to reach every component, define who needs to talk to whom.</p>
<p>The application&#39;s security group can accept traffic from the ALB group on the application port. The database can accept its database port from the application and worker groups that need it. Other callers do not acquire access simply by sharing a VPC.</p>
<p>Also consider origin bypass. If clients can call the public ALB directly, they may bypass restrictions applied only at CloudFront. Depending on the design, use a supported private-origin arrangement or restrict the public origin with appropriate network and request controls. The application must still enforce authentication and authorization itself.</p>
<p>These controls compose. A hidden origin is not an authorization system, and an authenticated route is not a reason to leave every network port open.</p>
<h2>The task has two different identities</h2>
<p>ECS distinguishes an execution role from a task role.</p>
<p>The execution role supports platform work such as pulling images and delivering configured logs or secrets. The task role is the identity the application uses when calling AWS APIs.</p>
<p>Giving the execution role permission to read an application bucket does not necessarily give the running code that access. Conversely, copying a broad deployment role into the task gives every application bug more authority than it needs.</p>
<p>Keep the roles narrow and separate. Use workload credentials rather than embedding long-lived AWS keys in the image. When access fails, identify which role made the request before adding permissions until the error goes away.</p>
<p>That last approach does work remarkably well at producing permissions nobody can later explain.</p>
<h2>Fargate tasks still need a resource budget</h2>
<p>Managed compute does not remove per-process connection pools or memory behavior. Two tasks with twenty PostgreSQL connections each are a different load from twenty tasks with the same setting.</p>
<p>Deployment overlap counts too. New tasks start before old tasks drain. The database must tolerate that temporary peak, or the release can run out of connections while average CPU remains comfortable.</p>
<p>RDS Proxy may help pool and manage connections, but it is not a cure for long transactions or unlimited concurrency. Application behavior still determines how long connections remain occupied.</p>
<p>Set a maximum scale consistent with downstream capacity. Separate workers from HTTP tasks so increasing export throughput does not quietly change the web service&#39;s deployment and resource profile.</p>
<h2>Managed availability is not application continuity</h2>
<p>Multi-AZ database deployment can improve availability during infrastructure failure. A read replica addresses a different concern and may lag. Neither undoes an accidental deletion that the database correctly accepted.</p>
<p>Redis failover also has semantics. If cached product descriptions disappear, the database can refill them. If sessions or coordination state disappear, the product consequence is different.</p>
<p>I would name those outcomes before choosing service options. &quot;Highly available&quot; is a useful infrastructure property, but it does not mean an order can be repeated without duplication or that a replica provides the latest permission change.</p>
<p>Backups and recovery get their own article, <a href="https://ma-x.im/blog/full-stack-backups-and-recovery">The Backup Exists. The Restore Still Needs Work.</a>. They belong in the initial infrastructure plan even if the first restore exercise comes later.</p>
<h2>Deploy an artifact, not a moving tag</h2>
<p>A controlled release records an image digest, configuration revision, migration step, and deployment outcome. Infrastructure as code records the intended topology so another environment can reproduce it without reconstructing yesterday&#39;s dashboard clicks.</p>
<p>Use an additive migration phase, deploy compatible tasks, observe health and business outcomes, then perform destructive cleanup in a later release. Give long-lived connections a drain policy and old browser assets a retention policy.</p>
<p>A rollback should name which pieces return to their previous state. The database does not automatically move backwards because ECS deploys the previous image.</p>
<p>Finally, add alarms with an owner: application error rate, latency, failed deployments, queue age, database saturation, and spend. Budgets are notifications unless explicit controls say otherwise; they are not a promise that AWS will stop the bill at an exact amount.</p>
<p>I would rather deploy this modest layout with known limits than a more elaborate one nobody can safely change. The diagram can stay small. The operating agreement needs enough detail to survive the first inconvenient event.</p>
<p>The next article examines <a href="https://ma-x.im/blog/full-stack-load-balancing">load balancing and scaling</a>, including why adding healthy replicas can make a shared database less healthy.</p>
<p>When reviewing an AWS layout, which relationship takes the longest to explain: network access, IAM, cache policy, or recovery? That is usually a useful place for the next design conversation.</p>
]]></content:encoded>
      <pubDate>Sat, 10 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>AWS</category>
      <category>Deployment</category>
      <category>Infrastructure</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>The Work Around the Agent Is Engineering Work</title>
      <link>https://ma-x.im/blog/agent-playbook-engineering-the-workflow</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-engineering-the-workflow</guid>
      <description>My use of coding agents gradually became work on the environment around them: current context, tool access, focused checks, review and recovery. The useful distinction is not how much an agent can do, but what makes its result defensible.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-engineering-the-workflow/cover.png" alt="The Work Around the Agent Is Engineering Work" /></p>
<p>A coding agent returns a plausible diff. The local tests pass. It has followed the requested format and written a sensible explanation.</p>
<p>There is still a fairly ordinary question left: what does that evidence actually establish?</p>
<p>Perhaps the tests cover only one service. Perhaps a hook added files after the diff was inspected. Perhaps another change already solved the problem, so the proposed update is now unnecessary. The output can be quite competent while the decision to ship it remains wrong.</p>
<p>That is the part of working with agents that increasingly interests me. Not another way to phrase the request. The conditions around the request that make a result possible to check, continue, and sometimes reject.</p>
<p>I have been designing and iteratively refining that environment as part of my engineering work. Repository instructions, connected tools, validation steps, review handling, and session handoffs became things to maintain rather than instructions to repeat in every conversation.</p>
<p>I would describe this as agentic workflow engineering. It is still software engineering. The responsibilities have moved around a little; they have not disappeared.</p>
<h2>The diff is one artifact in a longer operation</h2>
<p>Here is a deliberately generalized composite, not a reconstruction of one identifiable project.</p>
<p>Two services share a contract. An agent updates a dependency in one repository. The local build accepts the new types, and the tests confirm the behavior that repository knows about. A separate integration check finds that the other service still expects a different representation.</p>
<p>The first diagnosis is incomplete. Before making another patch, the engineer checks the actual package contents, current contracts, base branch, and related changes. A compatible solution has already arrived through another change. Continuing the original update would now introduce a regression rather than finish the task.</p>
<p>The useful result is to stop and close the obsolete proposal.</p>
<p>There is no new feature to celebrate. There is a clearer account of the system and one unsuitable change that did not enter it. I think that is worth recognizing as an engineering outcome, without pretending it measures a productivity gain.</p>
<p>The agent helped investigate. A person still had to decide what the evidence meant, correct the initial assumption, and change the workflow so freshness checks happened earlier next time.</p>
<h2>My contribution is the operating arrangement</h2>
<p>There is an unhelpful choice in some descriptions of AI-assisted work: either the engineer wrote everything personally, or they merely asked a chatbot to do it.</p>
<p>Neither explains what I am doing.</p>
<p>My contribution includes defining the task, organizing access to relevant context, choosing constraints, examining decisions, interpreting failures, and refining the next attempt. The agent can inspect sources, propose changes, run checks, and prepare reviewable artifacts inside that arrangement.</p>
<p>I do not need to claim manual authorship of every generated line to own those engineering decisions. Equally, a generated implementation does not make the environment around it automatically good.</p>
<p>I have integrated existing tools rather than built every tool involved. MCP connections help bring requirements and documentation into reach. Git, CLI/API access, tests, type checks, builds, and browser verification contribute different kinds of evidence. Each needs a clear reason to participate in the task.</p>
<p>The distinction matters when describing the work: integration, configuration, development, evaluation, and adoption are separate claims. I would rather use the smaller accurate verb than let a convenient headline imply the larger one.</p>
<h2>Context needs authority and a lifetime</h2>
<p>Giving an agent more text is not necessarily giving it better context.</p>
<p>Personal working preferences, shared engineering standards, repository rules, and task requirements answer different questions. A general suggestion should not override the API contract in the current code. A remembered convention should not defeat a test result observed now.</p>
<p>I use saved context to reduce repeated explanation and support continuation. I still expect the agent to verify current sources. Memory can become stale, and instructions can preserve a decision after the reason for it has changed.</p>
<p>For an active task, a compact continuation record might look like this:</p>
<pre><code class="language-text">Goal: accept the new response shape without changing existing callers
Current observation: one caller still consumes the previous field
Decision: retain the compatibility adapter for this release
Verified: caller inventory and focused adapter tests
Unverified: downstream deployment sequence
Next step: confirm the rollout contract before removal
</code></pre>
<p>This is an illustrative record, not an internal artifact. It separates observations, decisions, checks, and uncertainty. A new session can challenge it against the repository instead of treating the summary as a complete memory of reality.</p>
<p>I do not promise identical behavior across models or sessions. The more defensible promise is a documented place to resume and a way to notice when it no longer matches the work.</p>
<h2>Autonomy is partly a permissions problem</h2>
<p>Written boundaries are useful: which operations are allowed, when to ask, and what must not be disclosed. They are not enforcement by themselves.</p>
<p>Tool credentials, available commands, environment isolation, branch protections, and executable gates also determine what can happen. A rule that says &quot;do not release&quot; is weaker than an environment where the agent cannot release without an accountable person&#39;s action.</p>
<p>The design depends on the task. Low-risk investigation and focused checks can usually proceed without a sequence of tiny approvals. Business requirements, architectural commitments, sensitive actions, and release decisions need the appropriate owner.</p>
<p>I want the agent to have enough room to work, not enough authority to settle every question it encounters.</p>
<p>This also makes a stop meaningful. If acceptance criteria are unclear or current evidence contradicts the request, continuing to produce code is not necessarily progress. The next useful artifact may be a question or a bounded proposal.</p>
<h2>A small change deserves a discriminating check</h2>
<p>Before the first edit, I ask the agent to locate the code that controls the behavior, state a local hypothesis, and choose a check that could disprove it.</p>
<p>That is less impressive than a long repository survey, but often more useful. The goal is not to read every nearby file before acting. It is to know which assumption the next small edit will test.</p>
<p>After that edit, run the focused check before expanding the change. If it fails, decide whether it exposed a local defect or showed that the hypothesis was wrong. Broader tests, lint, build, CI, and acceptance checks follow according to the risk and repository requirements.</p>
<p>The focused check does not replace the broader gates. It keeps us from spending a large patch on an assumption we could have disproved cheaply.</p>
<p>For a small, clear task, I do not need a specification document that repeats the ticket. For a risky or multi-session change, writing down assumptions and a plan before implementation can be valuable. The timing is part of the evidence: a plan written after the work cannot demonstrate that the work was specification-driven.</p>
<h2>Tools have outputs that need their own review</h2>
<p>A command can succeed and still change more than the task authorized.</p>
<p>Generators, package installation, hooks, formatting, and deployment tooling can all transform the artifact after the initial edit. The working diff we reviewed may not be the commit tree that a hook produced.</p>
<p>I have used that kind of failure to refine the workflow: inspect the result after automation, separate unrelated generated drift, and verify what is actually being proposed.</p>
<p>It is a fairly undramatic control. That is one of its useful qualities. It does not require the agent to recognize every possible reason a generator changed a file. It requires the scope of the result to be checked before it leaves the local operation.</p>
<p>The next two articles, <a href="https://ma-x.im/blog/agent-playbook-green-checks">Green Checks Only Cover What You Checked</a> and <a href="https://ma-x.im/blog/agent-playbook-scope-after-tools">The Command Succeeded. The Scope Changed.</a>, examine those boundaries in more detail.</p>
<h2>Review is evidence attached to a changing object</h2>
<p>I have also developed personal PR-review tooling with AI assistance. The work includes tracking revisions, avoiding repeated unchanged findings, dry-run checks, and guarding actions against stale state.</p>
<p>That is a statement about mechanisms I developed, not a claim that the tool is continuously operating today, deployed across a company, or safely replacing human approval.</p>
<p>The engineering problem is interesting on its own. A finding can remain relevant while its line number changes. A new commit can remove the risk but leave an old comment. A review decision can become stale when the object it described changes.</p>
<p>I want the automation to provide useful evidence without silently taking over accountability. <a href="https://ma-x.im/blog/agent-playbook-review-belongs-to-commit">A Review Belongs to a Commit</a> follows that boundary, including the limitations of duplicate suppression and head checks.</p>
<h2>Improvements need careful attribution</h2>
<p>I participate in evaluating reusable agent skills against real engineering work. That does not make me the owner of an organization-wide transformation, and it does not establish that a particular framework caused a measured improvement.</p>
<p>A successful task can depend on repository instructions, tool access, a well-chosen test, current domain knowledge, or a person&#39;s correction. A skill may contribute, add ceremony, or simply describe a practice already present.</p>
<p>I try to keep those explanations apart. Record the outcome, corrections, residual risks, and what the available evidence can attribute. Unknown is a reasonable answer when the task does not support a stronger one.</p>
<p><a href="https://ma-x.im/blog/agent-playbook-what-a-skill-changed">What a Skill Changed, and What It Didn&#39;t</a> will make that distinction concrete without inventing comparative results.</p>
<h2>The useful specialization is not a new claim about the model</h2>
<p>This work does not mean I train models, conduct ML research, or operate production MLOps. It also does not mean role-based agent configurations have become reliable multi-agent execution simply because the definitions exist.</p>
<p>It means I design the conditions in which coding agents participate in software delivery: context, tools, boundaries, checks, human decisions, and a way to learn from ordinary failures.</p>
<p>My frontend and full-stack background is part of that, not something to replace with a new label. Interfaces, data ownership, service contracts, tests, and release behavior provide the questions against which generated output gets evaluated.</p>
<p>The diff at the beginning can remain plausible. I still need to know what changed, what was checked, and who owns the unresolved decision. I am comfortable letting an agent do a substantial part of the work when those answers are available. I am less comfortable calling the work complete because the explanation sounds finished.</p>
<p>If your agent workflow is reliable until one ordinary exception appears, tell me which assumption the exception exposed. That is usually a more useful starting point than another prompt template.</p>
]]></content:encoded>
      <pubDate>Fri, 09 Oct 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Agentic Engineering</category>
      <category>Developer Tooling</category>
      <category>Engineering Workflow</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Same Container Image, Different Application</title>
      <link>https://ma-x.im/blog/full-stack-container-configuration</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-container-configuration</guid>
      <description>Promoting one image through environments is useful until build-time browser settings, secret rotation, migrations, or resource limits disagree with that promise. Configuration is part of the release, even when it lives outside Git.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-container-configuration/cover.png" alt="Same Container Image, Different Application" /></p>
<p>The image passed staging. Production starts the same digest with different environment variables.</p>
<p>The browser still calls the staging API.</p>
<p>There is no contradiction. The server received its configuration at startup. The browser bundle received its configuration earlier, during the build. &quot;Same image&quot; was true. &quot;Same application, configured at runtime&quot; was only partly true.</p>
<p>I like building once and promoting an immutable artifact. It removes a whole class of uncertainty about what was tested. But that promise deserves a more careful definition than &quot;we use Docker now.&quot;</p>
<h2>Separate the configuration clocks</h2>
<p>There are at least three moments when configuration can become fixed:</p>
<ul>
<li>build time, when the artifact is produced;</li>
<li>process startup, when the runtime initializes;</li>
<li>request time, when a value is consulted again.</li>
</ul>
<p>A public frontend environment variable may be substituted into JavaScript during the build. Changing the container environment does not rewrite the compiled bytes. Next.js documents this behavior for <code>NEXT_PUBLIC_</code> values; Vite&#39;s <code>VITE_</code> values have a similar public-build role.</p>
<p>For a browser API address, I would first consider same-origin paths such as <code>/api</code>. The proxy selects the environment-specific backend without putting that address into the bundle.</p>
<p>If the browser truly needs runtime settings, publish a small, explicitly public configuration resource or inject them into the initial response. Validate it and decide its cache behavior. No secret becomes safe because the file is named <code>runtime-config.json</code>.</p>
<p>This also creates compatibility work: an old bundle may read new settings. Keep the public configuration schema additive, and record which artifact and configuration revision were deployed together.</p>
<h2>Environment variables do not form a typed contract</h2>
<p>The process can start with <code>ORDER_TIMEOUT_MS=ten</code>. It can also start with a missing variable that quietly activates a development default.</p>
<p>I would rather reject an invalid production configuration before the instance receives traffic:</p>
<pre><code class="language-typescript">import { z } from &#39;zod&#39;;

const RuntimeConfig = z.object({
  DATABASE_URL: z.string().url(),
  ORDER_TIMEOUT_MS: z.coerce.number().int().min(100).max(30_000),
  ENABLE_EXPORTS: z.enum([&#39;true&#39;, &#39;false&#39;]),
});

const config = RuntimeConfig.parse(process.env);
const exportsEnabled = config.ENABLE_EXPORTS === &#39;true&#39;;
</code></pre>
<p>The values are illustrative, not recommended timeouts. Notice the boolean handling: the string <code>&#39;false&#39;</code> is truthy in JavaScript. A generic truthiness conversion is not a configuration parser.</p>
<p>Errors should identify the invalid setting without dumping every environment value into logs. Validation is useful; printing a database password beside the validation failure is not.</p>
<h2>Secret mounts solve one part of the problem</h2>
<p>Builds sometimes need credentials to download private dependencies. Passing them through Docker build arguments or environment instructions can leave them in image metadata or layers. Deleting a file in a later layer does not remove it from the earlier layer.</p>
<p><a href="https://docs.docker.com/build/building/secrets/">BuildKit secret mounts</a> expose a credential to one build step without intentionally baking it into the image:</p>
<pre><code class="language-dockerfile">RUN --mount=type=secret,id=npmrc,target=/root/.npmrc \
    npm ci
</code></pre>
<p>That does not prevent the invoked tool from copying the credential into output or printing it. The mount is a delivery mechanism, not a guarantee about everything executed in that step.</p>
<p>Runtime secrets have a different lifecycle. An environment-injected database password usually stays unchanged in the process until it restarts. A mounted secret file may update while the application still holds the old value in memory. A secrets service can rotate credentials while existing database connections continue to work and new connections fail.</p>
<p>Rotation therefore includes application behavior: refresh or restart, connection-pool replacement, overlap between old and new credentials, and rollback. A secret manager can store the next password. It cannot decide when your client library rereads it.</p>
<h2>Pin the artifact you mean to deploy</h2>
<p>An image tag is a name. A digest identifies particular content.</p>
<p>If <code>checkout:production</code> is overwritten while deployment is in progress, instances pulling at different moments can receive different bytes under the same tag. It is possible to build safeguards around tags, but deploying a recorded digest gives the release a less ambiguous identity.</p>
<p>Record the source revision, image digest, configuration revision, and migration state. That is enough to explain considerably more than &quot;version 18 is running.&quot;</p>
<p>Architecture matters too. A locally built image for one CPU architecture is not automatically a usable artifact on another. Multi-platform builds help, but native dependencies still need testing in the runtime actually deployed.</p>
<h2>A container does not make local files durable</h2>
<p>A generated invoice written into the container filesystem belongs to that instance. The next request may land elsewhere, and the next deployment may remove it entirely.</p>
<p>Use the local filesystem for temporary work with a known lifetime. Put durable exports, uploads, and receipts in deliberately managed storage. Return an object identifier rather than a path inside the process that happened to create the file.</p>
<p>The same distinction applies to caches. A local cache is useful if losing it is safe and other instances can tolerate different entries. A shared product truth should not accidentally live in whichever container first computed it.</p>
<p>Read-only root filesystems and non-root execution can make accidental persistence and unnecessary privileges harder. They also expose implicit assumptions in libraries that want to write temporary files. Test the constrained runtime, not just the permissive development container.</p>
<h2>Resource limits change behavior before a process dies</h2>
<p>CPU throttling can stretch an ordinary request past its timeout without high host CPU usage. Memory limits can kill a process while the host still has plenty of RAM. Event-loop delay may explain latency that database timing does not.</p>
<p>I would set concurrency and heap behavior with the container&#39;s actual limits in mind. More parallel promises are not always more throughput.</p>
<p>The database pool deserves particular attention. A pool of twenty connections seems modest until twelve replicas each create one. During a rolling deployment, old and new replicas overlap, and the peak connection count can be larger than the steady-state count.</p>
<p>We will examine that budget in the <a href="https://ma-x.im/blog/full-stack-load-balancing">scaling and load-balancing article</a>. For now, the useful habit is to treat per-process settings as quantities that multiply when the scheduler adds processes.</p>
<h2>Do not let every replica migrate the database</h2>
<p>Running migrations in the application&#39;s startup command is convenient for one instance. Start ten instances and you have ten potential migration runners, possibly while the old application still serves traffic.</p>
<p>Use a controlled release step with explicit migration ownership, lock behavior, retry policy, and compatibility gates. Database tools may provide advisory locking; that still does not make a destructive change compatible with the old deployment.</p>
<p>The <a href="https://ma-x.im/blog/full-stack-zero-downtime-migrations">expand/contract approach</a> belongs to the release sequence, not merely to the SQL file.</p>
<p>Startup and readiness also need separate meanings. The process may be alive while warming caches or preparing required resources. Conversely, restarting every replica because one shared dependency is briefly unavailable can add more load to that dependency.</p>
<p>Health checks should support the failure response we want, rather than giving every unpleasant condition the same instruction: restart.</p>
<h2>Test shutdown as carefully as startup</h2>
<p>A container usually gets a termination signal and a finite grace period. If the process ignores the signal or an intermediate shell fails to forward it correctly, deployment ends by force-killing work.</p>
<p>Use a process arrangement that forwards signals. Stop accepting new work, allow in-flight requests to finish within a bound, and let queue workers acknowledge only completed jobs. Design what happens after the deadline too; the platform will not wait indefinitely because an export is nearly done.</p>
<p>An immutable image is useful precisely because it narrows uncertainty. It does not remove configuration, storage, or process lifecycle from the design. I would keep the artifact fixed and make those moving parts explicit enough to reproduce.</p>
<p>Next, <a href="https://ma-x.im/blog/full-stack-practical-aws">a practical AWS deployment</a> puts these decisions into a concrete infrastructure layout, without treating every available service as a requirement.</p>
<p>Which setting in your application changes at a different time than people assume? I&#39;d be interested in the small configuration detail that made an otherwise sound release behave differently.</p>
]]></content:encoded>
      <pubDate>Thu, 08 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Docker</category>
      <category>Configuration</category>
      <category>Deployment</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>The Domain Moved. Some Requests Did Not.</title>
      <link>https://ma-x.im/blog/full-stack-domains-and-proxies</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-domains-and-proxies</guid>
      <description>DNS cutovers, certificate renewal, forwarded headers, and cookie scope tend to become interesting after the first successful request. Moving an application means keeping several versions of its public address working at once.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-domains-and-proxies/cover.png" alt="The Domain Moved. Some Requests Did Not." /></p>
<p>The new domain works on your laptop. The certificate is valid. The application responds.</p>
<p>Someone else is still reaching the old server.</p>
<p>There is usually nothing particularly mysterious about this. The system has several caches, several connections, and several ideas about what &quot;the address&quot; means. They do not update together because we changed a record in a dashboard.</p>
<p>In the <a href="https://ma-x.im/blog/full-stack-after-fetch">first part of this guide</a>, I followed a request from the browser into the backend. Here I want to stay at the entrance, where fairly small configuration changes can leave an application working for one group of users and quietly wrong for another.</p>
<h2>A DNS change has a transition period</h2>
<p>Lowering a record&#39;s TTL immediately before changing its value does not shorten answers already cached under the previous TTL. A resolver that cached an hour-long answer ten minutes ago can keep it for the remaining fifty minutes.</p>
<p>I would lower the TTL ahead of a planned cutover, wait for the old lifetime to pass, and keep both destinations usable during the move. DNS TTL is a cache instruction, not a synchronized release mechanism.</p>
<p>There is another easy-to-miss cache: the absence of a record. If someone queried a hostname before it existed, their resolver may retain a negative answer. Creating the record does not necessarily make that user able to resolve it immediately.</p>
<p>Existing HTTP connections can also outlive a DNS update. A client does not need to resolve the name again for every request on an already-open connection.</p>
<p>None of this is a reason to avoid DNS cutovers. It is a reason not to delete the old destination the moment your own lookup looks correct.</p>
<h2>Check both address families</h2>
<p>A hostname can publish both an A record for IPv4 and an AAAA record for IPv6.</p>
<p>If the IPv4 route is healthy while IPv6 reaches an old endpoint, the result may depend on the user&#39;s network. Browsers have strategies for choosing between address families, but those strategies cannot turn a wrong AAAA record into the right infrastructure.</p>
<p>The symptom is frustratingly ordinary: &quot;It works here.&quot;</p>
<p>These are diagnostic commands, not a deployment script:</p>
<pre><code class="language-sh">dig +short A api.shop.example
dig +short AAAA api.shop.example
curl -4 -I https://api.shop.example/health
curl -6 -I https://api.shop.example/health
</code></pre>
<p>The last command requires a working IPv6 path from the machine running it. Failure there is evidence to interpret, not automatic proof that the server is broken.</p>
<p>I would also inspect the authoritative records and compare them with the resolver used by an affected client. &quot;Propagation&quot; is often used as a name for any uncertainty around DNS. It is more useful to identify which answer is cached where.</p>
<h2>The certificate belongs to a connection, not a diagram</h2>
<p>With a CDN in front of a load balancer, there may be two TLS connections: browser to CDN, and CDN to origin. Each has its own certificate validation and hostname expectations.</p>
<p>A valid browser-facing certificate says nothing about whether the CDN can validate the origin certificate. The origin address, TLS server name, certificate names, and HTTP Host header have related but distinct jobs.</p>
<p>Suppose the origin certificate covers <code>origin.shop.example</code>, while the CDN connects using a load balancer&#39;s generated hostname. Depending on the configured origin behavior, verification can fail before the application sees a request.</p>
<p>I would debug the two connections separately rather than temporarily disable origin verification. The latter makes the immediate error disappear by removing the property we were trying to establish.</p>
<h2>Renewal is the configuration test that arrives later</h2>
<p>A certificate issued successfully today still needs an automated renewal path.</p>
<p>An HTTP-01 challenge requires the validation resource to be reachable through the public route. Add authentication, a new redirect, or a different proxy rule later, and renewal may fail while ordinary application requests continue working.</p>
<p>DNS-01 avoids that HTTP path and supports wildcard certificates, but introduces credentials and DNS update behavior into the renewal system. Give the renewal process narrowly scoped access rather than the registrar&#39;s unrestricted credentials. Challenge delegation can help separate that responsibility.</p>
<p>The <a href="https://letsencrypt.org/docs/challenge-types/">Let&#39;s Encrypt challenge documentation</a> explains these trade-offs. A wildcard for <code>*.shop.example</code> also does not cover the apex <code>shop.example</code>; certificate names deserve the same explicit review as routing rules.</p>
<p>I would monitor expiry from outside the application and exercise renewal before the certificate becomes urgent. A calendar reminder is not quite the automation people usually mean when they say HTTPS is handled.</p>
<h2>The proxy changes what the application believes</h2>
<p>Behind TLS termination, the application may receive plain HTTP from the load balancer. Without trusted forwarding information, it can generate HTTP callback URLs, fail to set secure cookies, or redirect a request that is already HTTPS into a loop.</p>
<p>Forwarded protocol, host, and client-address headers provide that context. They are not trustworthy merely because they have conventional names.</p>
<p>An internet client can supply <code>X-Forwarded-For</code> too. The proxy must remove or normalize incoming values according to the topology, and the application must trust only the known proxy path. Blindly trusting every hop makes client-IP rate limits and audit records easier to mislead.</p>
<p>For password-reset and OAuth URLs, I prefer a configured, validated public origin rather than constructing an absolute URL from an arbitrary Host header. It is less flexible. That is sometimes the point.</p>
<blockquote>
<p>Public address, transport address, and trusted request context are separate configuration decisions.</p>
</blockquote>
<h2>Moving the hostname can change authentication</h2>
<p>A host-only cookie for <code>shop.example</code> is not sent to <code>api.shop.example</code>. A cookie scoped to the parent domain has a broader reach, which can be undesirable if another subdomain is less trusted.</p>
<p>Also, two subdomains can be same-site while still being different origins. SameSite cookie rules and CORS answer different questions. Cross-origin browser requests still need the right credential mode and CORS response; a credentialed response cannot use a wildcard allowed origin.</p>
<p>The distinction becomes visible after a seemingly harmless move from <code>/api</code> on the main origin to a separate API hostname. Authentication, preflight requests, callback allowlists, and browser storage behavior may all change.</p>
<p>I would test an existing session, not just a fresh login. I would test logout too. Deleting a cookie with the wrong path or domain can leave the original cookie present, which is a fairly unhelpful way to discover how cookie scope works.</p>
<h2>Preserve request semantics through redirects</h2>
<p>A canonical-domain redirect is fine for a public page. Applying the same rule indiscriminately to API writes is less comfortable.</p>
<p>Some redirect statuses can lead clients to change a POST into a GET. Others preserve the method and body. Even a method-preserving redirect can encounter credential and origin rules that an ordinary page navigation does not.</p>
<p>For a moved API, I would keep the old route compatible during the transition rather than expect every client to follow a redirect correctly. The client might be a browser tab, an SDK, or a webhook sender that cannot be upgraded today.</p>
<p>Permanent redirects can be cached too. A rollback of DNS or application code does not necessarily rewind the redirect already stored by a client.</p>
<h2>Test an address change as a release</h2>
<p>A useful cutover check includes more than a homepage returning 200: authenticated reads, writes, OAuth callbacks, uploads, streaming responses, certificate renewal, and the old endpoint during overlap.</p>
<p>Proxy buffering is worth checking if the application streams. A server can emit chunks correctly while a proxy collects them and sends everything at the end. The request technically works; the intended latency behavior does not.</p>
<p>I would keep the old endpoint alive until observed traffic has moved, then remove it deliberately. The decision comes from request logs and compatibility requirements, not the moment the DNS control panel says the record was saved.</p>
<p>The domain is a small part of the application, but it has a long memory. Caches, cookies, certificates, and old clients all remember slightly different versions of it. A good move gives those memories time to become harmless.</p>
<p>Next comes <a href="https://ma-x.im/blog/full-stack-container-configuration">container and environment configuration</a>, where the application can have the right address and still be running the wrong settings.</p>
<p>If a domain move left one awkward client behind, I&#39;d be interested in which layer retained the old assumption. Those details are more useful than another checklist that stops at a green padlock.</p>
]]></content:encoded>
      <pubDate>Tue, 06 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>DNS</category>
      <category>HTTPS</category>
      <category>Infrastructure</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>The Request Ended. The Work Did Not.</title>
      <link>https://ma-x.im/blog/full-stack-background-jobs</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-background-jobs</guid>
      <description>Emails, exports, webhooks, and slow integrations should not keep an HTTP request open. A queue helps only when retries, duplicate delivery, job state, and the database handoff are designed explicitly.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-background-jobs/cover.png" alt="The Request Ended. The Work Did Not." /></p>
<p>The order exists. Payment succeeded. The database transaction committed.</p>
<p>Now the API waits three more seconds for an email provider to accept the confirmation message.</p>
<p>Sometimes it waits ten. Sometimes the provider times out after doing the work, so the backend does not know whether the email was accepted. The user sees a failed checkout and presses the button again, even though the expensive part already succeeded.</p>
<p>The email was not part of the request&#39;s answer. We made it part of the request&#39;s lifetime.</p>
<p>That is the boundary background jobs are meant to fix. The API completes the durable work needed to answer the user, records what still has to happen, and returns. A worker continues separately.</p>
<p>Simple in one sentence. Much less simple once &quot;separately&quot; has to remain reliable.</p>
<h2>What belongs outside the request</h2>
<p>Good background-job candidates share a few traits:</p>
<ul>
<li>the work is slow or has unpredictable latency;</li>
<li>the user does not need its final result before continuing;</li>
<li>it calls an external service that may be unavailable;</li>
<li>it can be retried safely;</li>
<li>it may need independent scaling.</li>
</ul>
<p>Confirmation emails fit. So do image processing, CSV exports, webhook delivery, search indexing, analytics enrichment, and invoice generation.</p>
<p>Creating the order itself does not fit. The API should not tell the browser &quot;order created&quot; before the order is durably recorded. Queueing the core write would turn a clear result into &quot;we accepted your wish and may create it later.&quot;</p>
<p>Sometimes that asynchronous product is intentional. Then the API should say so with a job or operation id and a pending status. The important part is not pretending pending work is complete.</p>
<h2>A queue separates acceptance from execution</h2>
<p>The request path becomes:</p>
<pre><code class="language-text">browser
  -&gt; create order in PostgreSQL
  -&gt; enqueue confirmation job
  -&gt; return order

worker
  -&gt; receive job
  -&gt; send email
  -&gt; mark job complete
</code></pre>
<p>The queue buffers work between producers and consumers. The API can accept jobs quickly. Workers process them at the speed the downstream system allows. If email slows down, checkout does not have to slow down with it.</p>
<p>That buffer also lets workers scale independently. Ten image processors can consume from the same queue during a spike, then scale back later.</p>
<p>But a queue is not just an array somewhere on the server. It needs durable messages, acknowledgement, retry behavior, visibility into pending work, and a strategy for jobs that never succeed.</p>
<h2>The database-to-queue gap</h2>
<p>Here is a subtle failure:</p>
<pre><code class="language-typescript">const order = await database.orders.create(input);
await queue.publish(&#39;order-confirmation&#39;, { orderId: order.id });

return order;
</code></pre>
<p>The database write succeeds. The process crashes before publishing the message. The order exists, but no confirmation job was recorded.</p>
<p>Reversing the order does not help:</p>
<pre><code class="language-typescript">await queue.publish(&#39;order-confirmation&#39;, { orderId });
await database.orders.create(input);
</code></pre>
<p>Now the worker may receive a job for an order that does not exist because the database write failed.</p>
<p>Two reliable systems do not automatically form one atomic operation.</p>
<p>The transactional outbox pattern solves this by writing the order and an outbox record in the same database transaction:</p>
<pre><code class="language-sql">BEGIN;

INSERT INTO orders (id, customer_id, status)
VALUES ($1, $2, &#39;confirmed&#39;);

INSERT INTO outbox_events (id, type, payload, created_at)
VALUES ($3, &#39;order.confirmed&#39;, $4, now());

COMMIT;
</code></pre>
<p>A separate publisher reads unsent outbox rows, publishes them to the queue, and marks them as sent. If it crashes after publishing but before marking the row, it may publish the same event again.</p>
<p>That is acceptable only if the consumer handles duplicates.</p>
<h2>At-least-once means duplicates are normal</h2>
<p>Most practical queue systems give us at-least-once delivery: a job will be delivered, but under failure it may be delivered more than once.</p>
<p>The worker receives a job, sends the email, then crashes before acknowledging completion. The queue cannot know the email was sent, so it delivers the job again.</p>
<p>Exactly-once delivery is an attractive phrase. End-to-end exactly-once effects across a queue, worker, database, and external email provider are much harder than the phrase suggests.</p>
<p>Design the job to be idempotent instead.</p>
<p>Give the effect a stable identity:</p>
<pre><code class="language-typescript">type SendOrderConfirmationJob = {
  jobId: string;
  orderId: string;
  templateVersion: number;
};
</code></pre>
<p>Before sending, record or check whether that specific confirmation has already been completed. If the email provider supports an idempotency key, send the stable job id there too. If it does not, decide whether an occasional duplicate email is acceptable or whether stronger local coordination is worth the complexity.</p>
<p>Idempotency is not &quot;ignore every repeated order id forever.&quot; A resend requested by support is a new intent and should have a new job identity.</p>
<blockquote>
<p>A retry is the same intent attempted again. A new action deserves a new identity.</p>
</blockquote>
<p>That is the same rule we used for checkout requests. Queues make it impossible to avoid thinking about it.</p>
<h2>Retry only failures that may change</h2>
<p>A timeout from an email provider may succeed on the next attempt. An invalid recipient address will not become valid because we tried it twelve times.</p>
<p>Classify failures:</p>
<ul>
<li><strong>Transient:</strong> timeout, connection reset, provider rate limit, temporary outage.</li>
<li><strong>Permanent:</strong> invalid input, unsupported format, missing required data.</li>
<li><strong>Unknown:</strong> the remote side may have completed the operation before the connection failed.</li>
</ul>
<p>Transient failures get retries with exponential backoff and jitter. Permanent failures stop immediately and become visible. Unknown outcomes need idempotency or reconciliation before retrying blindly.</p>
<pre><code class="language-typescript">const delayMs = Math.min(60_000, 1000 * 2 ** attempt);
const jitterMs = Math.floor(Math.random() * 500);

scheduleRetry(job, delayMs + jitterMs);
</code></pre>
<p>Backoff gives the dependency time to recover. Jitter prevents thousands of failed jobs from retrying in perfect synchronization and creating another outage the moment the provider returns.</p>
<p>A maximum attempt count is not enough by itself. A job that gives up needs somewhere to go and someone to notice.</p>
<h2>Dead letters are unfinished product work</h2>
<p>After repeated failure, a job may move to a dead-letter queue. That name sounds final. It should not mean invisible.</p>
<p>The job still represents a promise: send a confirmation, generate an export, deliver a webhook. Moving it aside protects healthy work from one poison message, but the product obligation remains.</p>
<p>For each job type, decide:</p>
<ul>
<li>who is alerted;</li>
<li>what context is safe to inspect;</li>
<li>whether the job can be replayed;</li>
<li>whether the user should see a failed status;</li>
<li>when the underlying operation needs manual recovery.</li>
</ul>
<p>A dashboard that proudly shows &quot;queue healthy&quot; while 4,000 jobs sit dead is measuring the machinery instead of the outcome.</p>
<p>Queue depth, oldest-job age, processing duration, retry count, and terminal failures tell a more useful story than worker CPU alone.</p>
<h2>Job payloads should be small references</h2>
<p>It is tempting to put the complete order, customer, and rendered email content into the message. Then the worker has everything it needs.</p>
<p>It also freezes a large data snapshot inside the queue, exposes more sensitive information, and makes schema changes harder. A delayed job may execute days later with a payload produced by old code.</p>
<p>Prefer stable identifiers and intentional snapshots:</p>
<pre><code class="language-typescript">type GenerateInvoiceJob = {
  jobId: string;
  orderId: string;
  invoiceVersion: number;
};
</code></pre>
<p>The worker loads current durable data by <code>orderId</code>. If a value must reflect the moment of purchase, such as the charged price, that snapshot already belongs in the order model. The message does not need to invent another source of truth.</p>
<p>There are exceptions. A webhook event may intentionally preserve the event-time payload. The principle is to choose snapshot semantics, not accidentally serialize half the database into every job.</p>
<h2>Workers need deployment compatibility too</h2>
<p>A queued message can outlive the code that produced it.</p>
<p>Deploy version two of a worker while version-one messages are still waiting, and the new worker must understand the old payload. Roll back the producer, and old code may start publishing the previous shape again.</p>
<p>This is the migration problem from the previous article, now applied to messages.</p>
<p>Use additive payload changes, sensible defaults, and explicit versions when semantics change. Do not rename a required field and assume the queue emptied during deployment. Measure that it did, or support both versions.</p>
<p>Long-running jobs also need graceful shutdown. When a worker receives a termination signal during deployment, it should stop accepting new work, finish or safely release the current job, acknowledge only after completion, and exit within the platform&#39;s deadline.</p>
<p>Otherwise every deployment creates retries by killing work halfway through.</p>
<h2>The frontend needs a model for pending work</h2>
<p>Some background work is invisible to the user. An analytics enrichment job does not need a progress bar.</p>
<p>An export does.</p>
<p>The API can return <code>202 Accepted</code> with an operation id:</p>
<pre><code class="language-json">{
  &quot;operationId&quot;: &quot;export_7f2a&quot;,
  &quot;status&quot;: &quot;queued&quot;
}
</code></pre>
<p>The frontend polls an operation endpoint, subscribes through server-sent events or WebSockets, or lets the user leave and returns the finished export through notifications.</p>
<p>The important state is not only loading and done. It may be queued, running, retrying, completed, failed, or cancelled. Those states belong to the product contract even if a queue implementation uses different internal terminology.</p>
<p>Do not show an eternal spinner because the worker died. Persist operation state somewhere durable, define a timeout or terminal failure, and give the user a next action.</p>
<p>Async architecture becomes good UX only when uncertainty is visible without becoming alarming.</p>
<h2>Backpressure is a product decision</h2>
<p>If requests create work faster than workers can finish it, queue depth grows. The queue is doing its job, but the delay may eventually violate the product promise.</p>
<p>We can add workers until a downstream limit stops us. We can prioritize urgent jobs. We can reject or postpone new work. We can reduce expensive processing. What we cannot do is treat an ever-growing queue as healthy because no messages were lost.</p>
<p>For confirmation email, five minutes may be embarrassing but survivable. For a fraud check that gates order fulfilment, five minutes may block the business. &quot;How old can the oldest job become?&quot; is a product question with an operational metric attached.</p>
<p>Queues buy time. They do not create capacity.</p>
<h2>Keep the promise smaller than the machinery</h2>
<p>Background jobs are useful because they let an HTTP request end before every side effect is complete. That separation improves latency and isolates unreliable dependencies.</p>
<p>But the queue is not the guarantee. The guarantee comes from the whole path:</p>
<ul>
<li>durable intent recorded with the business change;</li>
<li>messages published despite process failure;</li>
<li>consumers safe under duplicate delivery;</li>
<li>retries limited to failures that may recover;</li>
<li>dead jobs visible and recoverable;</li>
<li>payloads compatible across deployments;</li>
<li>product states that admit work is still pending.</li>
</ul>
<p>Once those pieces are explicit, a queue stops being mysterious infrastructure. It becomes a controlled handoff between &quot;the user can continue&quot; and &quot;the system still owes some work.&quot;</p>
<p>The <a href="https://ma-x.im/blog/full-stack-domains-and-proxies">next part of this guide</a> moves back to the public edge: domains, DNS, HTTPS, and reverse proxies, and how a name typed into a browser reaches the right application without exposing every internal service directly.</p>
<p>If your application returns success before all the work is finished, write down what it still owes. That list is the beginning of the background-job design, not an implementation detail after it.</p>
]]></content:encoded>
      <pubDate>Sun, 04 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Background Jobs</category>
      <category>Queues</category>
      <category>Reliability</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>Redis Is Fast. Your Data Can Still Be Wrong.</title>
      <link>https://ma-x.im/blog/full-stack-redis-without-magic</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-redis-without-magic</guid>
      <description>Redis can make an application feel dramatically faster, but speed is only useful when ownership, expiration, invalidation, and failure behavior are explicit. Otherwise the cache becomes a second truth nobody can explain.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-redis-without-magic/cover.png" alt="Redis Is Fast. Your Data Can Still Be Wrong." /></p>
<p>The product page loads in 900 milliseconds. Someone adds Redis. Now it loads in 70.</p>
<p>That feels like the end of the story.</p>
<p>Then a price changes in PostgreSQL and the product page keeps showing the old value. One user sees the new price, another sees the cached one, and checkout has to decide which reality to charge.</p>
<p>Redis did not create the inconsistency. We did, the moment we copied data without deciding how that copy becomes stale.</p>
<p>This is the useful way to think about Redis: not as a faster database, and not as a performance switch. It is a fast, shared, in-memory data structure server. The moment we put something there, we need to know whether it is a cache, a session, a counter, a lock, a queue, or the actual source of truth.</p>
<p>Those jobs have different failure rules. Mixing them is where the magic starts, and magic is difficult to debug.</p>
<h2>Start with one slow read</h2>
<p>Our checkout needs a product summary before creating an order. The database query joins current price, availability, and a few display fields. It is read frequently and changes much less often than it is requested.</p>
<p>That is a reasonable cache candidate.</p>
<p>The common cache-aside flow looks like this:</p>
<pre><code class="language-typescript">async function getProductSummary(productId: string) {
  const key = `product-summary:${productId}`;
  const cached = await redis.get(key);

  if (cached) {
    return ProductSummary.parse(JSON.parse(cached));
  }

  const product = await productRepository.getSummary(productId);

  await redis.set(key, JSON.stringify(product), { EX: 300 });

  return product;
}
</code></pre>
<p>Read Redis first. On a miss, read PostgreSQL, cache the result for five minutes, and return it.</p>
<p>The database remains the source of truth. Redis holds a disposable copy. If the cache is flushed, the application gets slower while it warms again, but it does not forget what a product costs.</p>
<p>That last sentence is the design test I use most often:</p>
<blockquote>
<p>If deleting Redis destroys the truth, you are not using it as a cache.</p>
</blockquote>
<p>There are valid systems where Redis owns important data. They need persistence, replication, backup, and recovery designed accordingly. That is a deliberate database choice, not &quot;we already had Redis running.&quot;</p>
<h2>A TTL is not an invalidation strategy</h2>
<p>The five-minute expiration prevents the entry from living forever. It does not tell us whether five minutes of stale pricing is acceptable.</p>
<p>For a blog post list, it may be. For inventory, probably not. For authorization, almost certainly not.</p>
<p>Time-to-live is a business decision disguised as a number. It should come from how long the system can tolerate being wrong, not from a familiar round value copied from another cache.</p>
<p>When a product price changes, we have a few options.</p>
<p>We can delete the cache entry immediately after the database update:</p>
<pre><code class="language-typescript">await database.transaction(async (transaction) =&gt; {
  await transaction.products.updatePrice(productId, priceCents);
});

await redis.del(`product-summary:${productId}`);
</code></pre>
<p>The next read repopulates it. This is simple and often enough.</p>
<p>But notice the gap: the database commit can succeed and the Redis deletion can fail. The stale entry then remains until its TTL expires.</p>
<p>We can reduce the risk with retries or publish an invalidation event through an outbox. We can update the cache directly, but then we must decide what happens if the database succeeds and the cache write fails, or the other way around.</p>
<p>There is no universal &quot;keep it in sync&quot; command. There is only a chosen tolerance for stale data and a recovery path when the two writes do not complete together.</p>
<h2>Cache keys are part of the schema</h2>
<p>This key looks harmless:</p>
<pre><code class="language-text">product-summary:42
</code></pre>
<p>Then the response shape changes. Old application instances write one JSON structure, new instances expect another, and both use the same key.</p>
<p>The failure looks like an API contract problem because it is one. The cache has a data contract too.</p>
<p>Version keys when the shape changes incompatibly:</p>
<pre><code class="language-text">product-summary:v2:42
</code></pre>
<p>Include every dimension that changes the answer: tenant, locale, currency, permissions, or feature variant. If Canadian and US prices share one key, Redis will return the wrong answer extremely quickly.</p>
<p>At the same time, avoid encoding accidental details into keys. A stable key convention should be readable, bounded, and owned by one module. Scattered string construction creates the same drift as scattered query keys on the frontend.</p>
<h2>Fast misses can overwhelm the database</h2>
<p>Suppose a popular product&#39;s cache entry expires while 2,000 users are viewing it.</p>
<p>All 2,000 requests miss. All 2,000 query PostgreSQL. The cache was supposed to protect the database, but its expiration creates a traffic spike instead.</p>
<p>That is a cache stampede.</p>
<p>One mitigation is request coalescing: the first request acquires a short-lived right to refresh, while the others wait briefly or receive a slightly stale value. Another is adding random jitter to expiration times so thousands of related keys do not disappear in the same millisecond.</p>
<pre><code class="language-typescript">const baseTtlSeconds = 300;
const jitterSeconds = Math.floor(Math.random() * 60);

await redis.set(key, value, {
  EX: baseTtlSeconds + jitterSeconds,
});
</code></pre>
<p>For data where brief staleness is acceptable, stale-while-revalidate is often a pleasant trade: return the old value immediately and let one worker refresh it in the background.</p>
<p>The right choice depends on what being stale means. A product description can lag. A stock reservation cannot casually do so.</p>
<h2>Sessions are not ordinary cache entries</h2>
<p>Redis is also commonly used for server-side sessions.</p>
<p>The browser stores an opaque session id in a secure cookie. Redis maps that id to session state. Every application instance can read it, so a user does not lose their session when the load balancer sends the next request to another server.</p>
<p>This solves the in-memory-session problem from the first article in the series. It also raises the stakes of Redis failure.</p>
<p>If product cache entries disappear, the database can refill them. If all sessions disappear, everyone may be logged out.</p>
<p>Same technology, different product consequence.</p>
<p>Session data needs an intentional lifetime, secure cookie settings, rotation after privilege changes, and a failure policy. It should not contain a convenient copy of the entire user record that silently outlives permission changes.</p>
<p>Store the minimum needed to identify the session and enforce its lifecycle. Redis being fast is not an invitation to stop modelling identity carefully.</p>
<h2>Counters fit Redis naturally</h2>
<p>Some jobs align especially well with Redis because it can update values atomically.</p>
<p>Rate limiting is a good example. Increment a counter for one user or IP, set an expiration window, and reject requests after the limit:</p>
<pre><code class="language-typescript">const key = `rate-limit:create-order:${userId}`;
const attempts = await redis.incr(key);

if (attempts === 1) {
  await redis.expire(key, 60);
}

if (attempts &gt; 10) {
  throw new TooManyRequestsError();
}
</code></pre>
<p>Production implementations usually use a Lua script or a dedicated command pattern so increment and expiration are atomic, and more advanced limits may use sliding windows or token buckets. But the shape explains why Redis fits: a small shared counter, updated frequently, with a natural expiration.</p>
<p>This is not a replacement for abuse protection at the CDN or gateway. It is an application-aware layer: ten checkout attempts may mean something different from ten product-page reads.</p>
<h2>Distributed locks are easy to overtrust</h2>
<p>Redis can coordinate a short-lived lock between application instances. That can be useful for preventing duplicate cache refreshes or ensuring only one scheduler starts a non-critical task.</p>
<p>It is more dangerous when the lock is treated as proof that a financial or inventory operation happened exactly once.</p>
<p>Processes pause. Networks partition. Lock leases expire. A client may continue working after it no longer owns the lock. Correct distributed locking requires careful ownership tokens, expiration, and usually fencing at the resource being protected.</p>
<p>For shared business truth, I prefer database constraints, idempotency keys, and atomic database updates whenever they can express the rule. A Redis lock can reduce duplicate work. It should not be the only wall protecting an invariant the database can enforce directly.</p>
<h2>Decide what happens when Redis is unavailable</h2>
<p>Every Redis use needs a failure answer.</p>
<ul>
<li>Product cache unavailable: read from PostgreSQL, perhaps with traffic protection.</li>
<li>Rate limiter unavailable: fail open or fail closed, depending on the endpoint and threat.</li>
<li>Session store unavailable: reject authenticated operations rather than invent identity.</li>
<li>Duplicate-work lock unavailable: skip the optimization or pause the job.</li>
</ul>
<p>One blanket fallback is not honest because these features carry different risks.</p>
<p>Also watch the fallback itself. If Redis goes down and every request immediately hits PostgreSQL, the database receives the full traffic load at the worst possible moment. Timeouts, connection-pool limits, and circuit breakers keep one dependency failure from becoming two.</p>
<p>The frontend experiences this as latency and uncertainty. A cache outage may not return a clean error; it may make requests slow enough that users retry, which increases traffic further. Pending states need time limits, retries need idempotency, and &quot;try again&quot; should not mean &quot;create another order.&quot;</p>
<h2>Cache only after you know the expensive path</h2>
<p>Redis is not the first fix for every slow endpoint.</p>
<p>Sometimes the query lacks an index. Sometimes the API fetches the same data four times. Sometimes a huge response is serialized on every request. Sometimes the frontend asks for data it does not need.</p>
<p>Caching an inefficient path can hide it until the cache misses under load.</p>
<p>Measure the request. Find the expensive operation. Decide how stale its result may be. Choose the cache ownership and invalidation rule. Then add Redis.</p>
<p>That order makes Redis wonderfully boring. It is a fast copy, a short-lived session, or an atomic counter with an explicit job. Not a mysterious red box that &quot;handles performance.&quot;</p>
<p>The next article follows work that should not keep an HTTP request open at all: confirmation emails, exports, image processing, and other jobs that need to finish after the user already has a response.</p>
<p>If your Redis instance disappeared for ten minutes, which user-visible promise would break first? That answer is a much better description of your Redis architecture than the list of commands your code uses.</p>
]]></content:encoded>
      <pubDate>Fri, 02 Oct 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Redis</category>
      <category>Caching</category>
      <category>Architecture</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>The Database Changed Before the Deployment Finished</title>
      <link>https://ma-x.im/blog/full-stack-zero-downtime-migrations</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-zero-downtime-migrations</guid>
      <description>A schema migration and an application deployment do not happen at one instant. Zero-downtime changes work when old code, new code, and the database are allowed to coexist for a while.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-zero-downtime-migrations/cover.png" alt="The Database Changed Before the Deployment Finished" /></p>
<p>Renaming a database column looks like one line of SQL.</p>
<pre><code class="language-sql">ALTER TABLE orders
RENAME COLUMN total_cents TO total_amount_cents;
</code></pre>
<p>The change is correct. The new name is clearer. The migration takes almost no time on a small table.</p>
<p>And it can still break production before the new application finishes deploying.</p>
<p>The problem is not the rename itself. The problem is that a deployment is not one instant. For a while, old application instances and new application instances may run at the same time. Requests continue arriving. Background jobs keep working. Someone may roll the application back while the database stays exactly where the migration left it.</p>
<p>That overlap changes the question.</p>
<p>We are no longer asking, &quot;Does the new code work with the new schema?&quot; We are asking whether every version that can still receive traffic works with the database as it exists right now.</p>
<h2>Deployment is a period of mixed reality</h2>
<p>Imagine three application instances behind a load balancer.</p>
<p>The deployment replaces them one at a time:</p>
<pre><code class="language-text">minute 0: old, old, old
minute 1: new, old, old
minute 2: new, new, old
minute 3: new, new, new
</code></pre>
<p>If the migration renames <code>total_cents</code> before minute one, every old instance immediately starts querying a column that no longer exists.</p>
<p>If the migration runs after minute three, every new instance may start querying <code>total_amount_cents</code> before the column exists.</p>
<p>Stopping all traffic, changing the database, and starting the application again avoids overlap. It also creates downtime and turns rollback into a much sharper operation. That may be acceptable for a small internal tool. It is a poor default for a checkout that people are using while we deploy it.</p>
<p>Zero-downtime migration is not a special PostgreSQL command. It is a sequence of changes where compatibility is preserved at every step.</p>
<blockquote>
<p>Change the system so old and new versions can coexist before you ask either one to disappear.</p>
</blockquote>
<h2>Expand before you contract</h2>
<p>The usual pattern has two phases: expand the schema to support both versions, then contract it after the old version is gone.</p>
<p>For our rename, the safe path begins by adding the new column without removing the old one:</p>
<pre><code class="language-sql">ALTER TABLE orders
ADD COLUMN total_amount_cents INTEGER;
</code></pre>
<p>Old code still reads and writes <code>total_cents</code>. Nothing has broken.</p>
<p>Next, deploy code that understands both columns. During the transition, writes keep them synchronized:</p>
<pre><code class="language-typescript">await database.transaction(async (transaction) =&gt; {
  await transaction.orders.update(orderId, {
    totalCents,
    totalAmountCents: totalCents,
  });
});
</code></pre>
<p>Reads prefer the new value but can fall back while old rows are being migrated:</p>
<pre><code class="language-sql">SELECT COALESCE(total_amount_cents, total_cents) AS total_amount_cents
FROM orders
WHERE id = $1;
</code></pre>
<p>Now old instances can keep using the old column, and new instances can begin using the new one. The database supports both realities.</p>
<p>Only after every row has the new value, every writer populates it, and no old application instance remains do we remove <code>total_cents</code>.</p>
<p>That final removal is the contract phase. It should be the least exciting part of the change because all meaningful traffic moved away from the old column earlier.</p>
<h2>Backfill is production work</h2>
<p>Adding an empty column is usually cheap. Filling it across a large table may not be.</p>
<p>This looks harmless:</p>
<pre><code class="language-sql">UPDATE orders
SET total_amount_cents = total_cents;
</code></pre>
<p>On millions of rows, one giant update can hold locks, generate a large amount of transaction log, create replication lag, and compete with the queries serving users. The statement may finish successfully while making the application painfully slow.</p>
<p>Backfill in batches instead:</p>
<pre><code class="language-sql">UPDATE orders
SET total_amount_cents = total_cents
WHERE id IN (
  SELECT id
  FROM orders
  WHERE total_amount_cents IS NULL
  ORDER BY created_at
  LIMIT 1000
);
</code></pre>
<p>Run a batch, commit it, observe the database, then continue. The exact size depends on the table, hardware, traffic, and replication setup. There is no heroic batch size that is correct everywhere.</p>
<p>The important shift is treating backfill as a workload, not as setup. It consumes the same database resources as customer requests. It needs progress metrics, rate control, and a way to resume after interruption.</p>
<h2>Constraints can arrive later</h2>
<p>The new column should eventually be <code>NOT NULL</code>. Adding that requirement immediately would reject writes from old instances that know nothing about the column.</p>
<p>So the order matters:</p>
<ol>
<li>Add the nullable column.</li>
<li>Deploy dual-write code.</li>
<li>Backfill existing rows.</li>
<li>Verify that no null values remain.</li>
<li>Add the constraint.</li>
</ol>
<p>In PostgreSQL, validating a constraint separately can reduce the risky part of the operation on a busy table:</p>
<pre><code class="language-sql">ALTER TABLE orders
ADD CONSTRAINT orders_total_amount_present
CHECK (total_amount_cents IS NOT NULL) NOT VALID;

ALTER TABLE orders
VALIDATE CONSTRAINT orders_total_amount_present;
</code></pre>
<p>Once validated, the schema can be tightened further according to the database version and operational needs.</p>
<p>The point is not memorizing this exact syntax. It is separating &quot;start enforcing this for new changes&quot; from &quot;prove every old row already satisfies it.&quot; Large production changes become safer when those are not forced into one moment.</p>
<h2>Indexes have deployment behavior too</h2>
<p>Suppose the new application also needs to find pending orders by creation time:</p>
<pre><code class="language-sql">CREATE INDEX orders_pending_created_at_idx
ON orders (created_at)
WHERE status = &#39;pending&#39;;
</code></pre>
<p>Building a normal index can block writes depending on the database and operation. PostgreSQL provides <code>CREATE INDEX CONCURRENTLY</code> so the table can continue serving writes while the index is built:</p>
<pre><code class="language-sql">CREATE INDEX CONCURRENTLY orders_pending_created_at_idx
ON orders (created_at)
WHERE status = &#39;pending&#39;;
</code></pre>
<p>The trade-off is that concurrent creation takes longer, does more work, and cannot run inside a normal transaction block. If it fails, it may leave an invalid index that has to be removed before retrying.</p>
<p>&quot;Non-blocking&quot; does not mean &quot;free.&quot; Watch CPU, I/O, lock waits, and replication lag while it runs.</p>
<p>This is where migration tools can become misleading. A tool can generate valid SQL without knowing whether Tuesday afternoon is a safe time to run it against the largest table in the system.</p>
<h2>Destructive changes deserve their own deployment</h2>
<p>Dropping a column, narrowing a type, or removing an enum value is fundamentally different from adding something.</p>
<p>An additive change creates room. A destructive change removes a path that some code may still use.</p>
<p>I prefer to separate destructive cleanup into a later deployment. Not five minutes later. Later enough that we can confirm:</p>
<ul>
<li>no old instances remain;</li>
<li>no background worker uses the old shape;</li>
<li>no scheduled job wakes up with old code;</li>
<li>no external consumer depends on the field;</li>
<li>rollback no longer requires the old schema.</li>
</ul>
<p>That delay can feel untidy because the database carries two columns for a while. Temporary duplication is cheaper than a clean schema that breaks a rollback.</p>
<h2>Rollback is a compatibility test</h2>
<p>A good deployment plan asks what happens if the application must return to the previous version.</p>
<p>With the expanded schema, rollback is straightforward: old code still sees <code>total_cents</code>. New rows created during the deployment have both values because the transition code dual-wrote them.</p>
<p>After the old column is dropped, that rollback path disappears.</p>
<p>This is why database rollback rarely means &quot;run the migration file backwards.&quot; Recreating a dropped column does not restore its lost data. Converting a type back may not recover the original representation. A migration can be reversible in syntax and irreversible in meaning.</p>
<p>For destructive changes, forward recovery is often safer: deploy a fix that works with the current schema instead of trying to rewind production data through time.</p>
<h2>The frontend can be part of the overlap</h2>
<p>Static frontend assets are cached. A user may keep a tab open for hours. A service worker may serve an older bundle after the backend deployment is complete.</p>
<p>That means API compatibility often outlives server-instance compatibility.</p>
<p>If a response field changes, the expand phase may need to return both old and new shapes long enough for active clients to age out. If an enum gains a value, older clients need a fallback. If a request becomes stricter, the backend may need to accept the old shape during the transition.</p>
<p>The browser is not automatically upgraded because the deployment dashboard turned green.</p>
<p>This is one reason I prefer additive API and schema evolution. It gives independently deployed pieces room to move without pretending they change together.</p>
<h2>A migration needs evidence, not optimism</h2>
<p>Before contracting the schema, I want evidence that the new path owns production traffic:</p>
<ul>
<li>null count for the new column is zero;</li>
<li>old-column reads and writes have stopped;</li>
<li>backfill progress is complete;</li>
<li>query latency and lock waits stayed acceptable;</li>
<li>replicas caught up;</li>
<li>the new application version is stable;</li>
<li>rollback expectations are explicit.</li>
</ul>
<p>Some of this comes from database queries. Some comes from logs and metrics. The important part is that removal is triggered by observed usage, not by &quot;the deploy finished, so it should be fine.&quot;</p>
<p>The safest database migration is often boring and slightly slow: add, deploy, copy, observe, enforce, remove.</p>
<p>That is not ceremony around one line of SQL. It is how we change a shared truth while the system keeps using it.</p>
<p>The next article looks at a different kind of shared state. Redis is fast enough to hide many problems, which is exactly why it needs a clear job before we put it between the application and the database.</p>
<p>If a migration in your system still requires everyone to hold their breath during deployment, tell me which step feels dangerous. That point usually reveals the compatibility assumption we have not made explicit yet.</p>
]]></content:encoded>
      <pubDate>Wed, 30 Sep 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Database</category>
      <category>PostgreSQL</category>
      <category>Migrations</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>SQL for Frontend Engineers: Model the Relationship First</title>
      <link>https://ma-x.im/blog/full-stack-relational-database</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-relational-database</guid>
      <description>SQL becomes much easier when you stop thinking in tables and start thinking in facts that must remain true. A checkout model shows where keys, JOINs, transactions, and indexes actually earn their place.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-relational-database/cover.png" alt="SQL for Frontend Engineers: Model the Relationship First" /></p>
<p>A product card is one object in React.</p>
<p>An order summary is another object. A customer has an array of addresses. The API returns a comfortable tree, so it is tempting to imagine the database storing the same tree somewhere below the backend.</p>
<p>A relational database does not think in component-shaped trees. It stores facts and relationships.</p>
<p>That difference is where SQL starts feeling either confusing or surprisingly clean. If you begin with screens, the database looks fragmented. If you begin with what must remain true, the pieces make sense.</p>
<p>Let us model the checkout from the first two articles. Not as a complete commerce platform. Just enough to understand the decisions a frontend engineer eventually runs into.</p>
<h2>Start with facts, not tables</h2>
<p>Before writing SQL, I would write down the facts the system needs to preserve:</p>
<ul>
<li>a customer can place many orders;</li>
<li>an order belongs to one customer;</li>
<li>an order contains one or more line items;</li>
<li>each line item records a quantity and the price charged at purchase time;</li>
<li>changing a product&#39;s current price must not rewrite an old order;</li>
<li>an order total cannot be negative.</li>
</ul>
<p>That list is already most of the model. Tables are how we encode it.</p>
<pre><code class="language-sql">CREATE TABLE customers (
  id UUID PRIMARY KEY,
  email TEXT NOT NULL UNIQUE,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE orders (
  id UUID PRIMARY KEY,
  customer_id UUID NOT NULL REFERENCES customers(id),
  status TEXT NOT NULL,
  total_cents INTEGER NOT NULL CHECK (total_cents &gt;= 0),
  currency CHAR(3) NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE order_items (
  id UUID PRIMARY KEY,
  order_id UUID NOT NULL REFERENCES orders(id),
  product_id UUID NOT NULL,
  product_name TEXT NOT NULL,
  unit_price_cents INTEGER NOT NULL CHECK (unit_price_cents &gt;= 0),
  quantity INTEGER NOT NULL CHECK (quantity &gt; 0)
);
</code></pre>
<p>Nothing here is clever. That is a strength.</p>
<p>The primary key identifies one row. The foreign key makes an invalid relationship impossible: an order cannot point to a customer that does not exist. The checks keep negative prices and zero-quantity line items out of the data, no matter which application path attempts the write.</p>
<p>Validation in the API gives users good feedback. Constraints in the database protect the truth when every other layer makes a mistake.</p>
<h2>Relationships are the model</h2>
<p>The line between <code>customers</code> and <code>orders</code> is one-to-many. One customer can have many orders, while each order points back to one customer through <code>customer_id</code>.</p>
<p>Orders and products look many-to-many: an order contains many products, and a product appears in many orders. The <code>order_items</code> table sits between them and turns that relationship into data of its own.</p>
<p>That middle table is not plumbing. Quantity belongs to the relationship between this order and this product. So does the price charged at that moment.</p>
<p>This is the first place where copying data can be correct. <code>product_name</code> and <code>unit_price_cents</code> deliberately snapshot purchase-time facts. If the catalog renames a product or changes its price tomorrow, yesterday&#39;s receipt must not change.</p>
<p>Normalization does not mean &quot;never duplicate a value.&quot; It means each fact has a clear owner. The current product price belongs to the product. The charged price belongs to the order item. Those are two different facts that happen to match at checkout time.</p>
<h2>A JOIN rebuilds the view you need</h2>
<p>The frontend wants an order details object. The database stores related rows. A <code>JOIN</code> reconnects them for this query:</p>
<pre><code class="language-sql">SELECT
  orders.id,
  orders.status,
  orders.total_cents,
  orders.currency,
  customers.email,
  order_items.product_name,
  order_items.unit_price_cents,
  order_items.quantity
FROM orders
JOIN customers ON customers.id = orders.customer_id
JOIN order_items ON order_items.order_id = orders.id
WHERE orders.id = $1;
</code></pre>
<p>The result contains one row per line item, so order fields repeat. That is normal. The backend maps those rows into the nested response the frontend wants.</p>
<p>This is a useful separation: storage is organized around integrity and querying; API responses are organized around the consumer&#39;s task. They do not need to have the same shape.</p>
<p>Trying to make every table mirror one screen usually creates awkward data. Screens change frequently. The fact that an order belongs to a customer changes much less often.</p>
<h2>Transactions protect multi-step changes</h2>
<p>Creating an order is not one write. We insert the order, insert its line items, and reserve inventory. If the third step fails, the first two should not remain as a convincing-looking half-order.</p>
<p>A transaction groups the writes into one decision:</p>
<pre><code class="language-sql">BEGIN;

INSERT INTO orders (...);
INSERT INTO order_items (...);
UPDATE inventory
SET available = available - $1
WHERE product_id = $2
  AND available &gt;= $1;

COMMIT;
</code></pre>
<p>If any required step fails, the application rolls the transaction back. Other readers see either the state before checkout or the completed order, not the awkward middle.</p>
<p>Transactions are not a way to make an entire distributed system atomic. A payment provider and an email service do not participate in your PostgreSQL transaction. Keep the database transaction focused, commit durable state, then hand slower external work to explicit follow-up processes.</p>
<p>That boundary will matter when this series reaches queues and background jobs.</p>
<h2>Concurrency is where the model gets honest</h2>
<p>Suppose only one unit remains. Two users place an order at almost the same time.</p>
<p>If both requests first read <code>available = 1</code> and then separately write <code>available = 0</code>, both may believe they bought it. The UI cannot prevent this. Disabling a button only affects one browser.</p>
<p>The conditional update above makes the database perform the check and write together. The backend then verifies that exactly one row changed. One order proceeds; the other receives an out-of-stock result.</p>
<p>This is why I think of the database as more than persistence. It is the final place where concurrent claims about shared state meet.</p>
<blockquote>
<p>If two requests can disagree, the invariant belongs where both requests must pass.</p>
</blockquote>
<p>Frontend optimistic updates still have value. They make the interface responsive. They simply do not get the final vote on shared inventory, balances, permissions, or uniqueness.</p>
<h2>Indexes answer a specific question faster</h2>
<p>An index is often explained as &quot;something that makes queries fast.&quot; True, but incomplete. An index makes a particular access pattern faster, while adding storage and write cost.</p>
<p>This query will become common:</p>
<pre><code class="language-sql">SELECT id, status, total_cents, currency, created_at
FROM orders
WHERE customer_id = $1
ORDER BY created_at DESC
LIMIT 20;
</code></pre>
<p>A matching index is:</p>
<pre><code class="language-sql">CREATE INDEX orders_customer_created_at_idx
ON orders (customer_id, created_at DESC);
</code></pre>
<p>The column order reflects the query: first narrow rows to one customer, then read them in the requested time order.</p>
<p>Adding indexes to every column is not a strategy. Each index must be updated when data changes. The right question is not &quot;which fields look important?&quot; It is &quot;which query is slow, how does it filter, and how does it sort?&quot;</p>
<p>Use the database&#39;s query plan instead of guessing. In PostgreSQL, <code>EXPLAIN ANALYZE</code> shows what actually ran, how many rows were examined, and where time went. It turns performance from folklore into evidence.</p>
<h2>The N+1 problem starts as innocent code</h2>
<p>Imagine loading twenty orders, then loading items for each order in a loop:</p>
<pre><code class="language-typescript">const orders = await orderRepository.findByCustomer(customerId);

for (const order of orders) {
  order.items = await orderItemRepository.findByOrder(order.id);
}
</code></pre>
<p>One query loads the orders. Twenty more load their items. That is N+1: one initial query plus one query per result.</p>
<p>It can hide behind a clean ORM relationship and look harmless in development with three records. Under real latency and larger result sets, it becomes a page that gets slower as the product succeeds.</p>
<p>The fix may be a join, a batched <code>WHERE order_id IN (...)</code> query, or an ORM include that genuinely batches. The important part is knowing how many database round trips the convenient abstraction produces.</p>
<p>An ORM can save enormous amounts of repetitive work. It cannot make the cost model disappear.</p>
<h2>NULL is a domain decision</h2>
<p>Nullable columns deserve more thought than they usually get.</p>
<p>Does <code>shipped_at = NULL</code> mean the order has not shipped, shipping does not apply, or old data was never migrated? Those are different states hiding behind the same absence.</p>
<p>Sometimes null is exactly correct. A pending order genuinely has no shipment timestamp yet. But if absence carries several meanings, make the state explicit with a status or separate relation rather than forcing every query and component to guess.</p>
<p>This maps directly to TypeScript. A nullable database field becomes <code>string | null</code> somewhere in the application. If frontend code keeps asking &quot;why can this be null?&quot;, the uncertainty may begin in the data model, not the component.</p>
<h2>Learn enough SQL to see through the abstraction</h2>
<p>Typed query builders and ORMs are useful. I would rather write most application queries through one than concatenate SQL strings by hand.</p>
<p>But the abstraction should make SQL easier to produce, not unnecessary to understand. You still need to see the relationships, transaction boundary, indexes, constraints, and number of round trips. Otherwise a pleasant method chain can hide an expensive or incorrect operation.</p>
<p>The frontend parallel is familiar. A component library saves us from rebuilding every button, but we still need to understand HTML and accessibility. The database deserves the same respect.</p>
<p>Once the model is clear, SQL stops looking like backend punctuation. It becomes a precise way to ask questions about facts you have deliberately structured.</p>
<p>The next problem is changing that structure while old application instances are still running. That is where a harmless-looking rename becomes a production migration.</p>
<p>If there is a database concept you have been treating as &quot;backend only,&quot; tell me which one. The best topics in this guide are exactly the boundaries frontend engineers are expected to cross without anyone first drawing the map.</p>
]]></content:encoded>
      <pubDate>Mon, 28 Sep 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>SQL</category>
      <category>PostgreSQL</category>
      <category>Database</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>An API Is a Boundary, Not a URL</title>
      <link>https://ma-x.im/blog/full-stack-api-contracts</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-api-contracts</guid>
      <description>A working endpoint is not yet a reliable API. The real contract includes validation, errors, retries, idempotency, and a way to evolve without making yesterday’s frontend fail tomorrow.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-api-contracts/cover.png" alt="An API Is a Boundary, Not a URL" /></p>
<p>The most dangerous API response is not always a <code>500</code>.</p>
<p>Sometimes it is a <code>200</code> containing almost the data your frontend expected.</p>
<p>The request worked. JSON arrived. The loading state disappeared. Then a component tried to format <code>order.total.amount</code>, except the backend now returns <code>totalAmount</code>. Nothing failed at the network boundary, so the failure moved deeper into the UI where it became harder to explain.</p>
<p>That is what makes API work deceptive. We tend to describe an API as a list of URLs, but the URL is the least interesting part. The real API is an agreement about inputs, outputs, errors, timing, repetition, and change.</p>
<blockquote>
<p>An endpoint tells you where to send a request. A contract tells both sides what the request means.</p>
</blockquote>
<p>Let us keep following the checkout request from the previous article and make that contract explicit.</p>
<h2>Validate where trust changes</h2>
<p>The browser sends this body:</p>
<pre><code class="language-json">{
  &quot;cartId&quot;: &quot;cart_42&quot;,
  &quot;deliveryMethod&quot;: &quot;courier&quot;
}
</code></pre>
<p>TypeScript can prove that our frontend created the right object. It cannot prove that the server received it unchanged. Requests can come from an old frontend bundle, a mobile client, a script, a browser extension, or someone calling the endpoint by hand.</p>
<p>The network boundary erases TypeScript&#39;s guarantees. Runtime validation restores them.</p>
<pre><code class="language-typescript">import { z } from &#39;zod&#39;;

const CreateOrderRequest = z.object({
  cartId: z.string().min(1),
  deliveryMethod: z.enum([&#39;courier&#39;, &#39;pickup&#39;]),
});

const input = CreateOrderRequest.parse(request.body);
</code></pre>
<p>This is not defensive programming for unusually malicious users. It is how the backend prevents an assumption from travelling further into the system.</p>
<p>Without validation, an invalid <code>deliveryMethod</code> might reach pricing, inventory, and persistence before something finally throws. With validation, the request stops at the boundary and returns a useful problem while the system is still in a known state.</p>
<p>The response needs the same treatment on the frontend. A generated type or shared interface improves development, but it is not runtime evidence.</p>
<pre><code class="language-typescript">const OrderResponse = z.object({
  id: z.string(),
  status: z.enum([&#39;pending&#39;, &#39;confirmed&#39;]),
  total: z.object({
    amount: z.number(),
    currency: z.string().length(3),
  }),
});

const order = OrderResponse.parse(await response.json());
</code></pre>
<p>I do not validate every harmless response in every application. That can become ceremony. I validate the boundaries where wrong data would be expensive, confusing, or difficult to recover from: authentication, checkout, payments, persisted user work, and integrations owned by another team.</p>
<h2>Errors are part of the response model</h2>
<p>A successful response usually gets careful types. Errors often get whatever the framework happens to throw.</p>
<p>That creates frontend code like this:</p>
<pre><code class="language-typescript">try {
  await createOrder(input);
} catch (error) {
  showToast(&#39;Something went wrong&#39;);
}
</code></pre>
<p>The message may be acceptable for an unknown infrastructure failure. It is terrible for &quot;one product is no longer available&quot; or &quot;the delivery address is outside the service area.&quot; Those are expected business outcomes. The user can act on them.</p>
<p>A small structured error is enough:</p>
<pre><code class="language-typescript">type ApiProblem = {
  code: &#39;CART_CHANGED&#39; | &#39;OUT_OF_STOCK&#39; | &#39;INVALID_ADDRESS&#39;;
  message: string;
  field?: string;
  requestId: string;
};
</code></pre>
<p>The stable part is <code>code</code>. The backend can improve <code>message</code> without breaking frontend behavior. The optional <code>field</code> lets a form place the error next to an input. The <code>requestId</code> connects the user&#39;s failure to server logs.</p>
<p>HTTP status still matters. A <code>400</code> means the request itself is invalid. <code>401</code> means authentication is missing or no longer valid. <code>403</code> means identity is known but the action is forbidden. <code>409</code> means the request conflicts with current state. <code>429</code> means slow down. <code>500</code> means the server failed to complete an operation it should have been able to complete.</p>
<p>The status gives infrastructure and generic clients a category. The problem code gives the product a decision.</p>
<h2>Retrying changes the meaning of a request</h2>
<p>Our user presses <strong>Place order</strong>. The backend creates the order, but the connection drops before the response reaches the browser.</p>
<p>What should the UI do?</p>
<p>From its point of view, the result is unknown. Retrying might recover cleanly. It might also create a second order.</p>
<p>This is where idempotency stops being backend vocabulary and becomes a user-experience requirement. The frontend generates a key for the action and sends it with the request:</p>
<pre><code class="language-typescript">const idempotencyKey = crypto.randomUUID();

await fetch(&#39;/orders&#39;, {
  method: &#39;POST&#39;,
  headers: {
    &#39;Content-Type&#39;: &#39;application/json&#39;,
    &#39;Idempotency-Key&#39;: idempotencyKey,
  },
  body: JSON.stringify(input),
});
</code></pre>
<p>The backend stores the key with the result. If the same operation arrives again, it returns the original result instead of performing the write twice.</p>
<p>The detail people miss is scope. The key belongs to one user intent, not one HTTP attempt. A network retry reuses it. A genuinely new click after the user changes the cart gets a new key.</p>
<p>Disabling the button is still good interface behavior. It is not a correctness guarantee. Two tabs can submit. A client can retry. A timeout can hide a successful response. Correctness must survive outside the DOM.</p>
<h2>Versioning is mostly about restraint</h2>
<p>Teams often imagine API versioning as <code>/v1</code>, <code>/v2</code>, then a heroic migration. Most healthy evolution is much less dramatic.</p>
<p>Adding an optional field is usually safe. Adding a new enum value may not be safe if the frontend uses an exhaustive switch and has no fallback. Renaming a field is breaking. Changing <code>null</code> to an omitted property can be breaking. Turning cents into decimal currency units while keeping the field name is definitely breaking, and especially unpleasant because the value still looks valid.</p>
<p>My default rule is additive evolution:</p>
<ul>
<li>add before removing;</li>
<li>let old and new clients coexist;</li>
<li>measure whether the old field is still used;</li>
<li>remove only after the migration is complete.</li>
</ul>
<p>For a rename, the backend can temporarily return both fields:</p>
<pre><code class="language-json">{
  &quot;totalAmountCents&quot;: 4299,
  &quot;total&quot;: {
    &quot;amount&quot;: 42.99,
    &quot;currency&quot;: &quot;CAD&quot;
  }
}
</code></pre>
<p>The frontend moves to <code>total</code>. Telemetry confirms old consumers have disappeared. Only then does <code>totalAmountCents</code> go away.</p>
<p>That overlap can feel untidy. It is also much cheaper than requiring every deployed client to change at the same instant.</p>
<h2>Dates and money expose weak contracts quickly</h2>
<p>Two values deserve suspicion at every API boundary: dates and money.</p>
<p><code>42.99</code> is not a complete monetary value. Which currency? Is it a floating-point amount or a display representation? Can tax introduce more precision? A safer contract sends an integer in minor units or a decimal string, plus an explicit currency.</p>
<p>Likewise, <code>2026-09-26</code> is a calendar date, while <code>2026-09-26T14:30:00Z</code> is an instant. They are not interchangeable. A delivery day may intentionally have no timezone. An order creation timestamp absolutely does.</p>
<p>The frontend should not have to infer these semantics from a field name. Good contracts make units and meaning boringly explicit.</p>
<h2>Generate types, but keep a runtime boundary</h2>
<p>OpenAPI, GraphQL code generation, tRPC, and shared schema packages can remove a large amount of duplicated typing. I like that. If the server already knows the response shape, the frontend should not retype it from memory.</p>
<p>But generated types answer a build-time question: do these codebases agree on the declared contract?</p>
<p>Runtime validation answers a different question: did this particular response satisfy it?</p>
<p>You may need both. A generated client keeps everyday development fast. Runtime parsing protects the important places where deployment skew, stale clients, proxies, or external integrations can make reality differ from the declaration.</p>
<p>The useful architecture is not &quot;validate everything twice.&quot; It is to choose where a contract deserves enforcement and make that choice visible.</p>
<h2>The UI should consume decisions, not transport details</h2>
<p>A component should not contain a switch over raw status codes, parse arbitrary error bodies, or know that one endpoint wraps data under <code>result</code> while another does not.</p>
<p>Put that translation in the API layer:</p>
<pre><code class="language-typescript">type CreateOrderResult =
  | { ok: true; order: Order }
  | { ok: false; reason: &#39;cart-changed&#39; | &#39;out-of-stock&#39; | &#39;invalid-address&#39; };
</code></pre>
<p>Now the UI handles product outcomes. The API client handles HTTP, schemas, and server problem codes. The backend handles validation and business rules. The boundaries line up with the questions each layer can answer.</p>
<p>That is the part I care about most. Type safety is useful, but the real goal is not a beautiful generated interface. It is making failure unsurprising.</p>
<p>The next article goes one layer deeper. Once the API accepts &quot;create order,&quot; the database has to preserve customers, orders, products, and their relationships without letting them contradict each other.</p>
<p>If your frontend has to guess what an API error means, send me the shape of that guess. Those awkward branches are usually the clearest map of where the contract needs work.</p>
]]></content:encoded>
      <pubDate>Sat, 26 Sep 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>API Design</category>
      <category>TypeScript</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>What Actually Happens After fetch()</title>
      <link>https://ma-x.im/blog/full-stack-after-fetch</link>
      <guid isPermaLink="true">https://ma-x.im/blog/full-stack-after-fetch</guid>
      <description>A frontend request does not jump straight from the browser into your backend. It crosses DNS, encrypted connections, edge infrastructure, application code, and a database before the UI sees a result.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/full-stack-after-fetch/cover.png" alt="What Actually Happens After fetch()" /></p>
<p>A user clicks <strong>Place order</strong>. Your component calls <code>fetch(&#39;/orders&#39;)</code>. A moment later, it either receives JSON or lands in <code>catch</code>.</p>
<p>That can feel like one operation. It is not even close.</p>
<p>Between the click and the response, the request may cross a DNS resolver, an encrypted connection, a CDN, a load balancer, an application process, a cache, and a database. Then the response has to travel back through most of that chain. Any link can be slow, unavailable, misconfigured, or perfectly healthy while the next one is failing.</p>
<p>Frontend engineers do not need to become network engineers to work with this well. But we do need a useful picture of what happens after <code>fetch()</code>. Without it, every failure becomes &quot;the API is broken,&quot; and that sentence is usually too vague to help anyone.</p>
<p>So let me follow one request all the way through.</p>
<h2>Start with the URL</h2>
<p>Imagine the browser is about to send this:</p>
<pre><code class="language-typescript">await fetch(&#39;https://api.shop.example/orders&#39;, {
  method: &#39;POST&#39;,
  headers: { &#39;Content-Type&#39;: &#39;application/json&#39; },
  body: JSON.stringify({ cartId: &#39;cart_42&#39; }),
});
</code></pre>
<p>Before any application code runs, the browser has several questions to answer:</p>
<ul>
<li>Where is <code>api.shop.example</code>?</li>
<li>Can I establish a secure connection to it?</li>
<li>Which machine should receive this request?</li>
<li>Is there already a connection I can reuse?</li>
</ul>
<p>The URL is not an address a network understands directly. It is a human-readable instruction that has to be resolved into infrastructure.</p>
<h2>DNS finds the entrance</h2>
<p>DNS turns <code>api.shop.example</code> into an IP address, or more often into another hostname that eventually leads to one.</p>
<p>The browser first checks caches: its own, the operating system&#39;s, perhaps the local network&#39;s. If there is no usable answer, a DNS resolver looks up the records. The final result may point directly to a server, but production systems often point to a CDN or load balancer instead.</p>
<p>This is the first useful debugging boundary. If DNS is wrong, your backend can be completely healthy and still receive nothing. The browser may report <code>DNS_PROBE_FINISHED_NXDOMAIN</code>, while an application dashboard shows zero errors because the request never reached the application.</p>
<p>That distinction matters. &quot;No server logs&quot; does not always mean &quot;the browser did not send it.&quot; It may mean the browser could not find the server.</p>
<h2>The browser creates a secure connection</h2>
<p>Once the browser has an address, it needs a connection. With HTTP/1.1 or HTTP/2, that normally means TCP. With HTTP/3, it uses QUIC over UDP. The transport details differ, but the practical goal is the same: create a reliable path for exchanging data.</p>
<p>Because the URL uses HTTPS, TLS also has to establish trust and encryption. The server presents a certificate for the domain. The browser checks that the certificate is valid, trusted, not expired, and actually covers <code>api.shop.example</code>.</p>
<p>If that fails, React never gets an HTTP status. There is no <code>502</code> response to inspect. The secure conversation did not begin.</p>
<p>This is why &quot;network error&quot; is such an unhelpful bucket in frontend code. It can mean offline Wi-Fi, DNS failure, TLS failure, a blocked connection, a proxy problem, or a request cancelled by our own code. Those failures happen before an API can return a neat JSON error.</p>
<h2>The first machine may not be your backend</h2>
<p>Suppose DNS and TLS work. The request reaches the public edge of the system.</p>
<p>That edge might be a CDN such as CloudFront or Cloudflare. Static files can be returned there without touching the application. Dynamic requests usually continue inward, but the edge can still enforce rate limits, reject suspicious traffic, terminate TLS, or attach routing headers.</p>
<p>Next may come a reverse proxy or load balancer. Its job is not to understand the shopping cart. It decides which healthy application instance should receive the request.</p>
<pre><code class="language-text">browser
  -&gt; DNS
  -&gt; secure connection
  -&gt; CDN / edge
  -&gt; load balancer
  -&gt; application instance
</code></pre>
<p>This is where one public API becomes many private processes. The user sees <code>api.shop.example</code>. Behind it, five application instances may be serving traffic. A health check tells the load balancer which instances are ready. If one process crashes, new requests go elsewhere.</p>
<p>That sounds like an infrastructure detail until application state lives only in the memory of one instance. Then the first request creates a checkout session on server A, the second lands on server B, and the session appears to have vanished. Horizontal scaling has exposed an application design problem.</p>
<p>We will return to that later in this series. For now, the important point is simple: a domain name does not identify one process.</p>
<h2>Now the application gets a turn</h2>
<p>Only after all of that does the request reach the route handler we usually call &quot;the backend.&quot;</p>
<p>The application parses the body, authenticates the user, checks authorization, validates the input, and decides what operation &quot;create order&quot; means. It may call another service. It may read Redis. It may open a database transaction. It may publish a job to a queue.</p>
<p>For our order, the simplified path could be:</p>
<pre><code class="language-text">validate request
  -&gt; load cart
  -&gt; confirm current prices
  -&gt; reserve inventory
  -&gt; create order
  -&gt; return order id
</code></pre>
<p>Each arrow is another boundary and another possible failure.</p>
<p>This is why a backend can be &quot;up&quot; while a request still fails. The process responds to health checks, but the database connection pool is exhausted. Or Redis is unavailable. Or the inventory service times out. A green server is not the same thing as a successful operation.</p>
<h2>The database is not just storage at the end</h2>
<p>The database often decides whether the whole operation is trustworthy.</p>
<p>Creating an order may require several writes that must succeed together: insert the order, reserve stock, and record the payment attempt. If two writes succeed and the third fails, we do not want half an order. A transaction gives the backend a boundary: commit the whole change, or roll it back.</p>
<p>The query also has a cost. Missing indexes can turn a small lookup into a scan across millions of rows. A lock held by another transaction can make a fast query wait. Too many concurrent requests can exhaust the connection pool even when the database itself still has CPU available.</p>
<p>From the browser, all three may look like a slow spinner.</p>
<p>That is the recurring theme of this journey: the UI sees the total time, not the layer that spent it.</p>
<h2>The response walks back through the system</h2>
<p>When the application finishes, it returns an HTTP response. That response passes back through the load balancer and edge, crosses the encrypted connection, and reaches the browser.</p>
<p>Then <code>fetch()</code> does something that still catches people: it resolves for HTTP errors. A <code>400</code> or <code>500</code> is a successful network exchange, so the promise resolves. We have to inspect <code>response.ok</code> ourselves.</p>
<pre><code class="language-typescript">const response = await fetch(&#39;/orders&#39;, options);

if (!response.ok) {
  const problem = await response.json();
  throw new OrderRequestError(response.status, problem);
}

return response.json();
</code></pre>
<p>The promise rejects when the browser cannot complete the exchange at all, or when our code aborts it. Those are different categories from a backend that deliberately returned a validation error.</p>
<p>A useful frontend error model keeps that distinction:</p>
<ul>
<li><strong>No response:</strong> connection, DNS, TLS, offline state, timeout, or cancellation.</li>
<li><strong>HTTP response with a problem:</strong> authentication, validation, conflict, rate limit, or server failure.</li>
<li><strong>Successful response with unexpected data:</strong> contract or parsing failure.</li>
</ul>
<p>One generic &quot;Something went wrong&quot; may be the right user-facing sentence. It should not be the only thing the system knows.</p>
<h2>A request id connects the pieces</h2>
<p>There is one small habit that makes this entire chain easier to reason about: give every request a correlation id.</p>
<p>The edge can create it, or the frontend can send one. Each service includes it in logs, and the response returns it as a header. Now a bug report can connect one browser event to the exact load-balancer entry, application log, and database operation.</p>
<pre><code class="language-typescript">const requestId = crypto.randomUUID();

await fetch(&#39;/orders&#39;, {
  ...options,
  headers: {
    ...options.headers,
    &#39;X-Request-Id&#39;: requestId,
  },
});
</code></pre>
<p>That id is not observability by itself. But it gives the layers a shared thread, which is much better than comparing timestamps and hoping two errors belong to the same click.</p>
<h2>What this changes in frontend code</h2>
<p>Understanding the path does not mean the component should know about DNS or load balancers. Quite the opposite. It helps us create a cleaner boundary around network work.</p>
<p>A UI should know whether an action is pending, whether it can be retried, and what the user can do next. The API client should distinguish no-response failures from HTTP problems and invalid payloads. The backend should return structured errors. Infrastructure should expose enough context to trace the request across layers.</p>
<p>Each layer owns a different question.</p>
<blockquote>
<p>The browser sees one request. The system sees a chain of contracts.</p>
</blockquote>
<p>Once that picture is clear, several full-stack topics stop feeling unrelated. Idempotency protects a repeated request. Transactions protect a multi-step write. Redis changes where data is read. A load balancer changes where code runs. Observability connects the path after something fails.</p>
<p>They are all answers to the same question: what must stay true while a request crosses the system?</p>
<p>The next article follows the request into the application and focuses on the first contract we control directly: the API boundary.</p>
<p>If there is a part of the request path that still feels like a black box, tell me where you lose the thread. That is probably the part of this guide worth opening up next.</p>
]]></content:encoded>
      <pubDate>Thu, 24 Sep 2026 00:00:00 GMT</pubDate>
      <category>Full Stack</category>
      <category>Web Architecture</category>
      <category>Networking</category>
      <category>Full-Stack Field Guide</category>
    </item>
    <item>
      <title>AI Code Review Should Sound Like a Careful Engineer</title>
      <link>https://ma-x.im/blog/github-ai-pr-reviewer</link>
      <guid isPermaLink="true">https://ma-x.im/blog/github-ai-pr-reviewer</guid>
      <description>I built a personal AI reviewer for GitHub Pull Requests, not as a cold rule bot, but as a local and observable tool that checks known risk patterns and turns them into useful engineering conversation.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/github-ai-pr-reviewer/cover.png" alt="AI Code Review Should Sound Like a Careful Engineer" /></p>
<p>There is a strange cost to a queue of Pull Requests: it rarely looks like a problem.</p>
<p>One PR can be reviewed in the evening. The second between meetings. The third looks small, just dependencies. The fourth already passed CI, so it probably feels fine. Very quickly, review stops being focused engineering work and becomes a memory task: where exactly am I expected to pay attention?</p>
<p>The routine itself was not what bothered me. Review is part of the job. What bothered me was that the most repeatable risks require the most boring kind of attention. Did an auth guard disappear? Did unsafe <code>innerHTML</code> show up? Did a dependency update add an install lifecycle script? Was an export removed? Did an API, schema, or config contract change in a way the author may not have noticed?</p>
<p>These are not the places where you need a genius reviewer. They are the places where it is unpleasantly easy to miss something.</p>
<p>So I built a personal AI reviewer for GitHub Pull Requests. Not because I wanted to replace human review. And definitely not because I wanted another bot leaving comments like &quot;Potential security issue.&quot; I wanted a tool that keeps a baseline level of risk attention running consistently, while still speaking to the author like a reasonable engineer.</p>
<h2>I did not want a rule bot</h2>
<p>The simplest version of this tool is obvious: scan the diff for patterns, leave comments.</p>
<p>That becomes bad review very quickly.</p>
<p>The machine sees a fragment. It does not know the whole product history, the migration path, the team&#39;s local agreement, the temporary rollout plan, or why the author made that trade-off. If it writes with too much certainty, it starts sounding smarter than it has earned the right to be.</p>
<p>At the same time, I did not want a harmless toy that is afraid to say something real. Good review does not have to be soft. If a PR removes a permission guard or adds process execution from user-controlled input, the reviewer should stop and ask for changes. But even then, the job of the comment is to move the author toward verification and a decision, not to drop a red flag and leave.</p>
<p>The principle became:</p>
<blockquote>
<p>Automated review should be specific, checkable, and modest about what it knows.</p>
</blockquote>
<p>That modesty is not politeness theater. It reflects the reality of review. The reviewer often sees the diff, but not the whole context.</p>
<h2>The local reviewer</h2>
<p>The tool lives locally, outside cloned repositories. That was the first practical decision. I did not want a normal repository cleanup, reinstall, or update to accidentally delete the system that watches my review queue.</p>
<p>It runs through macOS <code>launchd</code>: it starts after login and then runs a review cycle roughly once an hour. Each cycle finds GitHub Pull Requests where my account is listed as a requested reviewer.</p>
<p>This is not a global GitHub scanner. It is my personal review inbox. If someone explicitly asks me to review a PR, the tool can put it into its queue.</p>
<p>Before reviewing, it waits until the checks for the current head commit are green. That constraint matters. If CI is already failing, an automated reviewer tends to comment on noise: formatting, types, broken tests, obvious things the author needs to fix before a real review. I want the tool to review something close to a human-reviewable state, not compete with CI.</p>
<p>It also stores local state: which PRs it has already reviewed and which head SHA it saw at the time. If new commits are pushed to a previously reviewed PR, the agent reviews it again, even if the requested-reviewer event did not happen again.</p>
<p>That small detail changes the behavior. A PR is not a static document. The author may respond to feedback, rewrite part of the logic, update dependencies, or fix tests. A reviewer that only remembers &quot;I have seen this PR&quot; becomes blind to the most important part of the process: what changed after the first round.</p>
<h2>What it does, and what it does not do</h2>
<p>The main path is simple: the agent reads the diff and publishes comments on the relevant diff lines. If GitHub does not accept an inline comment — for example, because the diff position is no longer valid or the API cannot attach the note to that line — it falls back to a body-only review and preserves the meaning of the finding.</p>
<p>That matters more than it sounds. Bad automation often breaks on a small API detail and silently drops the useful signal. I would rather leave a body review than pretend there was nothing to say.</p>
<p>The checks are intentionally simple and explainable: dynamic code execution, unsafe <code>innerHTML</code>, process execution, install lifecycle scripts, removed exports, risky API/schema/config changes, and disappearing auth or permission guards.</p>
<p>This is not a complete model of code quality. It is not a senior engineer replacement. It is a first, repeatable pass over known classes of risk.</p>
<p>There is also a boundary around approval. The agent does not approve PRs with unresolved review threads; it leaves a comment instead. It can auto-approve safe dependency-bot PRs, but normal PRs are limited by the number of reviews per run.</p>
<p>I want automation to help, not turn into an uncontrolled conveyor belt of approvals.</p>
<h2>The most important iteration was the comment style</h2>
<p>Technically, building the loop was not the most interesting part. Finding PRs, checking requested reviewers, waiting for checks, reading diffs, and leaving reviews are all annoying in the details, but conceptually straightforward.</p>
<p>The most important iteration was the language of the comments.</p>
<p>Early automated review text tends to collapse into dry warnings:</p>
<p>&quot;Potential security issue.&quot;</p>
<p>&quot;Validate compatibility.&quot;</p>
<p>&quot;This may break consumers.&quot;</p>
<p>Those comments can be technically correct and still be bad review. They do not tell the author what to check. They do not connect the concern to the specific change. They sound like a confident alarm with missing context.</p>
<p>So I rewrote the rules around how comments should be phrased. A comment should name the specific risk, attach it to the change, explain why the diff raised concern, and ask a question the author can answer.</p>
<p>For example, instead of &quot;Validate compatibility,&quot; a better comment is:</p>
<blockquote>
<p>Can you confirm whether this exported type is still used by downstream packages? I could be missing the migration path, but removing it here looks like it may break consumers outside this repo.</p>
</blockquote>
<p>The value is not that the sentence became softer. The value is that it gives the author a concrete check: downstream usage and migration path.</p>
<p>Another example:</p>
<blockquote>
<p>Can we make sure this HTML is sanitized before it reaches <code>dangerouslySetInnerHTML</code>? The diff makes the rendered value look closer to user-controlled content than before.</p>
</blockquote>
<p>That does not scream &quot;security bug.&quot; But the risk is direct: sanitization and user-controlled content.</p>
<p>One more:</p>
<blockquote>
<p>Can you confirm this route still requires the same permission check? I see the guard moved out of this branch, and I want to make sure the access rule did not become implicit.</p>
</blockquote>
<p>This is the tone I want. The reviewer does not say, &quot;you removed auth.&quot; It says: I see a change, it looks like a risk, help confirm the invariant.</p>
<p>That is the difference between humanizing text and pretending to be human. The goal is not to disguise AI as a teammate. The goal is to automate the best parts of good engineering review: specificity, respect for context, an argument behind the concern, and the willingness to be wrong.</p>
<h2>Why the control panel matters</h2>
<p>Automation that runs somewhere in the background can become a source of anxiety very quickly.</p>
<p>Is it checking anything? Did it crash? Is it writing a comment right now? Did it skip a PR because checks were red, or because it did not see the requested reviewer? Has it already reviewed this SHA? Did a new commit appear after the last run?</p>
<p>If those questions are hard to answer, the tool saves time in one place and spends trust in another.</p>
<p>So the reviewer has a local browser control panel, available only on localhost. It shows whether the service is running, the <code>launchd</code> state, and the current process state. It has Start, Stop, Restart, Refresh, and logs.</p>
<p>The panel shows the timer until the next review cycle, the last run time, and whether a cycle is currently active. It has an event feed: PRs found, skips, errors, published reviews, and links to the PR or review.</p>
<p>It also shows counters and a tracked PR table: total tracked PRs, how many are visible, how many need rereview, which SHA was reviewed, and which SHA appeared later.</p>
<p>The logs are not hidden. I can open the service output and see what actually happened.</p>
<p>That is not decoration. Observability is part of the product. If automation leaves comments in your name, you should not need a terminal investigation to understand what it did.</p>
<h2>The boundaries I want to keep</h2>
<p>I do not want a reviewer that pretends to understand everything.</p>
<p>It does not know the product better than the team. It should not approve normal PRs just because it did not find a risk pattern. It should not argue with the author instead of a person. It should not turn a simple dependency update into a philosophical review if the known safe conditions are satisfied.</p>
<p>Its value is narrower: it looks consistently at classes of risk that are easy to miss, waits for a reasonable check state, remembers reviewed SHAs, returns after new commits, and makes its comments visible inside the conversation humans are already having.</p>
<p>Most importantly, it does not have to be perfect to be useful. But it does have to be inspectable. I need to see what it did, why it skipped a PR, which SHA it reviewed, where it posted the review, and when the inline-comment fallback became a body-only review.</p>
<p>Mature automation should not feel like magic. Magic is hard to debug.</p>
<h2>What I built, really</h2>
<p>I did not start with the idea of replacing review. I started with the feeling that some attention could become continuous.</p>
<p>A Pull Request is still a conversation between engineers. The author knows the context of the change. The reviewer sees risk from the outside. CI checks formal properties. An agent can add another layer: a repeatable pass over known dangerous areas, with comments that help the author respond in a useful way.</p>
<p>That is the shape I like. Not a cold judge. Not autopilot approvals. Not a bot that writes &quot;Potential issue&quot; and disappears.</p>
<p>More like a careful junior reviewer: arrives on time, remembers what it saw, asks specific questions, and leaves the decision to people.</p>
<p>The value is not that AI replaced a reviewer. The value is that attention to risk became regular, observable, and part of the process.</p>
<p>That is the kind of automation I am interested in: limited, checkable, useful to humans, and modest enough not to get in the way of a normal engineering conversation.</p>
<p>If you&#39;re building an AI reviewer of your own, I&#39;d be curious to hear where you draw the line between useful vigilance and automated noise. Reach out and tell me what earned your trust, and what lost it.</p>
]]></content:encoded>
      <pubDate>Tue, 22 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Code Review</category>
      <category>GitHub</category>
      <category>Engineering Workflow</category>
    </item>
    <item>
      <title>The Columns Hidden Inside Your Customer Reviews</title>
      <link>https://ma-x.im/blog/jev-human-text-to-data</link>
      <guid isPermaLink="true">https://ma-x.im/blog/jev-human-text-to-data</guid>
      <description>What interests me about TypeSafe&apos;s Jev is turning human language into data we can query. Restaurant reviews, product complaints, and Reddit discussions make the idea much more concrete than another chatbot benchmark.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/jev-human-text-to-data/cover.png" alt="The Columns Hidden Inside Your Customer Reviews" /></p>
<p>A restaurant has ten thousand reviews. Its dashboard has an average rating, a monthly trend, and a search box.</p>
<p>Somewhere inside those reviews is a much more useful answer: people like the food, but delivery packaging keeps ruining it.</p>
<p>There is no column for that.</p>
<p>That is what interests me about TypeSafe&#39;s Jev. I want to look at it as a way to turn human text into data: reviews, comments, forum discussions, the messy sentences that contain signals our databases cannot currently query.</p>
<p>I recently wrote about a <a href="https://ma-x.im/blog/silpo-ai-factory-horeca-procurement-agent">restaurant procurement agent</a>. That project started with operational inputs: menus, portions, inventory, supplier availability. Here I want to look at another side of the same business. Customers are already describing problems. How do those descriptions become something an engineer can aggregate and a restaurant owner can investigate?</p>
<p>This is an architecture I would explore, not a report from a deployed Jev integration. The examples below are hypothetical. I haven&#39;t run a benchmark on restaurant reviews.</p>
<h2>A prep station for language</h2>
<p>Think about a restaurant kitchen before service. Ingredients arrive in different shapes. Someone washes, separates, portions, and puts them into containers the rest of the kitchen can work with.</p>
<p>I see a similar job between raw text and analytics.</p>
<p>A review arrives as a paragraph. The next stage produces a few useful attributes. Conventional software groups and counts them. A person, or a more capable language model with access to the evidence, can then work on the interpretation.</p>
<p>The distinction matters because <a href="https://typesafe.ai/blog/introducing-system-one-models-and-jev">Jev gives up free-form string generation</a>. TypeSafe presents it as a model for fast, structured decisions. It doesn&#39;t browse Reddit for you or write the research report. You supply the material and the questions.</p>
<p>Its <a href="https://docs.typesafe.ai/primitives">three question types</a> are Noul, a yes/no probability; Choice, a selection from supplied alternatives with probabilities; and Score, a position along defined levels. That is enough to add many useful attributes to an existing record. It doesn&#39;t make it an arbitrary extractor of new names, quotations, or product identifiers.</p>
<p>If I need the exact restaurant name, I would preferably get it from source metadata. If I need an exact quotation, I would preserve the original text. Asking a classifier to choose between known categories is a different operation from asking a model to discover and return an unrestricted string.</p>
<blockquote>
<p>The interesting AI feature is the column you couldn&#39;t populate before.</p>
</blockquote>
<h2>One review can contain several different problems</h2>
<p>Consider this invented delivery review:</p>
<p><em>“The noodles were great, but the sauce leaked through the bag. This is the second time. I&#39;d order again if they changed the containers.”</em></p>
<p>A single positive/negative label throws away most of what makes that useful.</p>
<p>I would ask whether the author praises the food, reports a packaging failure, describes a repeated problem, and makes a future order conditional on a change. Those signals can coexist. Forcing them into one winning category would lose information before the analysis even starts.</p>
<p>At the application boundary, a normalized record might look like this. The values are illustrative, and this is my storage shape, not a Jev API response:</p>
<pre><code class="language-typescript">type ReviewSignals = {
  reviewId: string;
  restaurantId: string;
  source: &#39;delivery&#39; | &#39;direct&#39; | &#39;forum&#39;;
  publishedAt: string;
  foodPraiseProbability: number;
  packagingFailureProbability: number;
  repeatProblemProbability: number;
  conditionalReturnProbability: number;
  modelVersion: string;
  questionSetVersion: string;
};
</code></pre>
<p>The first four fields come from ingestion. The next four are model judgments. The last two let me explain how those judgments were produced.</p>
<p>I would keep the raw review linked to this row. An owner clicking “packaging complaints increased” should be able to read the evidence, including the examples the classifier got wrong.</p>
<p>Question wording deserves as much care as the schema. “Is packaging bad?” invites a broad interpretation. “Does the author report that the container or bag leaked, broke, or failed to contain the food?” defines an observable claim.</p>
<p>Even then, a complaint describes the author&#39;s account. It doesn&#39;t establish which party caused the failure. A crushed meal might involve the container, the courier, or both. I would resist naming the field <code>restaurant_packaging_defect</code> unless the evidence actually supports that attribution.</p>
<h2>Parallel questions, then parallel records</h2>
<p>There are two kinds of parallel work here, and I would keep them separate in the design.</p>
<p>For one review, ask all the independent questions together. TypeSafe documents that questions in a request share the same input state and are evaluated independently. One answer does not become context for the next. If a later question requires information fetched using an earlier answer, that needs another step. <a href="https://docs.typesafe.ai/primitives">Question semantics</a>.</p>
<p>Across the dataset, workers process different reviews concurrently. That is our ingestion and scheduling problem: queues, bounded concurrency, retries, rate limits, and resumable jobs. Parallel question evaluation doesn&#39;t promise unlimited corpus throughput.</p>
<p>The pipeline I have in mind is quite ordinary:</p>
<pre><code class="language-text">Source exports / permitted APIs
        ↓
Normalize records, retain context, remove duplicates
        ↓
Queue → workers → Jev questions for each record
        ↓
Versioned semantic attributes + original record links
        ↓
SQL aggregates → evidence review → optional written report
</code></pre>
<p>I would give each result a key built from the source record, content hash, model version, and question-set version. Retrying a request should not create a second observation in the dashboard. Changing a question should not silently overwrite the previous measurement.</p>
<p>The kitchen analogy has a useful limit here. You can prepare ingredients in parallel, but putting twice as many cooks in one doorway won&#39;t double dinner service. The slowest part might be collecting the data, resolving duplicates, or reviewing ambiguous examples.</p>
<h2>Let SQL do the counting</h2>
<p>Once those columns exist, familiar tools become useful again.</p>
<p>Here is an illustrative PostgreSQL query over enriched delivery reviews. The 0.8 cutoff is a placeholder to validate against labeled examples, not a recommended universal threshold:</p>
<pre><code class="language-sql">SELECT
  restaurant_id,
  date_trunc(&#39;month&#39;, published_at) AS month,
  count(*) AS analyzed_reviews,
  count(*) FILTER (
    WHERE packaging_failure_probability &gt;= 0.8
  ) AS flagged_packaging_reviews,
  round(
    100.0 * count(*) FILTER (
      WHERE packaging_failure_probability &gt;= 0.8
    ) / count(*),
    1
  ) AS flagged_share_pct
FROM review_signals
WHERE source = &#39;delivery&#39;
  AND model_version = :model_version
  AND question_set_version = :question_set_version
GROUP BY restaurant_id, date_trunc(&#39;month&#39;, published_at);
</code></pre>
<p>That last number is the share of analyzed reviews flagged by this rule. It is not the percentage of orders with failed packaging. We don&#39;t have all orders in this table. We have people who wrote reviews, within whatever collection process we used.</p>
<p>I&#39;d put that distinction in the dashboard label. Otherwise a technically correct query becomes a misleading product feature.</p>
<p>The next useful question might be whether flagged reviews concentrate around one location, a delivery channel, or a period after a container change. Those joins require reliable operational metadata. Jev cannot supply missing order history by interpreting a paragraph more confidently.</p>
<h2>The same idea works for products</h2>
<p>Now replace the restaurant with a coffee grinder.</p>
<p>“Loud” can mean several things. One owner mentions the noise and still recommends it. Another says it wakes their child and explains why they returned it. A third repeats something they heard without owning the product.</p>
<p>I would want separate signals for claimed ownership, noise complaints, reported returns, and explicit reasons for returning. These are much more useful than a single sentiment score when deciding what to investigate about a product.</p>
<p>The model still only judges what the text supports. “The author claims to own it” is a defensible label. “Verified purchaser” requires purchase evidence outside the text.</p>
<p>There is also a discovery problem. A fixed question set finds the issues I thought to ask about. If a new failure mode appears and none of my questions cover it, a cheap classifier can miss it at enormous scale.</p>
<p>I would regularly inspect a diverse sample of unflagged and uncertain records, and use open-ended analysis to propose new categories. Then I would label examples, evaluate the revised questions, and backfill comparable periods. Otherwise the dashboard can look stable simply because its vocabulary stopped evolving.</p>
<h2>Reddit is a research corpus, with a boundary</h2>
<p>The Reddit version interests me even more: a deliberately scoped collection of posts and comments about a product or buying decision.</p>
<p>Suppose I want to investigate why people discussing home espresso equipment regret a purchase. I would define the communities, time window, collection method, and inclusion criteria first. Then I could classify first-hand reports, price objections, maintenance complaints, and explicit alternatives people are considering.</p>
<p>A comment like “same here” needs its parent. A sarcastic reply can reverse the apparent meaning. I would pass the relevant context with the comment and make clear which text is the target of the judgment. I would also retain thread relationships so that one lively discussion doesn&#39;t masquerade as fifty independent purchasing experiences.</p>
<p>Collection needs its own implementation, using access appropriate to the source. Jev begins after we have the records; it doesn&#39;t solve that access problem.</p>
<p>What could the output honestly say? Something like: “Within this collected set of discussions, maintenance complaints were more frequent among posts describing regret.”</p>
<p>It could not establish the percentage of all espresso-machine owners who regret buying one. A million comments still reflect the people, communities, search terms, and periods that brought those comments into the dataset.</p>
<blockquote>
<p>Scale makes a sample bigger. It doesn&#39;t automatically make it representative.</p>
</blockquote>
<p>Honestly, this is where I think the engineering gets interesting. Processing the text cheaply is only one part. Defining what the resulting number means is the part the product has to get right.</p>
<h2>The price changes what is worth trying</h2>
<p>As checked on September 17, 2026, TypeSafe&#39;s <a href="https://docs.typesafe.ai/cookbooks/parallel_questions">parallel-questions cookbook</a> uses a Jev input price of $0.042 per million tokens and zero output-token cost. That is a published pricing assumption, not my measured bill or a guarantee of future pricing.</p>
<p>At that rate, a hypothetical million records averaging 1,000 total billed input tokens each would cost $42 for model input. “Total” matters: the budget must include the text, context, questions, and other billed request content. Collection, storage, retries, evaluation, and human review are separate costs.</p>
<p>That makes a broad enrichment pass worth considering. I would still measure tokens and throughput on a realistic pilot before extrapolating.</p>
<p>The same cookbook reports roughly 12.2× lower cost and 10× lower latency for batching thirteen questions over one document instead of asking them separately. That is a vendor example of batching, not evidence that my million-review pipeline will finish ten times faster.</p>
<p>For me, the appealing consequence is practical: I can consider retaining several narrowly defined signals per record instead of asking one overloaded question and hoping its answer will support every future analysis.</p>
<h2>Correct types still need correct judgments</h2>
<p>TypeSafe&#39;s launch discussion distinguishes schema correctness from decision correctness. A model constrained to valid choices can still choose the wrong one. Its “zero hallucinations” language should be read in that narrower sense. <a href="https://typesafe.ai/blog/introducing-system-one-models-and-jev">Launch explanation</a>.</p>
<p>I would start with a labeled evaluation set spanning clear examples, mixed opinions, sarcasm, short replies, and the languages actually present in the corpus. For each signal, I would check false positives and missed cases. A packaging-complaint filter and a claim about purchase intent may need different acceptance rules.</p>
<p>A returned probability also deserves validation on that specific task. I wouldn&#39;t interpret every 0.9 as a demonstrated 90% correctness rate in my data. TypeSafe&#39;s <a href="https://docs.typesafe.ai/confidence">confidence documentation</a> explains the supplied measures; whether they support my operating threshold still needs evidence from the intended workload.</p>
<p>Ambiguous cases can remain uncertain, go to human review, or receive a second analysis. If I exclude them from a chart, I want their count visible. Quietly dropping the hard cases can make a trend look cleaner than the underlying evidence.</p>
<p>Finally, a writing model should receive computed aggregates, definitions, coverage limitations, and selected source excerpts. It can help explain the results. It should not invent the counts, turn a correlation into a cause, or claim that a few selected quotes prove a population-wide trend.</p>
<h2>The useful thing is what becomes queryable</h2>
<p>I keep coming back to that restaurant with ten thousand reviews.</p>
<p>The review text already contains more detail than the star rating can express. What I want is a way to preserve some of that detail in a form ordinary software can work with, while keeping the original words close enough to challenge the interpretation.</p>
<p>That is the role I would give Jev: a focused processing stage between human language and the database. The report comes later. First I want to know which columns are worth creating, how reliably we can populate them, and what decisions they actually help someone make.</p>
<p>If you have a pile of reviews or community discussions that your current dashboard can&#39;t explain, I&#39;d be interested to hear which missing column you wish you could query.
  </p>
]]></content:encoded>
      <pubDate>Thu, 17 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI</category>
      <category>Data Engineering</category>
      <category>Product Engineering</category>
    </item>
    <item>
      <title>Earned Autonomy: Comparing My Agent Workflow with a Corporate Experiment</title>
      <link>https://ma-x.im/blog/agentic-development-at-work</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agentic-development-at-work</guid>
      <description>I arrived at Liberty with a working model for agentic development. Now the company is exploring a more formal approach built around reusable agent skills. I don&apos;t know which model will prove better yet, and that uncertainty is the interesting part.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agentic-development-at-work/cover.png" alt="Earned Autonomy: Comparing My Agent Workflow with a Corporate Experiment" /></p>
<p>I came to Liberty roughly six months ago with a fairly strong opinion about what an AI coding agent should be doing.</p>
<p>Not autocomplete. Not a faster way to generate a function. Not a chat window that occasionally knows how to edit a file.</p>
<p>I had been building a working environment in which an agent could participate in most of the delivery lifecycle: understand a task, inspect the code, make a plan, change a small slice, run the right checks, prepare a pull request, and help work through what happened afterwards.</p>
<p>Now Liberty is starting to explore a more formal way of working with agents. The approach uses reusable agent skills and a more explicit, specification-driven sequence around planning, implementation, validation, and learning.</p>
<p>I find the comparison more useful than either approach in isolation. I already have a workflow that works for me. The company experiment gives me a chance to ask a harder question: which parts of that workflow are genuinely valuable, and which parts only feel valuable because I built them myself?</p>
<blockquote>
<p>The interesting question is not whether an agent can act autonomously. It is whether its autonomy has been earned by context, rules, and evidence.</p>
</blockquote>
<h2>What I brought with me</h2>
<p>My approach is based on earned autonomy.</p>
<p>That phrase matters. I don&#39;t mean giving an agent unlimited freedom and hoping that a capable model will make good decisions. I mean giving it room to move inside boundaries that are explicit enough to be checked.</p>
<p>In a typical task, the agent starts with a goal from a ticket or a conversation. It researches the relevant code and nearby context. It tries to find the code that actually controls the behavior instead of editing the first file that looks related. It identifies constraints, forms a local hypothesis about how the change should work, and chooses a cheap check that could prove the hypothesis wrong.</p>
<p>Then it changes a small slice and validates that change immediately. Broader checks come afterwards. When the work is ready, the agent can prepare a branch and a draft pull request, help investigate CI failures, and respond to review feedback.</p>
<p>The important part is not the length of that list. It is that the list describes a connected workflow rather than a collection of isolated prompts.</p>
<h3>Context before cleverness</h3>
<p>The workflow depends on several levels of context. There are personal rules about how I want an agent to work, shared engineering standards, and repository-specific instructions. More local and more current information should win over a general preference.</p>
<p>Memory helps the agent continue across sessions, but memory is not authority. The current code, tests, documentation, and actual tool output are more trustworthy than something saved from a previous conversation.</p>
<p>That distinction is easy to state and surprisingly important in practice. A remembered convention can be stale. A test failure in front of you cannot be argued away by a paragraph written last month.</p>
<h3>Tools instead of context theatre</h3>
<p>I also prefer integrations over manually copying the world into a prompt. When permitted, the agent can work with task systems, documentation, source code, tests, and pull requests through connected tools.</p>
<p>The benefit is not that the agent suddenly knows everything. It is that the relevant evidence can stay close to the task. Requirements, implementation, and validation do not have to be reconstructed from a chain of pasted fragments every time.</p>
<p>That still requires judgment. An integration can expose the wrong context just as efficiently as the right one. Tools expand an agent&#39;s reach; they don&#39;t decide what deserves attention.</p>
<h3>The human is still the owner</h3>
<p>An agent can research, edit, test, and prepare changes. It does not own the requirements, the architecture decision, the approval, or the final review.</p>
<p>That is not a ceremonial disclaimer. It is part of the design. Autonomy is useful when it removes mechanical coordination and preserves human attention for decisions that carry real risk. If a workflow asks a person to approve every harmless step, it has not created autonomy. It has created a queue of tiny approvals.</p>
<h2>What Liberty is exploring</h2>
<p>The approach Liberty is beginning to investigate puts more structure around the work. It uses reusable agent skills: packages of instructions and working patterns that help an agent perform a particular kind of engineering task more consistently.</p>
<p>A skill might support context discovery, planning, implementation, debugging, or review. It is not another intelligent employee. It is a reusable description of how to approach a task, with the aim of making useful practices easier to repeat across engineers and repositories.</p>
<p>The broader lifecycle is more explicit than the one I normally follow informally:</p>
<ol>
<li>Prepare the relevant instructions and context.</li>
<li>Clarify or create a specification.</li>
<li>Produce a plan.</li>
<li>Review the plan.</li>
<li>Implement the change.</li>
<li>Validate the result.</li>
<li>Record observations about quality and usability.</li>
</ol>
<p>There is a lot to like here. A team cannot rely on one engineer having spent months tuning a personal workflow. Reusable skills offer a possible common language. They might help engineers with different levels of experience start from a better baseline, and they create a clearer shape for comparing what happened across tasks.</p>
<p>The experiment is still early. Liberty is exploring and piloting this kind of systematic approach; it has not produced a final company-wide process or a conclusion about fully autonomous software development. That distinction matters because the value of the experiment is precisely that the result is not known yet.</p>
<h2>Where the approaches meet</h2>
<p>The overlap is larger than the difference.</p>
<p>Both approaches assume that an agent needs good context before it needs more freedom. Both treat clear requirements, planning, small changes, tests, and human review as controls rather than optional polish. Both value reusable knowledge, because repeating the same explanation in every session is a poor use of anyone&#39;s time.</p>
<p>Both also suggest that speed of generation is an incomplete measure. A change that appears quickly but needs extensive correction is not necessarily faster. The useful question includes quality, traceability, review effort, and what happens when the next engineer has to understand the result.</p>
<p>That is why I don&#39;t see reusable skills as the opposite of my workflow. They may be a way to package some of its useful habits so that they are less dependent on personal configuration. They may also expose parts of my process that are too implicit to teach to anyone else.</p>
<h2>Where I am cautious</h2>
<p>The concerns I have are hypotheses, not verdicts. The experiment needs to test them.</p>
<p>The first is process proportionality. A full specification, plan, review, implementation, and reporting sequence makes sense for risky or complex work. For a tiny, obvious change, it may create more text and more transitions than value. A good engineering process should scale with the risk of the task, not apply the same ceremony to everything.</p>
<p>The second is that artifacts can become the goal. A detailed specification can still describe a wrong assumption. An agent can produce a polished plan for a problem nobody actually has. Evidence and useful questions matter more than document length.</p>
<p>The third is the cost of formal stopping points. If a person must confirm every safe step, the workflow can become a stream of interruptions. My current model delegates many low-risk decisions through standing rules and brings a human in where the risk changes. I want to know whether a more formal process can preserve that shape.</p>
<p>There is also a locality problem. A reusable skill can explain how to plan or validate in general, but it cannot automatically know a repository&#39;s architecture, conventions, environmental limits, or history of decisions. Without that local layer, a skill can produce work that sounds reasonable and still does not belong in the system.</p>
<p>Finally, attribution will be difficult. If a task goes well, the cause might be the skill, the repository instructions, the connected tools, the tests, the quality of the original request, or the engineer&#39;s own experience. A fair experiment has to separate those effects as much as it reasonably can.</p>
<p>Reporting has a cost too. Tracking corrections, defects, rework, and subjective experience is necessary for an honest comparison. But if reporting takes a meaningful amount of engineering time, that time is part of the result, not an inconvenient detail to hide.</p>
<h2>What I want to learn</h2>
<p>I am not joining this experiment to prove that my existing approach is already the answer. That would make the comparison pointless.</p>
<p>I want to find out which skills prevent mistakes that would otherwise reach review. Where does an explicit specification improve the result, and where does it add ceremony? Does the process help an agent recover context in a new session? How much human correction is actually required? What happens to review quality and cost?</p>
<p>The useful comparison will come from real tasks, not from a polished demonstration. For each task, I would want to understand whether the acceptance criteria were met, how much correction and rework appeared, which defects were found by the agent, CI, or a person, and which parts of the framework produced observable value.</p>
<p>I would also separate ordinary engineering effort from framework overhead and reporting time. Those are different costs. Combining them into one number would make the result look cleaner while making it less informative.</p>
<p>The data will need to be aggregated and anonymized. The goal is to learn about the workflow, not to publish internal details or turn a small experiment into a claim about the whole company.</p>
<h2>This is the beginning, not the conclusion</h2>
<p>There is a temptation to choose a winner early. Personal workflows feel concrete because you can see their history. Formal frameworks feel credible because they have names, stages, and reusable artifacts. Neither feeling is evidence.</p>
<p>My current expectation is that the useful outcome will be a combination. Some skills may capture practices worth making more consistent. Some formal stages may be valuable only for certain classes of work. Some of my existing rules may turn out to be personal preferences rather than durable engineering principles.</p>
<p>That is a good result. The point is not to protect a workflow because I built it, or to accept a framework because it arrived with a process diagram. The point is to discover which controls help an agent do better work while leaving people responsible for the decisions that matter.</p>
<p>I am at the beginning of that comparison now. Later, once there is enough evidence and the results have been checked for accuracy and confidentiality, I&#39;ll come back with what actually happened: where reusable skills helped, where they added friction, and what I changed in my own way of working.</p>
<p>Until then, I am keeping the conclusion deliberately open. An agent does not become trustworthy because it has more freedom, more documents, or more steps. It becomes more trustworthy when the system around it makes good decisions easier to verify.</p>
<p>If you&#39;ve been comparing a personal agent workflow with a more formal team process, I&#39;d be interested in what you measured. The difference between a convincing demo and a useful engineering system usually appears only after the first few ordinary tasks.</p>
]]></content:encoded>
      <pubDate>Wed, 16 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>Engineering Workflow</category>
    </item>
    <item>
      <title>The Cart Was Not the Product: Building a HoReCa Procurement Agent for Silpo AI Factory</title>
      <link>https://ma-x.im/blog/silpo-ai-factory-horeca-procurement-agent</link>
      <guid isPermaLink="true">https://ma-x.im/blog/silpo-ai-factory-horeca-procurement-agent</guid>
      <description>For the Silpo AI Factory hackathon, the interesting problem was not building another AI shopping chat. It was figuring out where AI should stop, where deterministic procurement logic should begin, and how MCP could execute a controlled supplier workflow at the end.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/silpo-ai-factory-horeca-procurement-agent/cover.png" alt="The Cart Was Not the Product: Building a HoReCa Procurement Agent for Silpo AI Factory" /></p>
<p>The obvious idea was also the least interesting one.</p>
<p>The Silpo AI Factory hackathon gave participants an unusually tempting setup: AI, an official MCP server from a large grocery retailer, product search, carts, delivery, purchase history, and the requirement to build an agent that could actually use tools.</p>
<p>The easy version almost writes itself. A user asks what to buy. AI suggests products. The app puts them into a cart.</p>
<p>Useful? Maybe.</p>
<p>Interesting? Not enough.</p>
<p>That shape quickly becomes another AI shopping chat. I wanted to avoid that, partly because the hackathon itself was clearly nudging people toward something more agentic: not text generation around commerce, but a system where the agent receives context, uses tools, and helps complete a concrete task.</p>
<p>The project that came out of that became a HoReCa Procurement Agent: an AI-assisted procurement cockpit for a small restaurant that also handles catering and events.</p>
<p>Links for the project:</p>
<ul>
<li>Hackathon: <a href="https://ai-factory.silpo.ua/">Silpo AI Factory</a></li>
<li>Official Silpo MCP: <a href="https://ai-factory.silpo.ua/docs/mcp">ai-factory.silpo.ua/docs/mcp</a></li>
<li>Live demo: <a href="https://horeca-nine-alpha.vercel.app/">horeca-nine-alpha.vercel.app</a></li>
<li>Source code: <a href="https://github.com/MaximusFT/horeca">github.com/MaximusFT/horeca</a></li>
<li>Demo video: <a href="https://youtu.be/5D2QP2OFflM">YouTube</a></li>
</ul>
<h2>The shopping assistant was too small</h2>
<p>The first direction was some version of Smart Basket, AI cart, shopping assistant. It is a reasonable idea. It is also the first idea most people would reach for.</p>
<p>That was the problem.</p>
<p>I kept asking one question: if I remove the AI chat, what remains as a product?</p>
<p>For a generic shopping assistant, the answer was not strong enough. The AI was doing too much of the conceptual work. The product was mostly a conversation with a cart mutation at the end.</p>
<p>So we changed the buyer.</p>
<p>What if the customer is not one person deciding what to eat, but a small restaurant or catering business trying to plan procurement?</p>
<p>That changed everything. A grocery cart stopped being a list of preferences. It became the result of an operational problem: regular restaurant demand, bookings, events, guest counts, menus, recipes, inventory, incoming supply, safety stock, shelf life, and supplier availability.</p>
<p>Now the official commerce MCP had a more precise role. It did not define the business need. It helped execute a need the system had already calculated.</p>
<p>That boundary became the project.</p>
<h2>Catering was close, but not enough</h2>
<p>The first stronger version was catering procurement.</p>
<p>Wedding. 180 guests. Menu. Recipes. Ingredients. Shopping list. Supplier cart.</p>
<p>That already felt much better than a shopping assistant. A single change, like 180 guests becoming 200, can flow through portions, recipe quantities, ingredient demand, packages, and cart preparation.</p>
<p>Good demo.</p>
<p>Still not quite the product.</p>
<p>The flaw was simple: a restaurant continues to exist between events. It still needs vegetables, meat, dairy, eggs, coffee, bread, and all the ordinary supplies of daily operations. Event demand is only one source of demand.</p>
<p>After that correction, the model became much stronger:</p>
<p>Regular restaurant demand + event and catering demand + safety stock - current inventory - incoming supply = procurement need.</p>
<p>Then procurement need becomes: when to buy, which supplier products can satisfy it, what package sizes are available, what is in stock, and what should be prepared for approval.</p>
<p>That is the point where the idea stopped being an event shopping calculator and became a procurement layer for HoReCa.</p>
<blockquote>
<p>The cart is not the product. The procurement plan is the product.</p>
</blockquote>
<p>The cart is the final execution step. The product is the plan that explains what the business needs, when it needs it, and why.</p>
<h2>AI should not do the arithmetic</h2>
<p>Once the product model became clearer, another decision followed naturally: most of the important work was not AI work.</p>
<p>Take the hero scenario. A wedding changes from 180 to 200 guests. The menu includes salmon croissants, cheese croissants, ham croissants, skewers, salads, and dessert cups. If each guest gets 0.35 salmon croissant and each croissant uses a known amount of salmon, that is not a prompt. That is arithmetic.</p>
<p>The same is true for BOM expansion, unit conversion, inventory subtraction, FEFO, safety stock, package rounding, procurement dates, and plan diffing.</p>
<p>If an LLM calculates those numbers, the system becomes less reliable, not more intelligent.</p>
<p>So the procurement engine became ordinary TypeScript code. It owns the critical math: restaurant demand, event demand, recipes, ingredients, inventory lots, incoming supply, shortages, purchase dates, packages, and the procurement plan.</p>
<p>The AI got a different job: understand natural-language intent, choose application tools, explain where a number came from, help match internal ingredients to supplier products, evaluate replacements, and orchestrate a sequence of safe tool calls.</p>
<p>AI does not calculate procurement. AI helps operate the procurement system.</p>
<p>That was probably the most important architecture decision in the project.</p>
<h2>Dashboard first, agent second</h2>
<p>I also did not want the user to open a procurement system in the morning and ask an AI, &quot;so, what is happening today?&quot;</p>
<p>The application should already know.</p>
<p>It should know upcoming events, restaurant load, procurement batches, shortages, expiry risks, incoming supply, and the next actions that need attention. The agent can help explain and act, but the primary interface should be an operations dashboard, not a blank chat box.</p>
<p>So the UI settled into four main sections: Overview, Procurement, Events, and Inventory.</p>
<p>The agent did not become a fifth top-level screen. It became a contextual drawer: ask why a quantity is needed, explain the impact of the wedding change, find a replacement, prepare the next supplier order.</p>
<p>That distinction matters. A chat-first system makes the user discover the product by asking questions. A cockpit should surface the current operating state before the user asks.</p>
<h2>Misto Kitchen made the model concrete</h2>
<p>To avoid building against abstractions, we created a synthetic restaurant: Misto Kitchen.</p>
<p>One kitchen. Shared inventory. Around 55 seats. Regular restaurant operations. Catering and events. A 14-day planning horizon in the Europe/Kyiv business timezone.</p>
<p>The demo events gave the system real pressure: Birthday Breakfast, Office Lunch, Private Anniversary, Tech Conference, and Wedding. The wedding became the hero flow: 180 guests to 200 guests, total event guests moving from 445 to 465.</p>
<p>That tiny number change was useful because anyone can understand it. A manager gets a call: the wedding will have 200 people, not 180. In a manual process, that means recalculating menu portions, ingredients, inventory coverage, packaging, and supplier orders.</p>
<p>In this system, the change creates a preview first. The user inspects the impact, then approves it. Only after approval does it become Plan v2.</p>
<p>This is where the product became more than a planning spreadsheet. The system could explain what changed and why.</p>
<h2>Explainability had to be part of the model</h2>
<p>One number is not enough.</p>
<p>If the system says &quot;buy 31.4 kg of chicken,&quot; a real procurement manager has an immediate question: why 31.4?</p>
<p>So provenance became part of the domain model. Each procurement line needed to explain its sources: restaurant operations, specific events, current inventory, incoming supply, safety stock, shelf-life rationale, and supplier enrichment state.</p>
<p>The AI can summarize that explanation in human language. But it should not invent it. The source of truth is the deterministic calculation and structured provenance.</p>
<p>This is one of those product decisions that looks like UX until you build it. Then it becomes architecture. If explanation is generated after the fact by the model, you get storytelling. If explanation is part of the calculation, you get auditability.</p>
<h2>The first UI was too engine-shaped</h2>
<p>After the deterministic core started working, the first noticeable product flaw was not in the math. It was in the Overview.</p>
<p>The screen was too close to the engine. It wanted to show procurement batches, dated ingredient lines, restaurant operating days, and engine status. Those facts were correct, but they did not explain the product to a restaurant manager.</p>
<p>So the Overview had to be rebuilt around the business story:</p>
<p>Restaurant Operations + Events &amp; Catering -&gt; Combined Business Demand -&gt; Procurement Plan -&gt; Supplier Execution.</p>
<p>The first viewport needed to show regular operations, the five events, upcoming deliveries, and attention items. Not because those cards are prettier. Because the user has to understand in 10-15 seconds that the system combines daily restaurant demand and event demand into one procurement plan.</p>
<p>That was a useful reminder: a correct engine does not automatically create a clear product.</p>
<p>There was also a very normal frontend bug. The Ask Misto drawer first looked trapped inside a narrow header. The issue was not the drawer size. A sticky header used <code>backdrop-filter</code>, which changed the containing block for <code>position: fixed</code> descendants. The fix was to render the fullscreen overlay through a React portal into <code>document.body</code>.</p>
<p>Small bug. Good lesson. A feature can be logically correct and still be mounted in the wrong DOM and CSS context.</p>
<h2>Mock supplier first, live MCP later</h2>
<p>Before live Silpo access was reliable, we built the full supplier flow against a mock supplier.</p>
<p>That was not a fake replacement for integration. It was how we protected the product model from waiting on external access.</p>
<p>The mock supplier covered product mappings for all 38 ingredients, package sizes, prices, availability, deterministic package rounding, surplus and cost, substitution approval, cart preview, cart write, reread, reconciliation, and preservation of unrelated cart lines. The preferred salmon SKU was intentionally unavailable, with a compatible 400 g alternative, so the product could exercise replacement handling.</p>
<p>The architecture was simple on purpose:</p>
<p>Procurement engine -&gt; SupplierGateway -&gt; Mock supplier or Silpo supplier.</p>
<p>The mock stayed as an offline fallback. It should never be described as live Silpo.</p>
<p>When the agent arrived, it was also intentionally small. One Procurement Agent. Not Inventory Agent, Forecast Agent, Supplier Agent, Cart Agent, and Supervisor Agent. For this MVP, that would have been architecture theatre.</p>
<p>The agent worked through application-level tools like <code>get_event</code>, <code>get_procurement_plan</code>, <code>explain_requirement</code>, <code>preview_event_change</code>, <code>apply_event_change</code>, and <code>prepare_supplier_order</code>. It did not see the whole raw MCP catalog. It did not own procurement quantities. Every tool called an application use case with its own schema and approval rules.</p>
<h2>Live AI was not the proof</h2>
<p>The default agent mode became <code>AGENT_MODE=local</code>.</p>
<p>That sounds less exciting than a live model, but it was the right default for this project. It was deterministic, free, offline, used the same protected application tools, and made the demo stable enough to record.</p>
<p>The OpenAI Responses API path was implemented as an explicit opt-in with a limited number of consecutive tool steps. But the final demo should not be described as a fully autonomous live OpenAI session. The API key was not configured in this workspace, and the protocol was covered with mocked HTTP tests.</p>
<p>What the demo proves is still valuable: the agentic application flow, the tool orchestration, the approval boundaries, and the supplier execution path. But it is important to say what was actually tested.</p>
<h2>The official MCP changed the shape of the work</h2>
<p>The early generic JSON-RPC client and public docs were enough to understand the direction. They were not enough for a reliable live integration.</p>
<p>After access, the real path required OAuth 2.1: Dynamic Client Registration, authorization code, PKCE S256, refresh token, a session-scoped provider, the official <code>@modelcontextprotocol/sdk</code>, Streamable HTTP transport, and server-side OAuth state.</p>
<p>The rule at that stage was simple: do not invent Silpo arguments, fields, or output shapes.</p>
<p>First, we captured <code>tools/list</code> through an authorized session on September 1, 2026. It returned 40 unique tools, not the 39 suggested by the earlier public description. One additional branch was <code>silpo_create_shopping_cart</code>.</p>
<p>Input draft-07 schemas were saved and validated with Ajv before MCP calls. Output schemas were not treated as known in advance. The mapper only read documented paths and returned safe diagnostics when a shape did not match, without leaking values.</p>
<p>That is where integration work starts to become real. Not at the first successful tool call. At OAuth, schemas, expiry, idempotency, reread, and observability.</p>
<h2>Read first, then write</h2>
<p>The first live workflow was deliberately read-only:</p>
<p><code>silpo_get_my_shopping_cart</code> -&gt; <code>silpo_get_shopping_cart_by_id</code> -&gt; <code>silpo_get_time_slots</code> -&gt; <code>silpo_find_products_batch</code>.</p>
<p>Search ran for eggs, tomatoes, and salmon. The workflow stopped before writes if there was no cart, not enough cart context, an invalid delivery slot, or a response shape that did not match documented paths. Express delivery normalized to <code>DeliveryHome</code>, because that is what the product search tool description required.</p>
<p>Only after that came a constrained write spike:</p>
<p>preview -&gt; explicit human approval -&gt; one additive write -&gt; immediate cart reread -&gt; validation.</p>
<p>Existing cart lines could not be cleared or replaced automatically. On September 2, 2026, production confirmed one limited additive write for a test candidate with quail eggs. The sanitized trace showed <code>silpo_add_or_update_cart_products</code> followed by <code>silpo_get_shopping_cart_by_id</code>.</p>
<p>The UI reported a verified result only after reread.</p>
<p>That is not automatic checkout. It is not payment. It is a controlled cart mutation with evidence.</p>
<h2>Serverless made state honest</h2>
<p>The wedding change flow revealed another boundary.</p>
<p>Locally, an in-memory repository was convenient. In production, a memory singleton is not a storage strategy. Different Vercel workers can handle different requests, so planning state and event previews had to move behind a real repository boundary.</p>
<p>The final approach allowed local development to use in-memory state, while deployed planning state and event previews used Turso. Apply and reread went through the repository boundary, and tests covered preview, apply, and refresh through different repository instances.</p>
<p>That bug did not invalidate the product model. It made the deployment model more honest.</p>
<p>The corporate-machine constraints also shaped the workflow. Local coding, tests, and static checks happened on the work machine. Silpo OAuth and OTP happened in a personal browser. Deployed backend calls handled Silpo MCP. GitHub Actions covered sanitized trace and Turso smoke checks. Secrets and raw cart/product values were not routed through the model or stored in traces.</p>
<p>Not glamorous. Necessary.</p>
<h2>Live integration made the mock better</h2>
<p>The supplier-neutral gateway eventually connected the same application contract to the official MCP:</p>
<p>UI -&gt; SupplierOrderService -&gt; SupplierGateway -&gt; SilpoSupplierGateway -&gt; official MCP.</p>
<p>The first live rollout was bounded. It did not try to send the whole batch. It selected up to three light, fully fulfillable lines within a ten-line window, with total mass or volume capped at 20 kg/l, far below the observed 50 kg Silpo delivery validation. The cap was checked before preview and before write.</p>
<p>This was a pilot, not a claim that the entire procurement batch could safely go to Silpo.</p>
<p>The gateway accounted for display ratio, step, price, stock, package rounding, product IDs already in the cart, error-level cart validations, and reread after write. After live verification, matching became stock-aware and started skipping SKUs that could not fully satisfy the need.</p>
<p>The salmon replacement case was especially useful. In the mock catalog, the preferred salmon SKU was unavailable, so the offline flow showed a substitution. In live Silpo, <code>silpo_get_replacements</code> for sampled salmon returned success with an empty list.</p>
<p>That empty list was not an error. It meant there was no known picking or assembler risk for that product. The system had to accept reality instead of inventing a replacement for a prettier demo. Unresolved lines blocked cart preview. Unknown nested shapes did not become synthetic replacements. Exact nested replacement mapping would only be added after a separate capture.</p>
<p>The integration became more reliable because it allowed the real response to be boring.</p>
<h2>The failures after live verification were the useful ones</h2>
<p>Several issues appeared only after the live path existed.</p>
<p>A delivery slot could expire between preparation and apply, so the system needed recovery: reread cart, fetch available slots, preview alternatives, require approval, update the slot, reread the exact slot, and retry supplier preparation.</p>
<p>A repeated demo run could accidentally double the real cart quantity. Reset Demo must not delete real Silpo cart lines, so the gateway now reads existing product IDs before supplier initialization and stops before preview/write if a selected SKU is already present.</p>
<p>Insufficient stock also changed matching. A known product is not enough if there is not enough stock to fulfill the required quantity.</p>
<p>Later, i18n exposed a runtime boundary: a Dictionary with interpolation functions cannot be passed from a Server Component to a Client Component as a plain prop. Typecheck and build passed, but the browser failed. The fix was to pass a plain locale to client components and let them read a client-safe dictionary import. Backend locale also flows into agent explanations and supplier flow.</p>
<p>These are not glamorous failures. They are the difference between a demo that works once and a workflow that can be trusted enough to show.</p>
<h2>Observability had to be safe</h2>
<p>A normal browser cannot directly show a server-to-server call from Vercel backend to <code>mcp.silpo.ua</code>. It only sees requests to the Next.js routes.</p>
<p>So the demo needed a sanitized Agent request execution timeline.</p>
<p>It combines application decisions, application tool calls, official Silpo MCP calls, sequence number, status, and duration. In mock mode, the timeline shows only application steps. In deployed Silpo mode, it adds blue Silpo MCP entries from the server-side trace.</p>
<p>That mattered because a reviewer needs to see where the application made a decision and where the official supplier tool was actually called. But the trace stays safe: operation names, argument key names, status, duration, and structural summary. Not tokens. Not raw arguments. Not addresses. Not cart contents. Not product values.</p>
<h2>What the final demo actually proves</h2>
<p>The final demo flow was intentionally short.</p>
<p>Overview shows Restaurant Operations, Events &amp; Catering, Combined Procurement, a 14-day timeline, and attention items. Wedding starts at 180 guests. The user changes it to 200, reviews the preview, sees Plan v1 become Plan v2, and approves. The system updates total guests to 465 and records the change.</p>
<p>Then the user opens Why this quantity? and sees deterministic provenance: demand sources, inventory coverage, incoming supply, and timing. Finally, Ask Misto prepares the next supplier order, shows a bounded supplier card, handles availability/substitution state, creates a cart preview, requires approval, performs an additive write, rereads the cart, and reports a verified result.</p>
<p>The right ending is not &quot;AI ordered everything.&quot;</p>
<p>The right ending is: the agent guided the user from a business event change, through an explainable procurement plan, to a verified supplier cart, while keeping business-critical mutations under human control.</p>
<p>Testing followed the same philosophy. Unit and application tests covered demand calculation, BOM expansion, FEFO, incoming supply timing, package rounding, provenance, wedding preview/apply, stale preview protection, agent tool schemas, mock supplier flow, reread reconciliation, live schema validation, timeslot updates, sanitized MCP trace, and Turso repositories.</p>
<p>The handoff baseline had 122 tests in 36 files. Static checks included <code>npm test</code>, <code>npm run typecheck</code>, <code>npm run lint</code>, <code>npm run build</code>, and <code>npm audit</code>, with audit reporting 0 vulnerabilities after adding the MCP SDK.</p>
<p>Browser QA covered the wedding flow, explanation drawer, mock salmon substitution, cart preview -&gt; approval -&gt; reread, agent supplier preparation, expired slot recovery, mobile/tablet navigation at 850 px and 390 px, no page-level horizontal overflow, procurement search, expiry-risk filtering, and localized units.</p>
<p>Production Silpo verification confirmed live OAuth, 40 tools from <code>tools/list</code>, cart context, approved timeslot update, batch product search, one explicitly approved additive product write, immediate reread and validation, a bounded supplier flow through the normal Procurement Agent UI, one <code>silpo_add_or_update_cart_products</code> call followed by <code>silpo_get_shopping_cart_by_id</code>, and preservation of existing cart lines.</p>
<p>It did not prove everything. It did not test all 40 tools, full checkout, payment, full wholesale ordering, every replacement response shape, live OpenAI autonomy, or real restaurant usage.</p>
<p>That distinction is the whole point.</p>
<h2>What I would keep</h2>
<p>The hardest part of this project was not connecting an LLM to a store.</p>
<p>The hardest part was deciding what work the AI should not do.</p>
<p>The procurement engine calculates. The plan explains. The UI surfaces the operating state. The agent interprets intent and orchestrates tools. The supplier gateway executes bounded supplier operations. MCP is the final execution layer, not the product itself.</p>
<p>That boundary made the project more interesting than another shopping assistant.</p>
<p>Silpo is a connected commerce supplier in this story. I am not presenting it as a full HoReCa wholesale ERP, and the MVP is not a multi-supplier procurement platform. The future architecture could absolutely grow that way: Silpo, Metro, meat suppliers, vegetable suppliers, local suppliers, with AI helping compare availability, delivery, price, quality constraints, and preferences.</p>
<p>But that was not the MVP.</p>
<p>The MVP was narrower and, honestly, stronger because of it: one restaurant, one kitchen, one procurement plan, one agent, one supplier integration, explicit approvals, idempotent writes, reread validation, and a trace that shows what happened without leaking sensitive values.</p>
<p>If there is one thing I would take from the hackathon, it is this:</p>
<blockquote>
<p>Real agentic software is not proven by a model calling a tool. It is proven by the system around that tool: deterministic logic, explainable state, preview, approval, idempotency, reread, durable storage, and a clear boundary between application and supplier.</p>
</blockquote>
<p>That is where the work became interesting. Not when the cart changed. When the cart became only the last step of a procurement workflow the system could explain.</p>
]]></content:encoded>
      <pubDate>Tue, 15 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>Hackathon</category>
      <category>MCP</category>
      <category>Case Study</category>
    </item>
    <item>
      <title>Micro-Frontends Are an Org Chart Decision, Not a Technical One</title>
      <link>https://ma-x.im/blog/react-playbook-micro-frontends-org-chart</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-micro-frontends-org-chart</guid>
      <description>Every micro-frontends pitch I&apos;ve seen leads with the technical benefits — independent deploys, framework isolation, smaller bundles per team. Almost none of it holds up unless the real problem was organizational to begin with. Conway&apos;s Law isn&apos;t a fun fact here. It&apos;s the actual justification.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-micro-frontends-org-chart/cover.png" alt="Micro-Frontends Are an Org Chart Decision, Not a Technical One" /></p>
<p>I&#39;ve sat through more than one micro-frontends pitch, and they all open the same way: a slide about independent deployments, a slide about teams not blocking each other, maybe a slide about being able to use a different framework per app &quot;if we ever need to.&quot; It&#39;s a technical pitch, delivered with technical slides, aimed at a technical audience.</p>
<p>Here&#39;s the thing I think gets buried under all of that: almost none of it is actually a technical problem. It&#39;s an organizational one wearing a technical costume, and the pitch works better when it&#39;s honest about that from the start.</p>
<blockquote>
<p>You don&#39;t split a frontend into micro-frontends because the code is too big. You split it because the <em>teams</em> touching that code can no longer coordinate at the speed the org needs.</p>
</blockquote>
<p>That&#39;s not a knock on micro-frontends. It&#39;s a claim about what actually justifies them — and it means the right first question isn&#39;t &quot;is our bundle too big&quot; or &quot;do we need framework flexibility.&quot; It&#39;s: does our org chart already have the shape that micro-frontends are supposed to reflect?</p>
<h2>What the technical benefits actually cost</h2>
<p>Every micro-frontend benefit on the slide has a real, un-glamorous cost that doesn&#39;t make it into the pitch.</p>
<p><strong>Independent deploys</strong> sound great until you notice that a single-repo monolith with a decent CI pipeline can <em>also</em> deploy independently deployable features behind flags, without ever splitting the app. What micro-frontends actually buy you over that is independent deploys <em>without needing anyone else&#39;s review or coordination</em> — which is a genuine win, but only if the reason you needed that isolation was that coordinating with the other team was the actual bottleneck, not the deploy pipeline itself.</p>
<p><strong>Framework isolation</strong> — &quot;team B can use Vue while team A uses React&quot; — sounds like flexibility and is usually a cost nobody priced in. You now ship two frameworks&#39; runtimes to every user, you&#39;ve doubled your hiring and knowledge-sharing surface, and the actual reason teams reach for this is almost never &quot;Vue is better for this specific problem.&quot; It&#39;s normally &quot;team B inherited a Vue app during an acquisition and nobody wants to rewrite it.&quot; That&#39;s a real reason. It&#39;s an organizational one, not an argument that framework diversity is good architecture.</p>
<p><strong>Smaller bundles per team</strong> is the one that sounds most like a real technical win, and it&#39;s the one I&#39;d push back on hardest. Splitting an app into micro-frontends doesn&#39;t shrink the total code the user downloads — it usually grows it, because now you&#39;re shipping duplicated dependencies (React itself, your design system, your date library) once per micro-frontend unless you invest heavily in shared-dependency tooling, which is its own significant ongoing cost. If bundle size is your actual problem, code-splitting inside a single app solves it directly, cheaper, and without any of what follows in this article.</p>
<h2>The cost nobody puts on the slide</h2>
<p>Once you&#39;ve actually split the frontend, a list of problems shows up that no diagram warned you about.</p>
<p>Cross-app state gets hard in a way that&#39;s easy to underestimate. A shopping cart, a logged-in user, a feature flag — anything that needs to be consistent across micro-frontends now needs a real synchronization mechanism, and &quot;we&#39;ll just use custom events&quot; is the sentence every team says right before building an ad hoc, under-tested message bus by accident.</p>
<p>Design system drift becomes a constant, low-grade tax. Each micro-frontend can update its dependencies, including your shared UI kit, on its own schedule — which is the whole point — and that means you will, eventually, ship a page where the button in the header and the button in the body are visually two versions apart, because two teams updated on two different weeks.</p>
<p>And orchestration — routing between micro-frontends, sharing auth state, handling the moment one micro-frontend fails to load while the rest of the page is fine — is genuinely hard engineering that a single-app architecture simply doesn&#39;t need to solve, because there&#39;s only one app.</p>
<p>None of these costs are disqualifying. They&#39;re the price of the thing micro-frontends are actually good at: letting teams ship independently without waiting on each other. The mistake is paying that price for a benefit you didn&#39;t need.</p>
<h2>The question that actually decides it</h2>
<p>So here&#39;s the test I&#39;d apply before anyone reaches for micro-frontends: <strong>can two teams currently ship features to production without one blocking the other, in the architecture you already have?</strong></p>
<p>If the honest answer is yes — one repo, decent ownership boundaries, feature flags, a CI pipeline that doesn&#39;t make Team A wait on Team B&#39;s review — then you don&#39;t have the problem micro-frontends solve. You have a problem that looks similar (big app, multiple teams) but isn&#39;t actually causing the pain micro-frontends are priced to fix, and adopting them will hand you the costs above in exchange for nothing.</p>
<p>If the honest answer is no — teams are routinely blocked on each other&#39;s release windows, a shared repo&#39;s CI queue has become a real bottleneck, or (the case I&#39;d take most seriously) you&#39;ve inherited genuinely separate applications from an acquisition or a long history of independent teams and merging them isn&#39;t realistic — that&#39;s the org chart telling you the org is already split. Micro-frontends, in that case, aren&#39;t creating the split. They&#39;re the architecture that finally matches a split that already exists in how the company works.</p>
<p>That&#39;s Conway&#39;s Law, and I don&#39;t think it&#39;s a fun aside here — it&#39;s the actual decision criterion. Your system&#39;s structure is going to mirror your communication structure whether you plan for it or not. Micro-frontends are what you get when you deliberately let that happen instead of fighting it with an org chart your codebase doesn&#39;t reflect.</p>
<h2>What I&#39;d actually check first</h2>
<p>Before signing off on a micro-frontends architecture, I&#39;d want to see the org chart, not the bundle analyzer. How many teams actually own frontend code that needs to ship independently? Is the current blocker genuinely architectural, or is it a CI pipeline that could be fixed for a fraction of the cost of a rewrite? And if the teams merged into one tomorrow, would the case for micro-frontends survive that?</p>
<p>If it wouldn&#39;t — if the whole justification evaporates the moment you imagine one team owning all of it — then you weren&#39;t looking at an architecture problem. You were looking at a coordination problem, and there&#39;s usually a cheaper fix for that than teaching your frontend to run as five separate applications that now have to talk to each other.</p>
]]></content:encoded>
      <pubDate>Mon, 14 Sep 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>Micro-Frontends</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Layered vs Feature-Sliced: Two Ways to Cut the Same App</title>
      <link>https://ma-x.im/blog/react-playbook-layered-vs-feature-sliced</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-layered-vs-feature-sliced</guid>
      <description>Every frontend architecture is really just a decision about which axis you slice the app along — by technical kind, or by feature. Both are correct. Both eventually hurt. The question worth asking isn&apos;t which one is better, it&apos;s which one is cheaper to be wrong about later.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-layered-vs-feature-sliced/cover.png" alt="Layered vs Feature-Sliced: Two Ways to Cut the Same App" /></p>
<p>Take any non-trivial React app and you can describe its whole folder structure as the answer to one question: when you cut this codebase into pieces, which axis do you cut along? There are really only two honest answers, and almost every architecture discussion I&#39;ve ever sat in is secretly an argument about which one a team picked without realizing they&#39;d picked one.</p>
<p><strong>Layered</strong> cuts by technical kind. All the components live together, all the hooks live together, all the API calls live together, regardless of which feature they belong to.</p>
<pre><code>src/
  components/
  hooks/
  api/
  utils/
  pages/
</code></pre>
<p><strong>Feature-sliced</strong> cuts by product concern. Everything a feature needs — its components, its hooks, its API calls — lives together, and it&#39;s the <em>kind</em> of file that becomes secondary.</p>
<pre><code>src/
  features/
    payment-method/
    order-history/
    account-settings/
</code></pre>
<p>Neither of these is a mistake. They&#39;re both legitimate answers to &quot;how do I organize this,&quot; and the reason this keeps being a live debate is that each one is <em>correct</em> for a while and then becomes <em>expensive</em> in a completely different, entirely predictable way.</p>
<blockquote>
<p>Every architecture is a bet about which kind of future change you&#39;re optimizing for. Layered bets on stability of kind. Feature-sliced bets on stability of feature boundaries. Neither bet is safe forever.</p>
</blockquote>
<h2>What layered actually gives you</h2>
<p>Layered architecture is the one nobody has to be taught. A junior developer can guess where a new hook goes — <code>hooks/</code> — without reading a single doc. That&#39;s not a small thing. It&#39;s the lowest-friction structure there is for the first several months of a project, precisely because it doesn&#39;t ask anyone to make a judgment call about feature boundaries before those boundaries are even clear yet.</p>
<p>It&#39;s also genuinely good for a certain kind of change: anything that&#39;s cross-cutting by nature. If you&#39;re auditing every place the app calls the API layer, or refactoring every hook to a new data-fetching pattern, layered structure puts all of that in one folder, and you can see the whole blast radius by opening one directory.</p>
<p>Where it breaks is exactly where products stop being simple. Once a feature — say, an order-management flow — starts touching a dozen components, several hooks, a couple of API modules, and its own utils, &quot;layered&quot; stops meaning &quot;organized&quot; and starts meaning &quot;this feature&#39;s logic is now distributed across five folders that also contain twenty other features&#39; logic.&quot; Want to delete the order-management feature? You&#39;re now grepping across the whole codebase, hoping you find every file that only exists because of it. Layered structure doesn&#39;t track feature boundaries, so nothing in the folder tree tells you where a feature ends.</p>
<h2>What feature-sliced actually gives you</h2>
<p>Feature-sliced structure solves exactly that problem. A feature&#39;s folder is a feature&#39;s blast radius. Deleting <code>features/order-history/</code> deletes the feature — not perfectly, there&#39;s usually some shared code left behind, but close. Onboarding someone onto <em>one</em> feature means pointing at <em>one</em> folder, not explaining the layer system and hoping they can mentally reconstruct which fragments across five layers belong to the thing they&#39;re working on.</p>
<p>It scales with team size in a way layered structure doesn&#39;t. Multiple teams, each owning a handful of features, can work with almost no folder-level collision, because they&#39;re rarely in each other&#39;s slices. That&#39;s the real argument for feature-sliced at scale — not &quot;it&#39;s cleaner,&quot; but &quot;it lets N teams work without stepping on each other&#39;s files.&quot;</p>
<p>The cost shows up the moment two features need the same thing. Every feature-sliced codebase eventually accumulates a <code>shared/</code> folder, and that folder has exactly the failure mode you&#39;d expect: things get promoted to it reactively, usually the first time a second feature needs something, rarely with much thought about whether it&#39;s actually a stable, general-purpose piece or just two features that happen to look similar today. Left unmanaged, <code>shared/</code> becomes the layered architecture&#39;s <code>utils/</code> folder wearing a nicer name — a dumping ground, just one level removed.</p>
<p>And there&#39;s a subtler cost: feature-sliced structure asks you to correctly guess feature boundaries <em>before</em> the product has told you what they are. Early in a project, &quot;is this one feature or two&quot; is often genuinely unknowable, and picking wrong means a slice that either merges awkwardly with another later or splits painfully in two.</p>
<h2>The actual decision</h2>
<p>I don&#39;t think there&#39;s a universal right answer here, and I&#39;m suspicious of anyone who gives you one without asking what you&#39;re building. The question that actually matters is: <strong>what changes more often in this product — the technical kind of thing, or the feature it belongs to?</strong></p>
<p>If you&#39;re building something where features are genuinely stable and rarely added (an internal admin tool with five screens that haven&#39;t changed in two years), layered structure&#39;s simplicity is worth more than feature-sliced&#39;s isolation, because you&#39;re not paying feature-sliced&#39;s up-front cost of guessing boundaries for a product that isn&#39;t going to grow new ones.</p>
<p>If you&#39;re building something where the <em>number</em> of features is the thing that grows — a product adding new user-facing capabilities every quarter, with multiple teams shipping in parallel — feature-sliced earns its cost almost immediately, because the layered alternative means every new feature adds friction to five existing folders instead of creating one clean new one.</p>
<p>Most real products start in the first category and migrate into the second without anyone deciding to. That&#39;s the actual argument for feature-sliced conventions like FSD: not that they&#39;re objectively better, but that <a href="https://ma-x.im/blog/react-playbook-what-fsd-actually-fixed">the underlying problem they solve</a> — coordination at scale — is the one almost every successful product eventually has, even if it didn&#39;t start with it.</p>
<h2>A hybrid is not cheating</h2>
<p>The reasonable middle ground, and the one I actually reach for on real projects, is layered at the top for genuinely global concerns — routing, design tokens, a handful of app-wide providers — and feature-sliced underneath for everything that&#39;s actually product surface area.</p>
<pre><code>src/
  app/                 // routing, providers, global config — layered, rarely touched
  shared/              // genuinely cross-feature primitives, promoted deliberately
  features/
    payment-method/    // feature-sliced from here down
    order-history/
    account-settings/
</code></pre>
<p>The discipline that makes this work isn&#39;t the folder names. It&#39;s being honest about which things are actually global (rare, and worth naming explicitly) versus which things just look similar between two features today and might diverge next quarter. Promote to <code>shared/</code> when a third feature needs something, not when a second one does — that one rule prevents most of the premature abstraction that turns <code>shared/</code> into a second <code>utils/</code> folder.</p>
<p>Pick the axis that matches what actually changes in your product, be honest about which folder is doing that job right now, and don&#39;t confuse &quot;we chose a structure&quot; with &quot;we solved organization.&quot; You didn&#39;t solve it. You picked which future problem you&#39;d rather have.</p>
]]></content:encoded>
      <pubDate>Sat, 12 Sep 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>FSD</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Nivra: Turning a Hackathon Idea into a WebMCP Architecture Workspace</title>
      <link>https://ma-x.im/blog/nivra-webmcp-challenge</link>
      <guid isPermaLink="true">https://ma-x.im/blog/nivra-webmcp-challenge</guid>
      <description>How Nivra went from a vague hackathon direction to a tested WebMCP workspace: choosing the right idea, making architecture the shared context, and proving that human UI, agent tools, and deterministic validation could work from the same model.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/nivra-webmcp-challenge/cover.png" alt="Nivra: Turning a Hackathon Idea into a WebMCP Architecture Workspace" /></p>
<p>Hackathons usually start with a dangerous question: what can I build quickly?</p>
<p>I don&#39;t like that question very much. It sounds practical, but it pushes you toward the wrong kind of demo: a screen, a button, a little AI, and the feeling that something product-shaped exists. For a normal hackathon, maybe that is enough. For the OpenAI WebMCP Challenge, it wasn&#39;t.</p>
<p>The better question was narrower:</p>
<p>What idea actually shows why WebMCP should exist?</p>
<p>Not &quot;how do I add AI to an app.&quot; Not &quot;how do I put a chat panel next to a UI.&quot; I mean: where does an agent need to read the state of the application, move through the same workspace as the human, act on the same model, and help expose something that would otherwise stay hidden between layers of the system?</p>
<p>That question became Nivra.</p>
<p>Links for the project:</p>
<ul>
<li>Live app: <a href="https://nivra-psi.vercel.app">nivra-psi.vercel.app</a></li>
<li>GitHub repository: <a href="https://github.com/MaximusFT/nivra">github.com/MaximusFT/nivra</a></li>
<li>Demo video: <a href="https://youtu.be/BSz_Aqm4Ius">YouTube</a></li>
</ul>
<h2>The first ideas were not architecture</h2>
<p>At the beginning, I did not have the project. I had several possible directions, and we rejected most of them for reasons that were more useful than the ideas themselves.</p>
<p>One strong candidate was Senwia Sleep Lab, an evolution of an app idea I had around sleep analysis. I still like that direction. Sleep is understandable. The data can be visual. An AI agent could explore patterns with the user, highlight anomalies, ask questions, and help connect habits with outcomes.</p>
<p>But for this challenge, it had a weak point. To make the demo convincing, I would have needed a synthetic data layer: fake sleep history, fake events, fake metrics. WebMCP would have interacted with data I had invented for the demo. The product might have been interesting, but WebMCP would not have been the reason it worked.</p>
<p>There were other options too. Interview Forge, an AI tool for technical interviews. AI Newsroom, a system for processing and organizing news. And then an architecture workspace: a place where a human architect and an AI agent could work together over a structured model of a software system.</p>
<p>That last idea kept surviving the cuts.</p>
<p>Not because architecture sounds more serious. Because WebMCP was not decorative there. If an agent is working with architecture, it needs to inspect application state, move between views, find dependencies, show evidence, add findings, create proposals, and work inside the same workspace the architect is looking at.</p>
<p>That is a different product shape. WebMCP stops being a chat next to a diagram. It becomes the agent&#39;s interface into the architecture model.</p>
<h2>ArchPilot was the wrong name, usefully</h2>
<p>The first working name was ArchPilot.</p>
<p>It sounded fine at first. Short, obvious, close enough to architecture and agent-assisted work. Then we checked the market and found existing products with similar names, including products in software architecture and architecture governance.</p>
<p>So the name had to go.</p>
<p>That could have been just a naming problem. It turned into a product decision.</p>
<p>When a name stops working, you have to ask what you were actually trying to name. And that forced a clarification: Nivra should not be &quot;AI designs architecture for you.&quot; It should not be an architecture validator that stands above the system and declares what is correct. It should not be a governance platform. And it definitely should not be a chat box glued to the side of a diagram.</p>
<p>I needed a different sentence:</p>
<blockquote>
<p>Architecture is the shared context.</p>
</blockquote>
<p>That became the center of Nivra.</p>
<p>The human and the AI agent work with the same structured architecture model, but through different interfaces. The human sees a visual workspace. The agent gets access to the same model through WebMCP tools. One source of truth. Two ways to reason over it.</p>
<h2>The demo needed a real architectural tension</h2>
<p>Once the product shape was clear, the next problem was the scenario.</p>
<p>I did not want a toy example where service A talks to service B and the whole point is that the arrow is bad. That kind of demo teaches the user to agree with the author, not to inspect the system.</p>
<p>The scenario became Commerce Platform.</p>
<p>At the High-Level Design level, everything looks almost right. Product, Cart, Checkout, and Account are separated into microfrontends. Checkout looks independent. You can draw a box around it and say, quite reasonably, &quot;this is a separate part of the system.&quot;</p>
<p>Then the business requirement lands: the Checkout Team needs to evolve and deploy Checkout independently from Product.</p>
<p>The natural reaction is understandable:</p>
<blockquote>
<p>Checkout is already a separate microfrontend. Why wouldn&#39;t it be independently deployable?</p>
</blockquote>
<p>That is exactly where Nivra needed to become useful.</p>
<p>At HLD, Checkout looks ready. But when the agent moves deeper into Checkout LLD, two dependencies appear, and they are not the same kind of problem.</p>
<p>The first is <code>Basket Adapter -&gt; Product Store</code> through shared runtime state. That is runtime coupling. Checkout may be visually separate, but it still depends on Product state at runtime.</p>
<p>The second is <code>Pricing Module -&gt; Product Service</code> through an explicit REST API. That is also a dependency, but it is visible, named, and easier to reason about. An architect may decide it is acceptable for now.</p>
<p>That distinction became the core of the demo:</p>
<blockquote>
<p>A separate box on a diagram doesn&#39;t mean an independent system.</p>
</blockquote>
<p>I did not want Nivra to say &quot;all dependencies are bad.&quot; That is not architecture. The useful moment is when the human sees the evidence, understands the difference between shared runtime state and an explicit contract, and makes a trade-off.</p>
<p>The agent does not decide for the architect. It helps expose the layer where the decision actually lives.</p>
<h2>The canvas could not be the source of truth</h2>
<p>After the scenario clicked, we designed the product architecture around it.</p>
<p>The most important decision was boring on purpose: do not start with the diagram.</p>
<p>If the first thing you build is a React Flow canvas, it is very easy to confuse the graph with the architecture. The nodes on the screen start to feel like the system. Coordinates acquire meaning. Views drift into their own copies of elements. Then validation, WebMCP, and UI each interpret the &quot;architecture&quot; slightly differently.</p>
<p>So the first real artifact was a plain TypeScript Architecture Model. Commerce Platform v1.35 became a deterministic fixture model, not a decorative picture. HLD and Checkout LLD referenced the same entities and relations. Views stored IDs, not duplicated elements. Layout lived separately, because canvas coordinates are presentation metadata, not architectural meaning.</p>
<p>That decision paid for itself later. UI, validation, and WebMCP could all work against the same model independently. If the canvas changed, the architecture still existed. If the visual language changed, the domain model did not move with the pixels.</p>
<h2>The plan came before the code</h2>
<p>After the product idea settled, we still did not jump straight into implementation.</p>
<p>First came the Product Definition and MVP scope. Then Architecture Levels and the Domain Model. Then the WebMCP Tool Contract, UI/UX, technical implementation notes, demo fixture, golden scenario, seven-day implementation plan, and visual design specification.</p>
<p>All of that became Nivra Master Specification v1.1.</p>
<p>At that moment, Nivra still was not an application. But it was no longer just an idea. It had a scenario, a domain model, a WebMCP contract, a UI direction, a demo fixture, and a development plan.</p>
<p>The question changed.</p>
<p>It was no longer: what should we build?</p>
<p>It became: can we make this model actually work?</p>
<h2>Foundation first, WebMCP later</h2>
<p>Once the master specification existed, the temptation was obvious: start with tools. This was the WebMCP Challenge, after all. Show WebMCP.</p>
<p>But that would have been the wrong order.</p>
<p>We started with React, TypeScript, and Vite, but the real output of the first phase was not a screen. It was the Architecture Model and the operations around it.</p>
<p>Only then came the canvas, adapters from the Architecture Model into React Flow, and navigation from Commerce HLD into Checkout LLD.</p>
<p>That became Checkpoint A: does the product create a &quot;now I see it&quot; moment without the agent telling the user what to think?</p>
<p>At HLD, Checkout looked separate. In LLD, the shared runtime state dependency and the explicit REST call appeared together. The relations had different visual and semantic treatments. The product idea finally stood on the workspace itself, not on narration around the demo.</p>
<p>If a demo only works while I am explaining it out loud, the product does not work yet.</p>
<h2>The first serious bug was not where I expected</h2>
<p>The early version compiled. Then a browser smoke test immediately found a React maximum update depth problem, effectively an infinite render.</p>
<p>The cause was not the Architecture Model. It was an object-returning Zustand selector. The selector created a new object, React treated it as a new value, the component updated, the selector created another new object, and the loop continued.</p>
<p>The fix was ordinary: stable comparison with <code>useShallow</code>.</p>
<p>The lesson was larger than the fix. Even a foundation screen cannot be considered successful just because TypeScript is satisfied. The browser has its own failure modes: selectors, subscriptions, effects, render timing. If an architecture workspace is supposed to be trustworthy, it needs to be exercised in a live UI early.</p>
<p>Around the same time, another risk became obvious: the canonical fixture could not be mutated directly. If future write actions could silently change Current Architecture, the whole proposal story would lose credibility. Changes had to be immutable. Current Architecture had to remain the reference point.</p>
<p>That sounds like an implementation detail, but it is really a product detail. The user has to know what they are looking at: the current system, or a proposed alternative.</p>
<h2>The reasoning loop had to work manually first</h2>
<p>Before registering any WebMCP tools, we made the manual scenario work end to end.</p>
<p>The architect selects evidence, opens the Context Panel, sees relations and deployment information, creates a Finding, saves architectural constraints, runs deterministic validation, creates a Proposal, compares Current and Proposal, and validates the alternative.</p>
<p>That was Checkpoint B: the full manual Observe -&gt; Question -&gt; Inspect -&gt; Understand -&gt; Decide -&gt; Propose -&gt; Verify loop.</p>
<p>This is where the product became clearer to me: validation was not the main product.</p>
<p>Validation matters. Without it, the whole thing becomes a nice story. But Nivra is not centered on pass/fail. It is centered on shared understanding. The agent can help inspect, explain, drill into LLD, and propose changes. But once a rule is formalized, boring deterministic code should check it.</p>
<p>Agent-assisted reasoning. Deterministic verification.</p>
<h2>Constraints needed evidence</h2>
<p>We added four rule types: <code>forbidden-dependency</code>, <code>independent-deployment</code>, <code>no-cycles</code>, and <code>allowed-protocol</code>.</p>
<p>Validation was pure, typed, and evidence-bearing. Each check returned not only pass/fail, but references to the elements or relations that explained the result.</p>
<p>Golden Current Architecture returned 2 passed / 2 failed.</p>
<p>Honestly, that was better than making the demo pass perfectly from the beginning. If Current passes every rule, validation proves very little. It might just be confirming a happy path written for the demo. The 2/2 result showed that the rule was actually finding the hidden violation.</p>
<p>Some details mattered. A forbidden dependency has to account for descendants, not only exact IDs. LLD elements inherit deployment unit through the parent hierarchy. Protocol comparison is case-insensitive, even if the display model says REST. Validation does not create Findings, and it does not depend on React, Zustand, or the canvas.</p>
<p>An architecture rule should validate architecture, not whatever the UI happens to be rendering.</p>
<h2>Proposal, not mutation</h2>
<p>The next problem was trust.</p>
<p>If an agent can &quot;fix&quot; the architecture directly, the human loses the ability to understand the original state. What was Current? What did the agent propose? What has been accepted? What is still just an alternative?</p>
<p>So Current Architecture stayed immutable. A Proposal became a patch-based alternative with base version checking.</p>
<p>In the golden proposal, the runtime state dependency is removed and replaced with a Checkout Snapshot Contract. Not as a magical fix, but as a checkable architectural alternative.</p>
<p>The visual diff needed thought too. The added Snapshot Contract and the removed Product Store/runtime relation had to be visible at the same time. The effective Proposal view had to be computed from the patch, not stored as a separate diagram, because otherwise we would be back to two competing models instead of one source of truth.</p>
<p>The result:</p>
<p>Current: 2 passed / 2 failed.</p>
<p>Proposal: 4 passed / 0 failed.</p>
<p>That felt like architecture work. The human can see the trade-off, inspect the evidence, and evaluate a verified alternative before accepting anything.</p>
<blockquote>
<p>The agent doesn&#39;t draw architecture for you. It reasons inside your architecture with you.</p>
</blockquote>
<h2>WebMCP as an adapter boundary</h2>
<p>Only after manual Checkpoint B did we start the WebMCP integration.</p>
<p>That order mattered. WebMCP tools did not get their own architecture model. They did not mutate React Flow directly. They became a browser adapter over existing workspace operations. The human interface and the agent interface called the same logic.</p>
<p>The first group of tools covered reading and navigation: <code>get_architecture</code>, <code>inspect_element</code>, and <code>show_architecture_view</code>.</p>
<p>Then came write and compute tools: <code>annotate_architecture</code>, <code>add_constraint</code>, <code>create_proposal</code>, and <code>validate_architecture</code>.</p>
<p>Seven P0 tools in total.</p>
<p>On paper this sounds simple: write a JSON Schema, register a handler, done. In practice, the boundary was more interesting. The runtime can call a handler more than once. It can pass malformed data. It can send an ID that looks plausible but breaks the model. So the write tools also validate stable kebab-case IDs, enums, evidence references, duplicate and conflicting IDs, parent references, relation endpoints, proposal base version, and nested update values.</p>
<p>Every call creates an activity entry with running, success, or error status. The human can see what the agent actually did in the workspace. But activity is not persisted as architecture state.</p>
<p>That separation matters. Agent activity explains the process. It is not the architecture.</p>
<h2>Experimental API means experimental</h2>
<p>WebMCP was an experimental browser API available through a specific Chromium mode. A normal browser might not have <code>document.modelContext</code>. That could not be treated as the whole app failing.</p>
<p>So Nivra got an explicit WebMCP ready / WebMCP unavailable status, a guarded legacy fallback for earlier preview runtimes, AbortSignal-based registration, a complete manual fallback without WebMCP, and a guided demo simulation for cases where an external agent is not available.</p>
<p>The honesty of that simulation mattered.</p>
<p>It is labeled Demo simulation. It uses the same workspace actions and shows the same operations, but it does not pretend to be real AI. Challenge V1 is a client-side application. Not a production AI backend. Not real-time collaboration. Not a governance platform.</p>
<p>It is a verifiable workspace where WebMCP has a clear role when the runtime is available, and the product still works when the runtime is not.</p>
<h2>Unit tests do not prove a browser tool works</h2>
<p>Unit tests protected domain queries, adapters, validation, persistence, store actions, and tool input validation.</p>
<p>That was necessary. It was not sufficient.</p>
<p>A handler test does not prove that the browser registered a WebMCP tool. It does not prove that transport reached a workspace action. It does not prove that the human saw the result in the UI.</p>
<p>So we built a black-box harness: <code>npm run test:webmcp</code>.</p>
<p>It starts an isolated Vite server, launches a temporary Chromium profile with WebMCP enabled, discovers all seven tools, and calls them through browser <code>ModelContext.executeTool()</code>. Every run starts from Reset Demo. The harness intentionally repeats Finding, Constraint, and Proposal writes, checks DOM focus and Agent Activity, validates Current and Proposal, returns to Current, and confirms that the original <code>basket-adapter-shares-product-store</code> relation is still there.</p>
<p>Three full repetitions returned the same result.</p>
<p>That was Checkpoint C: Current 2/2 -&gt; Proposal 4/0. Current stayed unchanged. Retries were idempotent. Activity was visible. No errors.</p>
<p>That finally felt like evidence. Not &quot;it clicked locally.&quot; Not &quot;the handler returns JSON.&quot; The full chain worked: WebMCP registration -&gt; browser transport -&gt; workspace action -&gt; visible React state.</p>
<h2>Polish was not about making it pretty</h2>
<p>After functional freeze, we did not expand scope. We asked a different question: can someone understand the story without me narrating it?</p>
<p>The answer was: not quite yet.</p>
<p>HLD/LLD navigation could not look like unrelated tabs. Selecting an element had to open its own context, not accidentally show Checkout policy. Proposal action should not appear before Current validation exposes a problem. Added and removed evidence needed to be visible together. Agent Activity had to be readable without taking over the workspace. The standalone demo could not pretend it had a live agent connection.</p>
<p>So we moved toward contextual drill-down and breadcrumb navigation. We added a neutral Policy state without selection, scoped Checkout policy, timeline Agent Activity, guided demo, and a dismissible history drawer.</p>
<p>After successful Proposal validation, the workspace also got an implementation brief and the ability to save a Proposal as an architecture branch.</p>
<p>Important detail: architecture branch is not a Git branch. It is a durable verified alternative inside Nivra. <code>current/commerce-1.35</code> and <code>proposal/checkout-isolation</code> can be compared without overwriting Current.</p>
<p>The visual verification was practical: desktop layouts at 1440x900 and 1920x1080, no horizontal overflow, readable Checkout LLD evidence and Agent Activity, clean browser console.</p>
<h2>Production does not prove WebMCP</h2>
<p>After local stabilization, the app went live on Vercel: <a href="https://nivra-psi.vercel.app">nivra-psi.vercel.app</a>.</p>
<p>Deployment then became Git-driven: pushing to <code>main</code> runs the production build through the connected GitHub repository. But deployment itself proves almost nothing about WebMCP.</p>
<p>The app can load over HTTPS, the UI can look fine, and browser tool registration can still be broken. So the same black-box harness was run against the public HTTPS URL through <code>NIVRA_WEBMCP_URL</code>.</p>
<p>Three production repetitions confirmed the same behavior: all seven tools were registered, Checkout LLD evidence opened and focused, Current returned 2 passed / 2 failed, Proposal returned 4 passed / 0 failed, retries stayed idempotent, Current was not polluted by proposal-only state, and Agent Activity stayed visible without error state.</p>
<p>By the end, there were four layers of verification: TypeScript typecheck, unit tests, and production build; manual browser checkpoints for HLD/LLD and the reasoning loop; the WebMCP-enabled Chromium black-box harness; and the same checks against production HTTPS.</p>
<p>Only after that did I consider Nivra ready for demo and submission.</p>
<h2>What Nivra became</h2>
<p>Nivra started with a question: what idea is worth building for the WebMCP Challenge?</p>
<p>By the end, the question had changed. Nivra had become a concrete system with a source of truth, a reasoning loop, browser tools, deterministic validation, and a reproducible demo state.</p>
<p>It did not become an automatic architect.</p>
<p>Good.</p>
<p>It does not draw architecture instead of the human. It gives the human and the agent one shared context where they can expose a hidden dependency, define a rule, propose an alternative, and verify it without silently replacing Current Architecture.</p>
<p>At that point, the project stopped being just a concept. The human could see the architecture. The agent could work with the same model. Deterministic checks prevented a pretty proposal from quietly becoming the current system.</p>
<p>You can try the live app, read the source, or watch the demo here:</p>
<ul>
<li><a href="https://nivra-psi.vercel.app">Live app</a></li>
<li><a href="https://github.com/MaximusFT/nivra">GitHub repository</a></li>
<li><a href="https://youtu.be/BSz_Aqm4Ius">YouTube demo</a></li>
</ul>
<p>The next step was no longer deciding what to build. It was showing whether this combination — human, agent, WebMCP, and a shared Architecture Model — could hold up live, in front of someone who had not watched the whole thing being built.</p>
]]></content:encoded>
      <pubDate>Fri, 11 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>Hackathon</category>
      <category>WebMCP</category>
      <category>Case Study</category>
    </item>
    <item>
      <title>What FSD Actually Fixed (and What It Didn&apos;t)</title>
      <link>https://ma-x.im/blog/react-playbook-what-fsd-actually-fixed</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-what-fsd-actually-fixed</guid>
      <description>Feature-Sliced Design got popular because frontend spent years being treated as the part of the codebase that didn&apos;t need real architecture. It fixed that perception. It did not fix cohesion, and pretending it did is how you end up with a button smeared across ten folders.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-what-fsd-actually-fixed/cover.png" alt="What FSD Actually Fixed (and What It Didn&apos;t)" /></p>
<p>For a long time, &quot;frontend architecture&quot; was almost a contradiction in terms. Backend had layers, domains, bounded contexts, decades of literature. Frontend had <code>components/</code>, <code>utils/</code>, and a folder called <code>helpers</code> that everyone was quietly afraid to open. The general vibe, even among good engineers, was that the frontend didn&#39;t need real architecture — it was just where you rendered the thing the backend actually built.</p>
<p>Feature-Sliced Design showed up into that vacuum, and I think that context matters more than the methodology&#39;s specific rules. FSD didn&#39;t just propose a folder convention. It made an argument: the frontend is complex enough, and grows unpredictably enough, that it deserves the same seriousness backend systems get. That argument is the actual contribution, and it&#39;s the reason FSD spread as fast as it did — not the specific layer names.</p>
<blockquote>
<p>FSD&#39;s real achievement wasn&#39;t a folder structure. It was convincing an entire ecosystem that the frontend was worth architecting at all.</p>
</blockquote>
<p>I&#39;ve <a href="https://ma-x.im/blog/smeared-component">written before</a> about how following FSD&#39;s rules literally, without thinking, produced one of the worse codebases I&#39;ve built — a single button smeared across ten folders, each one &quot;correctly&quot; following the layer it was assigned to. That article was about the failure mode. This one is about something narrower: which problem FSD was actually solving, and where the credit for &quot;clean architecture&quot; quietly gets misattributed to a folder convention that never promised it.</p>
<h2>What FSD actually fixed</h2>
<p>Three things, and they&#39;re real.</p>
<p><strong>It gave teams a shared vocabulary for growth.</strong> Before FSD, &quot;where does this go&quot; was answered differently by every senior engineer on the team, usually based on whatever they&#39;d seen at their last job. FSD gives you <code>pages</code>, <code>features</code>, <code>entities</code>, <code>shared</code> — not perfect names, but names everyone on a team can agree mean the same thing. That alone kills a huge amount of bikeshedding.</p>
<p><strong>It made import direction a rule instead of a convention.</strong> The layer hierarchy — shared can&#39;t import from entities, entities can&#39;t import from features, and so on — is enforceable with a lint rule. That&#39;s the part I actually think is underrated. Most frontend codebases don&#39;t have architectural violations because someone made a bad call; they have them because nothing stopped the bad call from compiling. FSD turned &quot;please don&#39;t import upward&quot; from a code review comment into a build failure.</p>
<p><strong>It gave &quot;the frontend has architecture&quot; a name people could put on a job posting.</strong> This sounds cynical, but I mean it as a genuine point: before FSD (and a handful of contemporaries), it was hard to even have the conversation about frontend architecture in a hiring or planning context, because there wasn&#39;t a shared reference point. Now there is. That&#39;s a real, if unglamorous, win.</p>
<h2>What it never promised to fix</h2>
<p>Here&#39;s where I think people — including a version of me, a few years back — get it wrong. FSD tells you <em>where</em> a piece of code should live relative to other pieces. It does not tell you <em>whether the pieces you&#39;re splitting apart should have been split apart in the first place.</em></p>
<p>That distinction matters because the smeared-component failure isn&#39;t a bug in FSD. It&#39;s what happens when you use a layering convention to answer a question it was never designed to answer: how much of a single feature&#39;s logic should live together versus be distributed. FSD&#39;s layers describe <em>horizontal</em> concerns — what kind of thing is this (a hook, a type, an API call) — and following them faithfully will happily scatter every piece of a single <em>vertical</em> concern (one feature, one component, one story) across the whole hierarchy, because each piece genuinely does belong to a different layer by FSD&#39;s own rules.</p>
<pre><code>// &quot;Correct&quot; FSD placement for one payment-method component
src/
  features/payment-method/model.ts       // the hook and state
  entities/payment-method/types.ts       // the type
  shared/hooks/useDebounce.ts            // a helper it needs
  shared/api/paymentMethods.ts           // the fetch call
  features/analytics/paymentTracking.ts  // its analytics event
</code></pre>
<p>Every one of those placements passes an FSD lint rule. None of them help the next engineer who needs to change how the payment method selector behaves, because understanding it now requires reconstructing it from five files across three layers, in a codebase that will tell you, correctly, that nothing is architecturally wrong.</p>
<p>FSD also doesn&#39;t fix cohesion at the <em>page</em> level, and it doesn&#39;t fix team discipline. A team that argues about everything else will still argue about whether a given piece of logic is a <code>feature</code> or an <code>entity</code> — FSD gives you a vocabulary for the argument, not an answer to it. And a team with no code review discipline will produce a messy FSD codebase exactly as fast as it would have produced a messy flat one; the folders will just be tidier while it happens.</p>
<h2>The credit-misattribution problem</h2>
<p>I think this is the actual thing worth naming: teams that adopt FSD often experience a real improvement in their codebase, and then attribute all of it to the layering. Some of the improvement is the layering. A meaningful chunk of it is something else entirely — the simple fact that adopting <em>any</em> deliberate structure, discussed and agreed on as a team, forces the conversations that were previously being skipped. &quot;Where does this go&quot; gets asked out loud instead of guessed at. That conversation is valuable regardless of which methodology prompted it.</p>
<p>Which means the honest version of &quot;we adopted FSD and our codebase got better&quot; is often &quot;we adopted a shared convention, discussed it as a team, and enforced it with tooling — and FSD happened to be the convention we picked.&quot; That&#39;s still a win. It&#39;s just a different, more general win than &quot;FSD solved our architecture problem,&quot; and conflating the two is how teams end up surprised when FSD alone doesn&#39;t prevent a smeared component, a bloated <code>shared/</code> folder, or a <code>pages/</code> layer that&#39;s turned into a dumping ground.</p>
<h2>What I actually reach for now</h2>
<p>I still use FSD&#39;s structure as a starting point — <a href="https://ma-x.im/blog/react-playbook-code-structure">the version I use in practice</a> keeps the layer names but treats them as a coarse sort, not a mandate to split every feature into its smallest constituent parts. The rule I actually enforce is closer to: a feature&#39;s pieces get distributed across layers only when another feature genuinely needs to share them. Until then, they stay together, even if that means a <code>features/payment-method/</code> folder that internally holds its hook, its types, and its API call in one place, technically &quot;violating&quot; the purist reading of the layer boundaries.</p>
<p>That&#39;s not a rejection of FSD. It&#39;s treating it as what it actually is: a good answer to &quot;how do we agree on where things go as this project grows,&quot; and a tool that says nothing useful about &quot;how much should stay together.&quot; Knowing which question you&#39;re asking is the part that doesn&#39;t come in the docs.</p>
<p>If your team adopted FSD and it didn&#39;t fix the thing you hoped it would, I&#39;d genuinely ask which of the two problems you actually had — because they need different fixes, and only one of them is solved by a folder structure.</p>
]]></content:encoded>
      <pubDate>Wed, 09 Sep 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>FSD</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Checkout Isn&apos;t a Form. It&apos;s the Only Part of the App Where Architecture Has a Stopwatch on It</title>
      <link>https://ma-x.im/blog/checkout-architecture-time-to-goal</link>
      <guid isPermaLink="true">https://ma-x.im/blog/checkout-architecture-time-to-goal</guid>
      <description>Most of an app can afford to be a little slow, a little clunky, and still get forgiven. Checkout can&apos;t. What you&apos;re actually selling in that flow isn&apos;t a UI — it&apos;s time-to-goal. Every architectural choice in there either protects that number or quietly taxes it.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/checkout-architecture-time-to-goal/cover.png" alt="Checkout Isn&apos;t a Form. It&apos;s the Only Part of the App Where Architecture Has a Stopwatch on It" /></p>
<p>I&#39;ve spent a real chunk of my career around purchase flows — one-click purchase, multi-step checkout, the whole family of screens between &quot;I want this&quot; and &quot;I bought this.&quot; And the thing that took me the longest to internalize wasn&#39;t a pattern or a library. It was what we&#39;re actually selling in that flow.</p>
<p>It isn&#39;t the UI. It isn&#39;t even the product, at that point — the user already decided on the product two screens ago. What we&#39;re selling is <em>time-to-goal</em>. How fast, with how little friction, does this person get from &quot;I&#39;m ready to buy&quot; to &quot;it&#39;s done&quot;? Every metric that actually matters downstream — conversion, cart abandonment, repeat purchases — is a proxy for that one number.</p>
<blockquote>
<p>The product in a checkout flow isn&#39;t the form. It&#39;s the distance between intent and confirmation.</p>
</blockquote>
<p>This sounds like a UX statement, and it is one. But I want to argue it&#39;s also, maybe primarily, an <em>architecture</em> statement — because the UX-y stuff people file under &quot;polish&quot; (responsiveness, one-click purchase, graceful recovery from a dropped connection) is downstream of decisions made in how the checkout is built, not decisions made in Figma.</p>
<h2>Every step is round-trip debt</h2>
<p>Here&#39;s the mental model I use. Every screen, every confirmation, every &quot;are you sure&quot; in a checkout flow is a withdrawal against the user&#39;s patience. Doesn&#39;t matter how pretty it is. A step is a step.</p>
<p>Multi-step checkouts get justified all the time — shipping, then payment, then review — and sometimes that&#39;s genuinely the right shape. But I&#39;d push back on treating that as free. Each transition is a network round trip if it&#39;s fetching anything, a re-render if it isn&#39;t, and either way it&#39;s a moment where the user can get pulled away, get a spinner they don&#39;t trust, or hit the back button and lose state they already entered.</p>
<p>The architectural question isn&#39;t &quot;how do we make step 2 nice.&quot; It&#39;s &quot;does step 2 need to exist as a separate step at all, or is it three fields we could&#39;ve asked for on step 1 without anyone noticing the difference.&quot; That&#39;s a product conversation, sure, but it&#39;s the frontend architect&#39;s job to keep asking it, because engineers are the ones who feel the cost of <em>not</em> asking it — in state management, in the number of places cart data can go stale, in every edge case around &quot;what if they refresh here.&quot;</p>
<h2>One-click purchase is a state-management problem wearing a UX costume</h2>
<p>One-click purchase looks, from a design brief, like &quot;remove the button presses.&quot; Architecturally, it&#39;s a much harder problem: you&#39;re compressing an entire multi-step flow&#39;s worth of validation, payment, and inventory checks into a single optimistic action, and you have to do it without ever showing the user a screen that says &quot;wait.&quot;</p>
<p>That means the hard part isn&#39;t the click. It&#39;s everything that has to already be true <em>before</em> the click for the click to be safe:</p>
<pre><code class="language-ts">interface OneClickEligibility {
  hasValidPaymentMethod: boolean;
  hasCompleteShippingAddress: boolean;
  itemInStock: boolean;
  priceUnchangedSinceLastSync: boolean;
}

function canOneClickPurchase(state: OneClickEligibility): boolean {
  return (
    state.hasValidPaymentMethod &amp;&amp;
    state.hasCompleteShippingAddress &amp;&amp;
    state.itemInStock &amp;&amp;
    state.priceUnchangedSinceLastSync
  );
}
</code></pre>
<p>Everything hinges on that check being <em>current</em> at the moment of the click, not current as of when the page loaded. If price or stock data is stale by even a few seconds, &quot;one click&quot; turns into &quot;one click, then a screen apologizing that the price changed&quot; — which is worse than a normal checkout, because you promised speed and delivered a rug-pull.</p>
<p>So the real architectural commitment behind one-click purchase isn&#39;t a button component. It&#39;s a decision to keep a small slice of critical state (price, stock, payment validity) continuously fresh in the background, so the click can be trusted the instant it happens. That&#39;s a background-sync and cache-invalidation problem, not a UI problem, and it needs to be treated as first-class — not bolted on after the button ships.</p>
<h2>Optimistic UI, but with a real rollback story</h2>
<p>Checkout is one of the few places where optimistic UI actually earns its complexity, because the alternative — making someone stare at a spinner while you confirm a card charge — is its own kind of failure. But optimistic UI without a serious rollback path is just a faster way to lie to the user.</p>
<pre><code class="language-tsx">function useSubmitOrder() {
  const [status, setStatus] = useState&lt;&#39;idle&#39; | &#39;confirming&#39; | &#39;error&#39;&gt;(&#39;idle&#39;);

  const submitOrder = async (order: OrderDraft) =&gt; {
    setStatus(&#39;confirming&#39;); // show success-leaning state immediately

    try {
      const confirmed = await placeOrder(order);
      return confirmed;
    } catch (error) {
      setStatus(&#39;error&#39;);
      // the user already saw a hopeful state — the recovery message
      // has to explain what changed, not just that something failed
      throw error;
    }
  };

  return { status, submitOrder };
}
</code></pre>
<p>The detail that actually matters here is the comment. If you show a confident &quot;confirming your order&quot; state and then it fails, the failure message can&#39;t be a generic toast. The user&#39;s mental model has already moved forward — they think they&#39;re done. Rolling that back cleanly, with a message that says what actually happened (card declined vs. item went out of stock vs. network timeout), is architecture work: it means your error states need to carry <em>why</em>, not just <em>that</em>, all the way from the API layer up.</p>
<h2>Responsiveness is a checkout feature, not a checkout nice-to-have</h2>
<p>I&#39;d draw a hard line here: on most of an app, a layout that reflows awkwardly on a weird viewport is a bug you triage next sprint. On checkout, it&#39;s a lost sale, because a meaningful chunk of purchases happen one-handed, on a phone, possibly in a moving vehicle, possibly with one bar of signal.</p>
<p>That changes the priority order of what you architect for. Payment fields need to work with autofill without fighting the browser. Buttons need to be large enough that a shaky hand doesn&#39;t mis-tap into &quot;cancel.&quot; And the layout needs to survive the keyboard eating half the screen on mobile, because &quot;the confirm button is off-screen when the keyboard is open&quot; is a shockingly common way to lose an order that was otherwise complete.</p>
<p>None of that is exotic. It&#39;s just a different priority order than the rest of the app gets, and if your component architecture treats checkout screens as &quot;just more pages,&quot; they&#39;ll get the same generic responsive treatment as everything else — which is to say, good enough for a blog post, not good enough for a payment form.</p>
<h2>The metric that should be driving these decisions</h2>
<p>Here&#39;s the part I actually want to land, because it&#39;s the thing that reframes all of the above from &quot;best practices&quot; into an actual measurement problem.</p>
<p>If the product is time-to-goal, then the thing worth instrumenting isn&#39;t page views or even conversion rate in isolation — it&#39;s the <em>distribution</em> of time between &quot;entered checkout&quot; and &quot;confirmed order,&quot; broken down by step, and the drop-off at each step. That&#39;s a genuinely different question than &quot;does this page load fast,&quot; and it points architecture in a specific direction: every step needs a timestamp, every abandonment needs a last-known-step, and every retry needs to be attributable to a cause (validation error, payment decline, network failure, user just left).</p>
<pre><code class="language-ts">function trackCheckoutStep(step: CheckoutStep, elapsedMs: number, outcome: &#39;advanced&#39; | &#39;abandoned&#39; | &#39;errored&#39;) {
  analytics.track(&#39;checkout_step&#39;, { step, elapsedMs, outcome });
}
</code></pre>
<p>That&#39;s a small function. The architectural commitment behind it — instrumenting every step consistently, from the same source of truth as the state machine driving the flow, not bolted on separately by whoever remembers to add tracking — is the actual work. Get that right and you have a real answer to &quot;where is friction actually happening,&quot; instead of a guess dressed up as a redesign.</p>
<h2>Where this leaves the architecture conversation</h2>
<p>None of this is about picking the right library for multi-step forms. It&#39;s about recognizing that checkout is the one part of the app where the business metric (completed purchase) and the architecture metric (time-to-goal, state consistency across steps, recovery from failure) are the same number wearing two names. Everywhere else in the app, you can separate &quot;is this well-architected&quot; from &quot;does this convert.&quot; In checkout, you mostly can&#39;t — a badly architected checkout <em>is</em> a checkout with worse conversion, because every extra round trip, every stale price, every optimistic update with no honest rollback is friction the user feels as delay between wanting the thing and having it.</p>
<p>If you&#39;re the one arguing for a cleaner checkout architecture and getting pushback that it&#39;s &quot;just implementation detail&quot; — it isn&#39;t. I&#39;d make the case that of everything in the app, this is the part where the architecture review and the conversion review should be the same meeting.</p>
<p>I&#39;ve got a lot more from this part of my career than fits in one article — one-click purchase edge cases, retry strategies for flaky payment providers, the exact way stale cart state causes support tickets. If there&#39;s a piece of this you want me to go deeper on, tell me which one and I&#39;ll write it.</p>
]]></content:encoded>
      <pubDate>Sun, 06 Sep 2026 00:00:00 GMT</pubDate>
      <category>E-commerce</category>
      <category>Checkout</category>
      <category>Architecture</category>
      <category>UX</category>
      <category>Case Study</category>
    </item>
    <item>
      <title>Most Advanced TypeScript Isn&apos;t Worth What It Costs</title>
      <link>https://ma-x.im/blog/react-playbook-types-that-earn-their-keep</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-types-that-earn-their-keep</guid>
      <description>There&apos;s a point where a type stops protecting you and starts being a puzzle you have to solve again every time you touch it. I&apos;m not a type wizard and I&apos;ve stopped trying to be one — because on real React codebases, the complexity that pays for itself is a much shorter list than the internet suggests.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-types-that-earn-their-keep/cover.png" alt="Most Advanced TypeScript Isn&apos;t Worth What It Costs" /></p>
<p>Let me start with an admission, because it shapes everything below: I&#39;m not especially strong at advanced TypeScript. Conditional types nested three deep, recursive template literal parsers, the kind of thing that shows up in a library&#39;s internals and gets applauded on Twitter — I read that code slowly, and I don&#39;t enjoy writing it.</p>
<p>For a while I treated that as a gap to close. Now I mostly treat it as a constraint worth designing around, and I think that&#39;s the more useful position. Not because type-level programming isn&#39;t impressive. Because on an application codebase, a type that only one person on the team can modify is a liability wearing the costume of rigor.</p>
<p><a href="https://ma-x.im/blog/react-playbook-typescript-patterns">The earlier TypeScript article</a> covered the patterns I reach for constantly — typing the API boundary, discriminated unions for async state, inference at the router. This one is about the question that comes <em>after</em> you know the patterns: how much type complexity should a given piece of code carry, and how do you tell when you&#39;ve overshot?</p>
<h2>Types have a running cost</h2>
<p>The pitch for TypeScript is that types catch bugs. True, and I&#39;d never go back. But the framing hides something: a type isn&#39;t a one-time purchase. It&#39;s a thing every future reader has to understand before they can safely change the code underneath it.</p>
<p>So each type has a cost, and it shows up in three places. Someone has to read it to understand the contract. Someone has to modify it when requirements move. And when it goes wrong, someone has to decode the error message — which, for a sufficiently clever type, is a wall of text that names none of the things you actually wrote.</p>
<p>That last one deserves more weight than it usually gets. A type that produces an unreadable error at the call site has partly defeated its own purpose. The whole point was to tell someone they made a mistake. If the message needs its own investigation, you haven&#39;t caught the bug so much as relocated it.</p>
<blockquote>
<p>The value of a type is not how much it proves. It&#39;s how much it proves <em>per unit of understanding it demands</em>.</p>
</blockquote>
<p>That ratio is the thing I actually optimize for now, and it explains why my list of &quot;worth it&quot; is short.</p>
<h2>What earns its keep</h2>
<p><strong>Discriminated unions, everywhere.</strong> This is the highest-value construct in application TypeScript and it&#39;s barely &quot;advanced&quot; at all:</p>
<pre><code class="language-ts">type PolicyRequest =
  | { status: &#39;idle&#39; }
  | { status: &#39;loading&#39; }
  | { status: &#39;error&#39;; message: string }
  | { status: &#39;success&#39;; policies: Policy[] };
</code></pre>
<p>Four lines, and an entire class of bug is now unrepresentable — you cannot read <code>policies</code> without first proving you&#39;re in the success branch. The compiler narrows it for you, the errors are legible, and any developer can extend it without a conversation. Cheap to read, expensive bugs prevented. That&#39;s the ratio you want.</p>
<p><strong>Branded types, but only where mixups are real.</strong> If your codebase passes around several kinds of ID as bare strings and you&#39;ve actually shipped a bug from swapping two of them, this is worth it:</p>
<pre><code class="language-ts">type PolicyId = string &amp; { readonly __brand: &#39;PolicyId&#39; };
type CustomerId = string &amp; { readonly __brand: &#39;CustomerId&#39; };
</code></pre>
<p>Note the condition. <em>If you&#39;ve actually shipped that bug.</em> Branding every primitive in the app because it&#39;s theoretically safer is how you end up with casts scattered everywhere and a team quietly annoyed at you.</p>
<p><strong><code>satisfies</code> instead of annotation, when you want both.</strong> This one is underused and it&#39;s almost free:</p>
<pre><code class="language-ts">const routePermissions = {
  policies: [&#39;read&#39;, &#39;write&#39;],
  billing: [&#39;read&#39;],
} satisfies Record&lt;string, Permission[]&gt;;
</code></pre>
<p>You get the constraint checked <em>and</em> the literal types preserved, so <code>routePermissions.billing</code> is <code>[&#39;read&#39;]</code> rather than a widened <code>Permission[]</code>. No cleverness required, real inference gained.</p>
<p><strong>Generics on components, one parameter deep.</strong> A typed <code>DataTable&lt;TRow&gt;</code> that infers the row type from the data you pass it is genuinely good. Two or three interdependent type parameters with constraints referencing each other is where it stops paying, in my experience — the signature becomes the thing people copy-paste rather than read.</p>
<h2>What usually doesn&#39;t</h2>
<p><strong>Type-level logic that reimplements runtime logic.</strong> Deep conditional types that compute a shape based on five flags. Recursive types that parse a string format into a structure. These are legitimately impressive and they belong in libraries, where a small number of maintainers absorb the complexity so thousands of users don&#39;t have to. In application code the arithmetic is inverted: your whole team pays, nobody outside benefits, and the requirements will change next quarter anyway.</p>
<p><strong>Types that exist to avoid a small runtime check.</strong> Sometimes the honest answer is a validation at the boundary and a plain type afterward:</p>
<pre><code class="language-ts">const policy = policySchema.parse(response.data);
</code></pre>
<p>You&#39;ve now got a real guarantee — checked against actual data, not just asserted about it — and the type downstream is boring. I&#39;d take that over an elaborate type that describes what the server <em>should</em> send, every time. The type system can&#39;t see your API. A parser can.</p>
<p><strong>Generic abstractions built for one call site.</strong> The rule I&#39;d apply here is the same one that applies to component abstractions: don&#39;t generalize until you have the second case in front of you. A generic hook with three type parameters serving exactly one consumer is a puzzle you built for yourself.</p>
<h2>The test I actually use</h2>
<p>When I&#39;m not sure whether a type has gone too far, I ask two things.</p>
<p>First: <em>can a mid-level developer on this team change the code this type guards, without asking me?</em> If the honest answer is no, the type has become a bottleneck with my name on it. That&#39;s not safety — it&#39;s a bus factor problem I introduced on purpose.</p>
<p>Second: <em>what does the error look like when someone gets it wrong?</em> I&#39;ll take a slightly weaker type with a clear failure message over a stronger one that produces forty lines of inference noise. The error message is the user interface of a type, and it deserves the same consideration as any other interface.</p>
<p>Neither test involves how much the type proves. That&#39;s deliberate.</p>
<h2>Where I&#39;ve landed</h2>
<p>I used to think being weak at type-level programming meant I&#39;d eventually hit a ceiling in TypeScript. What actually happened is that the ceiling turned out to be somewhere I don&#39;t need to go very often — and that most of the safety I care about comes from a handful of unglamorous constructs applied consistently at the right places, mostly at the edges where data enters the app.</p>
<p>The interesting decisions aren&#39;t in the type system at all. They&#39;re about <em>where</em> you put the boundary, what you validate, and which mistakes you&#39;ve decided are worth making impossible. Once those are right, the types tend to stay simple on their own. When I find myself writing something genuinely gnarly, it&#39;s usually a sign the design underneath it is doing something it shouldn&#39;t.</p>
<p>I&#39;m aware this is the position of someone who isn&#39;t a type wizard, and I&#39;d take the counter-argument seriously. So if you&#39;ve got a case where deep type-level machinery genuinely paid for itself in an application — not a library — I want to see it. That&#39;s the example that would move me.</p>
]]></content:encoded>
      <pubDate>Thu, 03 Sep 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TypeScript</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Your Agent Instructions Are Rotting Right Now</title>
      <link>https://ma-x.im/blog/agent-playbook-instructions-rot</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-instructions-rot</guid>
      <description>Nobody writes agent rules once and walks away — they write them, then keep adding. A line here after a bad run, a paragraph there after a near-miss. Six months later the file contradicts itself, and the agent is following the version of your project that stopped existing in March.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-instructions-rot/cover.png" alt="Your Agent Instructions Are Rotting Right Now" /></p>
<p>There&#39;s a particular kind of file that only ever grows. You know the one. It started as a short list of project conventions for your agent, and every time something went sideways you appended a line to stop it happening again. <em>Always run the build before committing. Don&#39;t touch the lockfile. Use the existing helper instead of writing a new one.</em></p>
<p>Every line was correct on the day it was written. That&#39;s what makes this hard to see.</p>
<p>Because instructions aren&#39;t code. Nothing fails when they go stale. There&#39;s no red test, no type error, no failing pipeline. A rule that stopped being true six months ago sits there looking exactly like a rule that&#39;s still true, and the agent — being obedient — follows both with equal conviction.</p>
<blockquote>
<p>Instructions don&#39;t break loudly. They just quietly stop describing your project.</p>
</blockquote>
<p><a href="https://ma-x.im/blog/agent-playbook-context-is-the-product">The context is the product</a> made the case that what you feed an agent matters more than which model you picked. This is the uncomfortable follow-up: that context is an artifact you own, and artifacts you own need maintenance. Nobody budgets for maintaining a text file.</p>
<h2>How the rot actually happens</h2>
<p>It&#39;s never one dramatic mistake. It&#39;s four small, reasonable things.</p>
<p><strong>The project moves and the file doesn&#39;t.</strong> You migrate from one test runner to another, rename a directory, drop a library. The code changes in one commit. The instruction describing the old world changes in... no commit, because nobody thought about it. Now the agent is confidently steering toward a folder that isn&#39;t there.</p>
<p><strong>Rules get added to fix single incidents.</strong> The agent did something dumb once, so you wrote a rule. The rule is narrow, situational, and phrased as a universal law. Twenty of those later, you&#39;ve encoded a list of past accidents rather than a description of how the project works.</p>
<p><strong>Nothing ever gets deleted.</strong> Deleting a rule feels risky — what if it was load-bearing? So the file only accretes. And because instructions usually sit near the top of the agent&#39;s context, every dead line is taking up room that live information could be using.</p>
<p><strong>Contradictions arrive silently.</strong> Rule 4 says prefer the shared utility. Rule 19, added months later by someone solving a different problem, says write it locally in the feature folder. Both are in the file. The agent picks one, more or less arbitrarily, and now its behavior looks random — which is the symptom people usually misdiagnose as the model being unreliable.</p>
<p>That last one is worth sitting with. When an agent behaves inconsistently across similar tasks, the instinct is to blame the model or add <em>another</em> rule. Often the real cause is that you&#39;ve given it two rules and asked it to guess.</p>
<h2>The symptoms, before you go looking</h2>
<p>You can usually feel this before you can point at it.</p>
<p>The agent starts doing things you don&#39;t remember asking for, and when you grep the instructions, there it is — a line from months ago you&#39;d completely forgotten writing. Or it does the right thing four times out of five, and the fifth is a coin flip. Or you find yourself correcting the same class of thing in review over and over, adding a clarification each time, and the clarifications aren&#39;t helping. That&#39;s the tell: if adding rules isn&#39;t improving behavior, the problem isn&#39;t a missing rule. It&#39;s the pile.</p>
<p>Honestly, the strongest signal is simpler. When was the last time you read your instructions file top to bottom? If you can&#39;t remember, you&#39;re not maintaining a document, you&#39;re maintaining a sediment layer.</p>
<h2>Treat it like code, because it is</h2>
<p>The fix isn&#39;t clever. It&#39;s just deciding that this file has an owner and a lifecycle, the same as any other part of the system.</p>
<p><strong>Read it end to end on a schedule.</strong> Not when something breaks — on a cadence. It takes ten minutes. You will find at least one line that describes a project that no longer exists.</p>
<p><strong>Delete aggressively.</strong> This is the part people won&#39;t do, so let me put it plainly: a rule you can&#39;t justify today is doing damage today. It&#39;s consuming context, and it&#39;s a coin-flip waiting to happen when it eventually contradicts something newer. If you&#39;re wrong and it mattered, you&#39;ll find out fast and can put it back in thirty seconds. That&#39;s a cheap mistake. Carrying twenty dead rules is not.</p>
<p><strong>Prefer describing the system over listing incidents.</strong> &quot;Feature code lives under <code>src/features/{feature}</code>, and anything shared moves to <code>src/shared</code> only when a second feature needs it&quot; is worth ten rules about specific files. Principles survive refactors. Incident notes don&#39;t.</p>
<p><strong>Hunt contradictions directly.</strong> When you review, don&#39;t just ask <em>is this line true?</em> Ask <em>does this line disagree with another line?</em> Contradictions are the expensive failure here, and they&#39;re invisible unless you&#39;re specifically looking.</p>
<p><strong>Update instructions in the same commit as the change.</strong> You renamed the folder — the rule mentioning that folder is part of the rename. This is the one habit that stops rot at the source, and it costs nothing once it&#39;s automatic.</p>
<h2>I&#39;ve had to redo this myself</h2>
<p>I&#39;m not writing this from the outside. The memory system on this site is exactly this problem in a different shape — a set of files an agent reads before it does anything, describing conventions and decisions.</p>
<p>I wrote up the first version, published it, and then <a href="https://ma-x.im/blog/ai-agent-memory-redesign">redesigned the whole thing after a few weeks of actually using it</a>. Not because the original idea was wrong, but because the first design let information accumulate without ever forcing anything out. The rules that ended up mattering were about <em>overwriting</em> rather than appending: this file gets rewritten in full, that entry gets edited in place rather than duplicated, completed items get deleted rather than archived. Not because deletion is elegant. Because anything that only grows eventually stops being read — by me, and by the agent.</p>
<p>That&#39;s the lesson I&#39;d extract for instructions generally. The interesting design question isn&#39;t what to write down. It&#39;s what forces something to leave.</p>
<h2>Small is a feature</h2>
<p>There&#39;s a version of this article that ends with &quot;so audit your rules regularly,&quot; and that&#39;s true but weak. The stronger claim is that the size of an instruction file is itself a quality signal, in the wrong direction.</p>
<p>A short, current, internally consistent set of rules beats a comprehensive one, every time. The agent reads all of it. You can hold all of it in your head. When something&#39;s wrong you can find it. A long file fails on all three, and it fails invisibly, which is the worst property a piece of configuration can have.</p>
<p>So the question I&#39;d ask about your instructions isn&#39;t whether they&#39;re thorough. It&#39;s whether you&#39;d be comfortable reading them out loud to a new engineer joining tomorrow — and how many lines you&#39;d catch yourself apologizing for on the way through.</p>
<p>If you&#39;ve found a maintenance rhythm that actually sticks — something better than &quot;review it when the agent embarrasses you&quot; — I&#39;d like to hear it. That&#39;s the part I&#39;m still refining.</p>
]]></content:encoded>
      <pubDate>Tue, 01 Sep 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>They Asked Me to Build a Slider. It Was a Much Better Question Than I Thought.</title>
      <link>https://ma-x.im/blog/the-slider-interview</link>
      <guid isPermaLink="true">https://ma-x.im/blog/the-slider-interview</guid>
      <description>A carousel with next, previous, and a row of dots. No animation. I remember thinking: is this it? Then I watched my own first thirty seconds of typing and realized the task wasn&apos;t measuring whether I could build a slider. It was measuring the order I do things in.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/the-slider-interview/cover.png" alt="They Asked Me to Build a Slider. It Was a Much Better Question Than I Thought." /></p>
<p>I sat down for a technical interview recently and got handed this: here&#39;s a React page, here are ten image URLs from the internet, build a slider. Next button, previous button, a row of dots underneath. Click a dot, jump straight to that slide. No animation needed.</p>
<p>My honest first reaction was <em>is this it?</em> I&#39;m an architect. I&#39;d come in expecting to defend some structural decision, argue about boundaries, maybe get grilled on a system design. Instead I got a task I&#39;d have called kindergarten-level on paper.</p>
<p>Then I started typing, and about thirty seconds in I got genuinely interested — not in the slider, in what the slider was doing to me.</p>
<h2>The first thirty seconds</h2>
<p>Before writing anything I asked questions. Do you want keyboard support? Should it wrap around? Do the images need preloading? Some of those got a <em>no, don&#39;t worry about it</em>. Fine — that&#39;s an answer, and now the scope is pinned down instead of assumed.</p>
<p>Then I opened the component and the first thing I wrote was state:</p>
<pre><code class="language-tsx">const [currentSlide, setCurrentSlide] = useState(0);
</code></pre>
<p>One value. That&#39;s all this thing needs to know about itself — which slide is showing. Everything else is derived.</p>
<p>And the second thing I wrote was three empty functions:</p>
<pre><code class="language-tsx">const handleNext = () =&gt; {};
const handlePrevious = () =&gt; {};
const handleSelectSlide = (index: number) =&gt; {};
</code></pre>
<p>Empty. Nothing inside them. I hadn&#39;t decided the boundary behavior yet, hadn&#39;t written a single <code>div</code>. I just named the three things this component can <em>do</em>, and then kept going.</p>
<p>I want to be careful here, because this is the part I actually find interesting and it would be easy to oversell. I&#39;m not claiming that&#39;s the One True Order. But sitting there, watching myself do it, I realized I had described the entire component — its state and its complete behavior surface — before rendering anything. The markup afterwards was almost mechanical. There was nothing left to decide.</p>
<blockquote>
<p>The interesting part of a trivial task isn&#39;t whether you finish it. It&#39;s what you reach for first.</p>
</blockquote>
<h2>The same task, three ways</h2>
<p>I&#39;ve never sat on the other side of the table scoring people on this, so take what follows as a hypothesis rather than a verdict. But I think this task separates approaches cleanly, and the separation has nothing to do with knowing React.</p>
<p><strong>Start from the markup.</strong> Drop in a <code>div</code>, put an <code>img</code> in it, add two buttons. Then wire up the next button — oh, that needs a handler, scroll back up, write one. That needs state — scroll up again, add <code>useState</code>. Then the dots, which need to know the active index, which means going back to think about what the index actually means. The code arrives, but it arrives by accretion, each piece pulling the previous one apart a little.</p>
<p><strong>Start from the pieces, one at a time.</strong> State first, then a handler, then the markup for that handler, then the next handler. Better — but each behavior is decided in isolation, so the boundary rules (&quot;what happens at the last slide?&quot;) get discovered one at a time, in the middle of writing JSX, when you&#39;re least set up to think about them consistently.</p>
<p><strong>Start from state and behavior as a set.</strong> One <code>useState</code>, three named handlers, all declared before anything renders. The boundary question surfaces immediately and gets answered once, for all three, because they&#39;re sitting right next to each other. Then you render.</p>
<p>The difference isn&#39;t skill with React. All three people finish. The difference is whether the shape of the component was decided up front or discovered by bumping into it, and <em>that</em> is the thing this dumb little task exposes with surprising precision.</p>
<h2>Then they made it worse, and that&#39;s when it got good</h2>
<p>Part two: show three images per page instead of one. And add looping — from the last page, next goes to the first; from the first, previous goes to the last.</p>
<p>That sounds like a small tweak. It isn&#39;t, and this is the bit I enjoyed.</p>
<p>In the one-image version, the natural move at the end is to disable the button:</p>
<pre><code class="language-tsx">&lt;button onClick={handleNext} disabled={currentSlide === images.length - 1}&gt;
  Next
&lt;/button&gt;
</code></pre>
<p>The boundary is handled in the <em>markup</em>, as a disabled state. Now change the rules. With three-per-page and wrapping, &quot;the end&quot; isn&#39;t the last image anymore, it&#39;s the last <em>page</em> — and there&#39;s no such thing as the end, because it wraps. The button is never disabled. The logic moves out of the JSX and into the handler:</p>
<pre><code class="language-tsx">const pageCount = Math.ceil(images.length / imagesPerPage);

const handleNext = () =&gt; {
  setCurrentPage((page) =&gt; (page + 1) % pageCount);
};

const handlePrevious = () =&gt; {
  setCurrentPage((page) =&gt; (page - 1 + pageCount) % pageCount);
};
</code></pre>
<p>Two things changed underneath, and neither is visible in the requirement as stated. The unit of state stopped being <em>which image</em> and became <em>which page</em>, which is a different concept that happens to look the same on screen with ten images and one per page. And the boundary rule migrated from a rendering concern to a behavioral one.</p>
<p>If your first version had the wrapping logic smeared across the JSX, this is where you pay. If it was already sitting inside three named handlers, you edit two lines.</p>
<p>Ten images, three per page, and suddenly the task has an actual opinion about your first draft.</p>
<h2>Say it out loud or none of this counts</h2>
<p>Here&#39;s the thing that makes the whole exercise work, and it&#39;s not the code.</p>
<p>Nobody watching can see you reason. They see a cursor. So I narrated the entire time: <em>I&#39;ll start with state — I think all I need to store is which slide is selected. Now the handlers. I&#39;m not going to build arrow-key navigation unless you want it, just next and previous buttons.</em> And the answer came back: yeah, that&#39;s fine, don&#39;t bother.</p>
<p>That exchange took four seconds and it removed a chunk of work I would otherwise have guessed at. More importantly, it made my reasoning visible. Someone watching me could tell <em>why</em> the code was landing in that order, which is information they cannot get any other way.</p>
<p>This is the same argument I made in <a href="https://ma-x.im/blog/interviewing-senior-engineers">the piece on interviewing senior engineers</a> from the interviewer&#39;s side: the signal is in the reasoning, not the artifact. Sitting on the candidate side of it, I&#39;d put it even more bluntly. If you think silently and produce correct code, you&#39;ve handed over the least interesting half of what you know.</p>
<h2>And yes, I used Google</h2>
<p>At one point I needed to iterate a fixed number of times to render the dots. The simplest thing is:</p>
<pre><code class="language-tsx">{Array(pageCount).fill(0).map((_, index) =&gt; (
  &lt;Dot key={index} active={index === currentPage} onClick={() =&gt; handleSelectSlide(index)} /&gt;
))}
</code></pre>
<p>And I blanked on it. Couldn&#39;t remember how to make an empty array of a given length. So I asked whether I could look it up, they said sure, I looked it up, and moved on.</p>
<p>I&#39;d like that to be unremarkable, because it should be. Forgetting a piece of syntax you type maybe twice a year says nothing about anything. What would actually have mattered is if I hadn&#39;t known <em>what I was reaching for</em> — that I needed a fixed-length iteration, that the dots derive from page count rather than being their own state. That part I never doubted. The incantation to produce the array is a lookup.</p>
<p>The failure mode isn&#39;t forgetting an API. It&#39;s not knowing what shape of thing you need.</p>
<h2>What I took from it</h2>
<p>I don&#39;t know how the interview went. Genuinely — no result yet, and I&#39;m writing this without one.</p>
<p>But I came away thinking the task was much smarter than it looked, and I&#39;d been slightly snobbish about it in the first minute. A hard problem mostly tells you whether someone has seen that problem before. A trivial problem, watched closely, tells you the order someone&#39;s mind moves in — and then a small twist tells you whether their first draft was structured or just correct.</p>
<p>The slider was never the point. It was a transparent object they could watch me think through, and it cost them ten minutes to set up.</p>
<p>If you&#39;re interviewing people and reaching for something elaborate, I&#39;d at least consider the opposite: take something almost insultingly simple, watch the first thirty seconds, then change one requirement. And if you&#39;ve been on the receiving end of a task like this and read it as disrespect — I did too, briefly. I&#39;d like to hear whether you changed your mind the way I did.</p>
]]></content:encoded>
      <pubDate>Sun, 30 Aug 2026 00:00:00 GMT</pubDate>
      <category>Interviewing</category>
      <category>Career</category>
      <category>React</category>
      <category>Engineering Culture</category>
    </item>
    <item>
      <title>React Gives You the Loading State. It Does Not Give You Cancellation.</title>
      <link>https://ma-x.im/blog/react-playbook-abort-controller</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-abort-controller</guid>
      <description>React 19 hands you pending state for free, and that&apos;s genuinely nice. But a finished spinner is not a finished request. AbortController is the piece almost nobody reaches for — not because it&apos;s hard, but because it looks scarier than it is. Here&apos;s the small, boring version that covers most of what you need.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-abort-controller/cover.png" alt="React Gives You the Loading State. It Does Not Give You Cancellation." /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-error-handling">error handling article</a> argued that failures are a product decision — what breaks, how loudly, and what the user does next. There&#39;s one failure mode it didn&#39;t cover, and it&#39;s the one that produces the weirdest bug reports. Not &quot;the request failed.&quot; The opposite: the request <em>succeeded</em>, just too late, and nobody wanted the answer anymore.</p>
<p>React 19 made the front half of this much nicer. <code>useActionState</code> gives you the pending flag without a single <code>useState</code>:</p>
<pre><code class="language-tsx">const [result, submitSearch, isPending] = useActionState(searchPoliciesAction, null);
</code></pre>
<p>Three values, one line, and the loading state is handled. That used to be four lines of ceremony and at least one bug where <code>setLoading(false)</code> didn&#39;t run on the error path. Real improvement.</p>
<p>But look at what <code>isPending</code> actually tells you. It says React is still waiting on <em>this</em> action. It says nothing about the fetch you fired ninety milliseconds ago that&#39;s still in flight somewhere over the Atlantic. React flipped a boolean. The network doesn&#39;t know that happened.</p>
<blockquote>
<p>A spinner that stops is a UI event. A request that stops is a network event. React only does the first one.</p>
</blockquote>
<h2>The bug that survives <code>isPending</code></h2>
<p>Type into a search box. Every keystroke fires a request. Responses come back whenever they feel like it — the one for &quot;poli&quot; leaves first, the one for &quot;policy&quot; leaves second and returns first, then &quot;poli&quot; lands and overwrites it. Now the input says <em>policy</em> and the list below shows results for <em>poli</em>.</p>
<p>The UI is not loading. The UI is not erroring. The UI is confidently wrong, and it stays wrong until the user touches something.</p>
<p>This is a race, and pending state cannot fix a race. <code>isPending</code> was true, then false, exactly as designed. What you needed was for the earlier request to <em>stop existing</em> the moment it stopped mattering. That&#39;s the job AbortController does, and it&#39;s the reason I think it deserves a chapter of its own rather than a footnote in a data-fetching post.</p>
<p>Honestly, I think the main reason this stays exotic is the name. <code>AbortController</code> sounds like something you&#39;d find in an operating systems textbook. The actual API is three things.</p>
<h2>The whole API, in about ten lines</h2>
<pre><code class="language-ts">const controller = new AbortController();

fetch(&#39;/api/policies&#39;, { signal: controller.signal });

controller.abort();
</code></pre>
<p>That&#39;s it. You make a controller, you hand its <code>signal</code> to whatever is doing the async work, and calling <code>abort()</code> tells that work to give up. The pending <code>fetch</code> rejects immediately.</p>
<p>Two details worth knowing up front, because they&#39;re where people trip.</p>
<p>First, a signal is single-use. Once a controller is aborted, it stays aborted forever — you don&#39;t reset it, you make a new one. One controller per request, not one per component.</p>
<p>Second, aborting rejects the promise. It doesn&#39;t quietly resolve to nothing. Which brings us to the mistake I&#39;d bet is the single most common one in this whole area.</p>
<h2>Cancellation is not a failure</h2>
<p>When you abort a fetch, it throws. If you have a normal <code>try/catch</code> around it, that <code>catch</code> runs, and unless you say otherwise the user gets an error state for something <em>you</em> deliberately caused.</p>
<p>I&#39;ve seen this produce red toasts on perfectly healthy apps. Navigate away from a page mid-load, and the app cheerfully informs you that something went wrong. Nothing went wrong. You left.</p>
<p>So the rule that matters more than any other in this article:</p>
<pre><code class="language-ts">try {
  const response = await fetch(url, { signal });
  return await response.json();
} catch (error) {
  if (error instanceof DOMException &amp;&amp; error.name === &#39;AbortError&#39;) {
    return; // we cancelled this on purpose — not a user-facing failure
  }
  throw error;
}
</code></pre>
<p>An abort is an instruction you gave. It should never reach your error UI, your toast system, or Sentry. Treat it as control flow, not as a fault.</p>
<p>There&#39;s a newer, cleaner way to express the same check if you already have the signal at hand:</p>
<pre><code class="language-ts">if (signal.aborted) return;
</code></pre>
<p>Same idea, less <code>instanceof</code> archaeology. Both are fine. What&#39;s not fine is letting the abort fall through into the same branch as a 500.</p>
<h2>The small version that covers most cases</h2>
<p>Here&#39;s the shape I&#39;d reach for by default. A ref holding the current controller, aborted right before the next request starts:</p>
<pre><code class="language-tsx">function PolicySearchPanel() {
  const inFlight = useRef&lt;AbortController | null&gt;(null);

  const [policies, searchPolicies, isPending] = useActionState(
    async (_previous: Policy[] | null, formData: FormData) =&gt; {
      inFlight.current?.abort();
      const controller = new AbortController();
      inFlight.current = controller;

      const query = String(formData.get(&#39;query&#39;) ?? &#39;&#39;);
      const response = await fetch(
        &#39;/api/policies?query=&#39; + encodeURIComponent(query),
        { signal: controller.signal },
      );

      return (await response.json()) as Policy[];
    },
    null,
  );

  useEffect(() =&gt; () =&gt; inFlight.current?.abort(), []);

  return (
    &lt;form action={searchPolicies}&gt;
      &lt;input name=&quot;query&quot; /&gt;
      &lt;button type=&quot;submit&quot; disabled={isPending}&gt;Search&lt;/button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>Two lines do the real work. <code>inFlight.current?.abort()</code> kills the previous request before starting a new one, so the stale response can never land. The <code>useEffect</code> cleanup aborts on unmount, so navigating away doesn&#39;t leave a request writing into a component that no longer exists.</p>
<p>That&#39;s the universal, boring version. It isn&#39;t clever and it doesn&#39;t need to be. Most cancellation bugs I can think of are covered by exactly those two moments: <em>something newer started</em>, and <em>the thing that wanted this is gone</em>.</p>
<p>Notice what <code>useActionState</code> is and isn&#39;t doing here. It owns <code>isPending</code> and the result value, which is genuinely less code than before. The cancellation is still entirely yours. The new hooks didn&#39;t make this obsolete — they made the missing half more visible, because now the only thing you&#39;re hand-rolling <em>is</em> the abort.</p>
<h2>The signal has to reach the bottom</h2>
<p>Here&#39;s where this stops being a component concern and becomes an architecture one.</p>
<p>Most apps don&#39;t call <code>fetch</code> in components. They call <code>policiesApi.search(query)</code>, which calls a shared <code>httpClient</code>, which eventually calls <code>fetch</code>. If any layer in that chain doesn&#39;t take a signal, cancellation dies there — and you&#39;ll be sitting in the component wondering why abort does nothing.</p>
<p>So the API layer needs to pass it through, all the way down:</p>
<pre><code class="language-ts">export async function searchPolicies(
  query: string,
  options?: { signal?: AbortSignal },
): Promise&lt;Policy[]&gt; {
  const response = await httpClient.get(
    &#39;/api/policies?query=&#39; + encodeURIComponent(query),
    { signal: options?.signal },
  );
  return response.data;
}
</code></pre>
<p>An optional <code>signal</code> on every async function that touches the network. It costs nothing at the call sites that don&#39;t care, and it&#39;s the difference between cancellation being available and being theoretically available.</p>
<p>This is also why I&#39;d rather teach the primitive than the library helper. TanStack Query already hands your query function a signal — you just have to <em>use</em> it:</p>
<pre><code class="language-ts">useQuery({
  queryKey: [&#39;policies&#39;, query],
  queryFn: ({ signal }) =&gt; searchPolicies(query, { signal }),
});
</code></pre>
<p>That signal is there in every TanStack Query app in the world, and a lot of them pass it nowhere. The library already solved the plumbing. If your own layers drop the signal on the floor, the plumbing has nothing to plug into.</p>
<h2>Two extras that are worth knowing</h2>
<p>Timeouts, without the <code>setTimeout</code> dance:</p>
<pre><code class="language-ts">fetch(url, { signal: AbortSignal.timeout(8000) });
</code></pre>
<p>And combining reasons to stop — say, &quot;cancel if the user navigates away <em>or</em> if eight seconds pass&quot;:</p>
<pre><code class="language-ts">const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(8000)]);
</code></pre>
<p>A timeout abort surfaces as a <code>TimeoutError</code> rather than an <code>AbortError</code>, which is exactly what you want: one of those deserves a message to the user, the other doesn&#39;t. That distinction is the whole game — <em>why</em> did this stop, and does the person staring at the screen need to know?</p>
<h2>What to actually remember</h2>
<p>Strip it down and there are maybe five things:</p>
<ul>
<li>One controller per request. Never reuse an aborted one.</li>
<li>Abort the previous request before starting the next one.</li>
<li>Abort on unmount.</li>
<li>Never show an abort in the UI. It&#39;s control flow, not an error.</li>
<li>Accept an optional <code>signal</code> in every async function that reaches the network, or the chain breaks.</li>
</ul>
<p>Nothing there is advanced. It&#39;s five habits, and once they&#39;re muscle memory you stop writing an entire category of bug — the stale-write, the ghost update, the error toast on a page you already left.</p>
<h2>The part I keep coming back to</h2>
<p>What strikes me about React 19&#39;s async story is how much it improved the <em>visible</em> half of the problem. Pending state used to be where the boilerplate lived, and now it mostly isn&#39;t. That&#39;s real, and I&#39;d take it every time.</p>
<p>But it makes the asymmetry sharper. The framework got better at telling you it&#39;s waiting, and no better at stopping the thing it&#39;s waiting for — because it can&#39;t. React owns the component tree. It doesn&#39;t own the network. The moment you fire a request, you&#39;ve created something that outlives React&#39;s opinion of it, and the only way to reel that back in is to have kept a handle on it.</p>
<p>AbortController is that handle. It&#39;s fifteen lines of very unglamorous code, and I&#39;d argue it&#39;s the difference between an app that <em>looks</em> responsive and one that&#39;s actually consistent with what the user is doing right now.</p>
<p>If you&#39;ve got a cancellation pattern that&#39;s held up better than the ref-and-cleanup version above — especially in bigger apps where requests fan out across several layers — I&#39;d genuinely like to see it. Send it over.</p>
]]></content:encoded>
      <pubDate>Fri, 28 Aug 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Error Handling</category>
      <category>AbortController</category>
      <category>Async</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Evaluation Is the Missing Loop in Agent Workflows</title>
      <link>https://ma-x.im/blog/agent-playbook-evaluation-is-the-missing-loop</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-evaluation-is-the-missing-loop</guid>
      <description>An agent that feels better after a prompt change may still be getting worse. Without a small set of realistic tasks, explicit expectations, and recorded failures, agent improvement is just a sequence of impressions.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-evaluation-is-the-missing-loop/cover.png" alt="Evaluation Is the Missing Loop in Agent Workflows" /></p>
<p>Agent workflows are unusually easy to improve by accident.</p>
<p>You change an instruction. The next run looks good. You add another rule. A different task breaks. You give the model a stronger prompt. The original failure disappears, but a new one appears somewhere else.</p>
<p>After a few weeks, you have a growing pile of instructions and no reliable answer to the only question that matters: is the agent actually better?</p>
<blockquote>
<p>If you do not replay the same work, you are measuring your mood about the last run, not the quality of the system.</p>
</blockquote>
<h2>The demo is not an evaluation</h2>
<p>A successful demo proves that one path worked once. Agent workflows need more than that because the system is probabilistic and stateful. Small changes in context, tool output, or task wording can produce a different path.</p>
<p>An evaluation starts with representative work. Real tickets. Real repository structure. Real conventions. Not a toy prompt that makes the workflow look clean.</p>
<p>The task should have a clear expected result, but not necessarily one exact sequence of actions. If the agent takes a different safe path and produces the right change, that should count as success. Evaluate the outcome and the important constraints, not theatrical obedience.</p>
<h2>What to measure</h2>
<p>Correctness comes first: did the implementation solve the task? Then check the engineering constraints: did it touch the right boundary, preserve existing behavior, run the required checks, and produce a reviewable change?</p>
<p>There are operational measures too. How many tool calls did the run take? How often did it ask for help? How much did it cost? Did it retry the same action? Did it stop at the right boundary?</p>
<p>You do not need a giant scorecard. Start with five or ten tasks that represent the work you actually care about and a short list of pass conditions for each.</p>
<p>A task can pass while still teaching you something. The agent may have reached the result through a fragile route, used twice as many calls as necessary, or relied on a lucky document lookup. Record those details. Evaluation is not just a gate; it is a source of better design.</p>
<h2>Keep failures concrete</h2>
<p>&quot;The agent was confused&quot; is not a useful failure record. Write what happened: it selected the wrong package because two tools had overlapping names; it guessed a story-point estimate when the ticket was ambiguous; it changed a shared component without checking its consumers.</p>
<p>Then classify the failure. Was context missing? Was a tool too broad? Was the instruction ambiguous? Was the permission wrong? Did the task itself lack an expected outcome?</p>
<p>This classification keeps the fix proportional. Do not add a paragraph of prompt text to solve a permission problem. Do not add a new tool to solve a missing acceptance criterion.</p>
<h2>Test the workflow, not only the final diff</h2>
<p>The final code can look correct while the path to it is unsafe. An agent may edit production configuration, ignore a required check, or make a destructive action and then repair the result. The diff hides that story.</p>
<p>For important workflows, record the trace: the decisions, tool calls, approvals, retries, and stop conditions. You are looking for behavior that will fail on a slightly different task.</p>
<p>This is where the previous articles meet. The context should be sufficient. The tools should be narrow. The agent should ask at sharp edges. The guardrails should cap the run. Evaluation is the feedback loop that tells you which of those parts is weak.</p>
<h2>Do not overfit the benchmark</h2>
<p>A benchmark can become another form of theater. If you keep rewriting the test until the agent passes, you are measuring how well the test has adapted to the agent.</p>
<p>Keep some tasks private or rotate them. Add a fresh real task occasionally. Compare the agent&#39;s performance against a human review standard, not only against yesterday&#39;s score.</p>
<p>Also keep a manual path. An agent that passes a benchmark but makes work harder for the person reviewing it is not helping. The system should reduce cognitive load, not move the same uncertainty to the final reviewer.</p>
<h2>The reframe</h2>
<p>Prompt editing feels like progress because the feedback is immediate. Evaluation feels slower because it asks you to repeat work you already did. That repetition is the point. Without it, every improvement is anecdotal and every regression can hide in a different task.</p>
<p>I would rather have a small honest suite of real tasks than a dashboard full of synthetic numbers. The goal is not to prove that the agent is intelligent. It is to know whether it can perform the specific work I am asking it to perform, within the boundaries I am willing to give it.</p>
<p>Take the last five agent tasks that required correction. Turn them into a tiny evaluation set. Write down what success means, replay them after the next change, and keep the failures visible. That is how an agent workflow becomes an engineered system instead of a collection of hopeful instructions.</p>
]]></content:encoded>
      <pubDate>Wed, 26 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Memory Is Not a Bigger Context Window</title>
      <link>https://ma-x.im/blog/agent-playbook-memory-vs-context</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-memory-vs-context</guid>
      <description>A long transcript can remember everything and still help with almost nothing. Durable agent memory is the small set of current facts, decisions, and lessons that makes the next run start from a better baseline.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-memory-vs-context/cover.png" alt="Memory Is Not a Bigger Context Window" /></p>
<p>The easiest way to misunderstand agent memory is to treat it as a transcript that never ends.</p>
<p>Keep every message. Keep every tool call. Keep every correction. Eventually the agent will know everything that happened.</p>
<p>It will also have to search through everything that happened before making a simple decision, and it will not know which old decision is still valid. That is not memory. It is an attic.</p>
<blockquote>
<p>Memory is not everything the agent has seen. It is what the next run needs to know without making the same mistake again.</p>
</blockquote>
<h2>A transcript records; memory changes the baseline</h2>
<p>A transcript is valuable for audit and debugging. It tells you what the agent did, what tools returned, and where the run went wrong. But a future session does not need the whole conversation. It needs the durable result.</p>
<p>The branch convention belongs in memory. The abandoned approach probably does not. The decision to ask before estimating an unclear ticket belongs in memory. The five speculative alternatives discussed before that decision probably do not.</p>
<p>This distinction matters because a transcript is chronological while a working memory should be organized around use. The agent needs current facts, active constraints, decisions, and lessons. It does not need to relive every path that led there.</p>
<h2>Four useful kinds of memory</h2>
<p>I would keep project memory small and explicit.</p>
<p><strong>Facts</strong> describe stable reality: where the application lives, which command runs the checks, which service owns an endpoint, which branch is the production branch.</p>
<p><strong>Decisions</strong> record choices that future work must respect: why one workspace contains several repositories, why a boundary is intentionally strict, why a tool is read-only.</p>
<p><strong>Lessons</strong> capture failures and their prevention: update main before branching, do not use a broad mutation tool, stop when the same check fails repeatedly.</p>
<p><strong>Tasks</strong> describe what is active now. They should expire. A completed task is not a fact about the project and should not remain forever just because it once mattered.</p>
<p>The categories are useful because they give old information an expiration policy. Facts change slowly. Tasks change quickly. Lessons should be revisited when the workflow changes.</p>
<h2>Current beats historical</h2>
<p>The hardest memory bug is stale truth. An agent reads an old instruction that sounds authoritative and follows it after the codebase has moved on.</p>
<p>Every durable memory item needs an owner and a way to become obsolete. Keep dates where they help. Prefer one current entry over a pile of corrections. Remove a rule when the system no longer needs it. A memory store that only grows becomes another source of context pollution.</p>
<p>This is the same principle as code. Dead code is not harmless because it is still readable. Dead instructions are not harmless because an agent can still follow them.</p>
<h2>Write memory at the correction point</h2>
<p>The most useful time to update memory is immediately after a mistake has been understood. Not after a month of similar failures. Not as a quarterly cleanup exercise.</p>
<p>The entry should be short and operational: what happened, why it happened, and what rule prevents it. &quot;The build failed&quot; is not a lesson. &quot;The sitemap imports every post, so an unescaped backtick in any content field breaks the build&quot; is a lesson because it changes the next action.</p>
<p>This is how the agent improves without pretending the model itself has permanently changed. The next session inherits the correction because the environment carries it forward.</p>
<h2>Memory needs boundaries too</h2>
<p>Not every project should share the same memory. A global preference can be useful. A repository-specific deployment rule can be dangerous when applied elsewhere. Scope matters: user memory, workspace memory, project memory, and task memory should not all be one undifferentiated folder.</p>
<p>The agent should know where a rule came from and how far it applies. A local convention is not a universal law. A temporary workaround is not architecture.</p>
<h2>The reframe</h2>
<p>A bigger context window lets an agent carry more text. Good memory lets it carry the right lessons.</p>
<p>Those are different capabilities. One delays forgetting. The other improves the starting point.</p>
<p>When I add something to agent memory now, I ask whether it will change a future decision. If not, it probably belongs in a log, a document, or nowhere. Memory is a control surface for behavior, not a museum of conversations.</p>
<p>Open your current agent instructions and find one paragraph that no longer changes what the agent does. Delete it. Then find the last correction you repeated twice and turn it into one short rule. That is a better memory system than a thousand more lines of transcript.</p>
<p>The final question is how to know whether these rules actually improve the agent. Good intentions are not an evaluation strategy.</p>
]]></content:encoded>
      <pubDate>Mon, 24 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>When an Agent Should Ask Instead of Acting</title>
      <link>https://ma-x.im/blog/agent-playbook-when-to-ask</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-when-to-ask</guid>
      <description>Autonomy is not a single setting to turn up. A useful agent knows which decisions it owns, which ones it can reverse, and which moments need a human before the next step.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-when-to-ask/cover.png" alt="When an Agent Should Ask Instead of Acting" /></p>
<p>The most frustrating agent is not the one that asks too many questions. It is the one that asks about harmless details and confidently guesses at the dangerous ones.</p>
<p>&quot;Which branch should I use?&quot; after you have defined the convention is noise. &quot;Should I push this change to production?&quot; is exactly the question you want.</p>
<p>That difference is the whole design problem. An agent should not be maximally autonomous. It should be autonomous in the places where its decisions are clear, reversible, and within the rules you have agreed on.</p>
<blockquote>
<p>Autonomy is not a volume knob. It is a map of decisions, boundaries, and consequences.</p>
</blockquote>
<h2>Not every uncertainty matters equally</h2>
<p>An agent can be uncertain about a variable name and still make a safe choice. It should not be equally free to guess about an unknown business rule, a missing permission, or a destructive operation.</p>
<p>I find it useful to separate decisions into three questions:</p>
<ul>
<li>Is the intent clear?</li>
<li>Is the action reversible?</li>
<li>Is the consequence contained?</li>
</ul>
<p>When all three answers are comfortable, the agent can act. When one is uncertain, it may need a check. When several are uncertain, it should stop and ask before touching anything important.</p>
<p>This gives autonomy a shape. The agent is not trusted because it sounds confident. It is trusted because the decision sits inside a boundary we understand.</p>
<h2>Make the safe path obvious</h2>
<p>Asking is not automatically good. An agent that responds to every small ambiguity with a question simply moves the work back to you. The goal is not to eliminate judgment. It is to reserve human judgment for the decisions where it has the highest value.</p>
<p>That means documenting defaults. If the branch name follows a convention, the agent should know it. If a missing estimate means &quot;ask only when the ticket is genuinely unclear,&quot; write that down. If a draft pull request is safe but merging requires approval, make the two states distinct.</p>
<p>The agent should not ask because it lacks courage. It should ask because the workflow tells it this decision belongs to a person.</p>
<h2>Use staged actions</h2>
<p>A dangerous action becomes easier to govern when it is split into stages. Instead of one tool that changes production, expose inspection, preview, and apply as separate steps.</p>
<p>The agent can read the current state. It can prepare the proposed change. It can show the effect. A person can approve the final operation.</p>
<p>This pattern is useful far beyond deployment. A pull request can be drafted before it is merged. A database migration can be generated and checked before it runs. A bulk update can produce a count and sample before it changes records.</p>
<p>The agent keeps momentum through the reversible work. The human appears at the sharp edge, where a mistake becomes expensive to undo.</p>
<h2>Ask with a decision, not a shrug</h2>
<p>There is a big difference between &quot;I am not sure what to do&quot; and a useful escalation. A good question contains the context, the options, the recommendation, and the consequence of each choice.</p>
<p>For example: &quot;The ticket has no story points. I estimate it as five because it touches the policy form and validation flow, but the description does not say whether the API changes too. Should I use five, or wait for clarification?&quot;</p>
<p>That is not the agent giving up. It has done the investigation and isolated the unresolved decision. The human only needs to resolve the part that genuinely depends on intent.</p>
<p>Poor escalation pushes the whole problem back: &quot;Please clarify the task.&quot; Good escalation makes the remaining uncertainty small.</p>
<h2>Change the boundary when the workflow matures</h2>
<p>The right boundary is not permanent. Early in a workflow, the agent may need approval to open a pull request. After enough successful runs, drafting the pull request can become automatic while merging remains protected.</p>
<p>This is how I would increase autonomy: one decision at a time, based on evidence. Do not grant a broad permission because the demo looked good. Move one reversible action across the boundary, observe the results, and keep the approval where failures would be costly.</p>
<p>A rule that nobody revisits becomes a superstition. A rule that is changed after evidence becomes an operating model.</p>
<h2>The reframe</h2>
<p>People often ask whether agents are ready to work autonomously. That question is too blunt to be useful. Ready to do what, with which permissions, under which stop conditions, and with what recovery path?</p>
<p>A good agent can act independently for a long stretch and still be designed around human judgment. The human does not need to approve every keystroke. The human needs to own the decisions that carry intent, irreversible consequences, or unclear authority.</p>
<p>Look at the last time your agent asked a question. Was it asking about the right thing? Now look at the last time it guessed. That comparison will show you where the boundary actually belongs.</p>
<p>Once the boundary is clear, the next challenge is continuity. An agent that learns a rule in one session but forgets it in the next is not autonomous. It is repeatedly starting over.</p>
]]></content:encoded>
      <pubDate>Sat, 22 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Tools Are Part of the Agent&apos;s Intelligence</title>
      <link>https://ma-x.im/blog/agent-playbook-tools-are-intelligence</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-tools-are-intelligence</guid>
      <description>An agent cannot reason about information it cannot reach, and a vague tool description turns a precise capability into a guessing game. Tool design is not plumbing around the model. It is part of how the agent thinks.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-tools-are-intelligence/cover.png" alt="Tools Are Part of the Agent&apos;s Intelligence" /></p>
<p>People talk about giving an agent more tools as if they were adding buttons to a control panel. Add Jira. Add Confluence. Add GitHub. Add Postman. Now the agent is more capable.</p>
<p>Sometimes it is. Sometimes you have just built a larger menu of ambiguously named actions and made it harder for the agent to choose correctly.</p>
<p>A tool is not an API wrapper from the agent&#39;s point of view. It is a piece of reasoning infrastructure. Its name, description, parameters, return value, permissions, and failure behavior all shape the decisions the agent can make.</p>
<blockquote>
<p>A tool is part of an agent&#39;s intelligence because it determines what the agent can know and how safely it can act.</p>
</blockquote>
<h2>The hidden cost of a bad tool</h2>
<p>Imagine a tool called update_issue. It accepts an issue ID and a free-form payload. Technically, it can do everything: change the status, assign an owner, move the issue to a sprint, add a comment, or alter an estimate.</p>
<p>It is also asking the agent to reconstruct your entire process from one dangerous verb.</p>
<p>A better interface exposes the meaningful actions separately. Assign the issue. Move it to In Progress. Add it to the current sprint. Set story points. Each action has a narrower purpose and a smaller set of valid inputs. The agent has fewer ways to misunderstand the operation.</p>
<p>This is the same reason good application code uses domain functions instead of one universal mutate function. The boundary carries meaning.</p>
<h2>Descriptions are executable design</h2>
<p>Humans can fill in missing details from experience. Agents cannot reliably do that, especially when several interpretations are plausible. A tool description should answer the questions an agent will have at the moment of choice:</p>
<ul>
<li>What does this action change?</li>
<li>What does it leave untouched?</li>
<li>When should it be used?</li>
<li>What does success look like?</li>
<li>What happens when the requested state is already true?</li>
<li>Which actions are forbidden or require confirmation?</li>
</ul>
<p>A description that says &quot;updates a ticket&quot; is almost empty. A description that says &quot;moves an assigned ticket into the current sprint without changing its estimate&quot; gives the agent an actual contract.</p>
<p>The return value matters just as much. Returning &quot;done&quot; hides the new state. Returning the issue ID, status, assignee, sprint, and a clear error gives the next step something reliable to reason from.</p>
<h2>Prefer tools that make wrong actions difficult</h2>
<p>The best tools do not merely explain the right behavior. They make common wrong behavior hard to express. This is the poka-yoke idea applied to agent interfaces: shape the tool so a mistake becomes impossible or obvious.</p>
<p>A tool that opens a pull request should not silently choose the base branch. A deployment tool should expose the target environment explicitly. A production mutation should not look identical to a staging mutation with one invisible string changed.</p>
<p>Narrow parameters help. Typed enums help. Read-only previews help. Separate approval actions help even more. The agent should be able to inspect what it is about to do before it commits to the irreversible part.</p>
<p>That is not excessive ceremony. It is how you move risk out of the model&#39;s free-form language and into a boundary you can test.</p>
<h2>One excellent tool beats five overlapping ones</h2>
<p>Tool count is another place where agent design starts to resemble multi-agent design. More looks more capable. In practice, overlapping tools create routing uncertainty. The agent spends tokens deciding which nearly identical action to call, then has to recover when the chosen tool has subtly different semantics.</p>
<p>I would rather expose one clear tool for a meaningful operation than five endpoints that reflect how the backend happens to be organized. The agent does not need to see your internal service decomposition. It needs a stable action model.</p>
<p>This is also where MCP servers can either help or hurt. Connecting Jira, Confluence, docs, and Postman gives an agent access to the world. But access alone is not context. Each server still needs coherent names, useful descriptions, predictable errors, and permissions that match the workflow.</p>
<h2>Design tools from the failure backwards</h2>
<p>Start with a real mistake. The agent branched from stale main. It guessed a story-point estimate. It used the wrong API endpoint. It opened a pull request without the required context.</p>
<p>Now ask which tool boundary allowed the mistake. Was the information missing? Was the action too broad? Could the tool have returned the state the agent needed? Did the agent have a safe way to ask for confirmation?</p>
<p>Fix the boundary, not just the prompt. A prompt correction helps one context. A better tool helps every future context that reaches the same decision.</p>
<h2>The reframe</h2>
<p>We usually describe tools as the agent&#39;s hands. I think they are closer to its senses and its joints. They determine what it can perceive, which movements are available, and where the force of a bad decision lands.</p>
<p>A strong model with weak tools is a talented engineer working through a broken dashboard. A modest model with clear, narrow, truthful tools can often do much better because the environment removes whole categories of error.</p>
<p>Look at the most important tool your agent uses today. Can it explain its contract? Can it show the state it changed? Can it make a dangerous action explicit? If not, that tool is not finished just because the API call works.</p>
<p>The next boundary is not a tool at all. It is the moment where the agent should stop, surface uncertainty, and let a person decide.</p>
]]></content:encoded>
      <pubDate>Thu, 20 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>The Context Is the Product</title>
      <link>https://ma-x.im/blog/agent-playbook-context-is-the-product</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-context-is-the-product</guid>
      <description>A capable model with the wrong context is still a bad agent. The useful part of an agent is not the model in isolation, but the project knowledge, rules, tools, and decisions that shape what it can see before it acts.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-context-is-the-product/cover.png" alt="The Context Is the Product" /></p>
<p>A model can be brilliant and still be useless in your project.</p>
<p>Give it a ticket with no surrounding context, a repository with no map, and a handful of tools with vague descriptions. It will produce something plausible. That is exactly the problem. Plausible is not the same as correct.</p>
<p>I used to think of context as the thing I supplied to an agent before the real work began. The prompt, the files, the issue. Now I think that is backwards. Context is not the preamble to the work. Context is the product I am building around the model.</p>
<blockquote>
<p>An agent is only as good as the world it can see before it makes a decision.</p>
</blockquote>
<h2>The model is not the whole system</h2>
<p>The model gets most of the attention because it is the visible intelligence. But an agent run is closer to a small operating system than a chat completion. It has a goal, a working memory, tools, permissions, project rules, and a history of decisions. Change any of those and the same model behaves differently.</p>
<p>That is why comparing agents by model name alone is incomplete. Two teams can use the same model and get completely different results. One gives it the repository map, the conventions, the API documentation, and a clear stop condition. The other gives it a ticket and hopes the model guesses the rest.</p>
<p>The second team is not using a weaker model. It is giving the model a weaker world.</p>
<h2>Context has layers</h2>
<p>The first layer is the task: what needs to change and why. A ticket that says &quot;fix the payment flow&quot; is not a task. It is a label. A useful task includes the observed behavior, the expected behavior, the boundary of the change, and the check that proves it is done.</p>
<p>The second layer is the project: where code lives, which package owns the behavior, how data moves, and which commands validate a change. This is the map that stops an agent from editing the first file whose name looks relevant.</p>
<p>The third layer is convention. Naming, branching, testing, commit messages, API boundaries, and the moments where the agent must ask instead of guessing. Conventions turn a technically possible action into an acceptable one.</p>
<p>The fourth layer is history. Why is this abstraction here? Which migration is still in progress? Which shortcut is deliberate? Without that history, an agent sees an odd line and helpfully removes the reason it exists.</p>
<p>The last layer is access. A tool is part of context because it changes what the agent can know and do. Read-only access to Jira creates one kind of workflow. The ability to update a ticket and open a pull request creates another.</p>
<h2>More context is not always better</h2>
<p>There is a trap here. If missing context causes mistakes, the natural reaction is to attach everything. Every document, every repository, every old discussion. That creates a larger transcript, not necessarily a clearer world.</p>
<p>Good context is selected context. It answers the next decision the agent has to make. A small project map is better than a directory dump. A short rule about API ownership is better than a hundred pages of loosely related documentation. A current decision record is better than three contradictory conversations.</p>
<p>The test is simple: can the agent tell which information is authoritative? If it cannot, more context increases the number of ways it can be confidently wrong.</p>
<h2>Build the context deliberately</h2>
<p>I would start with four durable pieces: a project map, a set of working rules, a decision log, and a list of available tools. Keep each one short enough to remain current. A stale instruction is worse than no instruction because it makes the wrong action look official.</p>
<p>Then connect the context to the workflow. When a task arrives, the agent should be able to discover the relevant feature, read the owning documentation, inspect recent decisions, make the smallest change, and run the expected check. That sequence is more valuable than a clever initial prompt.</p>
<p>This is also why the investment compounds. Every correction that becomes a durable rule improves the next run. Every tool that removes a guess improves the next decision. Every obsolete instruction you delete removes one false path.</p>
<p>The agent is not becoming magically smarter. The environment around it is becoming more legible.</p>
<h2>The reframe</h2>
<p>The exciting part of agents is usually the model. The durable advantage is the context architecture around it. Models will change. A clear project map, coherent rules, useful tools, and visible decisions will keep paying rent.</p>
<p>So when an agent keeps making the same kind of mistake, I do not begin by asking whether I need a stronger model. I ask what part of the world was invisible, ambiguous, or contradictory at the moment it decided.</p>
<p>That question moves the work upstream. You stop polishing prompts and start designing the environment in which decisions happen. The next question is obvious: if tools define what the agent can see and change, tool design is part of the agent&#39;s intelligence too.</p>
<p>Take one recurring correction from your last agent session and ask what context would have prevented it. Write that context where the next session can actually find it. That is the beginning of the product.</p>
]]></content:encoded>
      <pubDate>Tue, 18 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Guardrails for a Runaway Agent</title>
      <link>https://ma-x.im/blog/agent-playbook-guardrails</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-guardrails</guid>
      <description>An agent is stateful, and its errors compound: one wrong step sends it down an entirely different path, and it can&apos;t tell that it&apos;s lost. Autonomy without guardrails isn&apos;t trust — it&apos;s hoping. The job isn&apos;t to make the agent never fail. It&apos;s to make sure that when it does, the blast radius is small.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-guardrails/cover.png" alt="Guardrails for a Runaway Agent" /></p>
<p>Everything in this cluster has circled the same failure: an agent that runs longer, spends more, and drifts further than you intended, with nothing to stop it. The <a href="https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents">multi-agent orchestra</a> fractures context, the <a href="https://ma-x.im/blog/agent-playbook-token-bill">token bill</a> balloons, the <a href="https://ma-x.im/blog/agent-playbook-test-on-change">scheduled loop</a> burns all night. Underneath all of them is one uncomfortable property, and Anthropic name it directly: agents are stateful, and their errors <em>compound</em>. One bad step doesn&#39;t just produce one bad result — it changes the state the next step reasons from, so the agent quietly walks off in a completely different direction and has no idea it&#39;s lost.</p>
<p>That&#39;s the thing traditional software doesn&#39;t really do. A bug usually breaks a feature and stops. An agent&#39;s bug can send it on a confident, expensive journey to nowhere. So the design question isn&#39;t &quot;how do I make the agent never fail?&quot; — you can&#39;t. It&#39;s &quot;when it fails, how do I make sure it fails <em>small</em>?&quot;</p>
<blockquote>
<p>You don&#39;t grant an agent autonomy by trusting it. You grant it autonomy by bounding what it can do when you&#39;re wrong to trust it.</p>
</blockquote>
<h2>Cap the loop</h2>
<p>The first guardrail is the dumbest one and the most important: a hard ceiling on how many steps or iterations a run can take. Anthropic build exactly this into their agents — a maximum number of iterations as a stopping condition, so a task that can&#39;t converge <em>terminates</em> instead of spinning. This is the single line that turns the overnight-burn story into a non-event: the agent tries twenty times, hits the cap, stops, and tells you it&#39;s stuck. &quot;Burned all night&quot; becomes &quot;gave up before breakfast.&quot;</p>
<h2>Budget the spend</h2>
<p>The step cap bounds <em>count</em>; a spend cap bounds <em>cost</em> directly. Give a run a token or dollar budget and a circuit breaker that trips when it&#39;s exceeded. Even if you got the iteration ceiling wrong, the budget is a second, independent wall between a stuck agent and a catastrophic invoice. Two different limits guarding the same failure is not redundancy — it&#39;s the point. The whole philosophy here is that any single guardrail might be the one you misconfigured.</p>
<h2>Notice when it stops making progress</h2>
<p>A cap stops an agent eventually; progress-detection stops it <em>sooner</em>. If the agent takes the same action, gets the same result, and tries again — that&#39;s not work, that&#39;s a loop, and you can detect it. Repeated identical tool calls, the same error over and over, no change in state across steps: these are all signals that the run has stopped advancing and should be cut short. It&#39;s the difference between letting it exhaust twenty iterations and killing it at the third pointless repeat.</p>
<h2>Put a human at the sharp edges</h2>
<p>Not every action deserves the same freedom. Reading a ticket, running a test, opening a draft PR — low consequence, let it run. Deleting data, pushing to a shared branch, spending real money, touching production — these are where autonomy should pause and check with a person. This isn&#39;t timidity; it&#39;s the <a href="https://ma-x.im/blog/agent-playbook-do-it-as-usual">same negotiated boundary</a> that makes &quot;do it as usual&quot; safe to run unattended. The agent owns the reversible actions and stops at the irreversible ones. You decide where that line sits per action, deliberately.</p>
<h2>Shrink the blast radius</h2>
<p>The guardrails above assume the agent <em>tries</em> something bad; sandboxing assumes it <em>succeeds</em> and makes that survivable. Least privilege is the whole idea: an agent that physically cannot reach production can&#39;t take it down, no matter how confused it gets. Run in an environment where the worst thing it can do is still recoverable — a scratch branch, a staging database, scoped credentials. Then a runaway is an annoyance, not an incident.</p>
<h2>See why it ran away</h2>
<p>You can&#39;t tighten a guardrail you can&#39;t see through. Anthropic lean hard on tracing agent decisions for exactly this reason — when an agent &quot;can&#39;t find obvious information&quot; or spirals, the only way to fix it systematically is to look at what it actually did, step by step. Observability isn&#39;t a nice-to-have bolted on at the end; it&#39;s how you learn which guardrail was missing. Every runaway you can inspect becomes a rule you can add.</p>
<h2>The reframe</h2>
<p>There&#39;s a temptation to read all of this as distrust — as clipping the agent&#39;s wings. I see it the opposite way. Guardrails are what let me hand an agent <em>more</em> autonomy, not less, because I know the failure modes are bounded. The reason I can say &quot;do it as usual&quot; and walk away is precisely that a stuck run caps out, a runaway can&#39;t reach anything that matters, and I can see afterward what went wrong. The bounds are what make the trust affordable.</p>
<p>An agent without guardrails isn&#39;t a bold, high-trust setup. It&#39;s an unbounded process with a credit card and access to your systems, and the only thing standing between it and a bad night is luck. Design the blast radius first, and autonomy stops being a gamble. That&#39;s the through-line of this whole cluster: the goal was never a more impressive agent. It was a <em>sane</em> one — one you can actually let run, because you decided in advance how it&#39;s allowed to fail.</p>
<p>Take one agent you run unattended and write down what happens if it gets stuck right now: does it stop on its own, and what can it reach while it&#39;s confused? If either answer is uncomfortable, you&#39;ve found your next guardrail. Tell me which one — I&#39;d bet it&#39;s the iteration cap, because it almost always is.</p>
]]></content:encoded>
      <pubDate>Fri, 14 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Test on Change, Not on a Timer</title>
      <link>https://ma-x.im/blog/agent-playbook-test-on-change</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-test-on-change</guid>
      <description>The instinct is to put the agent on a schedule: every few minutes, wake up and check the environment. That instinct is where a lot of runaway costs and pointless runs come from. An agent should be woken by a reason — a commit, a deploy, a failing check — not by a clock ticking over whether or not anything happened.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-test-on-change/cover.png" alt="Test on Change, Not on a Timer" /></p>
<p>Go back to the <a href="https://ma-x.im/blog/agent-playbook-token-bill">overnight-burn story</a> for a second, because the detail that matters most is the one everyone skips past. The agent was on a <em>schedule</em>. It woke up on a timer to check the environment, over and over, whether or not anything had actually changed. The loop was the accident. The schedule was the design flaw that made the accident possible.</p>
<p>And it&#39;s such a natural design flaw. When you want an agent to keep something healthy, the first thing your brain reaches for is a cron job: &quot;every N minutes, go check.&quot; It feels responsible — always watching, always on. But a timer is a fundamentally lazy trigger. It fires on the passage of time, which is almost never the thing you actually care about.</p>
<h2>A timer runs when nothing happened</h2>
<p>Think about what a scheduled agent actually does most of the time: it wakes up, looks at an environment that is <em>exactly</em> as it left it, does a bunch of work to confirm nothing changed, and goes back to sleep. You&#39;re paying full agent price — remember, <a href="https://ma-x.im/blog/agent-playbook-token-bill">4× a chat and up</a> — to repeatedly discover that nothing is different.</p>
<p>And on the rare occasion something <em>is</em> wrong, the timer is no help there either. If the environment breaks thirty seconds after a run, your schedule sits on that failure until the next tick. So you tighten the interval to catch problems faster, which means even more empty runs, which means more cost and more surface area for exactly the kind of loop that burned all night. The timer is bad at both jobs: it&#39;s wasteful when things are fine and slow when they aren&#39;t.</p>
<blockquote>
<p>A clock is not an event. Waking an agent on a schedule means waking it for no reason, most of the time — and paying for the privilege.</p>
</blockquote>
<h2>Wake it on a reason</h2>
<p>The alternative is almost obvious once you name it: run the agent when something <em>changes</em>. Not &quot;every five minutes,&quot; but &quot;when a commit lands,&quot; &quot;when a deploy finishes,&quot; &quot;when a check goes red,&quot; &quot;when this file is updated.&quot; The trigger is an event that carries actual meaning, and the agent&#39;s run is a response to that meaning.</p>
<p>This flips every problem the timer had:</p>
<ul>
<li><strong>No empty runs.</strong> No change, no event, no run. You stop paying to confirm the status quo.</li>
<li><strong>Immediate response.</strong> The agent reacts the moment the thing it cares about happens, not on the next tick.</li>
<li><strong>Naturally bounded work.</strong> One event produces one run with a clear scope: &quot;here&#39;s what changed, deal with it.&quot; That&#39;s a far easier task to reason about — and to cap — than an open-ended &quot;go check everything.&quot;</li>
<li><strong>Debuggable history.</strong> Every run traces back to a specific event. When something misbehaves, you know exactly what set it off, instead of squinting at a timeline of identical scheduled wake-ups.</li>
</ul>
<p>Testing is the cleanest example. Running your whole suite on a timer is nonsense — nothing changed, so nothing new can break. Running it when code changes is the entire point. The event <em>is</em> the reason the test is worth running. That principle generalizes: the right moment to act is the moment the world became different, because that&#39;s the only moment new information exists.</p>
<h2>The honest exceptions</h2>
<p>I don&#39;t want to pretend timers never have a place. Some work genuinely is periodic: a nightly summary, a daily digest, a report that&#39;s tied to the calendar rather than to a change. If the <em>business meaning</em> is &quot;once a day,&quot; then a schedule is correct — the time itself is the event.</p>
<p>But even then, two rules hold. First, it should still be bounded hard, because &quot;runs on a schedule&quot; and &quot;can run forever once started&quot; are independent properties, and the second one is what bites you. Second, be honest about whether the task is <em>actually</em> periodic or whether you reached for a timer because it was the easy primitive. Most &quot;check the environment every N minutes&quot; jobs are change-driven work wearing a schedule&#39;s clothing.</p>
<h2>The reframe</h2>
<p>The shift from timer to event is small in code — a webhook instead of a cron entry — and large in consequences. It&#39;s the difference between an agent that runs because a clock ticked and an agent that runs because something happened that it should respond to. The first is a machine idling in your driveway, burning fuel to go nowhere. The second only starts when there&#39;s somewhere to go.</p>
<p>That overnight bill wasn&#39;t really caused by a loop. The loop was the spark. The schedule was the pile of dry wood that let a single spark burn until morning. Trigger the agent on change instead, and most of that wood is never there to catch — the agent simply isn&#39;t running when there&#39;s nothing to do. What you still need is a firebreak for the runs that <em>do</em> go wrong, and that&#39;s the last piece: <a href="https://ma-x.im/blog/agent-playbook-guardrails">the guardrails that cap the damage</a>.</p>
<p>Find one scheduled agent job you&#39;re running and ask what event it&#39;s really waiting for. If you can name the change it&#39;s checking for, you can trigger on that change directly — and delete the clock. Tell me what the event turned out to be; naming it is usually the moment the cron job stops making sense.</p>
]]></content:encoded>
      <pubDate>Wed, 12 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>You Only Get to See It From the Outside Once</title>
      <link>https://ma-x.im/blog/the-outside-view</link>
      <guid isPermaLink="true">https://ma-x.im/blog/the-outside-view</guid>
      <description>The most valuable thing an experienced engineer brings to a new project isn&apos;t a skill — it&apos;s a vantage point. For a short window, you can still see the thing from the outside, the way a user or a newcomer does. Six months in, that view is gone. And most teams burn it in the first week without noticing.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/the-outside-view/cover.png" alt="You Only Get to See It From the Outside Once" /></p>
<p>I was in the middle of writing this article when my wife started giving me notes on it. And I did the thing I apparently do — I pushed back. I explained that engineering is different on the inside, that the processes don&#39;t work the way she was picturing, that she was missing context. I had reasons. I always have reasons.</p>
<p>She let me finish. Then she pointed out, pretty gently, that I had just acted out the entire premise of the article I was writing. She was the outsider looking at my argument with fresh eyes. I was the one who had already climbed inside it and could no longer see it straight. On an article that is <em>specifically about that</em>. She was out of the box. I was in it. And I couldn&#39;t tell until she said so.</p>
<p>That small, slightly embarrassing moment is the whole thing I want to talk about.</p>
<h2>The asset nobody writes on the offer letter</h2>
<p>When an experienced person — an architect, a staff engineer, a senior hire — joins a project, they show up with something that has nothing to do with their résumé. For a short window, they can see the project <em>from the outside</em>. They notice the thing that makes no sense. They ask &quot;why is it like this?&quot; about the part everyone else stopped seeing a year ago. They feel the friction a new user feels, because right now they basically <em>are</em> a new user.</p>
<p>That vantage point is not a bonus on top of the &quot;real&quot; work. For a lot of senior hires, it <em>is</em> the work — or it should be. You&#39;re not paying a premium for another pair of hands to type features. You&#39;re paying for someone who can still look at the whole thing and tell you, honestly, what&#39;s wrong with it before they&#39;ve been anesthetized into thinking it&#39;s normal.</p>
<blockquote>
<p>The outside view is the most valuable thing an experienced engineer brings — and it&#39;s the first thing a team destroys without meaning to.</p>
</blockquote>
<h2>It has a shelf life</h2>
<p>Here&#39;s the cruel part. That view is perishable. It doesn&#39;t last. Six months in, you&#39;ve internalized the map. You know where everything lives, which means you&#39;ve stopped noticing that nothing <em>should</em> live there. You know why the weird thing is weird — there&#39;s a Slack thread, a migration, a reason — and knowing the reason is exactly what makes you stop seeing that it&#39;s a problem. You&#39;ve learned where the bodies are buried, so you no longer notice you&#39;re standing in a graveyard.</p>
<p>Keeping the outside view alive past that honeymoon is genuinely hard. It takes deliberate effort from the person, and — much more — a culture that protects it. Most places have neither. So the clarity that walked in the door on day one quietly evaporates, and in a year that same brilliant hire is defending the exact weirdness they were hired to question.</p>
<h2>How teams burn it on day one</h2>
<p>The way companies destroy this asset is almost always the same, and it&#39;s well-intentioned. They hire a senior person because there&#39;s a hot new feature, or a new branch of the product, or a fire. And then, on day one, they submerge them: <em>&quot;Great, let us walk you through how everything works here.&quot;</em> Onboarding as immersion. The clock starts, the deadlines are already there, and the expectation is feature output by roughly week two.</p>
<p>Think about what that does. You took the one person who could still see the project from the outside, and you rushed them inside as fast as possible. You spent their most valuable, most perishable asset on getting them productive on tickets — and you never once asked them the question only they could answer for a few short weeks: <em>what looks wrong to you right now, before you get used to it?</em></p>
<p>What they actually needed was a little room. Time to pick the thing up, turn it over, feel how it behaves, and notice. Time to reflect. That&#39;s not slacking; that&#39;s the senior work. But reflection has no line in the sprint, so it&#39;s the first thing the deadline eats.</p>
<h2>I hit this wall myself</h2>
<p>Let me be specific, because I lived the other side of this. A few years back I joined a Canadian health-tech company in a good, senior role. I was there about six months, and then we parted ways — not dramatically, but because what they wanted and what I was doing had quietly stopped being the same thing.</p>
<p>I did a lot in those months. I set up practices, fixed how a bunch of things were used. Their setup was wild in places — a designer could change a button through an AI tool more or less directly. There was review, technically. But there was essentially <em>zero</em> consistency in the code. And every time I actually picked the project up to work in it, it came apart in my hands. Long legacy, dressed in modern libraries, with no architecture underneath — you genuinely could not tell where anything lived or why.</p>
<p>Here&#39;s the mismatch. What I was offering them was the outside view: I&#39;m looking at this thing fresh, and I&#39;m telling you it doesn&#39;t hold together. That was the most useful thing I had. But what they wanted, increasingly, was for me to stop looking and start shipping — hands inside the box, features out the door. They&#39;d hired a senior and then asked him to stop doing the senior part. I couldn&#39;t unsee what I was seeing, and they didn&#39;t want to act on it. So it ended.</p>
<p>I&#39;m not telling that story to settle a score. Honestly, I get it — the pressure to ship is real and it&#39;s not villainy. I&#39;m telling it because it&#39;s the clearest example I have of a company holding a valuable asset and spending it on the wrong thing. They paid for an outside view and then treated it as an obstacle to velocity.</p>
<h2>For the manager, and for the senior</h2>
<p>So this cuts two ways, and I want to be clear about both, because the two audiences own different halves of the fix.</p>
<p>If you <em>manage</em> senior hires: the outside view is an asset with an expiry date, and you are almost certainly burning it. The window where a new architect or staff engineer can tell you what&#39;s actually wrong is measured in weeks, not quarters. Protect it. Before you submerge them in &quot;how we do things here,&quot; ask them what looks broken while they can still see it — and write it down, because in three months they won&#39;t be able to tell you anymore. And then think about the harder, cultural problem: how do you keep <em>anyone</em> on the team seeing the product from the outside once the honeymoon ends? That&#39;s the real work of engineering culture, and it doesn&#39;t happen by accident.</p>
<p>If you <em>are</em> the senior: your value was never the volume of code you produce. Plenty of people can produce code. Your rare, decaying advantage is that right now you can still see the box from the outside. Guard it. In your first weeks somewhere new, write down everything that looks wrong, everything that makes you go &quot;wait, why?&quot; — because that document is worth more than the features you&#39;ll ship in that same period, and you will not be able to write it later. You&#39;ll have gone native.</p>
<h2>The reframe</h2>
<p>The thing that still gets me about the moment with my wife is how fast it happened, and how invisible it was to me. I wasn&#39;t being stubborn on some project I&#39;d spent years on. I&#39;d been inside <em>my own article</em> for a couple of days, and that was already enough to make me defend it against exactly the kind of outside perspective the article is arguing you should protect. If it happens that quickly to me, on a piece of writing, imagine what six months of deadlines does to a person&#39;s ability to see the system they work in.</p>
<p>The outside view isn&#39;t a personality trait. It&#39;s a position — you&#39;re either standing outside the thing or you&#39;re inside it — and you lose it the moment you step in and get comfortable. Which means the whole game, for teams and for individuals, is noticing that the window is open right now, and using it before it quietly closes.</p>
<p>Think about the last time someone from outside your project — a new hire, a designer, someone who doesn&#39;t live in the code — told you something was wrong, and your first instinct was to explain why they didn&#39;t understand. That instinct is the tell. I&#39;d genuinely like to know what they were seeing that you couldn&#39;t — reach out and tell me, because odds are they were standing exactly where you used to.</p>
]]></content:encoded>
      <pubDate>Tue, 11 Aug 2026 00:00:00 GMT</pubDate>
      <category>Engineering Culture</category>
      <category>Leadership</category>
      <category>Career</category>
    </item>
    <item>
      <title>The Token Bill Is Part of the Architecture</title>
      <link>https://ma-x.im/blog/agent-playbook-token-bill</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-token-bill</guid>
      <description>A story made the rounds: someone put an agent on a schedule to keep checking their environment, it got stuck in a loop one night, and instead of a couple of hours it burned through a fortune in tokens by morning. The lesson isn&apos;t &quot;watch your usage.&quot; It&apos;s that cost is a design constraint, and most agent setups treat it as a footnote.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-token-bill/cover.png" alt="The Token Bill Is Part of the Architecture" /></p>
<p>There&#39;s a story that made the rounds a while back, and I keep thinking about it. Someone set up an agent to run on a schedule — every so often it would wake up and check their environment, run some tests, make sure things were healthy. Reasonable idea. Then one night something went wrong, the agent got stuck in a loop, and instead of running for the couple of hours it was supposed to, it churned all night. By morning it had burned through an eye-watering pile of tokens doing nothing useful.</p>
<p>It&#39;s easy to read that as &quot;lol, watch your usage.&quot; I read it differently. That bill wasn&#39;t an accident of carelessness — it was a design that never treated cost as real. The agent had no sense that its own runtime cost money, and nothing in the system did either. And that&#39;s the norm, not the exception. We design agent workflows around capability and correctness, and we bolt cost on afterward as a thing to monitor. I think that&#39;s backwards.</p>
<blockquote>
<p>Cost isn&#39;t something you check after the fact. With agents, it&#39;s a first-class architectural constraint — as much a part of the design as latency or correctness.</p>
</blockquote>
<h2>Agents are expensive in a way chat isn&#39;t</h2>
<p>Let me put real numbers on it, because the gap is bigger than people expect. Anthropic published figures from their own systems: an agent uses roughly <strong>4× the tokens</strong> of a normal chat interaction, and a multi-agent system about <strong>15×</strong>. Every autonomous loop, every &quot;let me check that again,&quot; every re-read of a big context window — it all adds up per step, and agents take a <em>lot</em> of steps.</p>
<p>This is why Anthropic&#39;s own top-line advice for building with LLMs is to find the simplest thing that works and only add complexity when it demonstrably pays. That&#39;s not aesthetic minimalism. It&#39;s economics. Agentic autonomy trades latency and money for flexibility, and if the task didn&#39;t need the flexibility, you just paid a premium for nothing.</p>
<h2>Where the money actually leaks</h2>
<p>Once you start looking at cost as architecture, you start seeing where it drains:</p>
<p><strong>Unbounded loops.</strong> The overnight-burn story is the pure form. An agent that can retry forever will, eventually, retry forever. Without a hard ceiling, one stuck state is an open tap.</p>
<p><strong>Agents pinging agents.</strong> The multi-agent orchestras I complained about in <a href="https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents">the first article</a> don&#39;t just fracture context — they multiply token spend. One agent asking another &quot;are you done yet?&quot; is a paid API call every time it asks.</p>
<p><strong>Re-sending the world every turn.</strong> If every step drags the entire history back through the model, your cost grows with the square of the conversation. Long-running agents that don&#39;t compress or summarize their context pay for their whole past on every single step.</p>
<p><strong>Reaching for a big model on easy work.</strong> Routing a trivial classification through your most capable, most expensive model is money set on fire. Anthropic explicitly route easy, common cases to a small cheap model and reserve the expensive one for the hard cases.</p>
<h2>Designing like the meter is running</h2>
<p>None of these are exotic to fix. They just require treating the token meter as a signal you design against, the same way you&#39;d design against a latency budget:</p>
<ul>
<li><strong>Put a ceiling on every loop.</strong> A hard cap on iterations or steps turns &quot;burned all night&quot; into &quot;gave up after twenty tries and told me.&quot; This is the first guardrail, and it&#39;s the subject of <a href="https://ma-x.im/blog/agent-playbook-guardrails">its own article</a>.</li>
<li><strong>Give long tasks a memory, not a longer transcript.</strong> Summarize completed phases and store the essentials, so each step reasons over a compact context instead of re-paying for the entire history.</li>
<li><strong>Match the model to the job.</strong> Cheap model for routing and simple calls, expensive model where the reasoning actually earns it.</li>
<li><strong>Prefer a workflow to an autonomous loop when the task is well-defined.</strong> A fixed pipeline of LLM calls is predictable in both behavior <em>and</em> cost. You reach for open-ended autonomy when you truly can&#39;t predict the steps — not by default.</li>
</ul>
<h2>The reframe</h2>
<p>The overnight-burn story gets told as a cautionary tale about monitoring, but monitoring is the weakest possible response — it tells you about the fire after it&#39;s out. The real fix is upstream, in the design: an agent whose cost is bounded by construction can&#39;t run up that bill in the first place, because the ceiling, the compaction, and the stop conditions were part of the architecture, not a dashboard you forgot to check.</p>
<p>So I&#39;ve started treating the token bill the way I treat a slow render or a memory leak: as a property of the system I&#39;m responsible for shaping, not an invoice that shows up later. When I sketch an agent workflow now, &quot;what does one run cost, and what stops it from costing a hundred times that?&quot; is one of the first questions, not the last. The story that made the rounds is only a horror story if cost was never in the design. Put it in, and the worst case is a bounded disappointment instead of a night&#39;s worth of tokens gone.</p>
<p>The next two articles follow this thread straight down: <a href="https://ma-x.im/blog/agent-playbook-test-on-change">when to actually run the agent</a> — because that scheduled loop was the root of the burn — and <a href="https://ma-x.im/blog/agent-playbook-guardrails">the guardrails</a> that cap the damage when a run goes wrong anyway.</p>
<p>Go look at one agent workflow you run and answer a single question: what&#39;s the most it could spend if it got stuck right now? If you don&#39;t know, that&#39;s the number an attacker on your patience — a loop — already knows. Tell me what you find; the not-knowing is the whole problem.</p>
]]></content:encoded>
      <pubDate>Mon, 10 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Do It As Usual: Teaching One Agent Your Whole Workflow</title>
      <link>https://ma-x.im/blog/agent-playbook-do-it-as-usual</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-do-it-as-usual</guid>
      <description>I can open a fresh session, type &quot;take ticket 123 and do it as usual,&quot; and the agent runs the entire routine — assign, sprint, branch, commit, PR, pipeline. People assume that&apos;s a clever prompt. It isn&apos;t. It&apos;s months of small corrections turned into rules the agent never forgets.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-do-it-as-usual/cover.png" alt="Do It As Usual: Teaching One Agent Your Whole Workflow" /></p>
<p>Here&#39;s a thing I can do that sounds like a party trick. I open a brand-new session with my agent and type, almost word for word: &quot;take ticket 123 and do it.&quot; And it does — the whole thing, end to end, without me hovering. Not because I found a magic prompt. Because I spent months teaching it what &quot;as usual&quot; means.</p>
<p>I bring this up because of a complaint I keep hearing, some version of: <em>&quot;I can&#39;t get my agent to just do a task. I set it loose and then I have to walk behind it fixing everything.&quot;</em> And look — if you started last week, that&#39;s completely fair. I couldn&#39;t either at first. But when someone&#39;s been at it for months and still can&#39;t get the agent to carry a task cleanly, I get suspicious. Not of the agent. Of how they&#39;re working with it.</p>
<h2>&quot;As usual&quot; is a lot of things</h2>
<p>Let me show you what actually happens when I say &quot;do it as usual,&quot; because the phrase is hiding an entire workflow the agent has internalized:</p>
<ul>
<li>It takes the ticket and assigns it to me.</li>
<li>It moves it to In Progress.</li>
<li>If the ticket was sitting in the backlog, it pulls it into the current sprint.</li>
<li>If it has no story points, it estimates it — and if it genuinely can&#39;t tell, it asks me, the way we agreed it would.</li>
<li>It creates a branch by our naming convention, after first updating main so it&#39;s not branching off something stale.</li>
<li>It makes the changes, committing as it goes with human-readable messages that say what was actually done.</li>
<li>It opens the pull request, writes a real description, adds the necessary context and links, mentions the people who need to see it, and links the PR back to the ticket.</li>
<li>Then it waits for the pipelines, checks whether anything went red, and fixes it if it did.</li>
</ul>
<p>All of that from four words. And none of it was true on day one.</p>
<h2>The part nobody wants to hear</h2>
<p>That list didn&#39;t arrive as a single clever configuration. It arrived one failure at a time. The agent would branch off stale main — so I taught it to update first. It would write commit messages like &quot;fix stuff&quot; — so we worked out what a human-readable message looks like. It would guess a story-point estimate confidently when it had no business guessing — so we agreed that when it&#39;s unsure, it stops and asks me instead of inventing a number.</p>
<blockquote>
<p>Every step in &quot;as usual&quot; is a scar. It&#39;s there because something once went wrong, and I turned the fix into a rule instead of re-explaining it every session.</p>
</blockquote>
<p>That&#39;s the whole method, honestly. It&#39;s not prompt cleverness. It&#39;s accumulation. Each time the agent got something wrong, I didn&#39;t just correct it for that one run — I wrote the correction down somewhere durable, so the next session started from the corrected baseline. Do that for a few months across a dozen steps, and &quot;as usual&quot; stops being a phrase and becomes a reflex the agent actually has.</p>
<h2>Give it real context, not guesses</h2>
<p>The second half of this is tooling. An agent working from guesses is an agent you&#39;ll always be correcting. So I connected the things it needs to <em>know</em> the state of the world instead of hallucinating it: an MCP server to read Jira, another for Confluence, one for our docs, one for Postman — the tools the work actually runs on.</p>
<p>The difference is night and day. When the agent can read the ticket, it doesn&#39;t misunderstand the task. When it can see the docs and the API collection, it doesn&#39;t invent an endpoint. The tools aren&#39;t there to make the agent look impressive — they&#39;re there so its decisions are grounded in what&#39;s real. This is the <a href="https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents">augmented-LLM idea</a> done properly: one agent, made capable by good tools, is worth more than a crowd of agents guessing at each other&#39;s intent.</p>
<h2>One agent, not one per repo</h2>
<p>I told this story in <a href="https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents">the multi-agents piece</a>, but it belongs here too, because it&#39;s the same instinct. My project spans six or seven repositories. My first move was to set up an agent in each one. Then I noticed my tasks almost never span repos — they live in one, and multi-repo changes have come up maybe twice. So instead of maintaining seven configurations, I put every repo in one workspace and taught one agent. Every hour I <em>didn&#39;t</em> spend duplicating setup across seven repos, I spent making the single one better. That&#39;s why it can do &quot;as usual&quot; at all — the investment was concentrated, not scattered.</p>
<h2>The decision points matter most</h2>
<p>The subtle part — the thing that separates an agent that helps from one you babysit — is deciding where it should act and where it should stop and ask. I didn&#39;t tell it to just do everything autonomously. We agreed on the seams. Estimation it can&#39;t confidently do? Ask. Convention it knows cold? Just do it. That negotiated boundary is what makes &quot;as usual&quot; safe to run unattended. The agent isn&#39;t guessing about my intent at the risky moments, because we already talked through those moments and it knows the rule.</p>
<p>This is why &quot;just be more autonomous&quot; is bad advice. Autonomy isn&#39;t a slider you crank to the top. It&#39;s a map of which decisions the agent owns and which ones come back to you — and you build that map the same way you build everything else here, one correction at a time.</p>
<h2>The reframe</h2>
<p>So when I hear &quot;I can&#39;t get my agent to do a task in one shot,&quot; what I mostly hear is a missing investment. People want the &quot;do it as usual&quot; outcome without the months of turning mistakes into durable rules and wiring in the tools that ground the agent in reality. The agent that runs my whole ticket workflow from four words isn&#39;t smarter than yours. It&#39;s just been <em>taught</em> more, and the teaching stuck because I wrote it down instead of repeating it.</p>
<p>The uncomfortable truth is that this looks a lot less like prompting and a lot more like onboarding a junior engineer who happens to have perfect recall. You explain the convention once, correctly, and it never forgets it — but only if you actually captured it instead of hand-waving it in a single session that evaporates when the context does.</p>
<p>Pick the one task you do most often with your agent and write down every correction you find yourself repeating this week. That list is the start of your own &quot;as usual.&quot; Send it to me if you want a second pair of eyes — I&#39;ll tell you which corrections are rules and which are you doing the agent&#39;s thinking for it.</p>
]]></content:encoded>
      <pubDate>Sat, 08 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>You Probably Don&apos;t Need Multi-Agents</title>
      <link>https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents</link>
      <guid isPermaLink="true">https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents</guid>
      <description>Open almost any &quot;how I use AI&quot; post and you&apos;ll find an orchestra: a planner agent, a coder agent, a reviewer agent, a tester agent, all pinging each other. It looks impressive. It&apos;s also the most expensive, most fragile way to get worse results than one agent you actually taught to do the job.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/agent-playbook-you-dont-need-multi-agents/cover.png" alt="You Probably Don&apos;t Need Multi-Agents" /></p>
<p>I work across six or seven repositories on one project — a big hybrid thing, part microfrontend, part backend, several apps that all talk to each other. When I started bringing an AI agent into that work, my first instinct was the obvious one: set up an agent in each repo. Seven repos, seven configured agents. It felt right. Each repo is its own world, so each gets its own helper.</p>
<p>I got a couple of repos in before I stopped and asked myself what I was actually doing. My tasks almost never live in seven repos at once. They live in <em>one</em>. In months of work I&#39;ve had maybe two tasks that touched multiple repos. So I was about to build and maintain seven separate agent setups to serve a reality where the work is overwhelmingly single-repo. I threw it out, put every repo into one workspace, and configured one agent, once. That agent has been better than any swarm I could have wired together — and it cost me a fraction of the effort.</p>
<p>That small decision is the whole argument of this article, scaled up. The industry&#39;s default reflex right now is <em>more agents</em>. I think the default should be <em>one agent, taught well</em> — and you should have to justify every agent you add past the first.</p>
<h2>The orchestra is a status symbol</h2>
<p>Here&#39;s the thing that bugs me about most &quot;how I use AI&quot; posts. They read like a flex. &quot;I built an orchestrator that spawns a planner, which hands off to a coder, which pings a reviewer, which triggers a tester.&quot; Eight agents, a diagram with lots of arrows, and the unspoken message: look how sophisticated my setup is.</p>
<p>And honestly, some of it <em>would</em> work fine — if the coordination between the pieces were as good as the diagram implies. But that&#39;s exactly the part nobody shows you. The arrows in the diagram are doing an enormous amount of invisible work. In practice, an agent that pings another agent — &quot;are the tasks ready yet?&quot;, &quot;go run the tests now&quot; — is a coordination protocol you now own, built out of the least reliable component you have: one model trying to communicate intent to another model.</p>
<blockquote>
<p>The number of agents in your system is not a measure of its sophistication. It&#39;s a measure of how many places your context can fracture.</p>
</blockquote>
<h2>What a second agent actually costs</h2>
<p>Let&#39;s talk about the bill, because it&#39;s the part the impressive diagrams leave out. Anthropic published numbers from their own multi-agent research system, and they&#39;re worth sitting with: a single agent burns roughly <strong>4× the tokens</strong> of a normal chat, and a multi-agent system burns about <strong>15×</strong>. That&#39;s not a rounding error. That&#39;s your token budget, tripled and tripled again, in exchange for coordination overhead.</p>
<p>Sometimes that trade is worth it. Anthropic&#39;s own finding is that multi-agent setups genuinely win at <em>breadth-first</em> research — questions where you can fan out into many independent directions at once, each subagent exploring its own slice with its own context window, then compressing what it found. If the task is &quot;find every board member across these 400 companies,&quot; parallel agents crush a single agent that has to plod through sequentially. That&#39;s a real, honest use case.</p>
<p>But look closely at what makes it work: the subtasks are <em>independent</em>. One agent&#39;s findings don&#39;t change what another agent should do. That&#39;s the property that makes parallelism safe. And it&#39;s exactly the property most of our work doesn&#39;t have.</p>
<h2>Coding is the wrong shape for a swarm</h2>
<p>Anthropic say it plainly, and it matches everything I see: most coding tasks involve <em>fewer</em> truly parallelizable subtasks than research, and today&#39;s models aren&#39;t good at coordinating and delegating to each other in real time. Code is a web of dependencies. The change you make in one file constrains the change in the next. Split that across agents and you don&#39;t get parallelism — you get two agents making conflicting assumptions about the same system.</p>
<p>The team behind Devin wrote a whole piece called &quot;Don&#39;t Build Multi-Agents,&quot; and their core point is sharper than the cost argument. Every action an agent takes carries an <em>implicit decision</em>. When you split work across parallel subagents, each one makes its own implicit decisions, and those decisions quietly conflict. Their example is perfect: ask two subagents to build a &quot;Flappy Bird clone&quot; in parallel, and one builds a Super Mario background while the other builds a bird that moves nothing like Flappy Bird. Now your final agent has to reconcile two subtly incompatible worlds. Nobody was wrong on purpose. The context just couldn&#39;t be shared thoroughly enough for them to agree.</p>
<blockquote>
<p>Actions carry implicit decisions, and conflicting decisions carry bad results.</p>
</blockquote>
<p>That&#39;s why even Claude Code, when it spawns a subtask, usually only asks it to <em>answer a question</em> — never to go off and write code in parallel. The subagent investigates, reports back, and the main agent keeps making the actual decisions in one continuous line of context. It&#39;s a deliberately simple design, and the simplicity is the point.</p>
<h2>The default should be one continuous context</h2>
<p>Strip away the fashion and the reliable pattern underneath is almost boringly plain: a single agent, working in one continuous context, making decisions in sequence. It sees everything it did earlier because it never handed off. There&#39;s no telephone game, no reconciliation step, no second model guessing at the first one&#39;s intent.</p>
<p>Yes, you eventually hit a ceiling — a task so large the context window overflows. But the answer to that is rarely &quot;spawn a committee.&quot; It&#39;s better context management: summarizing completed phases, storing decisions in memory, spinning up a <em>fresh</em> context with a clean handoff when you truly need one. You reach for that when the single agent stops fitting, not before — and even then you&#39;re managing context, not orchestrating a crowd.</p>
<h2>The real lever isn&#39;t more agents</h2>
<p>Here&#39;s what actually moved the needle for me, and it wasn&#39;t a second agent. It was teaching the <em>one</em> agent my workflow properly and giving it the right tools. I wired in the MCP servers it needs — reading Jira, Confluence, docs, Postman — so it works with real context instead of guessing. And I invested the months it took to teach it how I actually work, step by step.</p>
<p>The payoff is that I can now start a fresh session and type &quot;take task 123 and do it as usual,&quot; and it knows what that means: assign the ticket to me, move it to In Progress, pull it into the sprint if it&#39;s sitting in the backlog, estimate it if it has no points, branch off an updated main by our convention, commit with human-readable messages, open the PR with a real description and the right people tagged, link it to the ticket, and watch the pipeline — fixing it if it goes red. One agent. One phrase. No orchestra.</p>
<p>That&#39;s a whole article of its own — <a href="https://ma-x.im/blog/agent-playbook-do-it-as-usual">how I taught one agent to &quot;do it as usual&quot;</a> — and it&#39;s the honest alternative to the swarm. The effort I <em>didn&#39;t</em> spend maintaining seven setups, I spent deepening one.</p>
<h2>The reframe</h2>
<p>The multi-agent orchestra is seductive because it looks like engineering. Boxes, arrows, roles, a system. But most of the time it&#39;s complexity cosplaying as capability — three times the token bill, a coordination protocol built on the flakiest possible substrate, and results that are <em>worse</em> because the context keeps fracturing between the players.</p>
<p>So my rule is simple: reach for the second agent only when you can name the work it&#39;s doing — work that is genuinely independent, genuinely parallel, and genuinely worth 15× the tokens. Research that fans out? Maybe. Almost everything else? One agent, one context, taught well. The sophistication was never in the number of agents. It was always in how much of your actual work you managed to hand off cleanly to a single one.</p>
<p>If you&#39;re running a multi-agent setup right now, do one honest audit: for each agent past the first, write down the independent, parallel task it owns and what it costs you. If you can&#39;t name that task without hand-waving, you don&#39;t have an architecture — you have a diagram. Tell me which agent you&#39;d delete first.</p>
]]></content:encoded>
      <pubDate>Thu, 06 Aug 2026 00:00:00 GMT</pubDate>
      <category>AI Agents</category>
      <category>Architecture</category>
      <category>The Agent Playbook</category>
    </item>
    <item>
      <title>Code-Splitting Is a Boundary Decision, Not a Bundle Trick</title>
      <link>https://ma-x.im/blog/react-playbook-code-splitting</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-code-splitting</guid>
      <description>Every performance guide tells you to lazy-load. Almost none tell you where to stop. Split on your file tree and you trade one big bundle for a hundred tiny chunks and a waterfall of spinners. The real question isn&apos;t how to split — it&apos;s finding the seams in the user&apos;s journey and cutting there.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-code-splitting/cover.png" alt="Code-Splitting Is a Boundary Decision, Not a Bundle Trick" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-command-palette">command palette article</a> closed by promising the last piece of this look-and-feel stretch, and it&#39;s the one that looks the most like a solved problem: code-splitting. Wrap a component in <code>React.lazy</code>, drop a <code>Suspense</code> boundary around it, watch the bundle shrink. Done, right?</p>
<p>Here&#39;s the thing that bugs me about most bundle-size advice. It&#39;s all about the <em>how</em> — <code>lazy</code>, dynamic <code>import()</code>, the router&#39;s lazy routes — and almost none of it is about the <em>where</em>. And the where is the entire problem. Splitting code is one line. Deciding what to cut, so the app loads fast without shattering into a hundred chunks you can&#39;t reason about, is an architecture decision. Get the how right and the where wrong, and you don&#39;t get a faster app. You get a slower one with more moving parts.</p>
<h2>A split is not free</h2>
<p>Start with what a split actually costs, because the mental model everyone skips is that lazy-loading isn&#39;t &quot;free performance.&quot; Every split you introduce is a boundary, and every boundary buys you three things you didn&#39;t have before: a separate network request, a loading state you now have to design, and the risk of a waterfall if that request can&#39;t start until another one finishes.</p>
<blockquote>
<p>A code-split isn&#39;t a size optimization. It&#39;s a boundary you&#39;re adding to the app — and boundaries have a cost on both sides.</p>
</blockquote>
<p>So the real question stops being &quot;can I split this?&quot; (you almost always can) and becomes &quot;is there a real seam here worth paying a boundary for?&quot; That reframe is the whole article.</p>
<h2>The default that quietly backfires</h2>
<p>The advice you&#39;ll read first is &quot;split at the route level,&quot; and honestly, that advice is correct. Route boundaries are the best seams in the entire app: the user is already waiting for a navigation, they don&#39;t expect the next screen to be instant, and each route pulls in a genuinely different slice of the app. With a type-safe router like the one from the <a href="https://ma-x.im/blog/react-playbook-tanstack-router">routing article</a>, lazy routes are the natural unit:</p>
<pre><code class="language-tsx">// each route is its own chunk — the user is already navigating, a boundary here is invisible
export const Route = createFileRoute(&#39;/reports&#39;)({
  component: lazyRouteComponent(() =&gt; import(&#39;./ReportsPage&#39;)),
});
</code></pre>
<p>Where it backfires is the next step people take. Route-level splitting works, so they conclude &quot;more splitting is more better&quot; and start wrapping individual components — a <code>UserAvatar</code> here, a <code>Badge</code> there — in <code>lazy</code>. Now every one of those is a network round-trip and a spinner. You&#39;ve turned a single fast download into a cascade of tiny requests, each with its own loading flicker, and the page assembles itself in front of the user like a slideshow. That&#39;s not a faster app. That&#39;s the same bytes, delivered worse.</p>
<h2>Where the seams actually are</h2>
<p>So if &quot;everywhere&quot; is wrong and &quot;routes&quot; is the safe default, what else genuinely earns a split? Three shapes, and they all have the same property: the code is <em>heavy</em> and <em>not needed for the first meaningful paint</em>.</p>
<p><strong>Heavy, rarely-used features.</strong> A rich text editor, a video player, a PDF viewer, the <a href="https://ma-x.im/blog/react-playbook-chart-libraries">chart library</a> you only render on the analytics tab. These can each be hundreds of kilobytes, and most sessions never touch them. Splitting here is pure win — the cost of the boundary is paid only by the users who actually open the feature.</p>
<pre><code class="language-tsx">// the editor is 300kb of the bundle and only the 5% who click &quot;edit&quot; ever need it
const RichTextEditor = lazy(() =&gt; import(&#39;./RichTextEditor&#39;));

function Note({ isEditing }: { isEditing: boolean }) {
  if (!isEditing) return &lt;NotePreview /&gt;;
  return (
    &lt;Suspense fallback={&lt;EditorSkeleton /&gt;}&gt;
      &lt;RichTextEditor /&gt;
    &lt;/Suspense&gt;
  );
}
</code></pre>
<p><strong>Behind an interaction.</strong> Anything that only appears after a click — a settings modal, a command palette, an export dialog. The boundary hides behind the click the user already made, so the tiny load feels like part of the action, not a delay.</p>
<p><strong>Below the fold, and truly optional.</strong> The stuff a user might scroll to but often won&#39;t.</p>
<p>Notice what&#39;s <em>not</em> on this list: anything needed for the first render, anything small, and anything that&#39;s split purely because it lives in its own folder. A 4kb component doesn&#39;t deserve a network request. The download of that request costs more than the bytes you saved.</p>
<h2>The waterfall you&#39;ll create by accident</h2>
<p>The nastiest failure mode isn&#39;t too many chunks — it&#39;s chunks that can&#39;t load in parallel. This is the same waterfall problem the <a href="https://ma-x.im/blog/react-playbook-data-fetching">data-fetching article</a> warned about, just moved from data to code. A lazy component that, once loaded, immediately lazy-loads its own child, which lazy-loads <em>its</em> child — the browser can&#39;t start request two until request one lands. You&#39;ve serialized what should have been parallel.</p>
<p>The fix is intent-based prefetching: start fetching the chunk when the user <em>signals</em> they&#39;re heading somewhere, not when they arrive. Hovering a link, focusing a button, opening the parent — all of these are the browser&#39;s chance to fetch ahead:</p>
<pre><code class="language-tsx">// kick off the chunk on hover, so by the time they click it&#39;s already in cache
&lt;Link
  to=&quot;/reports&quot;
  onMouseEnter={() =&gt; import(&#39;./ReportsPage&#39;)}
&gt;
  Reports
&lt;/Link&gt;
</code></pre>
<p>Do this and the boundary all but disappears: the request overlaps with the user&#39;s own reaction time instead of stacking behind another request. Good routers do a version of this for you. The point is that <em>when</em> a chunk starts loading matters as much as whether you split it.</p>
<h2>The chunk you can&#39;t reason about</h2>
<p>Here&#39;s where splitting collides with the rest of the Playbook. Remember the <a href="https://ma-x.im/blog/react-playbook-barrel-files">barrel-file article</a> — the <code>index.ts</code> that re-exports everything in a folder? Lazy-load a single component <em>through</em> a barrel, and you don&#39;t get that component. You get the whole barrel: every sibling it re-exports, dragged into the chunk because the import graph can&#39;t tell them apart. Your carefully-placed <code>lazy</code> boundary quietly pulls in half the folder, and the chunk you thought was small isn&#39;t.</p>
<pre><code class="language-ts">// looks like one small component...
const Chart = lazy(() =&gt; import(&#39;./widgets&#39;).then((m) =&gt; ({ default: m.Chart })));
// ...but ./widgets/index.ts re-exports Chart, Table, Map, Calendar — all now in this chunk
</code></pre>
<p>This is why I keep coming back to the idea that these topics aren&#39;t separate. A split is only as clean as the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">module boundaries</a> underneath it. If your imports reach sideways through barrels and shared grab-bags, your chunks will be blurry no matter how many <code>lazy</code> calls you sprinkle in. Import the file directly, and the split is exactly as big as you think it is.</p>
<h2>Split where it hurts, not where the folders are</h2>
<p>The <a href="https://ma-x.im/blog/react-playbook-rerenders-and-architecture">re-renders article</a> made an argument I want to borrow directly: optimize where it actually hurts, and don&#39;t let the optimization wreck the structure. Code-splitting is the same discipline one layer down. The temptation is to split on the shape of your file tree — one chunk per feature folder, because it&#39;s tidy. But the file tree is your convenience, not the user&#39;s journey. The seams that matter are the ones in <em>their</em> experience: the route they navigate to, the heavy tool they occasionally open, the dialog behind a click.</p>
<p>Before adding a <code>lazy</code>, I ask two questions. Is this code actually heavy enough that its bytes show up in the load? And is there a real moment in the user&#39;s flow where paying for a separate request is invisible? If both answers aren&#39;t yes, the split is making the app more complex for a number that doesn&#39;t move.</p>
<h2>The reframe</h2>
<p>Code-splitting reads like a bundle trick and behaves like an architecture decision — which makes it a fitting end to this look-and-feel cluster, because so did theming, and so did the command palette. Each one looks like polish and turns out to be a boundary in disguise. The visible artifact is trivial: a smaller bundle, a color toggle, a search modal. The value, every time, is in where you draw the line and whether the rest of the structure lets that line stay clean.</p>
<p>So the honest heuristic isn&#39;t &quot;split more.&quot; It&#39;s split <em>rarely and deliberately</em>, at the seams the user can feel, over module boundaries clean enough that a chunk means what its name says. Everything else is just trading one problem you can see — a big bundle — for a dozen you can&#39;t.</p>
<p>If you&#39;ve got a build with more than a handful of chunks, pull up your bundle analyzer and find the smallest lazy chunk you have. If it&#39;s a few kilobytes, that&#39;s a boundary you&#39;re paying for and getting nothing back — tell me what it is, because I&#39;d bet it snuck in on the &quot;more splitting is better&quot; reflex, and it&#39;s the first thing I&#39;d delete.</p>
]]></content:encoded>
      <pubDate>Tue, 04 Aug 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>Performance</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The Command Palette Is an Architecture, Not a Widget</title>
      <link>https://ma-x.im/blog/react-playbook-command-palette</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-command-palette</guid>
      <description>Cmd+K looks like a fuzzy-search modal you bolt on at the end. Build it that way and it immediately drifts out of sync with the buttons and menus it duplicates. The real thing is a command registry: one typed source of truth for everything the app can do, feeding the palette, the shortcuts, and the menus at once.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-command-palette/cover.png" alt="The Command Palette Is an Architecture, Not a Widget" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-theming-dark-mode">theming article</a> ended on a move from how an app looks to how power users drive it. The command palette — the <code>Cmd+K</code> menu that fuzzy-searches everything you can do — is the poster child for a piece of UI that people build as a widget and should build as an architecture. The difference shows up fast, and it&#39;s the difference between a palette that stays useful and one that quietly lies about what the app can do.</p>
<p>Here&#39;s the trap. You install a nice palette component, drop a modal in, and fill it with a hardcoded list of actions: &quot;New file,&quot; &quot;Toggle dark mode,&quot; &quot;Go to settings.&quot; It demos beautifully. Then someone adds a feature with its own toolbar button, forgets to also add it to the palette, and now the palette is missing things. Someone renames an action in a menu but not in the palette. Six months in, the palette is a stale, hand-maintained duplicate of logic that already exists elsewhere in the app. The modal was never the hard part. The hard part is that the palette needs to know everything the app can do — and so do the menus, and the keyboard shortcuts, and the toolbar.</p>
<h2>A command is a first-class object</h2>
<p>The reframe is to stop thinking about a search modal and start thinking about <em>commands</em> as data. A command is a real object: an id, a title, some keywords, the thing it does, and optionally whether it&#39;s currently allowed and what shortcut triggers it.</p>
<pre><code class="language-ts">interface Command {
  id: string;
  title: string;
  keywords?: string[];        // for fuzzy search: &quot;theme&quot;, &quot;appearance&quot;...
  group?: string;             // &quot;File&quot;, &quot;View&quot;, &quot;Navigation&quot;
  shortcut?: string;          // &quot;mod+k&quot;
  isEnabled?: () =&gt; boolean;  // context-aware availability
  run: () =&gt; void;            // the actual behavior
}
</code></pre>
<p>Once a command is data, the interesting shift happens: the palette stops <em>owning</em> actions and starts <em>rendering</em> them. This is the same instinct as <a href="https://ma-x.im/blog/react-playbook-headless-components">headless components</a> — separate the behavior (what the command does) from the presentation (how it&#39;s listed) — and the same instinct as the <a href="https://ma-x.im/blog/react-playbook-analytics-as-a-layer">typed event catalog</a> — one declared source of truth instead of scattered strings.</p>
<h2>The registry is the single source of truth</h2>
<p>The center of the whole design is a registry: one place every command is registered, and every surface that needs to know &quot;what can the app do?&quot; reads from it.</p>
<pre><code class="language-ts">class CommandRegistry {
  private commands = new Map&lt;string, Command&gt;();

  register(command: Command): () =&gt; void {
    this.commands.set(command.id, command);
    return () =&gt; this.commands.delete(command.id); // features clean up their own
  }

  all(): Command[] {
    return [...this.commands.values()].filter((c) =&gt; c.isEnabled?.() ?? true);
  }

  run(id: string) {
    this.commands.get(id)?.run();
  }
}

export const commands = new CommandRegistry();
</code></pre>
<p>Now features <em>contribute</em> commands instead of the palette knowing about features:</p>
<pre><code class="language-ts">// in the editor feature — it announces what it can do, and to whom it doesn&#39;t care
commands.register({
  id: &#39;file.new&#39;,
  title: &#39;New File&#39;,
  group: &#39;File&#39;,
  shortcut: &#39;mod+n&#39;,
  run: () =&gt; editor.createFile(),
});
</code></pre>
<p>Notice the direction of the dependency. The editor feature depends on the registry (a stable, shared thing); the palette depends on the registry; the two features never depend on each other. That&#39;s the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">dependencies-point-the-right-way</a> rule holding: everything points at a shared abstraction, nothing reaches sideways. Add a feature, it registers its commands, and the palette shows them with zero changes to the palette. Remove the feature, its cleanup runs, the commands vanish. Nothing drifts because nothing was duplicated.</p>
<h2>One dispatcher, not scattered key handlers</h2>
<p>The keyboard shortcuts fall out of the same registry, and this is where you avoid another mess. The naive approach sprinkles <code>onKeyDown</code> handlers across a dozen components, each checking for its own combo — the same scatter problem the <a href="https://ma-x.im/blog/react-playbook-event-bus">event bus</a> and analytics articles warned about, just with keystrokes. Instead, one global listener resolves keys against the registry:</p>
<pre><code class="language-ts">useEffect(() =&gt; {
  const onKeyDown = (e: KeyboardEvent) =&gt; {
    const combo = toCombo(e); // &quot;mod+n&quot;, &quot;mod+k&quot;, etc.
    const command = commands.all().find((c) =&gt; c.shortcut === combo);
    if (command) {
      e.preventDefault();
      command.run();
    }
  };
  window.addEventListener(&#39;keydown&#39;, onKeyDown);
  return () =&gt; window.removeEventListener(&#39;keydown&#39;, onKeyDown);
}, []);
</code></pre>
<p>One place owns the mapping from keys to commands. Shortcut conflicts become detectable (two commands claiming <code>mod+k</code> is now a thing you can check for, instead of a mystery). And because <code>isEnabled</code> gates <code>all()</code>, a shortcut simply does nothing when its command isn&#39;t valid in the current context — no scattered guard clauses.</p>
<h2>The palette is just a view over the registry</h2>
<p>And now the part everyone thought was the whole feature is the easy part. The palette is a thin view: read the registry, fuzzy-match the query against titles and keywords, render the list, run the chosen command.</p>
<pre><code class="language-tsx">function CommandPalette() {
  const [query, setQuery] = useState(&#39;&#39;);
  const results = useMemo(
    () =&gt; fuzzyFilter(commands.all(), query), // search titles + keywords
    [query],
  );
  return (
    &lt;Dialog&gt;
      &lt;input value={query} onChange={(e) =&gt; setQuery(e.target.value)} placeholder=&quot;Type a command…&quot; /&gt;
      &lt;ul&gt;
        {results.map((c) =&gt; (
          &lt;li key={c.id} onClick={() =&gt; { commands.run(c.id); }}&gt;
            {c.title}
            {c.shortcut &amp;&amp; &lt;kbd&gt;{c.shortcut}&lt;/kbd&gt;}
          &lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/Dialog&gt;
  );
}
</code></pre>
<p>Because commands are data, you get things for free that would be painful to hand-maintain: search across everything, a &quot;keyboard shortcuts&quot; help screen (just render the registry grouped by <code>group</code>), and an honest answer to &quot;what can I do here?&quot; that&#39;s always current because it&#39;s generated, never authored twice.</p>
<h2>The reframe</h2>
<p>The command palette is a systems problem wearing a UI costume — the same shape as theming. The visible artifact (a search modal, a color toggle) is trivial; the value is entirely in the architecture behind it (a command registry, a token system). Build either one as a widget and it rots, because you end up maintaining the same truth in several places and they drift apart. Build it as a single source of truth that many surfaces read from, and the palette, the menus, and the shortcuts can never disagree — because there&#39;s only one thing for them to agree with.</p>
<p>That&#39;s the through-line of this whole look-and-feel stretch: the parts of an app that seem like polish are usually boundaries in disguise, and the teams that treat them as boundaries end up with UI that scales instead of UI that quietly lies. Next the series turns to the last piece of this cluster — code-splitting, and the deceptively hard question of what to actually cut so the app loads fast without fracturing into a thousand lazy chunks you can&#39;t reason about.</p>
<p>If your app has a command palette, open it and then open your main menu side by side. If they don&#39;t list the same capabilities, you&#39;ve got two sources of truth drifting apart — tell me how far they&#39;ve diverged, because that gap is the whole argument for a registry.</p>
]]></content:encoded>
      <pubDate>Sun, 02 Aug 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Theming and Dark Mode Without the Flash: A Systems Problem in CSS Clothing</title>
      <link>https://ma-x.im/blog/react-playbook-theming-dark-mode</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-theming-dark-mode</guid>
      <description>Dark mode looks like a class you toggle. Then you meet the flash of the wrong theme on load, colors that drift because every component hardcodes its own, and a theme context that re-renders the whole app. Theming done right is tokens, one switch point, and a boundary — and it barely touches React at all.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-theming-dark-mode/cover.png" alt="Theming and Dark Mode Without the Flash: A Systems Problem in CSS Clothing" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-rerenders-and-architecture">last article</a> argued that a lot of React &quot;optimization&quot; is really architecture damage in disguise. Theming is a great place to keep that lens on, because it&#39;s the feature that looks the most like a trivial CSS toggle and is actually a small systems-design problem — one where the naive version fails in three specific, visible ways.</p>
<p>You start simple. A <code>dark</code> class on the body, some overrides, a bit of React state to flip it. Ship it. Then the reports come in. The page flashes white before going dark on every load. A few components didn&#39;t get the memo and stayed light. And your theme toggle mysteriously re-renders half the app. None of these is a CSS bug exactly — they&#39;re all consequences of putting theming in the wrong layer.</p>
<h2>Tokens are the foundation, not the paint</h2>
<p>The first mistake is letting every component decide its own colors. <code>color: #1a1a1a</code> here, <code>background: #fff</code> there, a slightly different grey in a third place. Now &quot;dark mode&quot; means hunting down every hardcoded color in the codebase, and &quot;the greys drifted&quot; is inevitable because there was never one grey to begin with.</p>
<p>The fix is design tokens expressed as CSS custom properties: one place that owns the actual color decisions, and components that only ever refer to them <em>semantically</em>.</p>
<pre><code class="language-css">:root {
  /* primitive palette — raw values, referenced by nobody directly */
  --gray-50: #f9fafb;
  --gray-900: #111827;
  --green-500: #22c55e;

  /* semantic tokens — what components actually use */
  --color-surface: var(--gray-50);
  --color-text: var(--gray-900);
  --color-accent: var(--green-500);
}

[data-theme=&#39;dark&#39;] {
  --color-surface: var(--gray-900);
  --color-text: var(--gray-50);
  --color-accent: var(--green-500); /* accent survives the theme */
}
</code></pre>
<p>The two-layer split matters more than it looks. Primitives (<code>--gray-900</code>) are the paint box; semantic tokens (<code>--color-surface</code>) are the meaning. Components consume only the semantic layer — <code>background: var(--color-surface)</code> — so they never know or care what actual color that is in a given theme. Switching themes is now just swapping what the semantic tokens point to. And critically, a component that says <code>var(--color-surface)</code> cannot drift, because there&#39;s exactly one definition of surface per theme. This is the same &quot;one source of truth, referenced by many&quot; idea from <a href="https://ma-x.im/blog/react-playbook-data-normalization">data normalization</a>, applied to color.</p>
<h2>The switch doesn&#39;t belong in React state</h2>
<p>Here&#39;s the part that surprises people, and it ties straight back to the last article. Flipping the theme should not be a React re-render at all. You change one attribute on the root element — <code>data-theme=&quot;dark&quot;</code> — and the CSS cascade does the entire rest of the work, instantly, for every element on the page, with zero components re-rendering.</p>
<pre><code class="language-ts">function applyTheme(theme: &#39;light&#39; | &#39;dark&#39;) {
  document.documentElement.setAttribute(&#39;data-theme&#39;, theme);
  localStorage.setItem(&#39;theme&#39;, theme); // persist, as a side effect
}
</code></pre>
<p>Contrast that with the common mistake: putting the theme in a React context that wraps the whole app, so that toggling it re-renders every consumer to pass down new colors. That&#39;s taking a job CSS does for free — cascading a variable change to the whole tree — and turning it into a render storm, the exact anti-pattern from the <a href="https://ma-x.im/blog/react-playbook-rerenders-and-architecture">re-render article</a>. Let CSS variables do what they&#39;re built for. You can still keep a <em>small</em> piece of React state for the toggle&#39;s UI (which icon to show), but the actual theming mechanism is a DOM attribute and a cascade, not a re-render.</p>
<h2>The flash is a rendering-order problem</h2>
<p>Now the ugliest one: the flash of the wrong theme (FOUC). On a statically exported, server-rendered site — which is <a href="https://ma-x.im/blog/nextjs-ssg-github-pages">exactly this stack</a> — the server has no idea what theme the user picked. It renders the default, ships the HTML, the browser paints it, and only <em>then</em> does your React code boot, read localStorage, and correct the theme. That gap between first paint and correction is the flash, and no amount of React can remove it, because React runs too late by definition.</p>
<p>The fix has to happen <em>before</em> first paint, which means a tiny blocking script in the document <code>&lt;head&gt;</code>, before any content renders:</p>
<pre><code class="language-tsx">// injected into &lt;head&gt;, runs synchronously before the body paints
const themeScript = `
  (function () {
    try {
      var stored = localStorage.getItem(&#39;theme&#39;);
      var theme = stored || (matchMedia(&#39;(prefers-color-scheme: dark)&#39;).matches ? &#39;dark&#39; : &#39;light&#39;);
      document.documentElement.setAttribute(&#39;data-theme&#39;, theme);
    } catch (e) {}
  })();
`;

// in the App Router root layout
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    &lt;html lang=&quot;en&quot; suppressHydrationWarning&gt;
      &lt;head&gt;
        &lt;script dangerouslySetInnerHTML={{ __html: themeScript }} /&gt;
      &lt;/head&gt;
      &lt;body&gt;{children}&lt;/body&gt;
    &lt;/html&gt;
  );
}
</code></pre>
<p>It feels a little dirty to hand-inject a script, but it&#39;s the correct tool: it sets <code>data-theme</code> on <code>&lt;html&gt;</code> synchronously, before the browser paints a single pixel, so the very first frame is already the right theme. This is also the disciplined version of the <a href="https://ma-x.im/blog/react-playbook-local-storage-and-state">localStorage lesson</a> — read persisted state in the right place at the right time, not during React&#39;s render where it causes hydration mismatches. Note <code>suppressHydrationWarning</code> on <code>&lt;html&gt;</code>: the attribute the script sets won&#39;t match the server&#39;s markup, and that&#39;s expected, so we tell React not to complain about this one specific, intentional divergence.</p>
<h2>Respect the system, but let people override</h2>
<p>A complete theming system has three inputs, in priority order: the user&#39;s explicit choice, then the OS preference, then a default. <code>prefers-color-scheme</code> gives you the OS signal, and <code>matchMedia</code> lets you react to it changing live (someone flips their laptop to dark at sunset):</p>
<pre><code class="language-ts">const media = matchMedia(&#39;(prefers-color-scheme: dark)&#39;);
media.addEventListener(&#39;change&#39;, (e) =&gt; {
  // only follow the system if the user hasn&#39;t set an explicit preference
  if (!localStorage.getItem(&#39;theme&#39;)) {
    applyTheme(e.matches ? &#39;dark&#39; : &#39;light&#39;);
  }
});
</code></pre>
<p>The rule that keeps this sane: an explicit user choice always wins and always persists; the system preference is only the fallback when the user hasn&#39;t decided. Conflating those two — following the OS even after the user picked something — is how you get the maddening bug where a user&#39;s chosen theme keeps getting overridden.</p>
<h2>The reframe</h2>
<p>Theming teaches the same lesson this stretch of the series keeps circling: the thing that looks like a surface detail is usually a boundary decision. Where do color decisions live? (In tokens, once.) What actually performs the switch? (A DOM attribute and the cascade, not React.) When does the theme get resolved? (Before first paint, not after hydration.) Get those three boundaries right and dark mode is almost anticlimactic — a few dozen lines, no flash, no drift, no render storm. Get them wrong and you&#39;re chasing flashes and mismatched components forever, patching symptoms of a structure that put theming in the wrong place.</p>
<p>Next, the series stays on architectural UI but moves from how the app <em>looks</em> to how power users <em>drive</em> it. The command palette — that <code>Cmd+K</code> menu — looks like a search modal and is really a command registry: one source of truth for everything the app can do, feeding the palette, the menus, and the keyboard shortcuts at once. That&#39;s where we go.</p>
<p>If your app has dark mode, do the two-second test: hard-refresh it a dozen times on a throttled connection and watch the first frame. If you catch a flash of the wrong theme, your theming is resolving too late — and I&#39;d love to hear whether it was the flash, the drift, or the render storm that got you first.</p>
]]></content:encoded>
      <pubDate>Fri, 31 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>CSS</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Re-Renders Are React&apos;s Superpower, Not a Disease to Cure</title>
      <link>https://ma-x.im/blog/react-playbook-rerenders-and-architecture</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-rerenders-and-architecture</guid>
      <description>There&apos;s a particular panic I keep running into: a component re-rendered, someone saw it in the profiler, and now they&apos;re tearing the architecture apart to stop it. But rendering isn&apos;t React&apos;s flaw — it&apos;s the whole idea. The word you want is optimize, not minimize, and the difference decides whether your app stays maintainable.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-rerenders-and-architecture/cover.png" alt="Re-Renders Are React&apos;s Superpower, Not a Disease to Cure" /></p>
<p>I was going to move the series straight into how an app looks and feels. But something has been bugging me for a while, and it&#39;s architectural enough that skipping it would be dishonest. So this is a short detour, and it&#39;s about a panic I keep seeing: someone opens the React Profiler, watches a component re-render when they didn&#39;t expect it to, and immediately starts demolishing their code to make the re-render stop. Props get removed. Components get split in strange places. <code>memo</code> gets sprayed over everything. All to drive a number down.</p>
<p>Here&#39;s the thing I want to say plainly, because almost nobody says it: <strong>re-rendering is not React&#39;s weakness. It&#39;s React&#39;s superpower.</strong> And a lot of the &quot;optimizations&quot; people perform to avoid re-renders are quietly worse for the codebase than the re-renders ever were.</p>
<h2>Rendering <em>is</em> the model</h2>
<p>Step back and remember what React actually is. Your UI is a function of state. State changes, React re-runs the function, produces a new description of the UI, and reconciles it against the last one. That re-run — the render — is not an accident or an overhead bolted onto React. It <em>is</em> React. The entire programming model, the reason React felt like a relief after manual DOM manipulation, is that you get to describe &quot;what the UI looks like for this state&quot; and never wire up the updates yourself.</p>
<p>Compare it to a signals-based library — Solid, or the fine-grained-reactivity family. There the model is different by design:</p>
<pre><code class="language-tsx">// Solid-style: fine-grained. The component function runs ONCE.
function Counter() {
  const [count, setCount] = createSignal(0);
  // this text node subscribes to count directly; only IT updates
  return &lt;button onClick={() =&gt; setCount(count() + 1)}&gt;{count()}&lt;/button&gt;;
}
</code></pre>
<pre><code class="language-tsx">// React: the component function re-runs on every state change.
function Counter() {
  const [count, setCount] = useState(0);
  // the whole function runs again; React diffs the result
  return &lt;button onClick={() =&gt; setCount(count + 1)}&gt;{count}&lt;/button&gt;;
}
</code></pre>
<p>Both are legitimate architectures. Signals update surgically and never re-run the component; React re-runs the component and diffs. But notice: the React version&#39;s &quot;re-run everything and figure out what changed&quot; is the very thing that makes React&#39;s mental model so simple. You don&#39;t track dependencies by hand. You don&#39;t wire subscriptions. You describe UI-as-a-function-of-state and let the render do the work. Treating that render as a defect is like buying a bird and being annoyed that it keeps flying. The flying is the point.</p>
<blockquote>
<p>A re-render is not React doing something wrong. It&#39;s React doing the one thing it exists to do. The goal was never zero renders — it was the freedom to stop thinking about updates.</p>
</blockquote>
<h2>So what&#39;s actually bad, then?</h2>
<p>I&#39;m not saying render count never matters. It does — but the problem is narrower than the panic suggests. A bad situation is a <em>render storm at scale</em>: every mousemove re-rendering every component on the page, a giant list re-rendering all thousand rows because one row&#39;s checkbox changed, an expensive tree re-running on every keystroke in an unrelated input. That&#39;s real, and it&#39;s worth fixing.</p>
<p>But a component re-rendering because its parent re-rendered, doing a bit of cheap work and producing the same DOM? That&#39;s usually nothing. React is <em>fast</em> at this. The reconciler is built for it. Most of the re-renders people agonize over cost less than the <code>useMemo</code> they add to prevent them. The panic treats every render as a cost when the real question is only ever &quot;is this render actually expensive, and does it actually happen often enough to matter?&quot;</p>
<p>Which is why the word matters so much. People say they want to <em>minimize</em> re-renders. The word you actually want is <strong>optimize</strong>. Minimize is a religion — zero renders at any cost. Optimize means find the balance: spend effort on the renders that hurt, and leave the cheap ones alone. Minimizing is something you do to a number. Optimizing is something you do to a system, with judgment about where the cost really is.</p>
<h2>The trade nobody prices: architecture for render count</h2>
<p>Here&#39;s where this becomes an architecture article and not a performance tip, because the real damage isn&#39;t the wasted effort — it&#39;s what people <em>break</em> to save a render. Let me give you a few shapes I keep seeing.</p>
<p><strong>Contorting to remove a &quot;guilty&quot; prop.</strong> A component re-renders because a prop changes. So instead of leaving the honest data flow in place, someone restructures so the component no longer <em>receives</em> that prop — it reaches into a store or context to grab the value itself, so the parent can&#39;t &quot;trigger&quot; it. The render is gone. But now the component&#39;s real dependency is hidden. It looks independent and isn&#39;t. You&#39;ve taken a visible prop — a traceable edge in your dependency graph — and turned it into an invisible reach-out, the exact anti-pattern the <a href="https://ma-x.im/blog/react-playbook-composition-and-di">composition and DI article</a> argued against. You optimized a render and de-optimized your ability to understand the code.</p>
<p><strong>Memo everywhere as a reflex.</strong> Every component wrapped in <code>memo</code>, every value in <code>useMemo</code>, every function in <code>useCallback</code> — not because anything was measured, but because renders are scary now.</p>
<pre><code class="language-tsx">// this &quot;optimization&quot; costs more than it saves
const Row = memo(function Row({ item, onSelect }: RowProps) {
  const handle = useCallback(() =&gt; onSelect(item.id), [onSelect, item.id]);
  const style = useMemo(() =&gt; ({ paddingLeft: item.depth * 8 }), [item.depth]);
  return &lt;div style={style} onClick={handle}&gt;{item.label}&lt;/div&gt;;
});
</code></pre>
<p>For a trivial row, the memoization machinery — the comparison, the dependency arrays, the mental overhead — is more expensive than just rendering the div. And every one of those dependency arrays is now a thing that can go subtly wrong and a thing the next person has to maintain. You bought a micro-optimization with a permanent tax on changeability.</p>
<p><strong>Splitting on render boundaries instead of responsibility boundaries.</strong> This is the worst one, because it looks like good decomposition. To isolate a re-render, someone fractures a cohesive component into oddly-shaped pieces — not because those pieces are meaningful units, but because the split happens to stop a render from propagating. Now a single logical concern is smeared across three components that only make sense together, which is precisely the <a href="https://ma-x.im/blog/smeared-component">smeared-component</a> problem wearing a performance badge. You decomposed for the profiler, not for the reader.</p>
<p>In each case the pattern is identical: a local win on render count, paid for with a structural loss that the profiler will never show you.</p>
<h2>The part everyone forgets: the component has to be <em>maintained</em></h2>
<p>The fixation on renders has tunnel vision. It optimizes for one moment — the render — and forgets that this component has a whole life ahead of it. It&#39;s going to grow. It&#39;s going to be reused somewhere the &quot;clever&quot; structure doesn&#39;t fit. Someone&#39;s going to need to change it in six months. The &quot;ideal&quot; low-render component is very often a maintenance liability, because its shape was dictated by a performance metric instead of by what the thing actually <em>is</em>.</p>
<p>This is where the balance genuinely lives, and it&#39;s context-dependent. Is this a marketing landing page? Then honestly, do nothing — render &quot;waste&quot; on a page that mounts once and barely updates is a non-problem, and every hour spent memoizing it is wasted. But a large, high-load, long-lived application — the kind with a real <a href="https://ma-x.im/blog/react-playbook-container-presentational">model / view-model / view separation</a>, data fetching, aggregation, and preparation layers — <em>that&#39;s</em> where the trade-off between architecture and render performance is a real engineering decision. And the answer there is deliberate: you optimize the hot paths you&#39;ve measured, and you refuse to sacrifice the structure that keeps the app maintainable for renders that don&#39;t hurt.</p>
<h2>The screw you can&#39;t reach</h2>
<p>I was reassembling my daughter&#39;s computer recently and it made this click for me. Ten screws on the back, off comes the panel, and everything inside is modular — the drive, the memory, the fan all lift out independently. Fifteen minutes to clean it. I remembered the old laptops I used to take apart years ago, where you had to remove things in one exact sequence, and if you wanted to reach one component you first had to dismantle four unrelated ones sitting on top of it. Miss the order and you&#39;re stuck.</p>
<p>Bad render-driven architecture is the old laptop. To save a render you bury a &quot;screw&quot; — you tuck a dependency somewhere awkward, you weld two concerns together, you hide a prop — and later, when you need to change that part, you can&#39;t get to it without taking apart everything piled on top. Good architecture is my daughter&#39;s computer: parts have obvious places, and when you want to move, replace, or fix one, you just can. The whole reason we care about structure is that day <em>later</em>, when requirements change and you need to reach in cleanly.</p>
<p>That serviceability is worth far more than a render count, and it&#39;s exactly what people trade away when the profiler scares them.</p>
<h2>Where I land</h2>
<p>So my actual rule is unglamorous. Treat re-rendering as the normal, healthy heartbeat of a React app, not a symptom. Measure before you optimize — find the renders that are genuinely expensive and genuinely frequent, and fix <em>those</em>, with the smallest intervention that works. And never, ever trade the reachability of your architecture for a number in a profiler, because one of those you can feel every day you maintain the code, and the other you mostly imagined.</p>
<p>Renders are React&#39;s superpower. Point it where it helps, tune it where it hurts, and stop apologizing for the thing that makes the whole model work.</p>
<p>With that off my chest, the series gets back on track — into how an app <em>looks and feels</em> at an architectural level, starting with the one that looks like a CSS detail and is really a systems problem: theming and dark mode, done so it doesn&#39;t flash on load, doesn&#39;t drift across components, and doesn&#39;t fight server rendering. That&#39;s next.</p>
<p>If you&#39;ve got a component in your app that was reshaped purely to cut a re-render, go look at it now and ask an honest question: was the render actually expensive, or did you just make the code harder to change to satisfy a profiler? Tell me what you find — I suspect the ratio surprises people.</p>
]]></content:encoded>
      <pubDate>Wed, 29 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>Performance</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Analytics Is Architecture: Stop Sprinkling track() Everywhere</title>
      <link>https://ma-x.im/blog/react-playbook-analytics-as-a-layer</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-analytics-as-a-layer</guid>
      <description>Most analytics code is a scatter of track(&apos;button_click&apos;) calls with drifting names, mystery payloads, and three vendor SDKs called inline. Analytics is a cross-cutting concern, and it belongs in a layer with a typed event catalog — not smeared through your components. Here&apos;s how I structure it.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-analytics-as-a-layer/cover.png" alt="Analytics Is Architecture: Stop Sprinkling track() Everywhere" /></p>
<p>This cluster has been about how the parts of an app communicate — <a href="https://ma-x.im/blog/react-playbook-event-bus">event buses</a> for ambient signals, <a href="https://ma-x.im/blog/react-playbook-feature-flags">feature flags</a> for toggling behavior. Both articles kept circling the same lesson: cross-cutting concerns want a typed contract and a single home, not a scatter of stringly-typed calls. Analytics is the purest example of that lesson, and the one almost everyone gets wrong first — myself included, years ago.</p>
<p>Here&#39;s what analytics looks like in most codebases I&#39;ve opened. A <code>track(&#39;button_click&#39;)</code> in one component. An <code>analytics.logEvent(&#39;Button Clicked&#39;)</code> in another. A raw <code>window.gtag(&#39;event&#39;, &#39;cta&#39;)</code> inline in a third because someone was in a hurry. Event names that mean the same thing spelled three ways. Payloads where one place sends <code>userId</code> and another sends <code>user_id</code> and a third helpfully includes the user&#39;s <em>email</em> into a third-party vendor. It works, in the sense that events show up somewhere. But it&#39;s un-auditable, un-typed, vendor-locked, and a privacy incident waiting to happen.</p>
<p>That&#39;s not a tracking problem. That&#39;s an architecture problem.</p>
<h2>Analytics is a cross-cutting concern</h2>
<p>Analytics has the same shape as logging, i18n, and error reporting: it touches almost every part of the app, but it <em>belongs</em> to none of them. And the <a href="https://ma-x.im/blog/smeared-component">smeared-component article</a> named exactly this failure mode — logic that should live in one layer instead gets spread thin across the whole UI until you can&#39;t change it in one place. A <code>track()</code> call baked into a button&#39;s onClick is analytics logic living in the wrong layer. The button&#39;s job is to be a button. Knowing that clicking it should emit a &quot;checkout started&quot; event to three vendors is not the button&#39;s concern.</p>
<blockquote>
<p>If you can&#39;t answer &quot;what does this app track, and where does that data go?&quot; by opening one folder, your analytics isn&#39;t a layer — it&#39;s a rash.</p>
</blockquote>
<p>The fix is the same as every other cross-cutting concern in this series: pull it into a dedicated layer with a clear, typed boundary, and let the rest of the app talk to that boundary instead of to a vendor SDK.</p>
<h2>A typed event catalog</h2>
<p>Step one is to stop passing strings and start declaring events, once, with typed payloads. This is the same move that tamed the <a href="https://ma-x.im/blog/react-playbook-event-bus">event bus</a>: a catalog the compiler knows about.</p>
<pre><code class="language-ts">// the single source of truth for everything the app can track
type AnalyticsEvents = {
  &#39;checkout_started&#39;: { cartValueCents: number; itemCount: number };
  &#39;checkout_completed&#39;: { orderId: string; amountCents: number };
  &#39;search_performed&#39;: { query: string; resultCount: number };
  &#39;signup_completed&#39;: { plan: &#39;free&#39; | &#39;pro&#39; | &#39;team&#39; };
};

export function track&lt;K extends keyof AnalyticsEvents&gt;(
  event: K,
  properties: AnalyticsEvents[K],
) {
  analyticsBus.emit(event, properties);
}
</code></pre>
<p>Now <code>track(&#39;checkout_started&#39;, { cartValueCents: 4200, itemCount: 3 })</code> autocompletes, the payload is type-checked, and a misspelled event name is a compile error instead of a silently-dropped data point six weeks of dashboards later. <code>AnalyticsEvents</code> is the catalog — one file that answers &quot;what does this product measure?&quot; That question having a single, readable answer is worth more than any individual event.</p>
<h2>Decouple your code from the vendor</h2>
<p>Step two is the part that pays off for years: your product code should emit <em>domain events</em>, and a separate adapter should decide what to do with them. Product code says &quot;checkout started.&quot; It does not know or care whether that goes to Amplitude, GA4, Segment, a data warehouse, or all four.</p>
<pre><code class="language-ts">interface AnalyticsSink {
  send(event: string, properties: Record&lt;string, unknown&gt;): void;
}

// each vendor is an adapter — swappable, testable, isolated
const amplitudeSink: AnalyticsSink = {
  send: (event, props) =&gt; amplitude.track(event, props),
};

// one subscriber wires the typed catalog to whatever sinks are configured
export function initAnalytics(sinks: AnalyticsSink[]) {
  analyticsBus.onAny((event, properties) =&gt; {
    for (const sink of sinks) sink.send(event, properties);
  });
}
</code></pre>
<p>This is <a href="https://ma-x.im/blog/react-playbook-composition-and-di">dependency injection</a> applied to tracking: the product depends on an abstraction (<code>track</code>), and the concrete vendor is injected at the edge. Swap Amplitude for Segment by changing one adapter — not by grep-and-replacing three hundred call sites. Add a second vendor without touching a single component. Run tests with a fake sink that just records calls, so you can assert &quot;checkout_completed fired once with this payload&quot; without any network. And notice this is a textbook <em>good</em> use of the event bus from earlier in the cluster: the emitter (product code) genuinely should not know who&#39;s listening (the vendors), so the indirection is real decoupling, not hidden coupling.</p>
<h2>Governance is the whole point, not an afterthought</h2>
<p>Once analytics is a layer, the things that were impossible when it was scattered become easy — and these are the things that actually matter when the company grows up:</p>
<ul>
<li><strong>Naming.</strong> One catalog means one convention. No more <code>Button Clicked</code> versus <code>button_click</code> versus <code>btnClick</code> for the same action.</li>
<li><strong>PII control.</strong> A single choke point is where you enforce &quot;no emails, no raw tokens, no free-text that might contain personal data.&quot; You can scrub, allowlist, or type-forbid sensitive fields in <em>one</em> place. When legal asks &quot;are we sending any personal data to this vendor,&quot; you have a file to point at instead of a codebase to spelunk.</li>
<li><strong>Auditing.</strong> GDPR, CCPA, a privacy review — all of them ask &quot;what do you collect and where does it go?&quot; With a catalog and adapters, that&#39;s a five-minute answer. Sprinkled <code>track()</code> calls make it a five-day investigation.</li>
</ul>
<p>None of that governance is possible when tracking is smeared across components. It&#39;s <em>trivial</em> when tracking flows through one typed layer. That&#39;s the real argument for the architecture — not elegance, but the fact that a scattered approach becomes a liability the moment anyone with a compliance question walks in.</p>
<h2>Closing the cluster</h2>
<p>That wraps this run on communication between the parts of an app, and the three pieces rhyme on purpose. Event buses, feature flags, and analytics all start life as innocent one-liners sprinkled through the UI — <code>emit</code>, <code>if (flag)</code>, <code>track</code> — and all three rot the same way: stringly-typed, un-auditable, impossible to change in one place. And all three are fixed the same way: a typed contract, a single home, and a boundary the rest of the app talks to instead of reaching past. Communication between parts done well isn&#39;t ambient magic. It&#39;s explicit, typed, discoverable contracts — the exact opposite of &quot;anything can talk to anything.&quot;</p>
<p>Next, the series turns to a short but overdue detour — the panic over React re-renders, why rendering is the framework&#39;s superpower rather than its disease, and the architecture people quietly break trying to avoid it. Right after that it gets into how an app <em>looks and feels</em> at an architectural level, starting with theming and dark mode. That&#39;s where we go.</p>
<p>If your app tracks analytics, try the one-folder test right now: can you open a single place and read the full list of what you collect and where it&#39;s sent? If the honest answer is &quot;no, it&#39;s everywhere,&quot; you&#39;ve found your next refactor — and I&#39;d like to hear how scattered it turned out to be.</p>
]]></content:encoded>
      <pubDate>Mon, 27 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Feature Flags: Every Flag Is a Branch You Promised to Delete</title>
      <link>https://ma-x.im/blog/react-playbook-feature-flags</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-feature-flags</guid>
      <description>Feature flags decouple deploy from release, and that&apos;s genuinely powerful. But every flag is a fork in your code, forks multiply, and the ones nobody deletes quietly become the most dangerous debt in the codebase. The architecture isn&apos;t the flag — it&apos;s how you evaluate, type, and eventually kill it.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-feature-flags/cover.png" alt="Feature Flags: Every Flag Is a Branch You Promised to Delete" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-event-bus">event bus article</a> ended on a warning about a certain kind of switch teams sprinkle everywhere with a casual &quot;it&#39;s just a toggle&quot; attitude — and then can&#39;t get rid of. Feature flags are that switch. They&#39;re one of the most genuinely useful tools in a mature frontend, and also one of the fastest ways to accumulate debt that never shows up on a dashboard until it hurts.</p>
<p>Let me be fair to them first, because they earn their place. A flag lets you deploy code without releasing it, roll a feature out to 5% of users and watch, keep a kill switch for something risky, or run an A/B test. Decoupling &quot;the code is in production&quot; from &quot;users can see it&quot; is a real superpower — it turns scary big-bang launches into calm, reversible dials. I&#39;m not here to talk you out of flags. I&#39;m here to talk about the bill that arrives later.</p>
<h2>The hidden cost: every flag forks your code</h2>
<p>Here&#39;s what a flag actually is, mechanically:</p>
<pre><code class="language-tsx">if (flags.newCheckout) {
  return &lt;NewCheckout /&gt;;
}
return &lt;LegacyCheckout /&gt;;
</code></pre>
<p>That&#39;s a fork. Your code now has two paths, and <em>both of them ship to production</em>. Fine for one flag. But flags don&#39;t come alone. Add <code>newCheckout</code>, <code>expressPay</code>, and <code>giftingV2</code>, and you don&#39;t have three toggles — you have up to eight combinations of code paths, and your test suite almost certainly covers two of them. The combination where <code>newCheckout</code> is on, <code>expressPay</code> is off, and <code>giftingV2</code> is half-rolled-out is a real state a real user can hit, and nobody has ever seen it.</p>
<blockquote>
<p>Every flag doubles the number of code paths you technically ship. Ten flags is a thousand possible combinations of your app, and you have tested maybe five of them.</p>
</blockquote>
<p>This is the cost that never makes it onto the ticket. The flag went in to <em>reduce</em> risk, and each one individually does. But their product quietly grows a combinatorial space of untested app states, and that space is where the weird, unreproducible bugs live.</p>
<h2>Not all flags are the same thing</h2>
<p>The biggest conceptual mistake I see is treating &quot;feature flag&quot; as one category. It&#39;s at least four, and they have completely different lifespans:</p>
<ul>
<li><strong>Release flags</strong> — &quot;show the new checkout.&quot; Short-lived. The whole point is to remove them once the feature is fully rolled out. If it&#39;s still there three months after 100% rollout, it&#39;s dead weight.</li>
<li><strong>Ops / kill switches</strong> — &quot;disable the recommendation engine if it&#39;s melting.&quot; Long-lived on purpose. These are operational controls, not temporary forks.</li>
<li><strong>Experiment flags</strong> — &quot;variant A vs B.&quot; Live exactly as long as the experiment, then die with a decision.</li>
<li><strong>Permission / entitlement flags</strong> — &quot;this customer&#39;s plan includes analytics.&quot; These aren&#39;t really flags at all. They&#39;re <em>configuration</em> or <em>authorization</em>, permanent by nature, and modeling them as feature flags muddies both systems.</li>
</ul>
<p>The failure is conflating these. A release flag that&#39;s secretly treated as permanent config never gets deleted. An entitlement check that&#39;s built as a &quot;flag&quot; ends up in the same dusty list as abandoned experiments. Name the <em>kind</em> of flag when you create it, because the kind determines whether — and when — it&#39;s supposed to disappear.</p>
<h2>Dead flags are the real debt</h2>
<p>A release flag that&#39;s been at 100% for six months isn&#39;t neutral. It&#39;s a permanent <code>if</code> with a dormant <code>else</code> branch full of code that no longer runs but still compiles, still ships, still shows up when someone greps the codebase, and still makes every reader pause to figure out whether the old path matters. It&#39;s a comment that lies, except the compiler enforces the lie.</p>
<p>So flags need a <em>removal</em> discipline baked in from creation, not bolted on never. The cheapest version that works: give every flag an owner and an expiry, in the definition itself, so a stale flag becomes visible instead of invisible.</p>
<pre><code class="language-ts">type FlagDefinition = {
  key: string;
  kind: &#39;release&#39; | &#39;ops&#39; | &#39;experiment&#39;;
  owner: string;
  /** Release/experiment flags carry a date they&#39;re expected to be gone by. */
  expires?: string; // ISO date
  description: string;
};

const flags: FlagDefinition[] = [
  {
    key: &#39;newCheckout&#39;,
    kind: &#39;release&#39;,
    owner: &#39;payments&#39;,
    expires: &#39;2026-09-01&#39;,
    description: &#39;Rollout of the redesigned checkout flow.&#39;,
  },
];
</code></pre>
<p>A tiny CI check that fails when a <code>release</code> flag is past its <code>expires</code> date turns &quot;we&#39;ll clean it up eventually&quot; into &quot;the build reminds us.&quot; This is exactly the <em>contract</em> step from the <a href="https://ma-x.im/blog/react-playbook-changing-shared-components">changing-shared-components article</a>: a deprecation is a promise to remove the old path, and a flag is a promise to remove one of two branches. Flags that never get contracted are the same debt, wearing a different hat.</p>
<h2>Centralize evaluation behind a typed door</h2>
<p>Architecturally, the thing that makes flags survivable is refusing to scatter raw checks through the codebase. If <code>if (config.flags[&#39;new_checkout&#39;])</code> appears inline in forty components with forty slightly different string keys, you can neither find all usages nor safely delete the flag. Put evaluation behind one typed interface:</p>
<pre><code class="language-ts">// keys are a closed set — typos are compile errors, usages are greppable
type FlagKey = &#39;newCheckout&#39; | &#39;expressPay&#39; | &#39;giftingV2&#39;;

export function useFlag(key: FlagKey): boolean {
  const flags = useFlagContext();
  return flags[key] ?? false; // safe default: off
}

// usage — one obvious, discoverable pattern everywhere
function Checkout() {
  const newCheckout = useFlag(&#39;newCheckout&#39;);
  return newCheckout ? &lt;NewCheckout /&gt; : &lt;LegacyCheckout /&gt;;
}
</code></pre>
<p>One door means you can list every flag from the <code>FlagKey</code> type, find every use of a flag by searching for one function, and delete a flag by deleting its key and chasing the compile errors — the same &quot;smaller public API is a smaller promise&quot; idea from the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">module boundaries</a> work. One more thing specific to this series&#39; <a href="https://ma-x.im/blog/nextjs-ssg-github-pages">static-export stack</a>: decide deliberately whether a flag is evaluated at <em>build time</em> (baked into the static output, changing it needs a rebuild) or at <em>runtime</em> (fetched client-side, changeable live). Build-time flags are simpler and free; runtime flags cost you an evaluation path but let you flip without deploying. Neither is wrong — but conflating them leads to &quot;why didn&#39;t my flag change do anything&quot; confusion.</p>
<h2>The reframe</h2>
<p>Feature flags are a borrowing tool. Each one lets you borrow flexibility now — ship dark, roll out slow, bail out fast — against a promise to pay it back by deleting the branch later. Teams get in trouble not because they use flags, but because they only ever do the borrowing half. The flag goes in, the feature ships, everyone moves on, and the <code>else</code> branch sits there forever accruing interest in the form of code paths nobody understands.</p>
<p>So the discipline is simple to say and hard to keep: know what <em>kind</em> of flag you&#39;re adding, give the temporary ones an owner and a death date, put them all behind one typed door, and treat deleting a flag as part of shipping the feature, not a someday-chore. A flag system with a removal habit is a superpower. A flag system without one is a slowly growing maze.</p>
<p>There&#39;s one more cross-cutting concern that gets sprinkled through components with the same casual carelessness as flags and events — scattered, stringly-typed, and impossible to audit — and it happens to be a perfect customer for the typed-event thinking from the last two articles: analytics. That&#39;s where this cluster ends.</p>
<p>If you&#39;ve got flags in your app, go count how many have been at 100% for more than a quarter. That number is your dead-branch debt, and I&#39;d bet it&#39;s higher than anyone on the team would guess. Tell me what you find.</p>
]]></content:encoded>
      <pubDate>Sat, 25 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The Event Bus: Great for Decoupling, Great for Making a Mess</title>
      <link>https://ma-x.im/blog/react-playbook-event-bus</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-event-bus</guid>
      <description>An event bus feels like the cure for prop drilling and tangled dependencies — anything can talk to anything, and nothing is wired together. That&apos;s exactly the problem. It doesn&apos;t remove the coupling, it hides it. Here&apos;s where a bus genuinely helps and where it quietly turns your app into action-at-a-distance.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-event-bus/cover.png" alt="The Event Bus: Great for Decoupling, Great for Making a Mess" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-state-machines">last cluster</a> was about making state inside a feature explicit and well-modeled. This one is about what happens <em>between</em> features — how the parts of a growing app talk to each other. And the very first tool people reach for is also the one most likely to undo all that hard-won explicitness: the global event bus.</p>
<p>The pitch is seductive. Component A, buried deep in one part of the tree, needs to tell component B, buried in a completely different part, that something happened. Lifting state up to their common ancestor means threading it through ten components that don&#39;t care. Prop drilling. Context that spans half the app. It all feels heavy. So you drop in an event bus — <code>bus.emit(&#39;cartUpdated&#39;)</code> here, <code>bus.on(&#39;cartUpdated&#39;, ...)</code> there — and suddenly A and B are talking without knowing about each other. Decoupled. Clean. Done.</p>
<p>Except you didn&#39;t remove the coupling. You hid it.</p>
<h2>Decoupling versus hiding</h2>
<p>There&#39;s a real difference between two components being <em>decoupled</em> and two components being <em>secretly coupled through a channel you can&#39;t see</em>. When A calls a function B gave it, the connection is explicit — you can find it, type it, trace it. When A emits <code>&#39;cartUpdated&#39;</code> into a global bus and B happens to listen, the connection still exists — B breaks if A stops emitting — but nothing in the code <em>shows</em> it. You can&#39;t right-click <code>emit(&#39;cartUpdated&#39;)</code> and find the listeners. You can&#39;t tell, six months later, whether anyone still cares about that event or whether you can safely stop firing it.</p>
<p>This is action-at-a-distance, and it&#39;s the exact thing the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">dependencies article</a> warned about: the real architecture is the graph of who depends on whom, and a bus makes edges of that graph invisible. Fire-and-forget quietly becomes fire-and-pray.</p>
<blockquote>
<p>An event bus doesn&#39;t remove coupling. It converts visible coupling into invisible coupling — and invisible coupling is the kind that rots.</p>
</blockquote>
<p>I&#39;m not saying never use one. I&#39;m saying the bus is not a way to <em>avoid</em> designing your data flow. It&#39;s a specific tool for a specific shape of problem, and using it for the wrong shape is how apps turn into a séance where components whisper to each other and nobody knows who&#39;s in the room.</p>
<h2>The shape a bus is actually for</h2>
<p>Here&#39;s my test. A bus is the right tool when the emitter <em>legitimately should not know</em> who cares — when the coupling, if you made it explicit, would be <em>accidental</em> rather than real. That&#39;s a narrow but genuine category:</p>
<ul>
<li><strong>One-to-many, fire-and-forget.</strong> &quot;The user logged out.&quot; Auth doesn&#39;t know — and shouldn&#39;t know — that the cart cache, the websocket connection, and the draft autosaver all need to react. Making auth import and call all three would be a false dependency pointing the wrong way.</li>
<li><strong>Cross-cutting concerns.</strong> Analytics, logging, toasts. A &quot;payment succeeded&quot; event that a toast system and an analytics layer both react to — neither of which the payment code should depend on.</li>
<li><strong>Truly ambient signals.</strong> &quot;Went offline,&quot; &quot;theme changed,&quot; &quot;session expiring soon&quot; — things many unrelated parts respond to independently.</li>
</ul>
<p>Notice the pattern: in every case, wiring it explicitly would force a module to depend on things <em>beneath</em> it or <em>beside</em> it that it has no business knowing. The bus is right precisely because the alternative is a worse dependency.</p>
<p>And here&#39;s the flip side — when there&#39;s a <em>real</em>, traceable data dependency, use the explicit tool. If B needs a value that A owns, that&#39;s not an event, that&#39;s state: put it in a <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">store</a> or pass it down. If A needs B to do something and A legitimately owns that relationship, that&#39;s a function call or a callback prop. Don&#39;t reach for events to dodge the work of designing where data lives. Using a bus to move owned state around is the single most common way this goes wrong.</p>
<h2>Untyped buses are where the mess compounds</h2>
<p>Even for a legitimate use, the default event bus API is a trap: stringly-typed names and <code>any</code> payloads.</p>
<pre><code class="language-ts">// the mess: no autocomplete, no safety, no discoverability
bus.emit(&#39;user_loggedOut&#39;, { usrId: 42 });     // typo in event name
bus.on(&#39;user_logged_out&#39;, (data) =&gt; {          // ...silently never fires
  clearCache(data.userId);                      // payload shape? who knows
});
</code></pre>
<p>Two different spellings of the same event, a payload field that might be <code>usrId</code> or <code>userId</code> depending on who wrote which side, and zero help from the compiler. Multiply that across a codebase and the bus becomes a pile of magic strings nobody dares touch. The fix is a typed event map, so the bus knows every event and its payload:</p>
<pre><code class="language-ts">type AppEvents = {
  &#39;auth/loggedOut&#39;: { userId: number };
  &#39;cart/updated&#39;: { itemCount: number };
  &#39;payment/succeeded&#39;: { orderId: string; amountCents: number };
};

function createTypedBus&lt;Events extends Record&lt;string, unknown&gt;&gt;() {
  const listeners = new Map&lt;keyof Events, Set&lt;(payload: never) =&gt; void&gt;&gt;();
  return {
    on&lt;K extends keyof Events&gt;(type: K, fn: (payload: Events[K]) =&gt; void) {
      const set = listeners.get(type) ?? new Set();
      set.add(fn as (payload: never) =&gt; void);
      listeners.set(type, set);
      return () =&gt; set.delete(fn as (payload: never) =&gt; void); // unsubscribe
    },
    emit&lt;K extends keyof Events&gt;(type: K, payload: Events[K]) {
      listeners.get(type)?.forEach((fn) =&gt; (fn as (p: Events[K]) =&gt; void)(payload));
    },
  };
}

export const bus = createTypedBus&lt;AppEvents&gt;();
</code></pre>
<p>Now <code>bus.emit(&#39;auth/loggedOut&#39;, { userId: 42 })</code> autocompletes, the payload is checked, and a typo in the event name is a compile error. Just as importantly, <code>AppEvents</code> is a <em>catalog</em> — one place that lists every event the app can raise. The bus stops being a fog of strings and becomes a documented contract. (You don&#39;t need to hand-roll this; <code>mitt</code> and similar libraries take a typed event map. But seeing the shape is the point.)</p>
<h2>Keeping the bus from becoming a god object</h2>
<p>The last discipline: scope it. One global bus that every feature emits into and listens to is just global mutable state wearing a bow tie — the god object the <a href="https://ma-x.im/blog/react-playbook-shared-code">shared-code article</a> is allergic to. Prefer buses scoped to a boundary: a feature-level bus for events within a feature, and a small, deliberate set of truly app-wide events at the top. Treat the app-wide event catalog like a public API — small, stable, reviewed when it changes. If your <code>AppEvents</code> type has forty entries, that&#39;s not decoupling, that&#39;s a distributed spaghetti dinner.</p>
<p>Because here&#39;s the thing that took me a while to accept: an event bus doesn&#39;t make coupling go away, it makes coupling <em>a design decision you have to be deliberate about</em>. Used for the narrow set of genuinely ambient, one-to-many, cross-cutting signals, it&#39;s the right tool and nothing else comes close. Used as a general-purpose &quot;let me talk to that far-away component without wiring anything,&quot; it reintroduces every bit of implicit, untraceable magic the last five articles worked to remove — just with a nicer-sounding name.</p>
<p>That framing — a bus is fine, an <em>undisciplined</em> bus is the enemy — sets up the next one perfectly, because there&#39;s a specific kind of switch that teams love to sprinkle everywhere with exactly the same &quot;it&#39;s just a quick toggle&quot; energy, and it multiplies your code paths behind your back: the feature flag. That&#39;s next.</p>
<p>If your app has an event bus, do one experiment: try to list every event it carries and who listens to each. If you can&#39;t do it from memory or from one file, the bus has already started hiding your architecture from you — tell me how long the list turned out to be.</p>
]]></content:encoded>
      <pubDate>Thu, 23 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>State Machines in React: When Booleans Start Lying to You</title>
      <link>https://ma-x.im/blog/react-playbook-state-machines</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-state-machines</guid>
      <description>isLoading, isError, isSuccess, isEditing — five booleans give you thirty-two combinations, and most of them are impossible states your code still has to survive. A state machine makes the impossible ones unrepresentable. Here&apos;s when to hand-roll one and when to reach for XState.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-state-machines/cover.png" alt="State Machines in React: When Booleans Start Lying to You" /></p>
<p>Look at a component you wrote recently and count its status booleans. <code>isLoading</code>, <code>isError</code>, <code>isSuccess</code>, <code>isEditing</code>, <code>isSubmitting</code>. Five of them. That&#39;s not five states — that&#39;s two-to-the-fifth, thirty-two combinations, and you only meant for about four of them to exist. What is your UI supposed to do when <code>isLoading</code> and <code>isError</code> are both true? You never decided, because you never meant for that to happen. But nothing in the code stops it, so one day a fast retry sets both, and you get a spinner layered over an error message, and a bug report you can&#39;t reproduce.</p>
<p>This is the quiet tax of modeling state as loose booleans. Every flag you add doubles the space of possible states, most of the new ones are nonsense, and your code silently becomes responsible for all of them. The <a href="https://ma-x.im/blog/react-playbook-undo-redo">undo/redo article</a> got us thinking about changes as explicit, named transitions. A state machine is that same idea applied to the states themselves.</p>
<h2>Impossible states, made impossible</h2>
<p>The core move of a state machine is to stop representing status as independent booleans and start representing it as <em>one</em> value that can only be one of a fixed set of named states:</p>
<pre><code class="language-ts">// before: 32 representable combinations, ~4 intended
type Status = {
  isLoading: boolean;
  isError: boolean;
  isSuccess: boolean;
};

// after: exactly the states that can actually exist
type Status =
  | { state: &#39;idle&#39; }
  | { state: &#39;loading&#39; }
  | { state: &#39;success&#39;; data: Data }
  | { state: &#39;error&#39;; message: string };
</code></pre>
<p>Look at what the second version buys you beyond tidiness. <code>loading</code> and <code>error</code> can no longer both be true — the type won&#39;t let you construct it. And notice <code>data</code> only exists in the <code>success</code> state and <code>message</code> only in <code>error</code>. You can&#39;t read <code>data</code> while loading because there is no <code>data</code> while loading. The compiler now enforces the thing you were previously enforcing by hoping. This is the payoff people mean when they say &quot;make impossible states unrepresentable&quot; — it&#39;s not aesthetics, it&#39;s deleting whole categories of bug by construction.</p>
<blockquote>
<p>Every boolean you add to describe status doubles your state space and trusts you to defend the nonsense corners by hand. A state machine shrinks the space to the states that can really happen and makes the rest impossible to write.</p>
</blockquote>
<h2>You often don&#39;t need a library</h2>
<p>Here&#39;s the part that gets lost in the XState hype: a state machine is a <em>concept</em>, not a dependency. Most of the time a <code>useReducer</code> with explicit states is a complete, honest state machine, and it&#39;s the right amount of tool:</p>
<pre><code class="language-ts">type State =
  | { state: &#39;idle&#39; }
  | { state: &#39;loading&#39; }
  | { state: &#39;success&#39;; data: Data }
  | { state: &#39;error&#39;; message: string };

type Event =
  | { type: &#39;FETCH&#39; }
  | { type: &#39;RESOLVE&#39;; data: Data }
  | { type: &#39;REJECT&#39;; message: string };

function reducer(state: State, event: Event): State {
  switch (state.state) {
    case &#39;idle&#39;:
      return event.type === &#39;FETCH&#39; ? { state: &#39;loading&#39; } : state;
    case &#39;loading&#39;:
      if (event.type === &#39;RESOLVE&#39;) return { state: &#39;success&#39;, data: event.data };
      if (event.type === &#39;REJECT&#39;) return { state: &#39;error&#39;, message: event.message };
      return state;
    // success / error handle their own transitions...
    default:
      return state;
  }
}
</code></pre>
<p>The thing I want you to notice is that transitions are keyed on <em>the current state first</em>. A <code>RESOLVE</code> event while <code>idle</code> does nothing, because you can&#39;t resolve a fetch you never started. That&#39;s a transition guard falling out naturally from the structure — the machine ignores events that don&#39;t make sense in the current state, instead of trusting every handler to check. For a data fetch, a form submit, a toggle with a few modes, this hand-rolled machine is all you need, and reaching for a library would be ceremony.</p>
<h2>When XState actually earns its weight</h2>
<p>So when do I pull in a real state-machine library? When the flow gets complex enough that the <em>machinery around</em> transitions becomes the hard part — and a reducer starts sprawling. Concretely, when I hit things like these together:</p>
<ul>
<li><strong>Guards</strong> — a transition is only allowed under a condition (&quot;can&#39;t go to <code>review</code> unless every step validated&quot;).</li>
<li><strong>Side effects tied to transitions</strong> — entering <code>submitting</code> should fire a request; leaving <code>editing</code> should autosave. XState models these as actions and services attached to states, instead of effects scattered across components.</li>
<li><strong>Hierarchy and parallel states</strong> — a media player that is <code>playing</code> <em>and</em> independently <code>muted</code> <em>and</em> <code>fullscreen</code>, or a wizard where each step has its own sub-states. Statecharts express &quot;these regions are active at once&quot; without the boolean explosion coming back through the side door.</li>
<li><strong>A flow worth visualizing</strong> — XState machines can be rendered as an actual diagram, which for a gnarly <a href="https://ma-x.im/blog/react-playbook-form-libraries">multi-step form</a> or checkout becomes a shared source of truth that a PM or designer can read.</li>
</ul>
<pre><code class="language-ts">import { createMachine } from &#39;xstate&#39;;

const checkoutMachine = createMachine({
  id: &#39;checkout&#39;,
  initial: &#39;cart&#39;,
  states: {
    cart:     { on: { NEXT: &#39;shipping&#39; } },
    shipping: { on: { NEXT: { target: &#39;payment&#39;, guard: &#39;shippingValid&#39; }, BACK: &#39;cart&#39; } },
    payment:  { on: { NEXT: { target: &#39;review&#39;, guard: &#39;paymentValid&#39; }, BACK: &#39;shipping&#39; } },
    review:   { on: { SUBMIT: &#39;placing&#39;, BACK: &#39;payment&#39; } },
    placing:  { invoke: { src: &#39;placeOrder&#39;, onDone: &#39;done&#39;, onError: &#39;review&#39; } },
    done:     { type: &#39;final&#39; },
  },
});
</code></pre>
<p>Read that and you can see the <em>entire</em> flow — every state, every legal move, every guard, where the order actually gets placed — in one place. The kind of flow this shines on is exactly the sort of thing I&#39;ve built before: a policy builder with conditional, dependent steps where &quot;which screen is even valid right now&quot; is genuinely hard. When the answer to &quot;what can happen next&quot; stops being obvious, a statechart stops being overhead and starts being the only readable description of the feature.</p>
<h2>The cost, said plainly</h2>
<p>XState has a learning curve and a vocabulary — actors, guards, actions, services, context — and if you drop it onto a problem that a four-state reducer would&#39;ve solved, everyone on the team pays that tax for nothing. Do not put a toggle in a statechart. Do not model <code>idle → loading → success → error</code> with the full actor system if a reducer says it in fifteen lines. The tool is proportional: booleans for one or two independent flags, a <code>useReducer</code> machine for a real-but-contained flow, and XState when hierarchy, guards, side effects, and visualization all show up at once. Matching the weight of the tool to the weight of the problem is most of the skill.</p>
<h2>Closing the cluster</h2>
<p>That&#39;s the end of this run on state and data at scale, and there&#39;s a single thread through all five pieces. We started with <a href="https://ma-x.im/blog/react-playbook-local-storage-and-state">where state even lives</a> — memory versus storage, and why confusing them breaks things. Then <a href="https://ma-x.im/blog/react-playbook-data-normalization">one coherent copy of server data</a> instead of five drifting duplicates. Then <a href="https://ma-x.im/blog/react-playbook-optimistic-ui">optimistic updates</a> — showing intent early and reconciling with truth. Then <a href="https://ma-x.im/blog/react-playbook-undo-redo">undo/redo</a> — changes as explicit, reversible transitions. And now state machines — the states themselves made explicit and finite. Every step was the same instinct: take something ambient and implicit, and make it a thing you can see, name, and reason about.</p>
<p>Which is exactly the instinct the next cluster needs, because it moves from state <em>inside</em> a part of the app to communication <em>between</em> the parts. Once you have well-modeled features, the new failure mode is how they talk to each other — and the moment you reach for a global event bus so anything can shout at anything, you can bring back all the implicitness you just spent five articles removing. When does an event bus actually help, and when is it just spaghetti with better branding? That&#39;s where we go next.</p>
<p>If you&#39;ve got a component right now with three or more status booleans in it, try the exercise: write down the states it&#39;s actually allowed to be in. If that list is shorter than two-to-the-number-of-booleans — and it always is — you&#39;ve just found the impossible states your code is currently paying to survive. Tell me how many you found; the gap is usually bigger than people expect.</p>
]]></content:encoded>
      <pubDate>Tue, 21 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Undo/Redo Without Losing Your Mind: The Command Pattern in React</title>
      <link>https://ma-x.im/blog/react-playbook-undo-redo</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-undo-redo</guid>
      <description>The instinct is to snapshot your whole state on every change and step backward through the copies. It works until it doesn&apos;t. A history of reversible commands scales further, describes what actually changed, and survives contact with a server. Here&apos;s how I build undo/redo that doesn&apos;t fall apart.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-undo-redo/cover.png" alt="Undo/Redo Without Losing Your Mind: The Command Pattern in React" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-optimistic-ui">optimistic UI article</a> ended on a small observation that turns out to be a big one. The rollback in <code>onError</code> was a single-step reversal: snapshot the truth, and if things go wrong, restore it. That&#39;s undo for exactly one action. Now a user opens your editor, makes twelve changes, and wants to walk back the last five — then redo two of them. Suddenly one snapshot isn&#39;t a feature, it&#39;s a rounding error. You need a <em>history</em>.</p>
<p>Undo/redo looks like a checkbox on a feature list and turns into one of those problems that quietly reshapes your state architecture. Get the model right early and it&#39;s almost pleasant. Get it wrong and you&#39;ll be rewriting it under deadline pressure the week before launch, because &quot;add undo&quot; arrived late and your state wasn&#39;t built for reversal.</p>
<h2>The snapshot trap</h2>
<p>The first idea everyone has — myself included, years ago — is to snapshot the entire state on every change and keep the snapshots in an array:</p>
<pre><code class="language-ts">// the tempting version
const history: EditorState[] = [];
let pointer = -1;

function commit(next: EditorState) {
  history.splice(pointer + 1);   // drop any redo future
  history.push(next);
  pointer = history.length - 1;
}
function undo() { if (pointer &gt; 0) pointer--; }
function redo() { if (pointer &lt; history.length - 1) pointer++; }
</code></pre>
<p>For a tiny bit of local state, this is genuinely fine — I&#39;ll come back to when it&#39;s the right answer. But scale it to a real document editor and the cracks show fast. Every keystroke clones the whole state, so memory balloons. You can&#39;t tell the user <em>what</em> an undo will do, because you never recorded the change — only the before-and-after blobs. And the moment any of that state came from a server, &quot;restore the old snapshot&quot; means shipping stale data back over changes other people may have made. The snapshot approach knows <em>that</em> something changed but never <em>what</em>, and that missing information is exactly what you need for everything interesting.</p>
<h2>Commands: store the change, not the world</h2>
<p>The pattern that scales is old and boring and correct: represent every change as a <em>command</em> — an object that knows how to apply itself and how to invert itself. History becomes a stack of commands, not a stack of snapshots.</p>
<pre><code class="language-ts">interface Command {
  label: string;          // human-readable: &quot;Rename layer&quot;, for a menu or tooltip
  apply(state: EditorState): void;
  invert(state: EditorState): void;
}

// a concrete command carries just enough to undo itself
function renameLayer(layerId: string, from: string, to: string): Command {
  return {
    label: &#39;Rename layer&#39;,
    apply: (s) =&gt; { s.layers[layerId].name = to; },
    invert: (s) =&gt; { s.layers[layerId].name = from; },
  };
}
</code></pre>
<p>The whole game is in what <code>renameLayer</code> captures: the id, the old value, the new value. That&#39;s the <em>delta</em> — the minimum needed to move forward and back. Not the entire editor, just this one reversible fact. A thousand of these cost almost nothing to keep, and each one can tell you what it does, because describing itself is part of the command&#39;s job.</p>
<h2>Two stacks and one rule</h2>
<p>With commands in hand, the history engine is small and, honestly, satisfying:</p>
<pre><code class="language-ts">class History {
  private undoStack: Command[] = [];
  private redoStack: Command[] = [];

  execute(command: Command, state: EditorState) {
    command.apply(state);
    this.undoStack.push(command);
    this.redoStack = []; // a fresh action erases the redo future
  }

  undo(state: EditorState) {
    const command = this.undoStack.pop();
    if (!command) return;
    command.invert(state);
    this.redoStack.push(command);
  }

  redo(state: EditorState) {
    const command = this.redoStack.pop();
    if (!command) return;
    command.apply(state);
    this.undoStack.push(command);
  }
}
</code></pre>
<p>Two stacks. Undo pops from one and pushes to the other; redo does the reverse. The single rule worth burning in: <strong>a brand-new action clears the redo stack.</strong> Once you undo three steps and then do something new, the future you undid is gone — you branched off it. Every editor works this way, and users expect it even though they&#39;ve never thought about it.</p>
<h2>Wiring it to the store</h2>
<p>In this series the app state lives in a <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">Reatom store</a>, so commands operate on the store and the history sits beside it. The important discipline is that <em>the only way to change undoable state is through a command</em>. If some code reaches in and mutates the store directly, that change is invisible to history and undo will silently corrupt the state — undoing things that were never recorded.</p>
<pre><code class="language-ts">const history = new History();

// every user-driven mutation goes through here
export function dispatch(command: Command) {
  history.execute(command, store.getState());
}

export function undo() { history.undo(store.getState()); }
export function redo() { history.redo(store.getState()); }
</code></pre>
<p>This is the same boundary lesson from the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">module-dependencies article</a>: if there&#39;s one door in, you can reason about what comes through it. Undo/redo only works if every change walks through the command door.</p>
<h2>Two things that separate a demo from a real feature</h2>
<p><strong>Coalescing.</strong> If every keystroke is its own command, hitting undo once removes a single character, and the user has to mash it thirty times to undo a sentence. Nobody wants that. You <em>coalesce</em> rapid same-kind commands into one: while the user is typing continuously into the same field, merge keystrokes into a single &quot;type text&quot; command, and only seal it when they pause or move focus. It&#39;s a small bit of bookkeeping and it&#39;s the difference between undo feeling native and feeling broken.</p>
<p><strong>The server tension.</strong> This is the one that trips people up. If a command already synced its change to the backend, undo is <em>not</em> time travel — you can&#39;t un-send a request that already happened. Undo becomes a new <em>forward</em> action that compensates: to undo a rename that persisted, you send a new rename back to the old value. Model it as &quot;apply the inverse as a fresh change,&quot; not &quot;reach into the past.&quot; Trying to literally rewind server state is how you get bugs that only appear when two people edit at once. Local, unsynced state can rewind freely; synced state can only be corrected forward.</p>
<h2>When the snapshot approach is actually right</h2>
<p>I promised I&#39;d come back to it, because dogma is expensive. If your undoable state is small, fully local, and cheap to copy, snapshotting is the <em>right</em> call — commands would be over-engineering. And there&#39;s a lovely middle path: if that state is built from <a href="https://ma-x.im/blog/react-playbook-immutable-data-structures">immutable data structures</a>, a &quot;snapshot&quot; is just a reference, since unchanged branches are shared structurally. Keeping a list of immutable roots costs a pointer each, not a deep clone. So the honest decision rule is: small, local, immutable state → snapshots are simplest and fine. Large, partially-server-backed, or you-need-to-describe-the-change → commands. Reach for the heavier pattern when the cheap one actually hurts, not before.</p>
<h2>The thread running underneath</h2>
<p>Step back and undo/redo is really about something bigger than a toolbar button. It forces you to model your changes as <em>discrete, named, reversible transitions</em> instead of a soup of mutations. The command stack is only possible because you stopped thinking &quot;state got mutated somehow&quot; and started thinking &quot;a specific, describable thing happened, and here&#39;s its inverse.&quot; That shift — from ambient mutation to explicit transitions — is quietly one of the most stabilizing things you can do to a complex client.</p>
<p>Which points straight at the last piece of this run. Undo/redo made your <em>changes</em> explicit. But in a genuinely complex flow — a multi-step wizard, an editor with modes, a checkout — the <em>states themselves</em> and the <em>transitions between them</em> get complicated enough that a pile of booleans can&#39;t describe them without letting impossible combinations sneak in. When modeling the transitions becomes the hard part, you want a tool built for exactly that: a state machine. That&#39;s where this cluster ends.</p>
<p>If you&#39;re about to add undo to something, tell me one thing first: is the state you need to reverse local, or has it already touched a server? That single answer decides whether you&#39;re building a rewind button or a compensating-action engine, and mixing them up is the most common way this feature goes sideways.</p>
]]></content:encoded>
      <pubDate>Sun, 19 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Optimistic UI: Updating Before the Server Says Yes</title>
      <link>https://ma-x.im/blog/react-playbook-optimistic-ui</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-optimistic-ui</guid>
      <description>Optimistic UI is a small lie: you show the result before the server confirms it. Done right, the app feels instant. Done wrong, the UI quietly tells the user something that never happened. The difference is entirely in how you roll back.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-optimistic-ui/cover.png" alt="Optimistic UI: Updating Before the Server Says Yes" /></p>
<p>At the end of the <a href="https://ma-x.im/blog/react-playbook-data-normalization">last article</a> I said that once you hold one coherent copy of server state and can invalidate it precisely, a door opens that used to be terrifying: updating the UI <em>before</em> the server confirms, and rolling back cleanly if it rejects. That&#39;s optimistic UI, and I want to be honest about what it actually is before we make it feel magic.</p>
<p>Optimistic UI is a lie. A small, well-intentioned, usually-correct lie — but a lie. You click the star, and the star fills in <em>immediately</em>, before the server has any idea you did anything. You&#39;re betting the request will succeed and showing the user the future you expect. Most of the time you&#39;re right and the app feels instant. The entire craft is in what happens the rare time you&#39;re wrong.</p>
<h2>Why we bother lying at all</h2>
<p>The alternative is honest and awful. User clicks, you show a spinner, you wait for the round trip, then you update. On a good connection that&#39;s 200-400ms of the UI sitting there doing nothing after a click that should have felt instant. On a train, it&#39;s two seconds of a spinning star. For a like button, a toggle, a reorder — high-frequency, low-stakes actions — that latency is the whole experience, and it makes a fast app feel slow.</p>
<p>So we front-run the server. The user&#39;s intent is almost always going to succeed, so we render success now and reconcile with reality when it arrives. The feeling is immediate; the correctness catches up a beat later.</p>
<blockquote>
<p>Optimistic UI is a promise to the user: &quot;I&#39;ll show you this now, and I guarantee I&#39;ll fix it if I was wrong.&quot; If you can&#39;t keep the second half, don&#39;t make the first.</p>
</blockquote>
<h2>The dangerous naive version</h2>
<p>Here&#39;s the version I see most often, and it&#39;s the one that ships bugs:</p>
<pre><code class="language-tsx">// DON&#39;T: optimism with no way back
function useToggleStar(id: number) {
  const [starred, setStarred] = useState(false);
  const toggle = async () =&gt; {
    setStarred((s) =&gt; !s);        // update immediately
    await api.setStar(id, !starred); // hope it works
  };
  return { starred, toggle };
}
</code></pre>
<p>Read what happens when <code>api.setStar</code> throws. The state already flipped. There&#39;s no snapshot, no catch, no rollback. The star stays filled, the server never recorded it, and the user walks away believing they starred something they didn&#39;t. This isn&#39;t a cosmetic bug — the UI is now actively lying with no plan to stop. Optimism without rollback isn&#39;t optimistic UI. It&#39;s just a race you&#39;re pretending you won.</p>
<h2>The real shape: snapshot, apply, reconcile</h2>
<p>A correct optimistic update always has the same four beats. Cancel anything in flight that could clobber you. Snapshot the current truth. Apply the optimistic change. Then, when the request settles, either keep it (success) or restore the snapshot (failure). Because this series uses <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a>, the cache is where the truth lives, and the mutation lifecycle gives you exactly these hooks:</p>
<pre><code class="language-tsx">const toggleStar = useMutation({
  mutationFn: (id: number) =&gt; api.toggleStar(id),

  onMutate: async (id) =&gt; {
    // 1. stop in-flight refetches so they can&#39;t overwrite our optimistic value
    await queryClient.cancelQueries({ queryKey: [&#39;todo&#39;, id] });

    // 2. snapshot the current truth so we can roll back
    const previous = queryClient.getQueryData&lt;Todo&gt;([&#39;todo&#39;, id]);

    // 3. apply the optimistic change to the cache
    queryClient.setQueryData&lt;Todo&gt;([&#39;todo&#39;, id], (old) =&gt;
      old ? { ...old, starred: !old.starred } : old,
    );

    // hand the snapshot to onError via context
    return { previous };
  },

  onError: (_err, id, context) =&gt; {
    // 4a. reality rejected us — restore exactly what we had
    if (context?.previous) {
      queryClient.setQueryData([&#39;todo&#39;, id], context.previous);
    }
  },

  onSettled: (_data, _err, id) =&gt; {
    // 4b. reconcile with the server&#39;s version, success or failure
    queryClient.invalidateQueries({ queryKey: [&#39;todo&#39;, id] });
  },
});
</code></pre>
<p>Every line earns its place. <code>cancelQueries</code> is the one people skip and then can&#39;t explain the flicker: if a background refetch is already in flight, it will land <em>after</em> your optimistic write and stomp it with stale data. Snapshotting into <code>context</code> is what makes rollback surgical — you restore the exact previous object, not a guess. And <code>onSettled</code> invalidating is the humility step: whatever we optimistically showed, the server&#39;s answer is the real one, so we refetch and let truth win. This is why the <a href="https://ma-x.im/blog/react-playbook-data-normalization">previous article&#39;s</a> precise invalidation mattered — optimistic UI is built directly on top of it.</p>
<h2>Reconciliation is not &quot;did it work&quot; — it&#39;s &quot;what&#39;s true now&quot;</h2>
<p>The subtle part is the reconcile step, because the world is not just success-or-failure. Sometimes the request succeeds but the server&#39;s result differs from what you optimistically drew. You optimistically added an item at the top of a list; the server inserts it sorted, so it&#39;s actually third. You optimistically set a counter to 5; someone else&#39;s change already made it 7.</p>
<p>The rule that keeps you sane: <strong>the server is the source of truth, and reconciliation means adopting its version, not defending yours.</strong> Your optimistic value was a placeholder to make the UI feel alive, nothing more. When the real data arrives, you replace the placeholder — even if it means the item you just &quot;added to the top&quot; visibly jumps to its sorted position. That tiny correction is honest and fine. Fighting to keep your optimistic guess because it looks smoother is how you end up with a client that slowly drifts out of sync with the backend.</p>
<h2>Where it belongs, and where the lie gets expensive</h2>
<p>I reach for optimistic UI when the action is cheap, reversible, and frequent, and when being wrong is cheap to correct. Toggling a star, liking a post, checking a checkbox, reordering a list, adding a comment. If the request fails, the rollback is a small, understandable flicker, and the user retries. The upside — instant feel — is huge and constant; the downside is rare and mild.</p>
<p>I do <em>not</em> reach for it when a wrong guess is expensive or confusing. Payments are the obvious one: never optimistically show &quot;Payment successful.&quot; Showing success for a charge that then fails is worse than any spinner. Same for anything irreversible, anything with legal or money weight, and anything where the rollback would be more jarring than the wait — imagine optimistically navigating someone into a screen that then vanishes because the action failed. In those cases the honest spinner is the right call. The question isn&#39;t &quot;can I make this optimistic?&quot; It&#39;s &quot;if I&#39;m wrong, is the correction cheap and clear?&quot; When the answer is no, don&#39;t tell the lie.</p>
<h2>The reframe</h2>
<p>What optimistic UI really taught me is that a good client models <em>intent</em> and <em>confirmed truth</em> as two different things, and knows how to move between them. The optimistic value is intent rendered early. The server response is confirmed truth. Reconciliation is the bridge. Once you hold those as separate ideas, the whole pattern stops feeling like a hack and starts feeling like what it is: a disciplined way to decouple how fast the UI feels from how fast the network actually is.</p>
<p>And notice what the rollback in <code>onError</code> really was — a single-step reversal back to a snapshot you took a moment ago. That works beautifully for one action. But the moment users want to reverse a <em>sequence</em> of actions — undo the last five edits in an editor, then redo two of them — a lone snapshot isn&#39;t enough. You need a history of reversible changes, which is a different and genuinely interesting problem. That&#39;s undo/redo, and it&#39;s next.</p>
<p>If you&#39;ve got an optimistic update in your app right now, do me a favor and check one thing: what happens on <code>onError</code>? If you can&#39;t point to the line that restores the previous value, you don&#39;t have optimistic UI — you have a lie with no exit. Tell me what you find.</p>
]]></content:encoded>
      <pubDate>Fri, 17 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Server State Is a Cache: Normalization, Duplicates, and Invalidation You Can Trust</title>
      <link>https://ma-x.im/blog/react-playbook-data-normalization</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-data-normalization</guid>
      <description>The same customer shows up in a list, a detail page, and a header badge — three copies of one truth you don&apos;t own. Edit one and the others go stale. Here&apos;s when to normalize server data into one source of truth, when the query cache is enough, and how to invalidate without nuking everything.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-data-normalization/cover.png" alt="Server State Is a Cache: Normalization, Duplicates, and Invalidation You Can Trust" /></p>
<p>The <a href="https://ma-x.im/blog/react-playbook-local-storage-and-state">previous article</a> drew a hard line: state the client owns versus data it doesn&#39;t. Everything there was about the stuff you own — theme, drafts, preferences — where you are the source of truth and storage just remembers it. This one is about the other side of that line, and it&#39;s where a lot of otherwise-clean apps quietly turn to mush.</p>
<p>Here&#39;s the situation. You fetch a list of customers for a table. You fetch one customer for a detail page. There&#39;s a little avatar-and-name badge in the header showing the currently selected customer. That&#39;s three network responses. And customer <code>#42</code> appears in all three. You now have three copies of one entity in memory, and — this is the part that bites — nothing keeps them in agreement. Edit the customer&#39;s name on the detail page, and the table still shows the old name until something refetches it. The badge shows a third version. Your UI is now lying to the user, and the bug report says &quot;the name didn&#39;t update,&quot; which tells you nothing about why.</p>
<p>This is the server-state problem, and it is fundamentally different from the local-state problem. You don&#39;t own this data. It has an owner on the backend. Every copy you hold is a <em>cache</em> of a truth that lives somewhere else and can change without telling you.</p>
<h2>The duplication trap</h2>
<p>The instinct is to treat each fetch as its own little pile of data. List query returns an array, you render it. Detail query returns an object, you render it. Header does its own thing. Each is correct in isolation. The problem is that the same entity is now stored in multiple unrelated places, and a write to one has no path to the others.</p>
<blockquote>
<p>The bug isn&#39;t that data goes stale. It&#39;s that you have more than one copy of the same truth, and no single place responsible for it.</p>
</blockquote>
<p>There are two honest ways out, and picking the wrong one is where teams either over-engineer or under-engineer for years.</p>
<h2>Option one: normalize into one source of truth</h2>
<p>Normalization is the database idea, moved into the client. Instead of storing whatever shape each response happened to have, you break responses into <em>entities keyed by id</em>, store each entity exactly once, and have every view reference it by id.</p>
<pre><code class="language-ts">// denormalized — the same customer duplicated across responses
const listResponse = [{ id: 42, name: &#39;Acme Corp&#39;, tier: &#39;gold&#39; }, /* ... */];
const detailResponse = { id: 42, name: &#39;Acme Corp&#39;, tier: &#39;gold&#39;, notes: &#39;...&#39; };

// normalized — one customer, referenced by id
const cache = {
  customers: {
    42: { id: 42, name: &#39;Acme Corp&#39;, tier: &#39;gold&#39;, notes: &#39;...&#39; },
  },
  // the list is now just an ordered array of ids into the store above
  customerList: [42, 17, 8],
};
</code></pre>
<p>Now there is exactly one <code>customers[42]</code>. The table reads <code>customerList</code> and looks each id up. The detail page reads <code>customers[42]</code>. The header badge reads <code>customers[42]</code>. Update that one record and every view re-reads the same object — they can&#39;t disagree, because there&#39;s nothing to disagree with. This is what normalized caches like RTK Query, Apollo, and the <code>normalizr</code>-style approach give you: a mini in-memory database where entities live once and views are just queries into it.</p>
<p>The cost is real, though, and it&#39;s why I don&#39;t reach for it by default. You need an identity for everything (what&#39;s the id? what about entities without one?), you need to teach the cache how to normalize each response shape, and nested relationships get fiddly. You&#39;ve essentially adopted a client-side ORM. For a CRM-scale app with heavy entity reuse and live updates, that&#39;s a good trade. For most apps, it&#39;s a lot of machinery for a problem you can solve more cheaply.</p>
<h2>Option two: let the query cache own it, invalidate precisely</h2>
<p>The cheaper answer — and the one this series defaults to, because it uses <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a> — is a <em>document</em> cache rather than a normalized one. Data is cached per query key, not per entity. <code>[&#39;customers&#39;]</code> is one cache entry; <code>[&#39;customer&#39;, 42]</code> is another. There genuinely are two copies of customer <code>#42</code>. The trick is that you don&#39;t try to keep them manually in sync — you make the cache re-fetch the affected keys after a change, and you do it <em>surgically</em>.</p>
<p>The blunt instrument everyone reaches for first:</p>
<pre><code class="language-ts">// after editing a customer — refetch literally everything customer-related
await queryClient.invalidateQueries();
</code></pre>
<p>This &quot;works&quot; and it&#39;s a performance and UX disaster. Every table, every dropdown, every cached query on the screen refetches, spinners bloom everywhere, and you&#39;ve turned one edit into a network storm. Invalidation is not a hammer. It&#39;s a scalpel:</p>
<pre><code class="language-ts">const mutation = useMutation({
  mutationFn: updateCustomer,
  onSuccess: (updated) =&gt; {
    // 1. patch the detail cache directly — no refetch needed, we have the data
    queryClient.setQueryData([&#39;customer&#39;, updated.id], updated);

    // 2. mark just the list stale, so it refetches when next observed
    queryClient.invalidateQueries({ queryKey: [&#39;customers&#39;], exact: true });
  },
});
</code></pre>
<p>Two precise moves. <code>setQueryData</code> writes the fresh entity straight into the detail cache, so the detail page updates instantly with zero network. <code>invalidateQueries</code> with a specific key marks only the list as stale — and critically, TanStack Query won&#39;t even refetch it until something is actually looking at it. Nothing else on the page moves. That precision is the whole skill. The library gives you a scalpel; most bugs come from swinging it like a hammer.</p>
<h2>The mental shift: query keys are your schema</h2>
<p>Here&#39;s the reframe that made this click for me. In a normalized cache, your entity ids <em>are</em> the schema — everything hangs off <code>customers[42]</code>. In a document cache, your <strong>query keys</strong> play that role. The structure and discipline of your query keys is what makes precise invalidation possible or impossible.</p>
<p>If your keys are ad hoc — <code>[&#39;customers&#39;]</code> here, <code>[&#39;customerList&#39;, filters]</code> there, <code>[&#39;allCustomers&#39;]</code> in a third place — you can&#39;t invalidate precisely because you can&#39;t <em>describe</em> the set of things that went stale. But if keys are hierarchical and consistent, invalidation becomes expressive:</p>
<pre><code class="language-ts">// a consistent, hierarchical key factory
const customerKeys = {
  all: [&#39;customers&#39;] as const,
  lists: () =&gt; [...customerKeys.all, &#39;list&#39;] as const,
  list: (filters: Filters) =&gt; [...customerKeys.lists(), filters] as const,
  details: () =&gt; [...customerKeys.all, &#39;detail&#39;] as const,
  detail: (id: number) =&gt; [...customerKeys.details(), id] as const,
};

// invalidate every customer list, regardless of filters, and nothing else
queryClient.invalidateQueries({ queryKey: customerKeys.lists() });
</code></pre>
<p>Because TanStack Query matches keys by prefix, <code>customerKeys.lists()</code> invalidates every filtered list variant in one call without touching a single detail cache. The key factory turns &quot;which things are now stale&quot; into a sentence you can write. This is the same instinct as a good <a href="https://ma-x.im/blog/react-playbook-module-dependencies">public API or module boundary</a>: a little structure up front makes the operations you&#39;ll do a thousand times both possible and safe.</p>
<h2>How to actually choose</h2>
<p>I&#39;ll be direct, because this is where people waste the most effort. Reach for a <strong>normalized cache</strong> when three things are true together: the same entities are reused heavily across many views, they update frequently (real-time, collaborative, or lots of mutations), and staleness between views is genuinely user-visible and unacceptable. Trading dashboards, chat apps, collaborative editors, big CRMs — normalization earns its keep there.</p>
<p>For everything else — which is most apps — a <strong>document cache with disciplined query keys and precise invalidation</strong> is not a compromise, it&#39;s the right call. It has less machinery, less to learn, fewer ways to corrupt the cache, and it scales further than people expect. Honestly, the number of teams who adopted a full normalized cache and then used it like a document cache anyway is not small. Start with the simpler model and let real pain — not anticipated pain — push you to the heavier one.</p>
<p>Either way, the principle underneath both is identical, and it&#39;s the thing to actually remember: <strong>there is one truth, it lives on the server, and your job is to hold one coherent copy of it — not five copies you hope agree.</strong> Normalization enforces that with entity ids. A query cache enforces it with keys and invalidation. Pick your mechanism, but don&#39;t skip the principle, because &quot;the name didn&#39;t update&quot; is what skipping it looks like in the bug tracker.</p>
<p>Once you&#39;ve got one coherent copy and precise invalidation, a door opens that used to be terrifying: updating the UI <em>before</em> the server confirms, and rolling back cleanly if it rejects. That&#39;s optimistic UI, and it&#39;s only sane once your cache has a single source of truth to optimistically edit and a precise way to reconcile when the truth comes back. That&#39;s next.</p>
<p>If your app has a &quot;why is this showing the old value&quot; bug right now, I&#39;d bet it&#39;s a duplication problem wearing a staleness costume. Tell me where the copies live — list and detail, cache and store, two tabs — and I&#39;ll tell you which of these two models you actually needed.</p>
]]></content:encoded>
      <pubDate>Wed, 15 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>localStorage Is Not State: Persistence, Hydration, and Tabs That Fight Each Other</title>
      <link>https://ma-x.im/blog/react-playbook-local-storage-and-state</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-local-storage-and-state</guid>
      <description>localStorage looks like a global variable that survives a refresh. That framing is the bug. Treat it as a persistence layer that mirrors your state — not as the state itself — and the hydration mismatches, stale tabs, and schema breakages mostly disappear.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-local-storage-and-state/cover.png" alt="localStorage Is Not State: Persistence, Hydration, and Tabs That Fight Each Other" /></p>
<p>At the end of the <a href="https://ma-x.im/blog/react-playbook-ui-kit-versioning">last article</a> I said I wanted to start this run with the deceptively simple persistence question: what belongs in localStorage, what belongs in state, and what breaks when you pretend they&#39;re the same thing. It sounds like a beginner question. It isn&#39;t. I&#39;ve seen senior codebases get this wrong in ways that produce flickering UIs, tabs that overwrite each other, and a crash the first time someone ships a new version of the app.</p>
<p>The root mistake is almost always the same, and it&#39;s a framing problem. <code>localStorage</code> looks like a global variable that happens to survive a refresh. <code>localStorage.getItem(&#39;theme&#39;)</code> reads like <code>window.theme</code>. So people start treating it as a place to <em>keep</em> state — reading from it in render, writing to it inline, scattering <code>getItem</code>/<code>setItem</code> across components. That framing is the bug. Everything downstream goes wrong because of it.</p>
<h2>Three concerns people mash into one</h2>
<p>When someone says &quot;I&#39;ll just store it in localStorage,&quot; they&#39;re usually blending three separate things that have nothing to do with each other:</p>
<p><strong>Source of truth</strong> — where the current value lives <em>right now</em>, in memory, reactive, driving the UI. That&#39;s your state (a store atom, context, <code>useState</code>).</p>
<p><strong>Persistence</strong> — how that value survives a reload. That&#39;s storage: localStorage, IndexedDB, a cookie.</p>
<p><strong>Cross-tab sync</strong> — how two open tabs agree on the value. That&#39;s a messaging problem.</p>
<p>localStorage can only honestly do the middle one. It is not reactive — writing to it doesn&#39;t re-render anything. It is synchronous and string-only. And it&#39;s shared across tabs in a way that will surprise you. The moment you ask it to be your source of truth, you&#39;ve handed the hardest job to the tool least suited for it.</p>
<blockquote>
<p>Storage is a mirror of your state, not the state itself. The state lives in memory and drives the UI; storage just remembers what it was so you can restore it later.</p>
</blockquote>
<p>Once that sentence is the rule, the architecture falls out of it. State is the thing components read and write. Storage is a side effect: you hydrate <em>from</em> it once at startup, and you persist <em>to</em> it when state changes. Components never touch storage directly.</p>
<h2>The render-time read that breaks SSR</h2>
<p>Here&#39;s the most common concrete failure, and it&#39;s worth seeing clearly. Someone initializes state straight from storage:</p>
<pre><code class="language-tsx">// looks fine, quietly broken
function useTheme() {
  const [theme, setTheme] = useState(localStorage.getItem(&#39;theme&#39;) ?? &#39;light&#39;);
  // ...
}
</code></pre>
<p>On a purely client-rendered app this mostly works. On anything server-rendered — Next.js, which is what this whole series builds on — it detonates two ways. First, <code>localStorage</code> doesn&#39;t exist on the server, so the render throws. Second, even if you guard it, the server has no idea what&#39;s in the user&#39;s storage, so it renders <code>light</code>, the client hydrates with <code>dark</code>, and React screams about a hydration mismatch — or worse, silently flips the UI after paint. That flash of the wrong theme is the visual signature of &quot;someone read storage during render.&quot;</p>
<p>The fix follows directly from the mirror rule. Render from state with a stable default, then hydrate from storage in an effect, after the first paint:</p>
<pre><code class="language-tsx">function useTheme() {
  const [theme, setThemeState] = useState&lt;&#39;light&#39; | &#39;dark&#39;&gt;(&#39;light&#39;);

  // hydrate once, on the client only
  useEffect(() =&gt; {
    const stored = localStorage.getItem(&#39;theme&#39;);
    if (stored === &#39;light&#39; || stored === &#39;dark&#39;) setThemeState(stored);
  }, []);

  const setTheme = useCallback((next: &#39;light&#39; | &#39;dark&#39;) =&gt; {
    setThemeState(next);
    localStorage.setItem(&#39;theme&#39;, next); // persist as a side effect
  }, []);

  return [theme, setTheme] as const;
}
</code></pre>
<p>Server and client both start at <code>light</code>, so hydration matches. The stored value takes over a tick later. If the theme flash bothers you, that&#39;s a real concern — but you solve it with a tiny inline script in the document head that sets a class before React boots, not by reading storage during React&#39;s render. The two problems stay separate.</p>
<h2>Persist the store, not a hundred components</h2>
<p>Doing this hook-by-hook gets old fast, and it scatters <code>setItem</code> calls everywhere — which is exactly the kind of smeared side effect that makes a codebase hard to reason about. Since this series uses <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">Reatom for state</a>, persistence belongs at the store layer, as one declarative concern, not a hundred manual writes.</p>
<pre><code class="language-ts">import { atom } from &#39;@reatom/core&#39;;
import { withLocalStorage } from &#39;@reatom/persist-web-storage&#39;;

// the atom is the source of truth; localStorage is a declared mirror of it
export const themeAtom = atom&lt;&#39;light&#39; | &#39;dark&#39;&gt;(&#39;light&#39;, &#39;themeAtom&#39;).pipe(
  withLocalStorage(&#39;theme&#39;),
);
</code></pre>
<p>Now the atom is where the value lives and where the UI reads it. The persistence adapter mirrors every change to localStorage and rehydrates on load — including, importantly, listening for changes from <em>other tabs</em>. You wrote zero <code>getItem</code>/<code>setItem</code> calls. The concern lives in one place you can see, test, and change. This is the same principle from the <a href="https://ma-x.im/blog/react-playbook-composition-and-di">composition and DI article</a>: don&#39;t let components reach out and grab infrastructure; hand it to them, or push it to a layer that owns it.</p>
<h2>Two tabs, one storage, a fight</h2>
<p>The surprise that catches people: localStorage is shared across every tab on the same origin. Open your app in two tabs, change the theme in one, and the other tab&#39;s <em>in-memory state</em> has no idea. Its localStorage updated underneath it, but React state didn&#39;t move. The two tabs now disagree, and whichever one writes next wins, clobbering the other.</p>
<p>The browser gives you exactly one tool for this — the <code>storage</code> event, which fires in <em>other</em> tabs (never the one that made the change):</p>
<pre><code class="language-ts">useEffect(() =&gt; {
  const onStorage = (e: StorageEvent) =&gt; {
    if (e.key === &#39;theme&#39; &amp;&amp; (e.newValue === &#39;light&#39; || e.newValue === &#39;dark&#39;)) {
      setThemeState(e.newValue);
    }
  };
  window.addEventListener(&#39;storage&#39;, onStorage);
  return () =&gt; window.removeEventListener(&#39;storage&#39;, onStorage);
}, []);
</code></pre>
<p>A good persistence adapter (like Reatom&#39;s above) already does this for you, which is another reason to centralize it. But you should know the event exists, because the &quot;tabs fight each other&quot; bug is invisible in development — you almost never have two tabs open while coding — and then very visible in production. If tab sync actually matters for your feature, <code>BroadcastChannel</code> is the more deliberate tool; <code>storage</code> is the free one you get for persisting at all.</p>
<h2>Versioning: the crash that ships with v2</h2>
<p>Here&#39;s the failure that only shows up <em>after</em> you succeed. You persist a settings object. Months later you change its shape — rename a field, split one setting into two. You deploy. Every returning user&#39;s browser hydrates the <em>old</em> shape into code that expects the <em>new</em> shape, and something reads <code>settings.notifications.email</code> on an object where <code>notifications</code> is now a string. Crash. On load. For existing users only — which is why your testing missed it.</p>
<p>Persisted data is data you shipped in the past, running against code you shipped today. It needs a version and a migration path, exactly like a database:</p>
<pre><code class="language-ts">type StoredSettings = { version: number; theme: &#39;light&#39; | &#39;dark&#39;; density: &#39;cozy&#39; | &#39;compact&#39; };

function loadSettings(): StoredSettings {
  const raw = localStorage.getItem(&#39;settings&#39;);
  const parsed = raw ? JSON.parse(raw) : null;

  // no data, or an old shape we no longer trust → start clean
  if (!parsed || parsed.version !== 2) {
    return { version: 2, theme: &#39;light&#39;, density: &#39;cozy&#39; };
  }
  return parsed;
}
</code></pre>
<p>Reject-and-reset is the cheapest correct answer for preferences — losing someone&#39;s theme choice is not a tragedy. For anything you can&#39;t throw away, write real migrations keyed on <code>version</code>. Either way, the non-negotiable part is: <strong>never trust the shape of persisted data</strong>. It came from a version of your app you don&#39;t control anymore.</p>
<h2>So what actually goes in there</h2>
<p>Pulling it together, the fit test is simple: store things the client owns and the client alone can restore. Theme and UI preferences. Sidebar collapsed or expanded. A draft of a form the user hasn&#39;t submitted. The last route they were on. Small, client-owned, non-sensitive, and fine to lose in the worst case.</p>
<p>What doesn&#39;t belong: anything sensitive (localStorage is readable by any script on the page — a token there is a token exposed to every XSS), anything large (multi-megabyte blobs want IndexedDB, which is async and built for size), and — the big one — server data. The list of customers, the current user&#39;s profile, the order you just fetched: that data is not yours. It has an owner on the backend, and your copy is a <em>cache</em>, not a persisted preference. Stuffing it into localStorage means you now have two stale copies fighting instead of one.</p>
<p>And that&#39;s the seam into the next article. Everything here was about state the client <em>owns</em> — you&#39;re the source of truth, storage just remembers it. Server state flips that on its head: the source of truth lives somewhere else, the same entity shows up in five different responses, and &quot;persistence&quot; stops being the problem while &quot;which copy is correct&quot; becomes it. That&#39;s data normalization and cache invalidation, and it&#39;s where a lot of otherwise-clean apps quietly turn to mush.</p>
<p>If you&#39;ve got a persistence bug you can&#39;t quite pin down — a flash on load, a tab that resets another, a crash that only hits returning users — tell me the symptom. Nine times out of ten it traces straight back to treating storage as state, and I&#39;d like to hear the tenth.</p>
]]></content:encoded>
      <pubDate>Mon, 13 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Keeping a UI Kit Updated Across Apps: Versioning, Contracts, and Trust</title>
      <link>https://ma-x.im/blog/react-playbook-ui-kit-versioning</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-ui-kit-versioning</guid>
      <description>A shared UI kit does not fail because the button component is hard to write. It fails when five applications are afraid to upgrade it. Versioning, contract tests, release channels, and migration paths are what turn a component library from a dependency into a product people can trust.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-ui-kit-versioning/cover.png" alt="Keeping a UI Kit Updated Across Apps: Versioning, Contracts, and Trust" /></p>
<p>The hardest part of a UI kit is not writing the components. That&#39;s the trap. You can build a clean <code>&lt;Button&gt;</code>, a flexible <code>&lt;Select&gt;</code>, a beautiful <code>&lt;Modal&gt;</code>, publish them to a package, and feel like the system exists. Then six months later one app is on version 1.4, another is on 2.1, a third has copied half the styles locally, and everyone is scared to upgrade because nobody knows what will break.</p>
<p>At that point the UI kit did not fail as a component library. It failed as a product.</p>
<p>The <a href="https://ma-x.im/blog/react-playbook-changing-shared-components">previous article</a> was about changing a shared component inside one codebase, where you can grep every call site and run a codemod yourself. This is the harder version: several applications consume the kit as a versioned package. You can&#39;t see every usage. You can&#39;t update every app in one pull request. You can&#39;t assume every team upgrades on the same day. So the question changes from &quot;how do I make the component better?&quot; to &quot;how do I make upgrades safe enough that people keep taking them?&quot;</p>
<p>That&#39;s the real work.</p>
<h2>A UI kit is not shared until upgrades are routine</h2>
<p>A lot of teams treat &quot;published package&quot; as the finish line. It isn&#39;t. A package can be shared and still be effectively frozen. If every upgrade requires a migration meeting, manual QA across five apps, and someone brave enough to merge first, the kit is not healthy. It&#39;s a museum.</p>
<p>The signal I care about is boring: can an app upgrade from 2.3.1 to 2.3.2 on a normal Tuesday without drama? Can it take a minor version without reading every diff? Can it delay a major version without blocking everyone else? If the answer is yes, the UI kit is alive. If the answer is no, every improvement you make piles up behind the same wall: adoption risk.</p>
<blockquote>
<p>A design system is only as real as its upgrade path. If people cannot safely move to the next version, the new version mostly exists in your repository.</p>
</blockquote>
<p>This is why &quot;one version everywhere&quot; is a nice goal and a terrible starting assumption. You may want every app on the latest version. You probably should. But the mechanism that gets you there is not hope. It&#39;s compatibility, contracts, release discipline, and migration support.</p>
<h2>Semver is a promise, not a decoration</h2>
<p>Version numbers are not vibes. They are a communication system between the kit and every app that depends on it. If you treat them casually, people stop trusting them.</p>
<p>The contract is simple:</p>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;@company/ui-kit&quot;,
  &quot;version&quot;: &quot;2.4.0&quot;
}
</code></pre>
<p>A patch release fixes bugs and should be safe. A minor release adds capabilities without breaking existing usage. A major release may require changes, and the migration path must be explicit. That&#39;s semver on paper. In practice, the hard part is being honest about what counts as breaking.</p>
<p>Changing a prop type is obviously breaking. Removing a component is breaking. But so is changing keyboard behavior in a menu. So is changing focus trapping in a modal. So is changing the DOM structure if consumers style nested elements, even if you wish they didn&#39;t. So is renaming a CSS variable that apps rely on. UI libraries have more contracts than TypeScript can see.</p>
<p>That means the rule has to be conservative: if a reasonable consumer could notice the change in code, behavior, accessibility, or visual output, treat it as a contract change. Maybe it&#39;s still worth doing. Just don&#39;t smuggle it into a patch release and call the upgrade safe.</p>
<h2>Public API must be smaller than the folder tree</h2>
<p>This connects directly back to the <a href="https://ma-x.im/blog/react-playbook-barrel-files">barrel files article</a>. A UI kit needs a public API, but that API has to be intentional. If apps can import from anywhere inside the package, every internal file becomes a promise you didn&#39;t mean to make.</p>
<p>Bad shape:</p>
<pre><code class="language-tsx">import { Button } from &#39;@company/ui-kit/src/components/button/button&#39;;
import { buttonRecipe } from &#39;@company/ui-kit/src/components/button/styles&#39;;
import { useModalMachine } from &#39;@company/ui-kit/src/internal/modal-machine&#39;;
</code></pre>
<p>Now your internal structure is part of the external contract. You can&#39;t reorganize files. You can&#39;t swap the modal implementation. You can&#39;t rename a style recipe. The consuming apps have turned your source tree into their API.</p>
<p>Good shape:</p>
<pre><code class="language-tsx">import { Button, Dialog, Select } from &#39;@company/ui-kit&#39;;
import { useCombobox } from &#39;@company/ui-kit/headless&#39;;
import { tokens } from &#39;@company/ui-kit/tokens&#39;;
</code></pre>
<p>Three doors. Components, headless behavior, tokens. Everything else is private. If someone needs a new door, you add it intentionally. You don&#39;t let them crawl through a window because the import path happened to work.</p>
<p>In package terms, that means using <code>exports</code> to make the boundary real:</p>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;@company/ui-kit&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./dist/index.js&quot;,
    &quot;./headless&quot;: &quot;./dist/headless/index.js&quot;,
    &quot;./tokens&quot;: &quot;./dist/tokens/index.js&quot;
  }
}
</code></pre>
<p>This is a small detail with a large consequence. You can only version what you control. A tight public API gives you something finite to protect.</p>
<h2>Contract tests are how apps talk back</h2>
<p>The uncomfortable part of maintaining a UI kit across apps is that unit tests inside the kit are not enough. They tell you your component works according to your assumptions. They do not tell you whether the apps use it in ways you forgot to protect.</p>
<p>The answer is not to pull every application into your test suite. That turns the kit into a build-system hostage. The better answer is a thin layer of contract tests: small fixtures that represent the promises the kit makes to consumers.</p>
<pre><code class="language-tsx">// contracts/dialog.contract.tsx
import { render, screen } from &#39;@testing-library/react&#39;;
import userEvent from &#39;@testing-library/user-event&#39;;
import { Dialog } from &#39;@company/ui-kit&#39;;

it(&#39;keeps focus inside the dialog until it closes&#39;, async () =&gt; {
  render(
    &lt;Dialog open title=&quot;Delete customer&quot;&gt;
      &lt;button&gt;Cancel&lt;/button&gt;
      &lt;button&gt;Delete&lt;/button&gt;
    &lt;/Dialog&gt;,
  );

  await userEvent.tab();
  expect(screen.getByRole(&#39;button&#39;, { name: &#39;Cancel&#39; })).toHaveFocus();

  await userEvent.tab();
  expect(screen.getByRole(&#39;button&#39;, { name: &#39;Delete&#39; })).toHaveFocus();
});
</code></pre>
<p>That test is not checking implementation. It is checking a consumer-visible promise: focus stays inside the dialog. If you rewrite the internals from one focus-trap library to another, the contract should still pass. If it doesn&#39;t, you didn&#39;t make a refactor. You changed behavior.</p>
<p>You need contracts in four layers:</p>
<p><strong>Type contracts.</strong> A small TypeScript test project that imports the public API the way apps do. If exported types break, the kit fails before publishing.</p>
<p><strong>Behavior contracts.</strong> Testing Library tests for keyboard behavior, focus, ARIA, controlled/uncontrolled state, and callback order.</p>
<p><strong>Visual contracts.</strong> Storybook or fixture pages with visual regression on the stable states: default, hover, disabled, loading, error, long text, right-to-left if you support it.</p>
<p><strong>Token contracts.</strong> Tests that verify CSS variables and design tokens still exist with expected names. Tokens are API. Treat them that way.</p>
<p>Not every component needs all four layers. A plain <code>Badge</code> probably needs visual and token coverage. A <code>Combobox</code> needs behavior and accessibility coverage badly. The point is to test the promises consumers depend on, not every internal branch.</p>
<h2>Release channels keep risk from spreading</h2>
<p>If every release goes straight to <code>latest</code>, your first consumer becomes the canary whether they agreed to it or not. That&#39;s not fair to them, and it trains people to wait. A better release flow gives the kit a place to be wrong before the stable version moves.</p>
<p>For example:</p>
<pre><code class="language-bash"># experimental builds for early adopters
npm publish --tag next

# stable builds after contracts and canary apps pass
npm publish --tag latest
</code></pre>
<p>The <code>next</code> tag is where breaking work, risky rewrites, and big visual changes can be tested by one or two willing apps. <code>latest</code> is the calm channel. It should feel boring. If a team installs <code>latest</code> and gets surprise churn, they will stop trusting the kit. And when trust goes, adoption goes with it.</p>
<p>This does not require a huge platform. Start with one canary app — ideally the app that uses the kit heavily and has the fastest feedback loop. Upgrade it first. Run its smoke tests. Look at the core screens. Fix the kit before asking everyone else to move. The canary app is not there to prove the kit is perfect. It is there to catch the obvious problems while the blast radius is still small.</p>
<h2>Migration support is part of the feature</h2>
<p>A breaking release without a migration path is not a release. It&#39;s homework assigned to every consuming app.</p>
<p>If you remove <code>Button color</code> in favor of <code>Button intent</code>, the release should include more than a changelog sentence. It should include the same tools from the previous article, but packaged for consumers who live outside your repo:</p>
<pre><code class="language-json">{
  &quot;scripts&quot;: {
    &quot;migrate:ui-kit&quot;: &quot;npx @company/ui-kit-codemods button-color-to-intent src&quot;
  }
}
</code></pre>
<p>The codemod can ship as a small companion package. The migration guide should show old usage, new usage, edge cases, and what the script can and cannot fix. The deprecation should exist for at least one minor cycle before the major removal, unless the old behavior is actively dangerous. And the major release should be boring precisely because the hard work already happened while the old API still existed.</p>
<p>Honestly, this is where many UI kits lose people. The maintainers ship a clean new API and assume consumers will be grateful. Consumers look at their backlog, see forty files to touch, and pin the old version for another year. The new API may be better, but the upgrade experience is worse, so nobody moves.</p>
<h2>Documentation has to be versioned too</h2>
<p>One small but painful failure mode: the documentation shows the latest version, but half the apps are still on an older one. A developer opens the docs, copies an example, and the prop doesn&#39;t exist. Now the UI kit feels broken even though the app is just behind.</p>
<p>If multiple apps can be on different versions, the docs need to acknowledge that reality. At minimum, every docs page should clearly say which version it describes. Better: keep versioned docs for major versions, or at least preserve migration pages long after the release goes out. A migration guide is most useful six months after you wrote it, when the last app finally upgrades.</p>
<p>This also applies to design tokens and screenshots. If your docs render from the latest package but an app is on v2, the visual examples can lie. Sometimes that&#39;s acceptable. Sometimes it creates bugs. Be explicit either way.</p>
<h2>The rule I use: optimize for trust</h2>
<p>There are many tactical choices here — monorepo or separate repo, Changesets or semantic-release, Storybook or Ladle, visual regression provider A or B. Those choices matter, but they&#39;re not the center. Trust is the center.</p>
<p>Trust means a patch release doesn&#39;t redesign the app. Trust means a minor release doesn&#39;t remove a prop. Trust means a major release comes with a migration path. Trust means public imports stay public and private imports are actually blocked. Trust means if a component&#39;s behavior changes, there is a test that calls it a behavior change. Trust means teams believe the kit maintainers are trying to make upgrades smaller, not throw work over the wall.</p>
<p>Once that trust exists, you can improve the UI kit continuously. Without it, every improvement becomes suspicious. People fork. They pin. They copy styles locally. They build one-off replacements because waiting for the shared kit feels slower than escaping it. That is how a design system dies: not from one bad component, but from a long series of upgrades people chose not to take.</p>
<p>This closes the component-patterns run: compound components for structure, headless components for behavior, incremental migration inside one repo, and now versioned upgrades across many apps. Next I want to move into state and data at scale, starting with the deceptively simple persistence question: what belongs in local storage, what belongs in global state, and what breaks when you pretend they&#39;re the same thing.</p>
<p>If your UI kit is stuck on an old version somewhere, I&#39;m curious what the real blocker is. Is it semver trust, missing migration tools, visual regression fear, or just nobody owning the upgrade? Send me the ugly reason. That&#39;s usually where the architecture lives.</p>
]]></content:encoded>
      <pubDate>Fri, 10 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Changing a Component Everyone Uses: Deprecation, Codemods, and No Big-Bang Rewrites</title>
      <link>https://ma-x.im/blog/react-playbook-changing-shared-components</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-changing-shared-components</guid>
      <description>Writing the new version of a shared component is the easy 10%. The other 90% is the hundred call sites that already depend on the old one — and the fear of touching them. Here&apos;s how I change a component everyone uses without a month-long rewrite that breaks the day it lands.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-changing-shared-components/cover.png" alt="Changing a Component Everyone Uses: Deprecation, Codemods, and No Big-Bang Rewrites" /></p>
<p>At the end of the <a href="https://ma-x.im/blog/react-playbook-headless-components">last article</a> I told you to go find the one component everyone reimplements slightly differently. So let&#39;s say you found it. You know exactly how it should work now. There&#39;s just one problem: a hundred files already import the old one, and half of them belong to teams you&#39;ve never met.</p>
<p>This is the part of building shared components nobody puts on the roadmap. Writing the new <code>&lt;Button&gt;</code> is the easy 10%. The other 90% is every place that already calls the old <code>&lt;Button&gt;</code>, and the very specific fear that comes with editing code you don&#39;t own to fix a component that, technically, still works. That fear is why so many &quot;we&#39;ll clean this up later&quot; components never get cleaned up. The change isn&#39;t hard. Changing it <em>everywhere, safely, without a flag day</em> is hard.</p>
<p>So this article isn&#39;t about how to design a better component. It&#39;s about how to move an existing one forward when it already has dozens of dependents — deprecation paths, codemods, and the discipline that lets you touch a hundred call sites without holding your breath.</p>
<h2>The big-bang rewrite is the trap</h2>
<p>Here&#39;s the instinct I have to talk teams out of constantly. You look at the old component, you see everything wrong with it, and you decide to rewrite it properly on a branch. Two weeks later that branch has touched sixty files, it&#39;s three hundred commits behind main, every rebase is a knife fight, and the only way to land it is a single terrifying merge that changes everything at once. If anything breaks — and something always breaks — you can&#39;t tell which of the sixty changes did it.</p>
<p>That&#39;s a flag day: a moment where the whole codebase has to flip from old to new simultaneously. Flag days feel decisive and they are almost always a mistake. The problem isn&#39;t your ability to write the new component. The problem is that you tried to <em>break</em> and <em>rebuild</em> in the same step, so there&#39;s never a moment where the system is in a known-good state. You&#39;re either fully migrated or fully broken, with nothing in between.</p>
<p>The fix is to refuse the whole framing. You don&#39;t rewrite. You <em>evolve</em>.</p>
<blockquote>
<p>You never break and rebuild in one step. You add the new path, move callers over one at a time, then delete the old path. Three boring, reversible steps instead of one heroic irreversible one.</p>
</blockquote>
<p>That shape has a few names — parallel change, expand/contract, the strangler pattern — but at the component level it&#39;s just this: <strong>expand</strong> the component so old and new both work, <strong>migrate</strong> the call sites gradually, then <strong>contract</strong> by removing the old way once nothing uses it. Every intermediate state ships. Every step is revertible on its own. Nothing has a flag day.</p>
<h2>Step one: make the change additive</h2>
<p>The whole strategy depends on old and new coexisting for a while. So the first move is to add the new API <em>next to</em> the old one instead of replacing it. Say I&#39;m moving a <code>&lt;Button&gt;</code> from a raw <code>color</code> prop to a semantic <code>intent</code> prop — because <code>color=&quot;green&quot;</code> tells you nothing about <em>why</em> it&#39;s green, and the theme can&#39;t remap it.</p>
<p>The tempting move is to rename <code>color</code> to <code>intent</code> and let TypeScript light up a hundred errors. Don&#39;t. Add <code>intent</code>, keep <code>color</code>, and translate internally:</p>
<pre><code class="language-tsx">type ButtonProps = {
  /** @deprecated Use `intent` instead. `color` is removed in v3. */
  color?: &#39;green&#39; | &#39;red&#39; | &#39;gray&#39;;
  intent?: &#39;success&#39; | &#39;danger&#39; | &#39;neutral&#39;;
  children: React.ReactNode;
};

const colorToIntent = {
  green: &#39;success&#39;,
  red: &#39;danger&#39;,
  gray: &#39;neutral&#39;,
} as const;

export function Button({ color, intent, children }: ButtonProps) {
  // new prop wins; old prop still works via a mapping
  const resolved = intent ?? (color ? colorToIntent[color] : &#39;neutral&#39;);

  if (process.env.NODE_ENV !== &#39;production&#39; &amp;&amp; color &amp;&amp; !intent) {
    console.warn(
      `Button: the \`color\` prop is deprecated. Use intent=&quot;${colorToIntent[color]}&quot; instead.`,
    );
  }

  return &lt;button className={intentClasses[resolved]}&gt;{children}&lt;/button&gt;;
}
</code></pre>
<p>Nobody&#39;s build breaks. Every existing <code>color=&quot;green&quot;</code> keeps rendering exactly as before. New code uses <code>intent</code>. And critically, the component itself now <em>knows</em> the mapping from old to new — which is going to matter in about thirty seconds when we automate the migration.</p>
<blockquote>
<p>A backward-compatible change buys you time, and time is the one thing a big-bang rewrite refuses to give you. Coexistence isn&#39;t a compromise. It&#39;s the whole plan.</p>
</blockquote>
<h2>Step two: deprecation is a message, not a delete</h2>
<p>Marking something <code>@deprecated</code> is not the same as deleting it, and treating them as the same thing is why deprecations get ignored. A deprecation is a <em>communication channel</em>. Its entire job is to tell the next person who touches this code what to do instead — clearly, at the exact moment they&#39;d otherwise reach for the old thing.</p>
<p>That JSDoc <code>@deprecated</code> tag isn&#39;t decorative. Editors strike through the prop and surface the message on hover, so a developer typing <code>color=</code> sees the replacement before they even hit save. The runtime warning catches the code that&#39;s already written. And if you want teeth, an ESLint rule turns &quot;please stop&quot; into &quot;the build is yellow until you stop&quot;:</p>
<pre><code class="language-js">// eslint config — nudge, don&#39;t block yet
&#39;no-restricted-syntax&#39;: [
  &#39;warn&#39;,
  {
    selector: &#39;JSXAttribute[name.name=&quot;color&quot;]&#39;,
    message: &#39;Button: the color prop is deprecated — use intent. Run the codemod: npx tsx scripts/button-color-to-intent.ts&#39;,
  },
],
</code></pre>
<p>The one rule I hold to: <strong>a deprecation must always name its replacement.</strong> &quot;This is deprecated&quot; with no path forward isn&#39;t a deprecation, it&#39;s nagging. Every warning I write ends with the exact thing to type instead, and ideally the command that&#39;ll do it for you. Which brings us to that command.</p>
<h2>Step three: codemods move the boring changes for you</h2>
<p>Here&#39;s where the hundred call sites stop being scary. If the change is mechanical — <code>color=&quot;green&quot;</code> becomes <code>intent=&quot;success&quot;</code>, every time, no judgment required — then asking humans to do it a hundred times is both slow and a source of typos. A codemod is a script that rewrites source code by editing its syntax tree, and this is exactly what they&#39;re for.</p>
<pre><code class="language-ts">// scripts/button-color-to-intent.ts — jscodeshift transform
import type { API, FileInfo } from &#39;jscodeshift&#39;;

const map: Record&lt;string, string&gt; = { green: &#39;success&#39;, red: &#39;danger&#39;, gray: &#39;neutral&#39; };

export default function transform(file: FileInfo, api: API) {
  const j = api.jscodeshift;
  const root = j(file.source);

  root
    .find(j.JSXAttribute, { name: { name: &#39;color&#39; } })
    // only touch color props on &lt;Button&gt;
    .filter((path) =&gt; {
      const el = path.parent.node;
      return el.name?.type === &#39;JSXIdentifier&#39; &amp;&amp; el.name.name === &#39;Button&#39;;
    })
    .forEach((path) =&gt; {
      const value = path.node.value;
      if (value?.type === &#39;Literal&#39; &amp;&amp; typeof value.value === &#39;string&#39; &amp;&amp; map[value.value]) {
        path.node.name.name = &#39;intent&#39;;
        value.value = map[value.value];
      }
    });

  return root.toSource();
}
</code></pre>
<p>Run it across the repo and every <code>&lt;Button color=&quot;green&quot;&gt;</code> becomes <code>&lt;Button intent=&quot;success&quot;&gt;</code> in one commit — a commit a reviewer can actually read, because the diff is uniform and mechanical. That&#39;s the real payoff: not just speed, but a <em>reviewable</em> migration instead of a hundred hand-edited files where any one of them could hide a mistake.</p>
<p>Codemods aren&#39;t always the answer, though, and I&#39;ve watched people burn a day writing one for a change they could&#39;ve done by hand in ten minutes. My test is two questions. Are there enough call sites that doing it manually is genuinely painful — dozens, not five? And is the transform <em>mechanical</em>, with no case where a human has to decide what the right replacement is? If either answer is no, skip the codemod. When the change needs judgment — &quot;replace this prop, but only where the old behavior was actually intended&quot; — a script can&#39;t make that call, and pretending it can just moves the bugs somewhere quieter.</p>
<h2>When it can&#39;t be additive: version it side by side</h2>
<p>Sometimes the new component isn&#39;t a tweaked version of the old one — it&#39;s a different thing that happens to share a name. A <code>&lt;DataTable&gt;</code> that moves from a props-driven column config to a headless row model isn&#39;t a new prop; it&#39;s a new mental model. You can&#39;t map the old API onto the new one, so coexistence-through-props doesn&#39;t work.</p>
<p>That&#39;s when you version at the module level instead of the prop level. Ship the new one alongside the old under its own name, and let call sites move when they&#39;re ready:</p>
<pre><code class="language-tsx">// old one keeps living, untouched, so nothing breaks
export { DataTable } from &#39;./data-table-legacy&#39;;
// new one ships next to it under a clear name
export { DataTableV2 } from &#39;./data-table-v2&#39;;
</code></pre>
<p>It&#39;s the same expand/migrate/contract shape, just one level up. Both exist. Features migrate to <code>DataTableV2</code> on their own schedule, each migration a small reviewable change instead of a codebase-wide flip. When the last import of the legacy one is gone, you delete it. The name <code>V2</code> is ugly on purpose — it&#39;s a temporary state that&#39;s supposed to disappear, and its ugliness is a standing reminder that the migration isn&#39;t finished. If <code>DataTableV2</code> is still around in a year, that&#39;s a signal, not a feature.</p>
<h2>Step four: contract — actually delete the old thing</h2>
<p>This is the step everyone skips, and skipping it quietly undoes all the discipline that came before. Expand and migrate without contract just leaves you with <em>two</em> ways to do everything, forever, plus a warning nobody reads anymore. The deprecation was a promise to remove the old path. Contract is keeping it.</p>
<p>The good news is that after the additive change, the deprecation, and the codemod, contract is almost anticlimactic. You check that usage has actually hit zero — and you gate it, don&#39;t eyeball it:</p>
<pre><code class="language-bash"># fails CI if a single old-style usage survives
grep -rn &#39;color=&#39; src/ --include=&#39;*.tsx&#39; &amp;&amp; echo &quot;still used — not safe to remove&quot; &amp;&amp; exit 1
</code></pre>
<p>When that returns nothing, you delete the <code>color</code> prop, the mapping, the warning, and the ESLint rule in one small commit. The component gets <em>simpler</em> — which is the tell that you did the whole thing right. A migration that ends with more code than it started with wasn&#39;t a migration, it was an accretion.</p>
<h2>What this really changes</h2>
<p>The thing I want to leave you with isn&#39;t the codemod or the deprecation tag — it&#39;s the reframing. Once you stop seeing &quot;change a shared component&quot; as one scary event and start seeing it as expand, migrate, contract, the fear mostly evaporates. Not because the work got smaller, but because every step is now individually safe and individually revertible. You&#39;re never one merge away from a broken build. You&#39;re always in a known-good state that happens to support two ways of doing something for a while.</p>
<p>And honestly, the willingness to make this move is a large part of what separates a component library people trust from one they&#39;re afraid of. A kit whose owners can evolve it safely keeps getting better. A kit whose owners are scared to touch it ossifies — every rough edge becomes permanent because fixing it feels too risky. The techniques here are what buy you the confidence to keep improving the thing everyone depends on.</p>
<p>Which raises the harder version of the same problem, and where I want to go next: it&#39;s one thing to evolve a component inside <em>one</em> repo where you can grep every call site. It&#39;s another to keep a shared UI kit in sync across <em>several</em> applications that each pull it in as a versioned package — where you can&#39;t see the call sites, can&#39;t run a codemod across them, and &quot;just delete the old prop&quot; can break an app you don&#39;t even have checked out. That&#39;s the next article.</p>
<p>If you&#39;ve got a shared component you&#39;ve been too scared to change, I&#39;d genuinely like to hear what&#39;s holding it back — is it the call-site count, a missing safety net, or just that nobody&#39;s allowed to own it? Reach out and tell me which.</p>
]]></content:encoded>
      <pubDate>Thu, 09 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Headless Components: Give Away the Behavior, Keep None of the Markup</title>
      <link>https://ma-x.im/blog/react-playbook-headless-components</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-headless-components</guid>
      <description>The most reusable UI code I write renders nothing at all. Headless components, render props, and hooks-as-API are three names for one idea: separate what a component does from how it looks — and hand over the behavior without a single opinion about the markup.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-headless-components/cover.png" alt="Headless Components: Give Away the Behavior, Keep None of the Markup" /></p>
<p>Here&#39;s a thing that took me too long to internalize: the most reusable UI code I&#39;ve ever written renders nothing. No <code>&lt;div&gt;</code>, no class name, no markup at all. It just computes behavior and hands it to whoever called it. That sounds backwards — a UI component that produces no UI? — but it&#39;s the idea behind every component library that survived contact with more than one design system.</p>
<p><a href="https://ma-x.im/blog/react-playbook-compound-components">Compound components</a> took us most of the way there: let the caller control structure while the component owns the coordination. But a compound component still ships markup. <code>&lt;Select.Option&gt;</code> renders a <code>div</code>, picks a class, decides on padding. The moment two products with different designs both want that select behavior, that markup becomes a fight. Headless components are the answer to &quot;what if the component gave up the markup entirely and kept only the behavior.&quot;</p>
<h2>The coupling nobody notices until it hurts</h2>
<p>Think about what a <code>&lt;Combobox&gt;</code> actually does. It manages an open/closed state. It tracks the highlighted option. It handles arrow keys, Enter, Escape, typeahead. It wires up the ARIA attributes so screen readers announce it correctly. It filters options as you type. That&#39;s a <em>lot</em> of genuinely hard logic — the kind with edge cases you&#39;ll get wrong the first three times.</p>
<p>And then, almost as an afterthought, it renders some divs with some classes.</p>
<p>The hard part — the behavior — is completely universal. Every combobox everywhere needs the same keyboard handling and the same accessibility wiring. The easy part — the markup and styles — is completely specific. Your combobox looks nothing like mine. Yet a normal component welds those two together, so when I want your combobox&#39;s excellent keyboard handling but my design, I can&#39;t have it. I either fork your styles or reimplement your logic. Both are bad, and I&#39;ve done both.</p>
<blockquote>
<p>The behavior is universal. The markup is personal. A normal component fuses them and forces you to take both or neither.</p>
</blockquote>
<p>Headless components unfuse them. They keep all the hard, universal behavior and give away <em>zero</em> markup, letting you supply the personal part yourself.</p>
<h2>Three shapes of the same idea</h2>
<p>&quot;Headless&quot; isn&#39;t one API. It&#39;s a family of three techniques that all separate behavior from rendering. They&#39;re worth knowing as a set, because you&#39;ll pick between them by feel.</p>
<p><strong>Render props / children-as-a-function.</strong> The component computes state and calls <em>your</em> function to render. It decides nothing about looks.</p>
<pre><code class="language-tsx">&lt;Toggle&gt;
  {({ on, toggle }) =&gt; (
    &lt;button onClick={toggle} className={on ? &#39;bg-[#22c55e]&#39; : &#39;bg-gray-300&#39;}&gt;
      {on ? &#39;On&#39; : &#39;Off&#39;}
    &lt;/button&gt;
  )}
&lt;/Toggle&gt;
</code></pre>
<p><code>Toggle</code> owns the on/off logic. You own every pixel. It renders literally nothing except by calling your function.</p>
<p><strong>Hooks-as-API.</strong> The same idea, but the behavior is a hook instead of a component. This is the modern default, and honestly the cleanest — no wrapper component in the tree at all, just logic you call and props you spread.</p>
<pre><code class="language-tsx">function ColorSwatch() {
  const toggle = useToggle();
  return (
    &lt;button
      onClick={toggle.toggle}
      aria-pressed={toggle.on}
      className={toggle.on ? &#39;bg-[#22c55e]&#39; : &#39;bg-gray-300&#39;}
    &gt;
      {toggle.on ? &#39;On&#39; : &#39;Off&#39;}
    &lt;/button&gt;
  );
}
</code></pre>
<p><strong>Prop getters.</strong> For complex widgets, the hook hands you functions that return the props you need to spread onto your elements — including all the fiddly ARIA and event wiring you&#39;d never remember to add yourself.</p>
<pre><code class="language-tsx">function Search() {
  const { getInputProps, getMenuProps, getItemProps, items, isOpen } = useCombobox({ items: options });
  return (
    &lt;div&gt;
      &lt;input {...getInputProps()} /&gt;
      &lt;ul {...getMenuProps()}&gt;
        {isOpen &amp;&amp; items.map((item, i) =&gt; (
          &lt;li key={item.id} {...getItemProps({ item, index: i })}&gt;{item.label}&lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>You wrote the <code>input</code>, the <code>ul</code>, the <code>li</code> — your elements, your styles. But <code>getInputProps()</code> spread the ARIA roles, the keyboard handlers, the focus management onto them. You got the world-class behavior and kept total control of the markup. That&#39;s the whole promise in one code block, and it&#39;s why libraries like Downshift, React Aria, TanStack Table, and the Radix primitives are built exactly this way.</p>
<h2>Why this is dependency injection wearing a costume</h2>
<p>If this feels familiar, it should. Back in the <a href="https://ma-x.im/blog/react-playbook-composition-and-di">composition and DI article</a> I argued that a component should be handed its dependencies instead of reaching out to grab them. Headless components are that same principle turned inside out: instead of injecting <em>data or services</em> into a component, you&#39;re injecting the <em>rendering</em> into a behavior. The behavior doesn&#39;t reach out and decide how things look — you hand the looks in.</p>
<p>That&#39;s why the same benefits show up. A headless <code>useCombobox</code> is trivially testable, because you can drive its logic without rendering any particular UI. It&#39;s endlessly reusable, because it makes no assumption about markup. And it lets you change your mind about design without touching the logic, because the logic never knew what it looked like in the first place. Separation of concerns stops being a slogan and becomes a physical fact: the concern that&#39;s hard and universal lives in one place, the concern that&#39;s easy and specific lives in yours, and they meet through a thin, explicit interface.</p>
<h2>The cost, honestly</h2>
<p>I&#39;m not going to pretend this is free, because the failure mode is real and I&#39;ve watched teams walk into it. Headless components move work <em>to the caller</em>. When you use a styled <code>&lt;Button&gt;</code>, you get a button and you&#39;re done. When you use a headless toggle, you have to render the button, style it, and handle the states yourself. Multiply that across every component and a fully headless design system can feel like it gives you a pile of hooks and a homework assignment.</p>
<p>So the real-world answer is usually a layer cake, not a religion. Headless primitives at the bottom — the hard behavior, unstyled. A <em>styled</em> layer on top of them — your design system&#39;s actual <code>&lt;Button&gt;</code>, <code>&lt;Combobox&gt;</code>, <code>&lt;Select&gt;</code>, built once on the headless base and used everywhere. Most of your app imports the styled layer and never thinks about the headless one. The headless layer is there for the 10% of cases where the styled component doesn&#39;t fit and you need to drop down and rebuild the look while keeping the behavior. You get convenience by default and an escape hatch when you need it — instead of choosing between a rigid component library and reinventing keyboard navigation.</p>
<p>That layering is also the answer to the <a href="https://ma-x.im/blog/building-design-systems-that-scale">UI-kit-across-many-apps problem</a> I&#39;ve written about before: the behavior is shared once at the headless layer, and each app skins it without forking the logic. Same accessibility, same keyboard handling, different paint.</p>
<h2>What I actually reach for</h2>
<p>My rule of thumb is short. If a component&#39;s <em>behavior</em> is hard and its <em>look</em> varies — combobox, date picker, dropdown menu, data table, anything with keyboard and ARIA — I want it headless underneath, styled on top. If a component&#39;s look is fixed and its behavior is trivial, a plain styled component is right and headless is overkill. Most of a codebase is the second kind. But the handful of components that are the first kind are exactly the ones that cause the most pain when they&#39;re not headless, because they&#39;re the ones everyone needs slightly differently.</p>
<p>The mindset shift, though, is the real takeaway — the same one compound components started. You stop thinking &quot;I&#39;m building a combobox&quot; and start thinking &quot;I&#39;m building combobox <em>behavior</em>, and the combobox is just one thing someone might render with it.&quot; Once you see UI logic as separable from UI markup, you can&#39;t unsee it, and you start noticing how much supposedly-reusable code isn&#39;t reusable at all — not because the logic is wrong, but because it&#39;s handcuffed to one specific set of divs.</p>
<p>That wraps the two most important component patterns — compose the structure, then hand off the behavior. Next in this run I want to get at the unglamorous half of building shared components: how you <em>change</em> one that dozens of places already depend on, without a giant risky rewrite — deprecation paths, codemods, and updating call sites without fear. If you&#39;ve got a component library, go find the one component everyone reimplements slightly differently. That&#39;s almost always a behavior that should have been headless — tell me which one it is in yours.</p>
]]></content:encoded>
      <pubDate>Tue, 07 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Compound Components: Designing an API, Not Just a Component</title>
      <link>https://ma-x.im/blog/react-playbook-compound-components</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-compound-components</guid>
      <description>The moment a component grows past a handful of props, you&apos;re not writing a component anymore — you&apos;re designing an API. Compound components are how you keep that API flexible without drowning it in configuration props.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-compound-components/cover.png" alt="Compound Components: Designing an API, Not Just a Component" /></p>
<p>You build a <code>&lt;Select&gt;</code>. It starts clean — options in, value out. Then the requests arrive. Someone needs a divider between groups. Someone needs an icon next to certain options. Someone needs a sticky &quot;create new&quot; row at the bottom. Someone needs a disabled option with a tooltip explaining why. Each request adds a prop: <code>groups</code>, <code>renderIcon</code>, <code>footerSlot</code>, <code>disabledReason</code>. Six months later your <code>&lt;Select&gt;</code> takes twenty-three props, half of them undocumented, and every new requirement means editing the component and praying you didn&#39;t break the other twenty-two use-cases.</p>
<p>This is the prop-explosion death spiral, and it happens to every successful shared component. The problem isn&#39;t that you added props badly. It&#39;s that props were the wrong tool for the job somewhere around request number three. What you actually needed was to stop configuring a component and start <em>composing</em> one. That&#39;s what compound components are.</p>
<p>The last few articles have been about boundaries and where code lives. This one starts a run on the <em>patterns</em> you build inside those boundaries — the techniques that decide whether the components you write get reused or quietly rewritten. Compound components are the first one because they&#39;re the most common thing people get wrong.</p>
<h2>The wall you hit with props</h2>
<p>Props are perfect right up until the caller needs control over <em>structure</em>. A boolean, a string, a callback — props handle those beautifully. But the moment the caller wants to decide <em>what goes where</em> — the order of things, what sits between them, what wraps them — props turn into a configuration language you&#39;re now forced to invent and maintain.</p>
<pre><code class="language-tsx">// the prop-explosion version — every layout decision is a prop
&lt;Select
  options={options}
  value={value}
  onChange={setValue}
  renderOption={(o) =&gt; &lt;Icon name={o.icon} /&gt;}
  groupBy=&quot;category&quot;
  groupHeaderRenderer={(g) =&gt; &lt;strong&gt;{g}&lt;/strong&gt;}
  footer={&lt;CreateNewRow /&gt;}
  showDividers
  dividerAfter={[2, 5]}
/&gt;
</code></pre>
<p>Look at that <code>dividerAfter={[2, 5]}</code>. That&#39;s the smell. You&#39;re passing array indices to describe where a line should appear, because the component owns the structure and the caller has to negotiate with it through props. Every new layout wish is a new prop, a new branch inside the component, and a new way for the component to break. The component knows too much about every caller, and every caller knows too much about the component&#39;s internal prop vocabulary.</p>
<blockquote>
<p>When callers start passing you layout instructions as props, they&#39;re asking for composition. Give it to them.</p>
</blockquote>
<h2>Compound components invert the control</h2>
<p>A compound component flips the relationship. Instead of one component that takes a pile of props describing structure, you expose a <em>set</em> of components that the caller arranges themselves. The classic shape everyone knows is the native <code>&lt;select&gt;</code>:</p>
<pre><code class="language-html">&lt;select&gt;
  &lt;option&gt;One&lt;/option&gt;
  &lt;optgroup label=&quot;More&quot;&gt;
    &lt;option&gt;Two&lt;/option&gt;
  &lt;/optgroup&gt;
&lt;/select&gt;
</code></pre>
<p>You don&#39;t pass <code>&lt;select&gt;</code> a <code>groups</code> prop. You <em>compose</em> <code>&lt;option&gt;</code> and <code>&lt;optgroup&gt;</code> inside it. The React version of that idea looks like this:</p>
<pre><code class="language-tsx">&lt;Select value={value} onChange={setValue}&gt;
  &lt;Select.Option value=&quot;one&quot;&gt;One&lt;/Select.Option&gt;
  &lt;Select.Divider /&gt;
  &lt;Select.Group label=&quot;More&quot;&gt;
    &lt;Select.Option value=&quot;two&quot; icon={&lt;StarIcon /&gt;}&gt;Two&lt;/Select.Option&gt;
  &lt;/Select.Group&gt;
  &lt;Select.Footer&gt;&lt;CreateNewRow /&gt;&lt;/Select.Footer&gt;
&lt;/Select&gt;
</code></pre>
<p>Every one of those twenty-three props just became a component the caller places wherever they want. Need a divider after the second item? Put a <code>&lt;Select.Divider /&gt;</code> there. Need an icon? Pass it to that one <code>&lt;Select.Option&gt;</code>. The component stopped owning the structure, and the caller took it back — which is exactly what they were trying to do with all those props. You didn&#39;t add flexibility by adding configuration. You added it by <em>removing</em> configuration and letting composition do the work.</p>
<h2>The part that makes it actually work: shared state</h2>
<p>Here&#39;s the question that trips people up: if <code>&lt;Select.Option&gt;</code> is a separate component sitting who-knows-how-deep inside the tree, how does clicking it tell <code>&lt;Select&gt;</code> about the new value? You can&#39;t pass props down — you don&#39;t own the JSX in between. The answer is React Context, and this is the one piece of machinery that turns a pile of dotted components into a real compound component.</p>
<pre><code class="language-tsx">const SelectContext = createContext&lt;{
  value: string;
  onChange: (v: string) =&gt; void;
} | null&gt;(null);

function Select({ value, onChange, children }: SelectProps) {
  return (
    &lt;SelectContext.Provider value={{ value, onChange }}&gt;
      &lt;div role=&quot;listbox&quot;&gt;{children}&lt;/div&gt;
    &lt;/SelectContext.Provider&gt;
  );
}

function Option({ value, icon, children }: OptionProps) {
  const ctx = useContext(SelectContext);
  if (!ctx) throw new Error(&#39;&lt;Select.Option&gt; must be used inside &lt;Select&gt;&#39;);
  const selected = ctx.value === value;
  return (
    &lt;div
      role=&quot;option&quot;
      aria-selected={selected}
      onClick={() =&gt; ctx.onChange(value)}
      className={selected ? &#39;bg-[#22c55e]/10&#39; : &#39;&#39;}
    &gt;
      {icon}
      {children}
    &lt;/div&gt;
  );
}

// attach the children as properties — this is what enables the dot syntax
Select.Option = Option;
Select.Divider = () =&gt; &lt;div role=&quot;separator&quot; className=&quot;my-1 border-t border-gray-200&quot; /&gt;;
Select.Group = Group;
Select.Footer = Footer;
</code></pre>
<p>The parent puts the shared state into context. Every child reads from context, so it doesn&#39;t matter how deeply it&#39;s nested or what the caller wraps it in — an <code>&lt;Option&gt;</code> three divs down still knows the current value and can report a click. That context is the invisible wire connecting components the caller arranged. Props flow explicitly to configure <em>one</em> option (its icon, its value); context flows implicitly to coordinate <em>all</em> of them. That division is the whole trick.</p>
<p>Notice the <code>throw</code> in <code>Option</code>. Because these components only make sense together, a compound component should fail loudly when used apart. A bare <code>&lt;Select.Option&gt;</code> with no <code>&lt;Select&gt;</code> around it is a bug, and catching it with a clear error is far kinder than letting it silently render nothing.</p>
<h2>When it earns its place — and when it doesn&#39;t</h2>
<p>Compound components are not free. They&#39;re more code, more indirection, and a context that has to exist. So I don&#39;t reach for them by default. I reach for them when three signals show up together.</p>
<p><strong>The caller needs to control structure.</strong> If people keep asking to reorder parts, inject things between parts, or wrap parts — that&#39;s the tell. Structure control is what props are bad at and composition is good at.</p>
<p><strong>The parts are meaningless alone but meaningful together.</strong> <code>Tabs</code>/<code>TabList</code>/<code>Tab</code>/<code>TabPanel</code>. <code>Accordion</code>/<code>AccordionItem</code>. <code>Menu</code>/<code>MenuItem</code>/<code>MenuSeparator</code>. None of those children make sense on their own, and all of them need to coordinate through shared state. That&#39;s the exact shape compound components fit.</p>
<p><strong>There&#39;s real shared state to coordinate.</strong> If the children don&#39;t need to know about each other or a common value, you don&#39;t need the context, and you probably don&#39;t need this pattern — you just need components that accept <code>children</code>.</p>
<p>And when <em>not</em> to: a component with two or three stable props and no structural variation is perfect as a plain component. Wrapping a <code>&lt;Button&gt;</code> in a compound API is pure ceremony. The pattern earns its complexity only when the alternative is prop explosion. If you&#39;re not drowning in configuration props, you don&#39;t have the problem this solves.</p>
<h2>The mental shift that matters</h2>
<p>The reason I wanted to open the patterns run with this one is that compound components force a change in how you think, and that change outlasts the specific technique. The first time your component grows past a handful of props, you&#39;re not building a component anymore. You&#39;re designing an API that other people — including future you — will program against. And APIs are judged by how gracefully they handle the requests you didn&#39;t anticipate.</p>
<p>A prop-based component handles the unanticipated request by growing a new prop, and eventually collapses under its own configuration surface. A compound component handles it by letting the caller compose a solution you never had to predict — the divider you didn&#39;t plan for, the footer you didn&#39;t imagine — without you touching the component at all. That&#39;s the difference between a component people configure and a component people <em>build with</em>. The first one you maintain forever. The second one mostly maintains itself.</p>
<p>Next I want to push this one step further, to the pattern that takes the same idea and removes the last assumption a compound component still makes — that <em>you</em> own the markup at all. Headless components, render props, hooks-as-API: handing over behavior while giving up the UI entirely. If you&#39;ve got a shared component in your codebase with more than a dozen props, go count them, then ask how many are really the caller trying to control structure. I&#39;d bet the number is higher than you&#39;d like — tell me what you find.</p>
]]></content:encoded>
      <pubDate>Fri, 03 Jul 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Is Container vs Presentational Dead? What Survived the Hooks Era</title>
      <link>https://ma-x.im/blog/react-playbook-container-presentational</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-container-presentational</guid>
      <description>The pattern that taught a generation to split &apos;smart&apos; from &apos;dumb&apos; components got quietly declared obsolete when hooks arrived. It wasn&apos;t. The folder convention died. The idea underneath it is more useful now than ever — including in Server Components.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-container-presentational/cover.png" alt="Is Container vs Presentational Dead? What Survived the Hooks Era" /></p>
<p>Somewhere around 2019, &quot;container vs presentational components&quot; got quietly filed under <em>obsolete</em>. Hooks had landed, the man who popularized the pattern had publicly walked it back, and every other blog post declared that <code>containers/</code> and <code>components/</code> folders were a relic. Move on, we have <code>useEffect</code> now.</p>
<p>That take was half right, and the half it got wrong is the interesting half. The <em>folder convention</em> deserved to die. The <em>idea</em> it was pointing at — separate the component that knows about the world from the component that just renders — didn&#39;t die at all. It went underground, changed shape, and quietly became one of the most useful instincts you can have. It&#39;s the exact split I kept landing on in the <a href="https://ma-x.im/blog/react-playbook-composition-and-di">composition and DI article</a> without naming it. Time to name it.</p>
<h2>What the pattern actually said</h2>
<p>For anyone who missed the original: the classic pattern split components into two kinds.</p>
<p><strong>Container components</strong> knew how the app worked. They fetched data, held state, talked to the store, ran side effects — and rendered almost nothing themselves. <strong>Presentational components</strong> knew how things <em>looked</em>. They took props, rendered markup, fired callbacks, and knew nothing about where their data came from or where it went.</p>
<pre><code class="language-tsx">// the old shape — a container that fetches, and a dumb view it feeds
class UserListContainer extends React.Component {
  state = { users: [], loading: true };
  componentDidMount() {
    fetchUsers().then((users) =&gt; this.setState({ users, loading: false }));
  }
  render() {
    return &lt;UserList users={this.state.users} loading={this.state.loading} /&gt;;
  }
}

function UserList({ users, loading }) {
  if (loading) return &lt;Spinner /&gt;;
  return &lt;ul&gt;{users.map((u) =&gt; &lt;li key={u.id}&gt;{u.name}&lt;/li&gt;)}&lt;/ul&gt;;
}
</code></pre>
<p>The value was real: <code>UserList</code> was trivial to test and reuse because it was just a function of its props, and the messy stateful part lived in one place. The problem was equally real: it was <em>ceremony</em>. Every stateful component needed a class wrapper around a function, entire files existed only to fetch-and-pass, and the two halves were welded together one-to-one anyway. You wrote twice the components for a separation that was mostly bookkeeping.</p>
<h2>What hooks actually killed</h2>
<p>Then hooks arrived, and here&#39;s the precise thing they changed: <strong>you no longer need a separate component to hold state or effects.</strong> The whole mechanical reason containers existed — &quot;a function component can&#39;t have state, so wrap it in a class&quot; — evaporated. A single function component can now fetch, hold state, run effects, <em>and</em> render:</p>
<pre><code class="language-tsx">function UserList() {
  const { data: users, isLoading } = useQuery([&#39;users&#39;], fetchUsers);
  if (isLoading) return &lt;Spinner /&gt;;
  return &lt;ul&gt;{users.map((u) =&gt; &lt;li key={u.id}&gt;{u.name}&lt;/li&gt;)}&lt;/ul&gt;;
}
</code></pre>
<p>One component. No container. And this is genuinely better for most cases — the ceremony is gone. So when people say &quot;hooks killed container/presentational,&quot; this is what they mean, and they&#39;re right: hooks killed the <em>requirement</em> to physically split components just to have state.</p>
<p>But look closely at what they did <em>not</em> kill. <code>UserList</code> above now knows how to fetch. It reaches out and grabs its data — the exact reach-out anti-pattern from last time. It&#39;s convenient, and for a component you&#39;ll only ever use in one place, convenient is correct. The trouble is that people took &quot;hooks removed the ceremony&quot; and heard &quot;the separation was pointless.&quot; Those are not the same sentence.</p>
<h2>The idea that refused to die</h2>
<p>Strip away the class wrappers and the folder names, and the pattern was never really about containers. It was about a <em>boundary</em>: one side knows about the outside world — data, state, side effects, where things come from and go — and the other side just takes inputs and renders. Hooks removed the boilerplate for drawing that boundary. They did not remove the reasons you&#39;d want to.</p>
<p>You still want it every single time you need to:</p>
<ul>
<li><strong>test rendering without a backend</strong> — a pure view takes props, no mock server required;</li>
<li><strong>reuse the same look with different data</strong> — the same table fed by a query, a websocket, or fixtures;</li>
<li><strong>show it in Storybook</strong> — a component that fetches its own data can&#39;t live in a story without a whole app around it;</li>
<li><strong>let a designer or a different feature reuse the visuals</strong> without inheriting your data layer.</li>
</ul>
<p>So the modern move isn&#39;t <code>containers/</code> folders. It&#39;s this: <strong>write the one-component version by default, and split along the data/render seam the moment you need the render half to stand on its own.</strong> Same <code>UserList</code>, split not because a rule said so but because you now have a reason:</p>
<pre><code class="language-tsx">// the &quot;container&quot; is just a function that fetches and delegates — no class, no folder
function UserListView({ users, isLoading }: { users: User[]; isLoading: boolean }) {
  if (isLoading) return &lt;Spinner /&gt;;
  return &lt;ul&gt;{users.map((u) =&gt; &lt;li key={u.id}&gt;{u.name}&lt;/li&gt;)}&lt;/ul&gt;;
}

function UserList() {
  const { data = [], isLoading } = useQuery([&#39;users&#39;], fetchUsers);
  return &lt;UserListView users={data} isLoading={isLoading} /&gt;;
}
</code></pre>
<p>Notice there&#39;s no class, no <code>containers/</code> directory, no naming ritual. Just a component that knows about the world wrapping a component that doesn&#39;t. The pattern survived; the ceremony didn&#39;t. That&#39;s the whole story of the &quot;death&quot; of container/presentational.</p>
<h2>Where hooks made it fuzzier — and better</h2>
<p>The honest update is that hooks also gave us a <em>third</em> option the old pattern didn&#39;t have, and it&#39;s often the best one: <strong>extract the world-knowledge into a custom hook instead of a component.</strong></p>
<pre><code class="language-tsx">// the &quot;container logic&quot; is now a hook, not a wrapper component
function useUserList() {
  const { data = [], isLoading } = useQuery([&#39;users&#39;], fetchUsers);
  return { users: data, isLoading };
}

function UserList() {
  const { users, isLoading } = useUserList();
  if (isLoading) return &lt;Spinner /&gt;;
  return &lt;ul&gt;{users.map((u) =&gt; &lt;li key={u.id}&gt;{u.name}&lt;/li&gt;)}&lt;/ul&gt;;
}
</code></pre>
<p>Now the &quot;smart&quot; part is reusable on its own — any component can call <code>useUserList</code> — and the rendering stays simple. This is the container/presentational split turned sideways: instead of <em>smart component + dumb component</em>, it&#39;s <em>smart hook + component that renders</em>. For a lot of cases this is the sweet spot, because the reusable unit you actually wanted was the data logic, not a wrapper component. The boundary is still there. It just moved from a component seam to a hook seam.</p>
<p>So the real menu today isn&#39;t &quot;container or not.&quot; It&#39;s three options, and you pick by what you need to reuse or isolate:</p>
<ol>
<li><strong>One component</strong> that does it all — the default, for anything single-use.</li>
<li><strong>Smart hook + component</strong> — when the <em>logic</em> is what you want to reuse or test.</li>
<li><strong>Fetching component + pure view</strong> — when the <em>rendering</em> is what you want to reuse, test, or hand to Storybook.</li>
</ol>
<h2>Server Components made the split matter again</h2>
<p>Here&#39;s the twist nobody saw coming when they were busy declaring the pattern dead. React Server Components brought back a <em>hard, physical</em> version of exactly this boundary — and this time the framework enforces it.</p>
<p>A Server Component runs on the server, can be async, can hit the database directly, and ships no JavaScript to the browser. A Client Component (<code>&#39;use client&#39;</code>) runs in the browser and is the only thing that can hold state or handle events. The natural, idiomatic shape is: <strong>Server Components fetch and compose the data; Client Components take that data as props and handle interactivity.</strong></p>
<pre><code class="language-tsx">// app/users/page.tsx — Server Component: the &quot;container&quot;, now literally server-side
export default async function UsersPage() {
  const users = await db.user.findMany();   // no client JS, runs on the server
  return &lt;UserList users={users} /&gt;;
}

// UserList.tsx — &#39;use client&#39;: the interactive, presentational half
&#39;use client&#39;;
export function UserList({ users }: { users: User[] }) {
  const [query, setQuery] = useState(&#39;&#39;);
  const shown = users.filter((u) =&gt; u.name.includes(query));
  return (/* search box + list */);
}
</code></pre>
<p>Read that and tell me it isn&#39;t container/presentational wearing a new uniform. The server component <em>knows about the world</em> — it fetches, it composes. The client component <em>takes props and renders</em>, plus handles the interactivity that has to live in the browser. The pattern everyone buried in 2019 is now baked into the newest thing React ships, except now the boundary isn&#39;t a convention you can ignore — it&#39;s a wall the runtime draws for you, and crossing it wrong is a build error.</p>
<h2>What I actually believe about it</h2>
<p>The pattern isn&#39;t dead and it isn&#39;t alive in its original form either. What happened is more useful: the <em>rule</em> dissolved and the <em>instinct</em> got sharper. I don&#39;t make <code>containers/</code> folders. I don&#39;t wrap functions in classes. I don&#39;t split a component just because a style guide from 2016 says smart and dumb should be separate files.</p>
<p>What I do — every time — is keep one question live while I write a component: <em>does this thing know about the world, or does it just render?</em> When the answer is &quot;both, and I need one of those halves on its own,&quot; I split along that seam, and I reach for whichever tool fits — a hook, a pure view, or a Server/Client boundary. The genius of the old pattern was never the folders. It was teaching a generation to notice that seam at all. That part was never obsolete. It just stopped needing a name to be worth doing.</p>
<p>That closes out this run on architecture and boundaries — where code lives, which way it depends, how to size and wire a component, and now where the smart/dumb line actually falls. Next I want to move from <em>structure</em> to <em>patterns</em>: the reusable-component techniques — <a href="https://ma-x.im/blog/react-playbook-compound-components">compound components</a>, headless UIs, behavior-as-API — that decide whether the things you build get used or quietly rewritten. If you&#39;ve still got a <code>containers/</code> folder in a codebase somewhere, don&#39;t rename it in a panic — just notice whether the split it&#39;s drawing is one you&#39;d still draw today. Tell me what you find; I&#39;d bet half of them are pulling their weight and half are pure ceremony.</p>
]]></content:encoded>
      <pubDate>Tue, 30 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>How Big Should a Component Be? Composition, Decomposition, and Handing Over Dependencies</title>
      <link>https://ma-x.im/blog/react-playbook-composition-and-di</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-composition-and-di</guid>
      <description>Split a component too early and you get a maze of tiny files. Split too late and you get a 600-line monster. The real question isn&apos;t size — it&apos;s who owns which dependency, and whether the component reaches for it or is handed it.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-composition-and-di/cover.png" alt="How Big Should a Component Be? Composition, Decomposition, and Handing Over Dependencies" /></p>
<p>Every code review eventually gets the comment: &quot;this component is too big, split it up.&quot; It&#39;s good advice roughly half the time. The other half, someone dutifully chops a working 200-line component into eight files, and now understanding it means opening all eight, following props through four of them, and reassembling the whole thing in your head. The component wasn&#39;t fixed. It was smeared.</p>
<p>So &quot;split it up&quot; isn&#39;t the rule. Size is a symptom, not the disease. The real question underneath — the one the last three articles have been circling — is about <em>dependencies</em>: what does this component need to do its job, who gives it those things, and does the component go reach for them itself or get handed them? Get that right and the size mostly sorts itself out. Get it wrong and no amount of splitting helps.</p>
<h2>Decomposition isn&#39;t about line count</h2>
<p>Let me start with the part people get backwards. You don&#39;t split a component because it&#39;s long. You split it because it&#39;s doing more than one <em>thing</em> — because two different reasons to change are living in the same file.</p>
<p>Here&#39;s a component that&#39;s long but shouldn&#39;t be split:</p>
<pre><code class="language-tsx">function PolicyDetails({ policy }: { policy: Policy }) {
  // 180 lines of one coherent thing: rendering the details of a policy.
  // Header, status, coverage table, footer. All one job. All change
  // together, for the same reason (&quot;we redesigned the policy view&quot;).
}
</code></pre>
<p>It&#39;s 180 lines and I&#39;d leave it alone. There&#39;s one reason for it to change, and everything in it changes for that reason. Splitting it into <code>&lt;PolicyHeader&gt;</code>, <code>&lt;PolicyStatus&gt;</code>, <code>&lt;PolicyFooter&gt;</code> buys you nothing but four files to keep in sync — the classic mistake I wrote about in <a href="https://ma-x.im/blog/smeared-component">the smeared component</a>. Now here&#39;s a component that&#39;s <em>short</em> but absolutely should be split:</p>
<pre><code class="language-tsx">function UserBadge({ userId }: { userId: string }) {
  const { data: user } = useQuery([&#39;user&#39;, userId], () =&gt; fetchUser(userId));
  const theme = useContext(ThemeContext);
  const track = useAnalytics();

  useEffect(() =&gt; { track(&#39;badge_shown&#39;, { userId }); }, [userId]);

  if (!user) return &lt;Spinner /&gt;;
  return &lt;span style={{ color: theme.accent }}&gt;{user.name}&lt;/span&gt;;
}
</code></pre>
<p>Forty lines, and it&#39;s doing four unrelated jobs: fetching data, reading theme, firing analytics, and rendering. Four reasons to change, tangled together. This one I&#39;d split — not because it&#39;s big, but because it&#39;s <em>mixed</em>.</p>
<p>That&#39;s the actual rule: <strong>decompose along reasons to change, not along line count.</strong> One component, one job, one reason to change. A long file doing one thing is fine. A short file doing four things is a problem waiting to surprise you.</p>
<h2>The reach-out anti-pattern</h2>
<p>Look again at <code>UserBadge</code>. The thing that makes it hard to reuse and impossible to test in isolation isn&#39;t its length. It&#39;s that it <em>reaches out</em> and grabs everything it needs from the ambient environment: it calls <code>fetchUser</code> directly, pulls theme from a context, grabs the analytics client from a hook. It&#39;s wired straight into the world.</p>
<p>This is the component equivalent of an arrow pointing the wrong way — the exact problem from the <a href="https://ma-x.im/blog/react-playbook-module-dependencies">dependency-direction article</a>, now one level down, inside a single component. <code>UserBadge</code> claims to be a small presentational thing, but it depends on your data layer, your theme system, and your analytics pipeline. Drop it into a Storybook story or a test and it explodes, because it demands a whole app around it just to render a name.</p>
<p>You feel this the moment you try to reuse it. &quot;I just want to show a user&#39;s name in a different color&quot; — but you can&#39;t, because the color is hard-wired to <code>ThemeContext.accent</code>, and the name is hard-wired to <em>this</em> query. The component grabs its dependencies instead of accepting them, so it only works in the one spot it was born in.</p>
<h2>Hand it over instead</h2>
<p>The fix is old and boring and it&#39;s called dependency injection, which sounds like a Java framework and is actually just this: <strong>don&#39;t let a component fetch its own dependencies — hand them in.</strong> On the frontend, &quot;inject&quot; mostly means &quot;pass as a prop&quot; or &quot;provide through context,&quot; and the entire idea is that the component stops knowing <em>where</em> things come from.</p>
<pre><code class="language-tsx">// UserBadge now just renders. It&#39;s handed everything it needs.
function UserBadge({ name, color }: { name: string; color: string }) {
  return &lt;span style={{ color }}&gt;{name}&lt;/span&gt;;
}
</code></pre>
<p>That&#39;s it. It doesn&#39;t fetch, doesn&#39;t read context, doesn&#39;t track. It takes a name and a color and renders. Now it works in a test with two strings, works in Storybook with no providers, works anywhere. The fetching, theming, and analytics didn&#39;t disappear — they moved <em>up</em> to a component whose job is exactly that wiring:</p>
<pre><code class="language-tsx">function ConnectedUserBadge({ userId }: { userId: string }) {
  const { data: user } = useQuery([&#39;user&#39;, userId], () =&gt; fetchUser(userId));
  const theme = useContext(ThemeContext);
  const track = useAnalytics();

  useEffect(() =&gt; { track(&#39;badge_shown&#39;, { userId }); }, [userId, track]);

  if (!user) return &lt;Spinner /&gt;;
  return &lt;UserBadge name={user.name} color={theme.accent} /&gt;;
}
</code></pre>
<p>We split <code>UserBadge</code> — but notice <em>how</em> we split it. Not into &quot;the top half and the bottom half.&quot; Along a dependency seam: one component that knows about the outside world (fetching, context, side effects), and one that knows nothing and just renders what it&#39;s given. That seam is the useful cut. The line-count cut isn&#39;t.</p>
<h2>Injection is a spectrum, not a switch</h2>
<p>You don&#39;t have to reach for a DI container or some ceremony. Frontend dependency injection is mostly choosing, per dependency, how far to push it out of the component. From least to most flexible:</p>
<p><strong>Hard-coded (no injection).</strong> The component imports and calls the thing directly. Fine for genuinely stable, app-agnostic dependencies — a component importing <code>clsx</code> or a pure <code>formatDate</code> doesn&#39;t need those injected. Not everything is a dependency worth managing.</p>
<p><strong>Props.</strong> The default and the one you&#39;ll use most. Pass the value, the callback, the data. Explicit, type-checked, trivially testable. If you&#39;re not sure how to inject something, pass it as a prop and stop overthinking it.</p>
<pre><code class="language-tsx">// the data source is injected — the list doesn&#39;t know or care where rows come from
function DataTable&lt;T&gt;({ rows, onRowClick }: { rows: T[]; onRowClick: (row: T) =&gt; void }) { /* ... */ }
</code></pre>
<p><strong>Context.</strong> For dependencies that are truly cross-cutting and tedious to thread — theme, current user, a query client, an analytics sink. Context is injection at a distance: the component pulls from context, but <em>what&#39;s in that context</em> is decided at the provider, so tests and stories can hand it a fake. The danger is overusing it — every context dependency is an invisible input that doesn&#39;t show up in the props, and a component that reads six contexts is just as wired-in as one that imports six modules. Use context for the few things that are genuinely app-wide, and props for everything else.</p>
<p><strong>Render props / children as a function / slots.</strong> Inject <em>behavior or markup</em>, not just values. This is how you build something reusable without it knowing what it renders — a <code>&lt;List&gt;</code> that takes a <code>renderItem</code>, a <code>&lt;Form&gt;</code> that takes children. It&#39;s dependency injection where the dependency is a piece of UI.</p>
<p>The skill isn&#39;t picking one. It&#39;s matching each dependency to the lightest mechanism that still lets you swap it when you need to. Most things: props. A few app-wide things: context. Behavior you want to vary: a function. Genuinely fixed, generic utilities: just import them.</p>
<h2>The payoff you actually feel</h2>
<p>This all sounds abstract until the day it isn&#39;t. Three moments where injecting dependencies instead of grabbing them pays for itself:</p>
<p><strong>Testing.</strong> A component handed its dependencies is a component you test with plain values — no mocking the fetch layer, no wrapping in five providers, no fighting the framework. <code>render(&lt;UserBadge name=&quot;Ada&quot; color=&quot;#22c55e&quot; /&gt;)</code> and you&#39;re done. The reach-out version needs a mock server and a provider tree just to assert on a name.</p>
<p><strong>Reuse.</strong> The whole reason the &quot;small&quot; <code>UserBadge</code> couldn&#39;t be reused was that it grabbed its color from one specific context. Hand the color in and it drops into any screen, any theme, any story. Injected dependencies are what make a component portable — which, not coincidentally, is the same <a href="https://ma-x.im/blog/react-playbook-shared-code">portability promise</a> that decides whether something belongs in <code>shared/</code> at all.</p>
<p><strong>Changing your mind.</strong> Swap <code>fetchUser</code> for a different endpoint, replace the analytics provider, feed the table rows from a websocket instead of a query — and the leaf components don&#39;t change at all, because they never knew where their data came from. You only touch the wiring layer. The thing you were afraid to change turns out to have one owner, in one place.</p>
<h2>The rule I actually carry around</h2>
<p>I&#39;ve stopped asking &quot;is this component too big.&quot; It&#39;s the wrong question and it leads to smearing. The questions I ask instead are two: <em>how many reasons does this component have to change</em> (that tells me whether to split, and where), and <em>does it reach for its dependencies or get handed them</em> (that tells me whether it&#39;ll be testable and reusable, or welded to one spot).</p>
<p>Composition, in the end, isn&#39;t about making things small. It&#39;s about making the seams fall in the right places — cutting along dependencies, not along line counts — and being deliberate about which component owns the wiring and which just renders what it&#39;s told. Do that and your big components stay big and fine, your small ones stay focused, and the word &quot;reusable&quot; starts meaning something instead of being a hope you wrote in a PR description.</p>
<p>Next I want to take this exact idea — the split between &quot;knows about the world&quot; and &quot;just renders&quot; — and hold it up against the pattern that&#39;s been arguing about it for a decade: <a href="https://ma-x.im/blog/react-playbook-container-presentational">container versus presentational components</a>, and whether that split still makes sense now that we have hooks and Server Components. If you&#39;ve got a component you&#39;re scared to reuse, look at what it reaches for instead of what it&#39;s handed. That list is usually the whole reason — tell me what&#39;s on yours.</p>
]]></content:encoded>
      <pubDate>Fri, 26 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The Direction of the Arrows: Dependencies Are Your Real Architecture</title>
      <link>https://ma-x.im/blog/react-playbook-module-dependencies</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-module-dependencies</guid>
      <description>You can draw whatever folder diagram you like. The honest picture of your app is which module imports which — the direction of the arrows. Get that wrong and no folder structure will save you.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-module-dependencies/cover.png" alt="The Direction of the Arrows: Dependencies Are Your Real Architecture" /></p>
<p>Draw your app&#39;s architecture on a whiteboard and it always looks clean. Neat boxes, sensible names, tidy layers stacked top to bottom. Everyone nods. Then you open the code and it doesn&#39;t match the drawing at all — a UI component reaches into a data-fetching file, a &quot;shared&quot; helper imports a specific feature, two features import each other through a back door nobody remembers adding.</p>
<p>The whiteboard is the architecture you <em>wish</em> you had. The import statements are the architecture you <em>actually</em> have. And the gap between them is where most large React apps quietly rot.</p>
<p>I&#39;ve spent the last two articles on where code lives — the <a href="https://ma-x.im/blog/react-playbook-shared-code"><code>shared/</code> folder</a> and <a href="https://ma-x.im/blog/react-playbook-barrel-files">barrel files</a>. This one is about the thing underneath both: not where a module sits, but which way it <em>points</em>. Who is allowed to import whom. Because that — the direction of the arrows — is the real shape of your app.</p>
<h2>Your folders lie, your imports don&#39;t</h2>
<p>Here&#39;s the uncomfortable part. Folder structure is a suggestion. You can put a file in <code>shared/</code> and it means nothing about how coupled it is — what matters is what it imports and who imports it. Two apps with identical folder trees can have completely different architectures, because architecture isn&#39;t the tree. It&#39;s the graph.</p>
<p>Every <code>import</code> statement is a directed edge: &quot;this module depends on that one.&quot; Collect all of them and you get your dependency graph — the one true map of your app. And that map answers the questions folders can&#39;t:</p>
<ul>
<li>If I change this file, what breaks? (everything that points <em>at</em> it)</li>
<li>Can I delete this feature cleanly? (only if nothing outside points into it)</li>
<li>Why is this &quot;small&quot; component impossible to reuse? (because it points at half the app)</li>
<li>Why did one route pull in the entire bundle? (follow the arrows — you&#39;ll find a bad one)</li>
</ul>
<p>You already met that last one in the barrel article. That wasn&#39;t really a barrel problem. It was an <em>arrow</em> problem — an import pointing somewhere it shouldn&#39;t, and a barrel making it easy.</p>
<blockquote>
<p>Folders describe intent. Imports describe reality. When they disagree, reality wins.</p>
</blockquote>
<h2>Arrows have a correct direction</h2>
<p>The single most useful architectural rule I know fits in one sentence: <strong>dependencies should point in one direction — from specific to general, from volatile to stable, never back.</strong></p>
<p>Think of your app in rough layers, top to bottom:</p>
<pre><code>app          ← wires everything together
features     ← &quot;edit policy&quot;, &quot;create claim&quot; — business use-cases
entities     ← domain models: Policy, Claim, User
shared       ← generic, app-agnostic: Button, formatDate, http client
</code></pre>
<p>The rule is simple: <strong>arrows only ever point down.</strong> A feature may import an entity. An entity may import from <code>shared</code>. <code>shared</code> imports <em>nothing</em> from above it — not an entity, definitely not a feature.</p>
<p>Why down and not up? Because the lower you go, the more stable and general the code is. <code>shared/Button</code> doesn&#39;t know your app sells insurance. <code>entities/Policy</code> knows about policies but not about the specific &quot;renew a lapsed policy&quot; screen. The <code>edit-policy</code> feature knows the most and changes the most. If a stable module (<code>Button</code>) points <em>up</em> at a volatile one (<code>edit-policy</code>), then every time that feature changes, your generic button is at risk — and it&#39;s no longer generic. You just welded the most reusable thing in your app to the least reusable one.</p>
<p>The moment <code>shared</code> imports a feature, the word &quot;shared&quot; becomes a lie. This is the mechanical reason the <code>shared/</code> folder turns into a junk drawer: not because people are messy, but because they let arrows point the wrong way, and an upward arrow is exactly how a generic folder gets chained to a specific feature.</p>
<h2>The two arrows that kill you</h2>
<p>There are two directions of arrow that do almost all the damage. Learn to see them and you&#39;ve caught 90% of architectural decay.</p>
<p><strong>The upward arrow — general depending on specific.</strong> A shared utility importing a feature. An entity importing a screen. A design-system component importing app-specific business logic. Every one of these takes something that was supposed to be reusable and staples it to something that isn&#39;t.</p>
<pre><code class="language-ts">// shared/lib/format.ts  — a &quot;generic&quot; formatter
import { POLICY_STATUS_LABELS } from &#39;@/features/edit-policy/constants&#39;;
//                                    ^^^^^^^^^^^^^^^^^^^^^^ upward arrow

export function formatStatus(code: string) {
  return POLICY_STATUS_LABELS[code] ?? code;
}
</code></pre>
<p>This looks harmless. It compiles, it works, the PR gets approved. But <code>shared/lib/format</code> now depends on <code>edit-policy</code>. Import that formatter anywhere — a totally unrelated screen — and you&#39;ve dragged <code>edit-policy</code> in with it. &quot;Shared&quot; now means &quot;shares a dependency on one specific feature.&quot; The fix is to push the specific thing up to where it belongs and keep the shared thing ignorant:</p>
<pre><code class="language-ts">// shared/lib/format.ts — knows nothing about policies
export function formatStatus(code: string, labels: Record&lt;string, string&gt;) {
  return labels[code] ?? code;
}

// features/edit-policy/... — the feature owns its own labels
formatStatus(policy.status, POLICY_STATUS_LABELS);
</code></pre>
<p>The generic module stayed generic. The specific knowledge stayed in the feature. The arrow points down again.</p>
<p><strong>The sideways arrow — feature importing feature.</strong> <code>edit-policy</code> reaches directly into <code>billing-dashboard</code>. Now you can&#39;t touch one without understanding the other, you can&#39;t delete either cleanly, and if <code>billing-dashboard</code> also imports <code>edit-policy</code> back, you&#39;ve got a cycle — two modules that can only be understood as one tangled unit. Cross-feature imports are how a codebase where every feature was supposed to be independent becomes one where nothing is.</p>
<p>The fix isn&#39;t &quot;never let features talk.&quot; It&#39;s <em>how</em> they talk. Pull the shared concept down into an entity both can depend on, or lift the coordination up into the <code>app</code> layer that&#39;s allowed to know about both. Features don&#39;t import each other sideways; they meet in a layer above or below. Arrows down, never across.</p>
<h2>A feature&#39;s public API is the only door in</h2>
<p>Here&#39;s what makes the &quot;arrows down&quot; rule enforceable instead of a vibe: <strong>each feature exposes one public entry point, and nobody imports past it.</strong></p>
<pre><code>features/edit-policy/
  index.ts            ← the ONLY thing outsiders may import
  ui/EditPolicyForm.tsx
  model/useEditPolicy.ts
  model/validation.ts   ← private
  lib/mapPolicyDto.ts   ← private
</code></pre>
<pre><code class="language-ts">// features/edit-policy/index.ts — deliberate public surface
export { EditPolicyForm } from &#39;./ui/EditPolicyForm&#39;;
export { useEditPolicy } from &#39;./model/useEditPolicy&#39;;
// validation, DTO mapping, internal helpers stay private
</code></pre>
<p>Anything outside the feature imports from <code>@/features/edit-policy</code> and nothing deeper. The insides — validation rules, DTO mapping, private hooks — are free to change, because from the outside they don&#39;t exist. This is the <em>good</em> use of a barrel I argued for last time: not to shorten imports, but to draw a wall with exactly one door in it.</p>
<p>Reach past that door — <code>import { validatePolicy } from &#39;@/features/edit-policy/model/validation&#39;</code> — and you&#39;ve done two bad things at once: coupled to an internal that was never promised to stay, and created an arrow that no longer respects the feature&#39;s boundary. The public API is what turns &quot;arrows point down&quot; from a hope into a checkable rule. There&#39;s a door. You use the door.</p>
<h2>Make the graph impossible to get wrong</h2>
<p>You will not hold this in your head. Nobody does. On a real team, someone adds an upward import at 6pm on a Friday, it works, it ships, and the arrow is now part of your architecture forever. Good intentions don&#39;t scan the dependency graph. Tooling does.</p>
<p>I let a linter own the rule. <code>eslint-plugin-import</code> with <code>no-restricted-imports</code>, or a boundaries plugin (<code>eslint-plugin-boundaries</code>, or the import rules in Biome), encoding the layer directions as actual errors:</p>
<pre><code class="language-js">// eslint — shared may not import upward, features may not import each other
&#39;no-restricted-imports&#39;: [&#39;error&#39;, {
  patterns: [
    { group: [&#39;@/features/*&#39;, &#39;@/entities/*&#39;], message:
      &#39;shared/ must not import from features or entities — arrows point down only.&#39; },
  ],
}],
</code></pre>
<p>Now the wrong arrow doesn&#39;t get a code review debate. It gets a red squiggle before the commit. The architecture stops being a document people forget and becomes a constraint the tooling enforces. That&#39;s the whole game: an architectural rule that isn&#39;t mechanically enforced is a rule that&#39;s already being broken somewhere you haven&#39;t looked.</p>
<p>And once the arrows are constrained, a dependency-graph visualiser (<code>madge</code>, <code>dependency-cruiser</code>, or your bundler&#39;s analyzer) stops being a curiosity and becomes a health check. Run <code>madge --circular</code> and it lists your cycles — every one a place where two modules secretly became one. A clean graph with arrows all pointing down is not an aesthetic preference. It&#39;s the difference between a change that touches one file and a change that touches forty.</p>
<h2>Why this is the article under the others</h2>
<p>The <code>shared/</code> folder turns into a junk drawer because people let it depend upward. Barrel files blow up your bundle because they make a bad arrow cheap to draw. Features become impossible to delete because they point sideways at each other. Every one of those is the same disease wearing a different symptom: <strong>an arrow going the wrong way.</strong></p>
<p>So when I evaluate an architecture now, I don&#39;t start with the folder tree — that&#39;s the story the code tells about itself. I start with the graph: pull the imports, look at the direction of the arrows, and find the ones pointing up or sideways. Those arrows are the whole diagnosis. Fix them and the folders mostly sort themselves out. Ignore them and you can reorganize folders every quarter and still be stuck in the same tangle, because you were rearranging the boxes while the arrows — the thing that actually mattered — stayed exactly where they were.</p>
<p>Next I want to turn from <em>where the arrows point</em> to <em>how big the boxes should be</em> — <a href="https://ma-x.im/blog/react-playbook-composition-and-di">composition versus decomposition</a>, and how to hand a component its dependencies instead of letting it reach out and grab them. If you want a fast, slightly alarming read on your own app tonight, run <code>madge --circular src</code> and count the cycles. Tell me the number — I&#39;m curious whether you&#39;re prouder or more horrified than you expected.</p>
]]></content:encoded>
      <pubDate>Tue, 23 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>FSD</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Barrel Files: The Import You Love and the Bundle You Hate</title>
      <link>https://ma-x.im/blog/react-playbook-barrel-files</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-barrel-files</guid>
      <description>A barrel file makes imports look beautiful and can quietly drag half your app into a bundle that only needed one function. Here&apos;s what index.ts actually does, when it earns its place, and when it&apos;s a trap wearing a clean interface.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-barrel-files/cover.png" alt="Barrel Files: The Import You Love and the Bundle You Hate" /></p>
<p>There&#39;s a specific kind of import that feels like good engineering:</p>
<pre><code class="language-ts">import { Button, Card, Modal, Tooltip } from &#39;@/shared/ui&#39;;
</code></pre>
<p>One line. One path. Clean. Compare it to the honest version:</p>
<pre><code class="language-ts">import { Button } from &#39;@/shared/ui/Button&#39;;
import { Card } from &#39;@/shared/ui/Card&#39;;
import { Modal } from &#39;@/shared/ui/Modal&#39;;
import { Tooltip } from &#39;@/shared/ui/Tooltip&#39;;
</code></pre>
<p>The first one looks like a tidy public API. The second looks like clutter. So of course teams reach for the first — they add an <code>index.ts</code> that re-exports everything, and the whole folder gets a single front door. That file is a <em>barrel</em>, and it&#39;s one of the most quietly expensive habits in a React codebase. Not because it&#39;s wrong. Because it&#39;s right just often enough that people stop questioning it.</p>
<p>I brought this up at the end of the <a href="https://ma-x.im/blog/react-playbook-shared-code">shared/ article</a> and promised to come back to it. Here&#39;s the come-back.</p>
<h2>What a barrel actually is</h2>
<p>A barrel is just a file — almost always <code>index.ts</code> — whose only job is to re-export other modules:</p>
<pre><code class="language-ts">// shared/ui/index.ts
export { Button } from &#39;./Button&#39;;
export { Card } from &#39;./Card&#39;;
export { Modal } from &#39;./Modal&#39;;
export { Tooltip } from &#39;./Tooltip&#39;;
</code></pre>
<p>Now <code>@/shared/ui</code> resolves to this file, and consumers import named things from one place. The appeal is real: imports get shorter, the folder gets a defined surface, and you can move <code>Button.tsx</code> around internally without breaking any consumer, as long as the barrel keeps pointing at the right place. That last part — decoupling the internal file layout from the public import path — is a genuine architectural benefit, not just cosmetics. It&#39;s the same &quot;public API of a folder&quot; idea that makes feature boundaries work.</p>
<p>So far this sounds like something you&#39;d want everywhere. That&#39;s exactly the trap.</p>
<h2>The cost hides where you&#39;re not looking</h2>
<p>Here&#39;s the problem. When you write <code>import { Button } from &#39;@/shared/ui&#39;</code>, you are not importing <code>Button</code>. You are importing the <em>barrel</em>, and the barrel imports <strong>everything</strong>. <code>Button</code>, <code>Card</code>, <code>Modal</code>, <code>Tooltip</code>, and whatever those pull in — the whole folder loads to hand you one component.</p>
<p>&quot;But tree-shaking removes the unused ones,&quot; you say. Sometimes. Tree-shaking is real, but it&#39;s fragile, and a barrel is exactly the shape that breaks it. All it takes is one module in the barrel with a side effect — a file that runs code at import time, registers something, mutates a global, or is marked as having side effects in <code>package.json</code> — and the bundler can no longer prove the other exports are safe to drop. To stay correct, it keeps them. Your one-line <code>Button</code> import just pulled in <code>Modal</code>, its focus-trap library, and the animation dependency <code>Tooltip</code> uses.</p>
<p>You won&#39;t see it in code review. The import line looks identical whether the tree shakes cleanly or not. You&#39;ll see it in a bundle analyzer six months later, staring at a route that weighs 400KB and wondering why a page with two buttons is importing a charting library. The answer is almost always a barrel somewhere in the chain, quietly gluing unrelated things together.</p>
<p>There&#39;s a second cost that shows up sooner: build and dev-server speed. Every barrel is a fan-out. Import one thing through <code>@/shared/ui</code> and the module graph has to resolve every file the barrel touches, and every file <em>those</em> touch. In a big app with barrels re-exporting barrels re-exporting barrels, you get a dependency graph that explodes on almost any import. Cold dev starts crawl. Hot updates touch more than they should. The tooling spends its time walking a graph you created for the sake of a shorter import line.</p>
<blockquote>
<p>A barrel trades a real cost you can&#39;t see for a cosmetic benefit you can.</p>
</blockquote>
<h2>The worst offender: the app-wide barrel</h2>
<p>The pattern that does the most damage is the ambition to have one barrel at a high level that re-exports a whole layer:</p>
<pre><code class="language-ts">// features/index.ts — please don&#39;t
export * from &#39;./edit-policy&#39;;
export * from &#39;./create-claim&#39;;
export * from &#39;./billing-dashboard&#39;;
export * from &#39;./user-settings&#39;;
// ...twenty more
</code></pre>
<p>Now <code>import { EditPolicyForm } from &#39;@/features&#39;</code> reaches into a barrel that transitively imports <em>every feature in the app</em>. Import one feature and you&#39;ve referenced all of them. Code-splitting — the entire point of which is that a route loads only what it needs — quietly stops working, because from the bundler&#39;s view everything is connected to everything through this one file. I&#39;ve watched a lazy-loaded route pull in the whole app because a single import went through a top-level barrel instead of reaching for the specific module.</p>
<p>The <code>export *</code> form is especially nasty. It&#39;s not just eager; it forces the bundler to load a module just to find out what names it exports, so it can figure out which star-export a given name came from. Named re-exports (<code>export { X } from &#39;./x&#39;</code>) are at least explicit. <code>export *</code> is a blank check.</p>
<h2>When a barrel earns its place</h2>
<p>I&#39;m not anti-barrel. I use them. The question is never &quot;barrels: yes or no.&quot; It&#39;s &quot;does this specific barrel pay for itself.&quot; A barrel earns its place when three things are true at once.</p>
<p><strong>It wraps a genuine, stable public API.</strong> A design-system package where <code>Button</code>, <code>Card</code>, and <code>Modal</code> are <em>the whole point</em> and consumers legitimately pull from across it — that&#39;s a real interface, and a barrel is the right way to express it. The barrel isn&#39;t hiding a mess; it&#39;s naming a surface that was always meant to be used as a whole.</p>
<p><strong>It&#39;s small and internally cohesive.</strong> A barrel over five related components in one feature is cheap — importing one probably means you&#39;re near the others anyway, and the fan-out is tiny. The cost of a barrel scales with what&#39;s behind it. Small folder, small cost.</p>
<p><strong>Nothing behind it has surprising weight or side effects.</strong> Pure, light modules tree-shake fine through a barrel. The danger is heavy or effectful modules — a component that drags in a 200KB dependency, a file that runs setup at import. One of those behind a barrel poisons every import that passes through it.</p>
<p>Miss any of those and the barrel is working against you. A top-level <code>@/features</code> barrel fails all three: not a stable surface, not small, and full of heavy things. A <code>shared/ui/index.ts</code> over a real design system passes all three. Same mechanism, opposite verdict — which is the whole reason &quot;always use barrels&quot; and &quot;never use barrels&quot; are both wrong.</p>
<h2>What I actually do</h2>
<p>My rules are boring, which is how I like architecture rules.</p>
<p><strong>Import from the specific file by default.</strong> <code>import { Button } from &#39;@/shared/ui/Button&#39;</code> is my normal. The import is three words longer and I never think about it again. No fan-out, no tree-shaking gamble, no accidental coupling.</p>
<pre><code class="language-ts">// default: reach for the exact thing
import { Button } from &#39;@/shared/ui/Button&#39;;
import { formatCurrency } from &#39;@/shared/lib/money&#39;;
</code></pre>
<p><strong>Add a barrel only at a real boundary I&#39;ve decided to publish.</strong> A feature&#39;s public entry point, a design-system package&#39;s root — places where &quot;this is the surface, use it as one&quot; is a deliberate architectural statement, not a convenience. And when I do, I write explicit named re-exports, never <code>export *</code>:</p>
<pre><code class="language-ts">// features/edit-policy/index.ts — a deliberate public surface
export { EditPolicyForm } from &#39;./ui/EditPolicyForm&#39;;
export { useEditPolicy } from &#39;./model/useEditPolicy&#39;;
// internal helpers are NOT re-exported — they stay private
</code></pre>
<p>That barrel is doing architectural work: it defines what the feature exposes and hides everything else, so the rest of the app can&#39;t reach into the feature&#39;s guts. That&#39;s a barrel as an <em>encapsulation boundary</em>, which is worth having. A barrel as a <em>typing convenience</em> usually isn&#39;t.</p>
<p><strong>Never barrel a whole layer.</strong> No <code>@/features</code>, no <code>@/entities</code> mega-barrel. Layers are not modules; they&#39;re categories. Giving a category a single import door is how code-splitting dies.</p>
<p><strong>Verify with the analyzer, not with faith.</strong> Tree-shaking is a claim, not a guarantee. Run a bundle analyzer, find the route that&#39;s too heavy, and trace it. More often than not the culprit is a barrel doing exactly what I described — and the fix is changing one import from the barrel to the specific file.</p>
<h2>The real lesson isn&#39;t about barrels</h2>
<p>Strip away the mechanics and this is the same idea as the <code>shared/</code> article, pointed at a different symptom. A barrel is a public API for a folder. Public APIs are worth having when there&#39;s a real boundary to protect and a real surface to name. They&#39;re pure cost when you add them out of habit, to make imports prettier, over a pile of things that were never meant to be used as a unit.</p>
<p>So the question to ask isn&#39;t &quot;should this folder have an index.ts.&quot; It&#39;s &quot;does this folder have a public API worth defining — and am I willing to pay the fan-out to define it.&quot; When the answer is yes, barrel it, name the surface, and hide the rest. When the answer is &quot;I just want shorter imports,&quot; reach for the specific file and move on. The prettier import was never worth the bundle you couldn&#39;t see.</p>
<p>Next in this stretch on architecture, I want to get at the thing underneath both of these articles: <a href="https://ma-x.im/blog/react-playbook-module-dependencies">how modules should <em>depend</em> on each other</a> at all — the direction of the arrows, who&#39;s allowed to import whom, and why a dependency graph is the most honest picture of your app&#39;s health. If you&#39;ve got a bundle that mysteriously balloons on one route, open the analyzer and go hunting for a barrel — then tell me what you found. I&#39;m always curious how far the fan-out reached.</p>
]]></content:encoded>
      <pubDate>Fri, 19 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>Performance</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The shared/ Folder Is Not a Place, It&apos;s a Promise</title>
      <link>https://ma-x.im/blog/react-playbook-shared-code</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-shared-code</guid>
      <description>Every codebase grows a shared/ folder, and given enough time every shared/ folder becomes a junk drawer. Not because the team got lazy — because the folder was never given a rule. Here&apos;s why it rots, and how to keep it boring.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-shared-code/cover.png" alt="The shared/ Folder Is Not a Place, It&apos;s a Promise" /></p>
<p>Open the <code>shared/</code> folder in almost any React app that&#39;s been alive for a year. Count the files. Then try to explain, out loud, what the folder is <em>for</em> — not what&#39;s in it, what it&#39;s <em>for</em>. You&#39;ll stall. There&#39;s a date formatter, sure, and a Button. But there&#39;s also <code>useUserPermissions</code>, a <code>formatPolicyNumber</code>, half a checkout state machine, a <code>legacyApiAdapter</code>, and three hooks whose names start with <code>use</code> and end in a shrug.</p>
<p>That folder didn&#39;t get that way because someone was careless. It got that way because <code>shared/</code> is the one folder in your app with no rule attached to it. Everything else has a reason to reject a file. <code>shared/</code> accepts everything. And a folder that accepts everything isn&#39;t a category — it&#39;s a drain.</p>
<p>I already flagged this in the <a href="https://ma-x.im/blog/react-playbook-code-structure">code structure article</a>: <code>shared/</code> is the most abused folder in React architecture. That piece told you the rule. This one is about why the rule is so hard to keep, and how to dig out once you&#39;ve already lost.</p>
<h2>Why the drawer fills up</h2>
<p>The junk drawer isn&#39;t a discipline problem. If it were, telling people &quot;be more careful about <code>shared/</code>&quot; would fix it. It never does. The rot is structural, and it comes from three forces that all push the same direction.</p>
<p><strong>The naming trap.</strong> You write a hook. Where does it go? If it has an obvious owner — <code>features/edit-policy</code> — the answer is easy. But a lot of code sits in the awkward middle: not clearly owned, not clearly generic. <code>useDebouncedValue</code> is generic. <code>usePolicyDraftAutosave</code> is owned. But <code>useAutosave</code>, the one that <em>started</em> generic and quietly grew a <code>policyId</code> parameter — that one has no obvious home. So it goes to <code>shared/</code>, because <code>shared/</code> never says no.</p>
<p><strong>The reuse reflex.</strong> The moment a second file wants something, the instinct is to &quot;lift it up&quot; so both can reach it. This feels like good engineering. It&#39;s usually premature. Two components in the same feature using one helper is not a reason to move that helper three layers up into <code>shared/lib</code> — it&#39;s a reason to keep it <em>inside the feature</em>. Reuse across a boundary is a real signal. Reuse within a boundary is just normal code living near the thing that uses it.</p>
<p><strong>The path of least resistance.</strong> <code>shared/</code> is at the bottom of the dependency graph, so everything can import from it. That makes it the safest place to drop something when you&#39;re in a hurry and don&#39;t want to think about ownership. No import will ever break. And that&#39;s exactly the problem: the folder that&#39;s safe to import from is also the folder that&#39;s frictionless to dump into.</p>
<p>Put those three together and you don&#39;t get a junk drawer because people are bad at their jobs. You get one because the folder is doing precisely what an unconstrained folder does under pressure. Delivery pressure always wins over categorization, and <code>shared/</code> is where &quot;I&#39;ll sort it later&quot; goes to never be sorted.</p>
<blockquote>
<p><code>shared/</code> is not a place you put things. It&#39;s a promise you make about them.</p>
</blockquote>
<p>That reframing is the whole article. The promise is: <em>this code is domain-agnostic and portable — it would make sense in a completely different product.</em> Every file in <code>shared/</code> is you signing that promise. The drawer fills up because people put things there without noticing they&#39;re signing anything.</p>
<h2>The test that actually works</h2>
<p>The rule I gave in the structure article still holds, and it&#39;s the only test I&#39;ve found that survives contact with a real team:</p>
<blockquote>
<p>A file belongs in <code>shared/</code> only if you could move it to a <em>different product at the same company</em> and it would still make sense.</p>
</blockquote>
<p>A <code>Button</code>? Moves fine. A <code>formatCurrency</code>? Moves fine. An <code>http</code> client with your auth interceptor? Borderline — it moves if the auth scheme is company-wide, stays if it&#39;s app-specific. A <code>useUserPermissions</code> hook that knows your permission strings? Doesn&#39;t move. It&#39;s dressed up as generic — it takes a permission key, it returns a boolean, it <em>looks</em> reusable — but it&#39;s welded to your domain&#39;s idea of what a permission is. That&#39;s not shared code. That&#39;s domain code wearing a generic costume.</p>
<p>Here&#39;s the tell, in code. This looks shared:</p>
<pre><code class="language-ts">// shared/hooks/usePermission.ts
export function usePermission(key: string): boolean {
  const { permissions } = useCurrentUser();
  return permissions.includes(key);
}
</code></pre>
<p>It isn&#39;t. <code>useCurrentUser</code> is a domain concept. The moment this hook reaches sideways into <code>entities/user</code>, it stopped being portable — it now depends on <em>your</em> app&#39;s notion of a user. Move it to a different product and it breaks. It belongs in <code>entities/user</code>, exported as part of that entity&#39;s public surface. What&#39;s left in <code>shared/</code> should be the genuinely dumb primitive:</p>
<pre><code class="language-ts">// shared/lib/includes.ts — portable, knows nothing
export const hasKey = (keys: string[], key: string) =&gt; keys.includes(key);
</code></pre>
<p>The portability test isn&#39;t about whether code <em>can</em> be reused. Almost anything can be reused if you squint. It&#39;s about whether the code <em>knows about your domain</em>. Knowledge is the boundary, not usage.</p>
<h2>Make the boundary a wall, not a suggestion</h2>
<p>A rule that lives only in a code review is a rule you will lose. Reviewers get tired. New people don&#39;t know it. The pressure that fills the drawer doesn&#39;t take weekends off. So the direction of dependencies — lower layers never importing from higher ones — has to be enforced by tooling, not vigilance.</p>
<p>ESLint&#39;s <code>no-restricted-imports</code> gets you most of the way with zero extra dependencies:</p>
<pre><code class="language-js">// eslint.config.js
export default [
  {
    files: [&#39;src/shared/**&#39;],
    rules: {
      &#39;no-restricted-imports&#39;: [
        &#39;error&#39;,
        {
          patterns: [
            {
              group: [
                &#39;@/features/*&#39;,
                &#39;@/entities/*&#39;,
                &#39;@/widgets/*&#39;,
                &#39;@/pages/*&#39;,
                &#39;@/app/*&#39;,
              ],
              message:
                &#39;shared/ must not import from higher layers. If this file needs domain code, it does not belong in shared/.&#39;,
            },
          ],
        },
      ],
    },
  },
];
</code></pre>
<p>Now the <code>usePermission</code> example from earlier fails the build the instant it imports <code>useCurrentUser</code>. The error message isn&#39;t just &quot;no&quot; — it <em>explains the promise</em>: if this file needs domain code, it doesn&#39;t belong here. The lint rule becomes the reviewer who never gets tired and never forgets the rule. For richer needs — enforcing that features can&#39;t import each other, that entities stay isolated — a dedicated boundaries plugin does the same job with more expressive rules, but the built-in gets a small team surprisingly far.</p>
<p>The point isn&#39;t the exact config. It&#39;s that the promise stops depending on everyone remembering it. You made <code>shared/</code> say no. That single change is the difference between a folder that stays small and one that quietly triples every quarter.</p>
<h2>Digging out of one you already have</h2>
<p>Prevention is easy to talk about when the drawer is empty. Most of the time it isn&#39;t. You&#39;ve inherited forty files and a folder that&#39;s load-bearing in ways nobody documented. You can&#39;t stop shipping features to go on an architecture retreat, and a big-bang &quot;fix shared/&quot; pull request is a merge-conflict machine that reviewers will rubber-stamp out of exhaustion. So don&#39;t do that. Dig out incrementally.</p>
<p><strong>First, audit without moving anything.</strong> Go through <code>shared/</code> file by file and tag each one with a comment — three buckets only:</p>
<ul>
<li><strong>Portable</strong> — passes the test. Leave it. This is what <code>shared/</code> is <em>for</em>, and seeing what legitimately qualifies is calming.</li>
<li><strong>Owned</strong> — knows about a domain concept. It has a real home in an <code>entity</code> or a <code>feature</code>; it&#39;s just sitting in the wrong place.</li>
<li><strong>Split</strong> — genuinely two things fused together: a portable core with a domain-specific wrapper grown around it. The <code>useAutosave</code>-that-grew-a-<code>policyId</code> case.</li>
</ul>
<p><strong>Then move the easy ones.</strong> The <em>Owned</em> bucket is pure relocation — cut the file, paste it into its real home, fix the imports, done. Your lint rule from the previous section will actually help here: as you move domain code out, anything still illegally reaching into higher layers lights up red, showing you the next thing to fix. The compiler turns into a to-do list.</p>
<p><strong>Then split the hard ones.</strong> For each <em>Split</em> file, separate the portable core from the domain wrapper. The core stays in <code>shared/</code> as a dumb primitive. The wrapper moves down to the feature that needs the domain-specific behavior:</p>
<pre><code class="language-ts">// before — one file in shared/, quietly domain-aware
// shared/hooks/useAutosave.ts
export function useAutosave(policyId: string, draft: PolicyDraft) { /* ... */ }

// after — the portable half
// shared/lib/useAutosave.ts — generic, no domain knowledge
export function useAutosave&lt;T&gt;(key: string, value: T, save: (v: T) =&gt; Promise&lt;void&gt;) { /* ... */ }

// after — the domain half, where it belongs
// features/edit-policy/model/usePolicyAutosave.ts
export function usePolicyAutosave(policyId: string, draft: PolicyDraft) {
  return useAutosave(`policy:${policyId}`, draft, savePolicyDraft);
}
</code></pre>
<p>Notice the generic <code>useAutosave</code> got <em>better</em> by being forced portable — it now takes its save function as an argument instead of hard-coding <code>savePolicyDraft</code>, which makes it genuinely reusable for the first time. Enforcing the boundary didn&#39;t just tidy the folder. It improved the primitive.</p>
<p><strong>Then delete.</strong> The satisfying part. Once the <em>Owned</em> and <em>Split</em> files are gone, you&#39;ll almost always find a handful of files nothing imports anymore — helpers that existed only to serve code that moved. Delete them. A shrinking <code>shared/</code> is the clearest signal the dig-out is working.</p>
<p>You don&#39;t have to finish in one sitting. Do a bucket a week. The direction matters more than the speed: as long as <code>shared/</code> is getting smaller and more portable over time instead of larger and vaguer, you&#39;re winning.</p>
<h2>A word on the barrel</h2>
<p>While you&#39;re in here, you&#39;ll be tempted to add an <code>index.ts</code> that re-exports everything so imports look clean: <code>import { Button, formatCurrency } from &#39;@/shared&#39;</code>. Resist making that reflexive. A barrel over a genuinely small, stable, portable <code>shared/</code> is fine. A barrel over the junk drawer just gives the junk drawer a tidy front door — and it can quietly drag your whole <code>shared/</code> tree into every bundle that touches one export. The clean import is cosmetic; the coupling underneath is real. Fix the contents first. The public API of a folder is only worth having once the folder actually keeps a promise. That trade-off — barrels, bundles, and what a folder&#39;s public surface should really be — is a whole topic on its own, and one I want to come back to.</p>
<h2>The folder was never the problem</h2>
<p>It&#39;s easy to read all this as &quot;<code>shared/</code> is bad, avoid it.&quot; That&#39;s the wrong lesson. Every non-trivial app needs a home for genuinely portable code, and <code>shared/</code> is a perfectly good name for it. The folder was never the enemy.</p>
<p>The missing promise was. A junk drawer is just a drawer that nobody agreed on the contents of. The fix isn&#39;t a better folder name or a stricter reviewer — it&#39;s making the promise explicit, and then making a machine enforce it so the promise survives the next deadline. Do that, and <code>shared/</code> stops being the place your architecture goes to die and goes back to being the boring, dependable bottom of the stack. Boring is the goal. In <code>shared/</code>, boring is the whole point.</p>
<p>Next, I want to pick up the barrel thread I brushed past earlier: <a href="https://ma-x.im/blog/react-playbook-barrel-files">why the tidy <code>index.ts</code> that gives a folder a clean front door</a> can quietly drag half your app into a bundle that only needed one function — and when it&#39;s still worth it.</p>
<p>If you&#39;ve got a <code>shared/</code> folder you&#39;re a little afraid to open — or a war story about one that got completely out of hand — I&#39;d genuinely like to hear it. Reach out; I collect these.</p>
]]></content:encoded>
      <pubDate>Tue, 16 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>FSD</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>React in 3D: When &lt;div&gt; Becomes &lt;mesh&gt;</title>
      <link>https://ma-x.im/blog/react-playbook-vr-ar</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-vr-ar</guid>
      <description>The idea that React could render a 3D scene sounds like a stretch — React is for interfaces, 3D is a different universe. Except it isn&apos;t a stretch at all, and understanding why closes out this whole series with its central idea: React was never really about the DOM. This is where that finally becomes obvious.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-vr-ar/cover.png" alt="React in 3D: When &lt;div&gt; Becomes &lt;mesh&gt;" /></p>
<p>The first time someone tells you that you can build a 3D scene — a rotating model, a lit environment, a VR world — using React, it sounds like a category error. React is for interfaces. Buttons, forms, lists. Three-dimensional graphics are a different universe with their own math, their own pipeline, their own everything. Surely gluing them together is a hack.</p>
<p>It isn&#39;t, and the reason it isn&#39;t is the perfect note to end this series on. Because after twenty-nine articles about data fetching and forms and auth and desktop and mobile, the thing rendering React in 3D quietly proves is the idea that was underneath all of it the whole time: <strong>React was never really about the DOM.</strong> React is a system for describing a UI as a function of state and letting a renderer figure out how to make it real. The DOM was just the first and most common target. Point that same system at a 3D scene graph and it works — not as a trick, but because 3D was always within the reach of what React actually is.</p>
<p>So this last article is short, and it&#39;s really about a realization more than a technology. Let me show you what I mean.</p>
<h2>The Scene Graph Is Just Another Tree</h2>
<p>Here&#39;s the conceptual key. React renders trees. The DOM is a tree of elements — a <code>div</code> containing a <code>span</code> containing text. A 3D scene is <em>also</em> a tree: a scene contains a mesh, a mesh has a geometry and a material, lights and cameras sit alongside them. Same fundamental shape — a nested hierarchy of things — which means React&#39;s component model, built for exactly that shape, fits a 3D scene as naturally as it fits a webpage.</p>
<p><a href="https://r3f.docs.pmnd.rs">React Three Fiber</a> is the renderer that makes this literal. It&#39;s React — real React, same hooks, same state, same composition — where the elements are 3D objects instead of HTML tags. You don&#39;t write <code>&lt;div&gt;</code>; you write <code>&lt;mesh&gt;</code>. You don&#39;t nest <code>&lt;p&gt;</code> inside <code>&lt;section&gt;</code>; you nest a geometry and a material inside a mesh.</p>
<pre><code class="language-tsx">import { Canvas } from &#39;@react-three/fiber&#39;;

function Box() {
  return (
    &lt;mesh rotation={[0.4, 0.2, 0]}&gt;
      &lt;boxGeometry args={[1, 1, 1]} /&gt;
      &lt;meshStandardMaterial color=&quot;#22c55e&quot; /&gt;
    &lt;/mesh&gt;
  );
}

export function Scene() {
  return (
    &lt;Canvas&gt;
      &lt;ambientLight /&gt;
      &lt;pointLight position={[10, 10, 10]} /&gt;
      &lt;Box /&gt;
    &lt;/Canvas&gt;
  );
}
</code></pre>
<p>Read that and the uncanny thing is how <em>ordinary</em> it is. It&#39;s components. <code>&lt;Box /&gt;</code> is a component you could reuse, wrap in a list, conditionally render. <code>rotation</code> and <code>color</code> are just props — drive them from state and the cube animates or recolors, because it&#39;s the same reactivity you&#39;ve used all series. <code>&lt;mesh&gt;</code> is to a 3D renderer what <code>&lt;div&gt;</code> is to the DOM renderer: a primitive the renderer knows how to draw. Everything you know about composing React components — props down, state-driven rendering, reuse, hooks — applies unchanged. Only the primitives at the bottom of the tree are different.</p>
<h2>VR and AR Are Just More of the Same Realization</h2>
<p>Once you accept that React can describe a 3D scene, VR and AR stop being a separate leap. A VR headset is, from the code&#39;s point of view, a 3D scene rendered in stereo with head tracking. AR is a 3D scene composited over a camera feed. The <em>scene</em> — the tree of meshes and lights and cameras — is the same tree React Three Fiber already renders; the WebXR layer adds the headset and the tracking on top.</p>
<p>So the progression is one idea extended, not three separate technologies to learn. Describe a 3D scene as a React tree. Render that tree to a screen — that&#39;s 3D on the web. Render it to a headset in stereo — that&#39;s VR. Composite it onto the real world — that&#39;s AR. At every step the component model is identical; what changes is the output device, exactly the way it changed when we went from browser tab to <a href="https://ma-x.im/blog/react-playbook-desktop">desktop window</a> to <a href="https://ma-x.im/blog/react-playbook-react-native">native phone</a>. Same React, different target. You are not learning 3D-React and then VR-React and then AR-React. You are learning that React describes a tree, and the tree can be pointed at increasingly immersive places.</p>
<h2>The Thing This Whole Series Was Actually About</h2>
<p>I saved this topic for last on purpose, because it makes the through-line of everything before it visible.</p>
<p>Look back at what we did. We fetched data and let a renderer reconcile the result. We managed state and let components re-render as a function of it. We moved React onto the desktop, onto phones, and now into three-dimensional space — and at every single stop, the <em>same</em> mental model carried: describe what the UI should be for a given state, and let a renderer make it real on whatever the target happens to be. The target was a browser, then a native window, then a native view, then a scene graph. The model never changed. That&#39;s not a coincidence you stumble on at article thirty — it&#39;s the thing the whole series was quietly teaching under the cover of specific problems.</p>
<blockquote>
<p>React is not a DOM library. It is a way of describing UI as a function of state, with a renderer that reconciles your description against some target. The DOM was always just the first target.</p>
</blockquote>
<p>That&#39;s why a React developer can move across this entire landscape — web, desktop, mobile, 3D, immersive — without starting over each time. The specifics are real and each one takes genuine learning, as every article in this series insisted. But the foundation is portable in a way that&#39;s rare in software, and 3D is the clearest proof: the moment <code>&lt;div&gt;</code> becomes <code>&lt;mesh&gt;</code> and <em>nothing else about how you think has to change</em>, you understand what you actually learned. You didn&#39;t learn the DOM. You learned React.</p>
<h2>How I&#39;d Approach It — and Where the Series Lands</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>3D isn&#39;t a hack on top of React.</strong> A scene is a tree, React renders trees, and React Three Fiber makes the elements 3D objects. <code>&lt;mesh&gt;</code> is the new <code>&lt;div&gt;</code>.</li>
<li><strong>Your whole component model transfers.</strong> Props, state, hooks, composition, reuse — all unchanged. Only the leaf primitives differ.</li>
<li><strong>VR and AR are the same tree, a different output device.</strong> Screen, headset, camera-composited world. Learn the scene, not three separate stacks.</li>
<li><strong>The portable asset was never the platform.</strong> It&#39;s the model: UI as a function of state, reconciled by a renderer against a target.</li>
</ul>
<p>And that&#39;s where I&#39;ll leave the React Playbook. Thirty articles, from spinning up a project to rendering a cube in VR, and the honest summary of all of them is smaller than the page count suggests: pick tools that own the messy edges, keep your source of truth in one place, treat the client as untrusted and the boundary as sacred, and describe your UI as a function of state so it can follow you to whatever comes next. The specific libraries will churn — some of the ones I recommended will be replaced, and that&#39;s fine. The way of thinking is the part that lasts, and if this series left you with that instead of a list of dependencies, it did its job.</p>
<p>Thanks for reading this far. If the Playbook changed how you approach even one of these problems — or if you disagree with where I landed on some of them, which is entirely fair — I&#39;d genuinely like to hear about it. That&#39;s the conversation that makes writing thirty of these worth it.</p>
]]></content:encoded>
      <pubDate>Fri, 12 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>3D</category>
      <category>VR</category>
      <category>AR</category>
      <category>React Three Fiber</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>React Native: What &apos;Learn Once, Write Anywhere&apos; Really Means</title>
      <link>https://ma-x.im/blog/react-playbook-react-native</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-react-native</guid>
      <description>React Native&apos;s promise gets misremembered as &apos;write once, run everywhere&apos; — and that misremembering is why so many web developers hit a wall. The knowledge transfers; the code and the instincts often don&apos;t. Here&apos;s the honest line between what your React experience buys you on mobile and what it quietly doesn&apos;t.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-react-native/cover.png" alt="React Native: What &apos;Learn Once, Write Anywhere&apos; Really Means" /></p>
<p>The single most useful thing to get straight about React Native is a slogan, and it&#39;s one people consistently misquote. The promise was never &quot;write once, run anywhere.&quot; It was &quot;<em>learn</em> once, write anywhere&quot; — and the difference between those two words is the entire reason web developers either love React Native or feel betrayed by it.</p>
<p>Because here&#39;s what actually transfers to mobile: your React knowledge. Components, props, state, hooks, the mental model of describing UI as a function of data — all of it, intact, on day one. You are not starting over. What does <em>not</em> transfer is a lot of your code and, more subtly, a lot of your instincts. The <code>&lt;div&gt;</code> you&#39;ve typed a hundred thousand times doesn&#39;t exist. CSS as you know it doesn&#39;t exist. The layout habits your fingers have memorized quietly stop working. So you arrive on mobile fluent in the language and unfamiliar with the streets, and whether that feels empowering or frustrating comes down entirely to expecting it.</p>
<p>This article is about drawing that line honestly — what your React experience genuinely buys you on mobile, and where it stops — so you show up expecting &quot;learn once,&quot; not &quot;write once,&quot; and don&#39;t waste a week being angry at the wall.</p>
<h2>There Is No DOM. That&#39;s the Whole Shift.</h2>
<p>Start with the concept that reorganizes everything else: React Native does not render to a browser, because there is no browser. Your components render to <em>actual native platform views</em> — a real iOS <code>UIView</code>, a real Android widget. React is the engine describing the UI; the thing it draws is native, not HTML.</p>
<p>This is why the primitives change. You don&#39;t write <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> and <code>&lt;p&gt;</code>, because those are HTML and there&#39;s no HTML here. You write <code>&lt;View&gt;</code> and <code>&lt;Text&gt;</code>, which map to genuine native components:</p>
<pre><code class="language-tsx">import { View, Text, Pressable } from &#39;react-native&#39;;

export function Greeting({ name }: { name: string }) {
  return (
    &lt;View style={styles.card}&gt;
      &lt;Text style={styles.title}&gt;Hi {name}&lt;/Text&gt;
      &lt;Pressable onPress={() =&gt; console.log(&#39;tapped&#39;)}&gt;
        &lt;Text&gt;Tap me&lt;/Text&gt;
      &lt;/Pressable&gt;
    &lt;/View&gt;
  );
}
</code></pre>
<p>Notice how <em>familiar</em> the shape is — it&#39;s components, props, a style prop, an event handler. And notice how many small things are different: <code>View</code> not <code>div</code>, <code>Text</code> wrapping every piece of text (you can&#39;t just drop a bare string like in HTML), <code>onPress</code> not <code>onClick</code>, <code>Pressable</code> instead of a <code>button</code>. That&#39;s &quot;learn once, write anywhere&quot; in miniature — the <em>shape</em> of your React knowledge fits perfectly, and every <em>specific</em> has to be relearned. The engine is the same; the vocabulary is new.</p>
<h2>Styling: It Looks Like CSS and Isn&#39;t</h2>
<p>The place this bites hardest, and the one worth dwelling on, is styling — because it looks close enough to CSS that you trust it, then it violates that trust.</p>
<p>React Native styling <em>resembles</em> CSS: you write style objects with camelCased properties, and many familiar names work. But it is not CSS, and the differences are exactly the ones your instincts will trip over. There are no cascading stylesheets, no selectors, no inheritance in the way you rely on — styles are explicit, per-component objects. There&#39;s no <code>display: block</code> versus <code>inline</code>. Most jarring of all: <strong>everything is flexbox, and it&#39;s vertical by default.</strong> On the web, <code>flexDirection</code> defaults to <code>row</code>; in React Native it defaults to <code>column</code>, because a phone screen is tall. Your web layout muscle memory, built on horizontal-by-default flow, is subtly wrong on every screen until you internalize the flip.</p>
<pre><code class="language-tsx">import { StyleSheet } from &#39;react-native&#39;;

const styles = StyleSheet.create({
  card: {
    // flexDirection defaults to &#39;column&#39; here — the opposite of the web.
    padding: 16,
    backgroundColor: &#39;#fff&#39;,
  },
  title: { fontSize: 18, fontWeight: &#39;600&#39; },
});
</code></pre>
<p>None of this is hard. It&#39;s just <em>different</em>, and the danger is precisely that it looks similar enough that you don&#39;t respect the difference and spend an afternoon confused why your row is a column. Expect styling to be &quot;CSS-flavored, not CSS,&quot; and the friction drops away. Treat it as the CSS you know and it will quietly punish you.</p>
<h2>Just Use Expo</h2>
<p>A practical fork, stated plainly because it saves the most pain: when you start a React Native project, use <a href="https://expo.dev">Expo</a>. Bare React Native drops you into native build toolchains — Xcode, Android Studio, native dependency management — which is a heavy, platform-specific world that has nothing to do with React and everything to do with mobile plumbing you didn&#39;t sign up to learn on day one.</p>
<p>Expo is to React Native roughly what <a href="https://ma-x.im/blog/react-playbook-starting-new-project">a good meta-framework is to React on the web</a>: it owns the miserable setup and configuration so you can stay in the part you&#39;re good at. It handles the build tooling, gives you a clean managed workflow, and bundles access to native device APIs — camera, location, notifications — behind simple JavaScript, so reaching for the camera doesn&#39;t mean touching native code. It&#39;s the same lesson this whole series keeps landing on: don&#39;t hand-assemble the plumbing when a well-made tool already owns it. For the overwhelming majority of apps, Expo is simply the right starting point, and starting bare is a choice you should have a specific reason for.</p>
<h2>The Instinct That Actually Needs Retraining</h2>
<p>Beyond the vocabulary, there&#39;s a deeper adjustment, and it&#39;s the one that separates a web app running on a phone from an app that belongs there. <strong>Mobile is not a small desktop, and users can feel the difference instantly.</strong></p>
<p>Touch is not a mouse. There&#39;s no hover — a whole category of web interaction just doesn&#39;t exist, and designs that lean on it fall flat. Targets have to be finger-sized, not cursor-precise. Gestures — swipe, long-press, pull-to-refresh — are expected vocabulary, not enhancements. Platform conventions differ, and iOS and Android users each have expectations about how navigation and controls should feel that a lowest-common-denominator layout ignores at its peril. And the device itself imposes realities the desktop rarely forces you to think about: constrained screens, variable network, battery, memory. The web developer&#39;s instinct to fill horizontal space, to treat the network as reliable, to assume hover and precise pointing — those instincts need retraining, and the app quality lives in that retraining far more than in the syntax.</p>
<p>This is the honest edge of &quot;learn once, write anywhere&quot;: your React <em>transfers</em>, but your sense of what a good interface <em>is</em> has to expand from the web&#39;s assumptions to the phone&#39;s. That&#39;s not a knock on React Native — it&#39;s the reality of the platform being genuinely native, which is the whole point of using it.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>It&#39;s &quot;learn once,&quot; not &quot;write once.&quot;</strong> Your React knowledge transfers whole; a lot of your code and instincts don&#39;t. Expect that and the wall becomes a curriculum.</li>
<li><strong>There is no DOM.</strong> <code>View</code> and <code>Text</code>, not <code>div</code> and <code>span</code>; <code>onPress</code>, not <code>onClick</code>. You render to real native views.</li>
<li><strong>Styling is CSS-flavored, not CSS.</strong> No cascade, no selectors, flexbox everywhere, and column-by-default. Respect the difference instead of trusting the resemblance.</li>
<li><strong>Use Expo.</strong> It owns the native build plumbing so you stay in React. Go bare only with a specific reason.</li>
<li><strong>Retrain your interface instincts.</strong> Touch, gestures, finger targets, platform conventions, device limits — mobile is native, not a narrow desktop.</li>
</ul>
<p>The reason React Native either delights or frustrates comes down to which promise you showed up believing. Arrive expecting &quot;write once, run everywhere&quot; and every difference feels like a broken promise. Arrive expecting &quot;learn once, write anywhere&quot; and the exact same differences feel like a reasonable tax on real native rendering — you keep the expensive thing, your React fluency, and pay for the platform specifics, which is a genuinely good trade. The knowledge is the asset. The code was always going to be somewhat local to where it runs.</p>
<p>For the final article, the series leaves flat screens behind completely: <a href="https://ma-x.im/blog/react-playbook-vr-ar">React in VR and AR</a> — where the same component model gets pointed at three-dimensional space, and what it means to write <code>&lt;mesh&gt;</code> instead of <code>&lt;div&gt;</code>.</p>
<p>If you&#39;re a web developer eyeing your first React Native project and bracing for a total restart, don&#39;t — it&#39;s far more transfer than restart, once you know which parts are which. Tell me what your app needs to do on the phone and I&#39;ll tell you where your web instincts will help and where they&#39;ll trip you.</p>
]]></content:encoded>
      <pubDate>Tue, 09 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>React Native</category>
      <category>Mobile</category>
      <category>Expo</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>React on the Desktop: When a Window Beats a Tab</title>
      <link>https://ma-x.im/blog/react-playbook-desktop</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-desktop</guid>
      <description>The pitch is irresistible — ship your React app as a real desktop program, same code, native icon in the dock. Electron and Tauri both promise it. But the interesting question isn&apos;t &apos;can I,&apos; it&apos;s &apos;what do I actually gain that a browser tab can&apos;t give me,&apos; and the answer decides which tool, and whether you should bother at all.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-desktop/cover.png" alt="React on the Desktop: When a Window Beats a Tab" /></p>
<p>There&#39;s a specific moment where the desktop pitch becomes irresistible. You&#39;ve built a genuinely good React app, it lives in a browser tab, and someone asks &quot;can we make it a real app?&quot; — with a dock icon, a name, a window of its own. And the answer is yes, easily, with the same React code you already have. Electron and Tauri will both wrap it into a program that installs like any native app. The demo takes an afternoon and feels like magic.</p>
<p>So the tempting question is &quot;how do I turn my React app into a desktop app,&quot; and it&#39;s the wrong one to lead with. The right question — the one that actually determines whether this is worth doing and which tool to reach for — is <em>what do I gain by leaving the browser at all?</em> Because a desktop wrapper that just shows your website in a frameless window is a worse browser tab: heavier, harder to update, and offering the user nothing they didn&#39;t already have. The whole justification for going desktop is the stuff a browser tab structurally <em>cannot</em> do, and if you don&#39;t need that stuff, you probably shouldn&#39;t be here.</p>
<p>This article is about that line — what the desktop actually buys you — and the honest tradeoff between the two tools that get you there.</p>
<h2>What a Browser Tab Genuinely Can&#39;t Do</h2>
<p>Start with the reason to leave the browser, because it&#39;s the whole point. A web page runs in a sandbox, deliberately walled off from the machine it&#39;s on — that&#39;s a security feature, not a limitation to complain about. But it means there are real capabilities a tab simply doesn&#39;t have, and those capabilities are your entire reason for going desktop.</p>
<p>Deep filesystem access — reading and writing arbitrary files on the user&#39;s disk, not the sandboxed trickle a browser allows. Real OS integration — a tray icon, native menus, global keyboard shortcuts that work when your app isn&#39;t focused, notifications that feel like the system&#39;s own. Talking to native or system-level resources the browser keeps off-limits. Working genuinely offline as a first-class program, not a web page hoping its service worker cached the right things. If your app needs one of these, the desktop isn&#39;t a nicety — it&#39;s the only place the feature can exist.</p>
<p>And the contrapositive matters just as much: if your app needs <em>none</em> of these — if it&#39;s fundamentally a web app that someone just wants to &quot;feel&quot; installed — then wrapping it in Electron mostly buys you weight and update headaches. Be honest about which side of that line you&#39;re on before you install anything. The desktop is for apps that need to touch the machine, not for apps that want a fancier bookmark.</p>
<h2>The Two Tools, and the Real Tradeoff</h2>
<p>Assuming you genuinely need out of the browser, there are two serious choices, and they represent a real architectural fork — not a matter of taste.</p>
<p><strong>Electron</strong> is the established one — VS Code, Slack, Discord are all Electron. Its model is straightforward: it ships an entire copy of Chromium <em>and</em> Node.js inside your app. Your React runs in a real Chrome, and you get full Node in the backend process, so everything you know works exactly as expected. The cost is equally straightforward: because every app bundles its own whole browser, a trivial Electron app starts at a hundred-plus megabytes and a chunk of memory before it does anything. You&#39;re shipping a browser per app, times every Electron app the user has installed.</p>
<p><strong>Tauri</strong> is the modern challenger with a different bet. Instead of bundling Chromium, it uses the operating system&#39;s <em>own</em> built-in webview — the browser engine already on the machine. And instead of Node, its backend is Rust. The payoff is dramatic: Tauri apps are a fraction of the size and memory, often a few megabytes instead of a hundred. The cost is the two edges of that same bet. Using the OS webview means you&#39;re rendering on WebKit on macOS, WebView2 on Windows — <em>different engines</em> — so you inherit cross-browser inconsistency as a desktop concern, which is a genuinely strange thing to have to test for in an installed app. And the native backend is Rust, so any serious system-level logic pulls you into a language your React team may not know.</p>
<pre><code>                Electron                     Tauri
  Renderer      bundled Chromium (same       OS webview (WebKit / WebView2,
                everywhere)                  differs per platform)
  Backend       Node.js                      Rust
  App size      100MB+ (ships a browser)     a few MB (uses the OS&#39;s)
  You trade     size &amp; memory                cross-engine testing + Rust
</code></pre>
<p>So the choice isn&#39;t &quot;which is better,&quot; it&#39;s which cost you&#39;d rather pay. Electron trades disk and memory for total consistency and a stack your web team already knows. Tauri trades a small footprint for cross-engine testing and a Rust backend. Neither is wrong; they&#39;re priced differently, and the right pick depends on whether size or familiarity hurts you more.</p>
<h2>The Part That&#39;s Actually Different: Two Processes and a Bridge</h2>
<p>Whichever you choose, there&#39;s one architectural shift that catches web developers off guard, and it&#39;s worth understanding because it shapes everything you build. A desktop app is not one environment — it&#39;s <em>two</em>, with a guarded door between them.</p>
<p>Your React UI runs in the renderer, which is still fundamentally a web context, sandboxed. The powerful stuff — filesystem, OS APIs, native calls — runs in a separate, privileged backend process (Node in Electron, Rust in Tauri). They can&#39;t just call each other&#39;s functions; they communicate over an explicit bridge, passing messages across. Your React code doesn&#39;t read a file directly — it <em>asks</em> the backend to, and the backend, which actually has the permission, does it and sends the result back.</p>
<pre><code class="language-ts">// Renderer (your React UI): it cannot touch the disk itself.
// It asks the privileged backend to do it — Tauri example.
import { invoke } from &#39;@tauri-apps/api/tauri&#39;;

async function loadNote(id: string) {
  // Crosses the bridge to the Rust backend, which has real file access.
  const contents = await invoke&lt;string&gt;(&#39;read_note&#39;, { id });
  return contents;
}
</code></pre>
<p>This should feel familiar, because it&#39;s the same pattern the whole series keeps circling: a sandboxed, untrusted frontend asking a privileged, trusted backend to do the dangerous thing — exactly like the <a href="https://ma-x.im/blog/react-playbook-authentication">client/server split in auth</a>, just moved onto one machine. The renderer is the browser; the backend process is your server; the bridge is the API between them. Treat that boundary with the same seriousness — validate what crosses it, expose only the specific operations you mean to — and a desktop app stops feeling exotic and starts feeling like a client/server app you happen to ship in one installer.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Ask what you gain, not whether you can.</strong> If you don&#39;t need filesystem, OS integration, or true offline, the desktop is a heavier browser tab. Don&#39;t ship one for a fancy bookmark.</li>
<li><strong>The desktop is for touching the machine.</strong> Deep file access, tray/menus/global shortcuts, native resources, first-class offline — that&#39;s the whole reason to leave the browser.</li>
<li><strong>Electron vs Tauri is a cost choice, not a quality one.</strong> Electron: big and memory-hungry, but consistent and web-stack-familiar. Tauri: tiny, but cross-engine testing and a Rust backend.</li>
<li><strong>It&#39;s two processes with a bridge.</strong> Your React renderer asks a privileged backend to do the powerful things. Treat that bridge like a client/server boundary, because it is one.</li>
</ul>
<p>The thing I want you to take from this is that &quot;turn my React app into a desktop app&quot; is the easy, uninteresting half. The wrapper is a solved afternoon. The real work — and the only justification for the extra weight and the two-process complexity — is the native power you reach for once you&#39;re out of the sandbox. Go desktop when your app needs the machine. Stay in the tab when it doesn&#39;t. And when you do go, remember you didn&#39;t escape the client/server split, you just packaged both halves into one window.</p>
<p>The next article stays off the web but changes machines entirely: <a href="https://ma-x.im/blog/react-playbook-react-native">React Native and mobile</a> — where &quot;same code, new platform&quot; gets far more honest about what actually carries over and what doesn&#39;t.</p>
<p>If you&#39;re weighing a desktop build and can&#39;t tell whether you truly need to leave the browser, that&#39;s the exact question worth answering first — and it&#39;s usually clearer than it feels. Tell me what native capability you&#39;re reaching for and I&#39;ll tell you if the desktop is really the only place to get it.</p>
]]></content:encoded>
      <pubDate>Fri, 05 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Desktop</category>
      <category>Electron</category>
      <category>Tauri</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Documenting Components: The Only Docs That Don&apos;t Rot</title>
      <link>https://ma-x.im/blog/react-playbook-component-documentation</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-component-documentation</guid>
      <description>Every team writes component docs the same way — a wiki page, a screenshot, a table of props — and every team watches them go stale within a month. The problem isn&apos;t discipline. It&apos;s that the docs live in a different place than the code. The fix is docs that can&apos;t drift because they&apos;re generated from the component itself.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-component-documentation/cover.png" alt="Documenting Components: The Only Docs That Don&apos;t Rot" /></p>
<p>Every team I&#39;ve watched try to document their components starts in the same place: a page in the wiki. Someone writes up the <code>Button</code> — here are the variants, here&#39;s a screenshot, here&#39;s a table of props — and for about three weeks it&#39;s great. Then someone adds a <code>loading</code> prop and doesn&#39;t update the page. Then the primary color changes and the screenshot is now a lie. A month in, the docs describe a component that no longer exists, and the whole team has quietly learned to not trust them. So they read the source instead, and the docs rot in place, occasionally misleading a new hire.</p>
<p>This isn&#39;t a discipline problem, and treating it as one — &quot;we just need to be better about updating the docs&quot; — is why it keeps happening. People aren&#39;t lazy; the docs are structurally doomed. The reason is simple and it&#39;s the whole point of this article: <strong>the documentation lives in a different place than the code it describes.</strong> Two sources of truth, updated by two separate acts of will, and one of them always falls behind. You&#39;ve seen this exact shape before — it&#39;s the same two-sources-of-truth failure as the <a href="https://ma-x.im/blog/react-playbook-websockets">real-time cache</a>, just applied to prose instead of state.</p>
<p>So the question isn&#39;t &quot;how do we write better component docs.&quot; It&#39;s &quot;how do we make docs that <em>can&#39;t</em> drift&quot; — and the answer is to stop writing docs that sit next to the code and start generating them from the code itself.</p>
<h2>Why the Wiki Always Loses</h2>
<p>Sit with why the wiki page decays, because the mechanism matters. When you change a component, updating its separate documentation is a <em>second, optional action</em>. The code change ships value on its own; the doc update is extra work with no immediate payoff, easy to defer, easy to forget, and nothing breaks when you skip it. So under any deadline — which is always — the doc update is the first thing to go. Multiply that across a team and a year, and every hand-maintained doc trends toward wrong.</p>
<p>The insight that fixes it is to remove the second action entirely. If the documentation is <em>derived from the code</em> rather than maintained alongside it, then changing the code changes the docs, automatically, with no act of will required. The drift becomes impossible not because everyone got more disciplined, but because there&#39;s no longer a gap for drift to live in. This is the same instinct as generating types from a <a href="https://ma-x.im/blog/react-playbook-typescript-patterns">schema</a> or <a href="https://ma-x.im/blog/react-playbook-database-turso">migrations from a model</a>: one source of truth, everything else derived. Documentation is just another thing that should be derived, not duplicated.</p>
<h2>The Props Table Should Come From the Props</h2>
<p>The most concrete version of this: that table of props everyone hand-writes and forgets to update? It should not be hand-written. Your component&#39;s props are <em>already</em> fully described — in its TypeScript types. That&#39;s a machine-readable specification of every prop, its type, whether it&#39;s required, sitting right there in the code.</p>
<pre><code class="language-tsx">interface ButtonProps {
  /** Visual style of the button. */
  variant: &#39;primary&#39; | &#39;secondary&#39; | &#39;danger&#39;;
  /** Shows a spinner and disables interaction. */
  loading?: boolean;
  /** Called when the button is clicked. */
  onClick: () =&gt; void;
}

export function Button({ variant, loading, onClick }: ButtonProps) {
  /* ... */
}
</code></pre>
<p>Everything a props table would contain is in that interface — the names, the exact allowed values of <code>variant</code>, which props are optional, and even the descriptions in the doc comments. Tooling like <a href="https://storybook.js.org">Storybook</a> reads those types directly and generates the props table for you. Add a prop, the table gains a row. Change <code>variant</code>&#39;s options, the table updates. Delete a prop, its row disappears. You didn&#39;t maintain anything — the documentation is a <em>view</em> of the types, and the types can&#39;t be wrong because they&#39;re the code that actually runs. The single most rot-prone piece of component docs becomes the single most reliable one, purely by deriving it instead of writing it.</p>
<h2>Living Examples Beat Screenshots</h2>
<p>The other half of component docs is showing the thing in use, and here the screenshot is the villain. A screenshot is a photograph of the component at one moment in the past — it stops being accurate the instant anything changes, and it can&#39;t show a single interactive state. Hover, focus, loading, disabled, the error variant — a static image shows none of it and silently goes stale on all of it.</p>
<p>The fix is the same move again: don&#39;t take a picture of the component, <em>render the real component</em>. A story in Storybook is a live instance of the actual component, mounted with real props, that you can interact with:</p>
<pre><code class="language-tsx">import type { Meta, StoryObj } from &#39;@storybook/react&#39;;
import { Button } from &#39;./Button&#39;;

const meta: Meta&lt;typeof Button&gt; = { component: Button };
export default meta;

export const Primary: StoryObj&lt;typeof Button&gt; = {
  args: { variant: &#39;primary&#39;, onClick: () =&gt; {} },
};

export const Loading: StoryObj&lt;typeof Button&gt; = {
  args: { variant: &#39;primary&#39;, loading: true, onClick: () =&gt; {} },
};
</code></pre>
<p>Those aren&#39;t images of a button — they&#39;re the button, running, with the exact props your app would pass. If the button changes, the stories change with it, because they <em>are</em> the component, not a snapshot of it. You can hover the real hover state, watch the real spinner, tab to the real focus ring. A screenshot documents what the component looked like once; a story documents what the component <em>is</em> right now, and stays honest by construction.</p>
<p>And there&#39;s a compounding benefit that ties back to the <a href="https://ma-x.im/blog/react-playbook-design-prototyping">design conversation</a>: a living component catalog is where designers and engineers actually agree on what exists. When the real <code>Button</code>, in all its states, is browsable in one place, &quot;does this already exist?&quot; has an answer you can see and click — which is the difference between reusing the component and rebuilding it slightly differently for the fifth time.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Separate docs always rot.</strong> It&#39;s not a discipline problem — it&#39;s two sources of truth. Stop maintaining docs next to the code.</li>
<li><strong>Derive, don&#39;t duplicate.</strong> Generate documentation from the code so changing the code changes the docs, automatically.</li>
<li><strong>The props table comes from the types.</strong> Your TypeScript interface already is the spec; let tooling render it. Never hand-write a props table.</li>
<li><strong>Render real components, not screenshots.</strong> A live story stays accurate and shows interactive states; an image is stale the moment you save it.</li>
<li><strong>A living catalog drives reuse.</strong> When every component is browsable in all its states, &quot;does this exist?&quot; becomes answerable — and things get reused instead of rebuilt.</li>
</ul>
<p>The reason component docs have such a bad reputation is that almost everyone builds them to fail, then blames themselves when they do. A wiki page and a screenshot are drift waiting to happen, and no amount of resolve fixes a structural flaw. The shift that makes documentation actually last is to stop treating it as a thing you <em>write</em> and start treating it as a thing you <em>derive</em> — from the types, from the real components, from the code that&#39;s already the truth. Do that, and your docs stop being a chore you fall behind on and become a side effect of building the components at all. That&#39;s the only kind of documentation I&#39;ve seen survive contact with a deadline.</p>
<p>This is where the &quot;build a real app&quot; spine of the series ends — from starting a project all the way through documenting what you built. The remaining articles step outside the browser entirely: next up, <a href="https://ma-x.im/blog/react-playbook-desktop">React on the desktop</a> — when Electron or Tauri actually earns its place, and what changes when your React app is a window instead of a tab.</p>
<p>If your team has component docs that nobody trusts, it&#39;s almost certainly the separate-source-of-truth problem, and it&#39;s fixable without a documentation crusade. Tell me how your docs are set up and I&#39;ll tell you which piece to derive instead of write.</p>
]]></content:encoded>
      <pubDate>Tue, 02 Jun 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Documentation</category>
      <category>Storybook</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Design Prototyping for React: Where the Architecture Actually Starts</title>
      <link>https://ma-x.im/blog/react-playbook-design-prototyping</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-design-prototyping</guid>
      <description>Most engineers treat the Figma file as a picture to reproduce. That framing is why so many handoffs go badly. A good design isn&apos;t a picture — it&apos;s the first draft of your component architecture, and the decisions that decide whether your codebase stays sane are made before a single line of React is written.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-design-prototyping/cover.png" alt="Design Prototyping for React: Where the Architecture Actually Starts" /></p>
<p>The way most engineers relate to a design file is as a target to hit. The designer hands over a Figma frame, and the job — as everyone tacitly agrees — is to make the screen look like the picture. Pixel for pixel, color for color. Reproduce it, get it approved, move on.</p>
<p>I want to push on that framing, because I think it&#39;s the root of a lot of avoidable pain. A design isn&#39;t a picture you copy. It&#39;s the first draft of your component architecture, whether anyone treats it that way or not. The structure of that screen — what repeats, what nests, what varies, where the boundaries fall — <em>is</em> the structure of the code you&#39;re about to write. Which means the most consequential architectural decisions in a feature are often made in a design tool, by someone thinking about spacing and hierarchy, long before an engineer opens their editor. And when the design&#39;s structure and the code&#39;s structure disagree, you get the codebases everyone recognizes: technically matching the mockup, quietly miserable to maintain.</p>
<p>So this article is about reading a design the way it deserves to be read — as architecture — and why that shift changes both what you build and how you work with the people who design it.</p>
<h2>A Design Has Structure, Not Just Appearance</h2>
<p>Look at any real screen and there are two different things present at once. There&#39;s the <em>appearance</em> — the colors, the fonts, the shadows, the exact pixels. And there&#39;s the <em>structure</em> — this card repeats six times, that button is the same button as the one on the other screen, this panel contains a header and a list, these three stats are the same shape with different data.</p>
<p>Reproducing appearance is the shallow job. Reading structure is the real one, because structure is what maps to components. When you see six cards, the architectural fact isn&#39;t &quot;six cards&quot; — it&#39;s &quot;one <code>Card</code> component, rendered six times.&quot; When you see a button here that&#39;s clearly the same button as three other screens, the fact is &quot;this is one shared <code>Button</code>, not a thing I restyle locally each time.&quot; The design is quietly telling you what your components are, what your reusable primitives are, and how they compose. A developer who only sees the picture reproduces the pixels and invents an ad-hoc structure underneath. A developer who reads the structure lets the design hand them their <a href="https://ma-x.im/blog/react-playbook-code-structure">component boundaries</a> for free.</p>
<blockquote>
<p>A mockup shows you what the screen looks like. Read carefully, it also shows you what your components are, which ones repeat, and where they compose. The second reading is the one that keeps your codebase sane.</p>
</blockquote>
<h2>Same Component, Different States — the Thing Static Mockups Hide</h2>
<p>Here&#39;s where the &quot;design is a picture&quot; framing does the most damage, and it&#39;s the single most valuable thing to internalize about prototyping for React.</p>
<p>A static mockup shows you one moment. But a React component is never one moment — it&#39;s every state that data can put it in. The screen you were handed shows a list with eight items. Your component also has to handle: zero items (the empty state), one item, hundreds (does it scroll, paginate, virtualize?), the loading moment before any data arrives, and the error case when the fetch fails. The mockup showed you one of five or six realities, and if you only build the one you were shown, you&#39;ll ship a component that looks perfect in the demo and breaks the first time real data is empty or slow.</p>
<p>This is why the most useful thing you can do with a design <em>isn&#39;t</em> to start coding it — it&#39;s to interrogate it. Before writing the component, I go looking for the states the mockup didn&#39;t show. What does this look like with no data? While it&#39;s loading? When it fails? When the text is three times longer than the placeholder? When there are two hundred rows instead of eight? Half the time the honest answer is &quot;the designer didn&#39;t think about that yet,&quot; and surfacing it <em>before</em> you build is enormously cheaper than discovering it in QA. The static frame is the happy path; your component is responsible for all the paths, and prototyping is where you find the missing ones.</p>
<h2>This Is a Conversation, Not a Handoff</h2>
<p>That interrogation is exactly why I don&#39;t think of design-to-code as a handoff. A handoff is one-directional: design finishes, throws the result over a wall, engineering catches it. But the questions that matter — what are the empty and error states, is this really the same component as that one, does this pattern already exist in our system — can only be answered <em>together</em>, and answering them early is where the leverage is.</p>
<p>When engineering engages with design <em>during</em> prototyping instead of after, a few things change. Reusable pieces get spotted before they&#39;re built five slightly-different times. Missing states get designed instead of improvised by whichever developer hits them first. And the design starts to align with the components that already exist — which connects straight back to the <a href="https://ma-x.im/blog/building-design-systems-that-scale">design systems</a> idea: a prototype built from the components you already have is a prototype that ships in a fraction of the time, because the answer to &quot;how do I build this&quot; is &quot;you mostly already did.&quot; The handoff model forecloses all of that by making the conversation happen too late, after the structural decisions have already hardened on both sides.</p>
<p>I&#39;m not arguing engineers should design. I&#39;m arguing that the boundary between &quot;design work&quot; and &quot;architecture work&quot; is far blurrier than the org chart suggests, and pretending it&#39;s a clean line — a wall with a handoff over it — is how you get designs that fight the code and code that fights the design.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Read a design for structure, not just appearance.</strong> What repeats, what nests, what varies — that&#39;s your component tree, handed to you.</li>
<li><strong>A static mockup is one state.</strong> Before coding, hunt for the ones it hides: empty, loading, error, overflow, huge data. Build all the realities, not the one you were shown.</li>
<li><strong>Interrogate before you implement.</strong> The cheapest place to discover a missing state is a design review, not QA.</li>
<li><strong>Treat it as a conversation, not a handoff.</strong> Engaging during prototyping surfaces reuse and missing states while they&#39;re still cheap to fix.</li>
<li><strong>Prototype from your existing components.</strong> A design aligned to the system you already have ships far faster than one drawn from scratch.</li>
</ul>
<p>The reframe that changed how I work is small but load-bearing: the design tool is where architecture starts, not where it waits. The person arranging cards and spacing is making decisions about repetition, hierarchy, and composition — the exact decisions I&#39;ll be encoding in components an hour later. When I treat the mockup as a picture to copy, I throw all that structural information away and rebuild it, worse, in code. When I treat it as the first draft of the architecture, the design does half the hard thinking for me — and the half it left out becomes a conversation instead of a bug.</p>
<p>Next, the flip side of building components well: <a href="https://ma-x.im/blog/react-playbook-component-documentation">documenting them</a> — so the reusable pieces you and your designers just agreed on actually get reused instead of quietly rebuilt for the fifth time.</p>
<p>If your team&#39;s design-to-code process feels like reproducing pictures and constantly rediscovering missing states in QA, that&#39;s the handoff model doing its usual damage. Tell me where it hurts — the reuse, the missing states, the drift — and I&#39;ll tell you where the conversation needs to move earlier.</p>
]]></content:encoded>
      <pubDate>Fri, 29 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Design</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Sending Email from a React App: A Backend Problem in Disguise</title>
      <link>https://ma-x.im/blog/react-playbook-mails</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-mails</guid>
      <description>&quot;Just send them an email&quot; is one of those sentences that sounds like a five-minute task and turns into a week. The React part is trivial. The hard part is that email is a deceptively deep backend system with deliverability, templates, and reputation — and none of it belongs in your frontend. Here&apos;s where the real work actually is.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-mails/cover.png" alt="Sending Email from a React App: A Backend Problem in Disguise" /></p>
<p>&quot;When someone signs up, just send them a welcome email.&quot; It&#39;s the kind of request that gets tossed into a standup like it&#39;s nothing, and the person saying it genuinely believes it&#39;s a five-minute task. Send an email. How hard could it be?</p>
<p>Here&#39;s the thing that makes email sneaky: the part you&#39;d expect to be hard is trivial, and the part you&#39;d expect to be trivial is where the whole thing lives. Triggering an email from your app is a single function call. <em>Actually getting that email into someone&#39;s inbox</em> — not their spam folder, not bounced, not silently dropped — is a genuinely deep backend problem involving deliverability, sender reputation, authentication protocols, and a rendering environment stuck two decades in the past. And almost none of it is a React problem, which is exactly why it catches frontend developers off guard.</p>
<p>So this article is a map of where the real work is. Because the most useful thing I can tell you about email in a React app is which parts to not do yourself, and why the boundary sits where it does.</p>
<h2>The Line: The Client Never Sends Email</h2>
<p>Start with the hard boundary, because it&#39;s the one people are tempted to cross. <strong>Your React app does not send email. Ever.</strong> It triggers a request to your backend, and your backend sends the email.</p>
<p>This isn&#39;t a style preference, it&#39;s a security wall. Sending email requires credentials — API keys for your email service — and anything in your React bundle is public. Ship that key to the client and you&#39;ve handed the entire internet the ability to send email <em>as you</em>, which means spammers now have a free megaphone wearing your domain&#39;s reputation. Within days your domain is blacklisted and even your legitimate mail stops arriving. So the flow is always the same shape as <a href="https://ma-x.im/blog/react-playbook-payments">payments</a> and <a href="https://ma-x.im/blog/react-playbook-authentication">auth</a>: the client asks, the server acts, and the credential never leaves the server.</p>
<pre><code class="language-ts">// The client&#39;s entire involvement: ask the backend to do it.
async function requestWelcomeEmail(userId: string) {
  await fetch(&#39;/api/emails/welcome&#39;, {
    method: &#39;POST&#39;,
    body: JSON.stringify({ userId }),
  });
}
</code></pre>
<p>That&#39;s the whole frontend story. There is no <code>sendEmail</code> in your components, no email API key in your environment variables with a public prefix, no SMTP in the browser. The moment email touches the client, you&#39;ve done it wrong. Everything interesting from here happens on the server.</p>
<h2>Why You Don&#39;t Talk to SMTP Yourself</h2>
<p>On the backend, the naive mental model is that you connect to a mail server over SMTP and send. You <em>can</em> do that. You shouldn&#39;t, and understanding why is the core of this whole topic.</p>
<p>Getting an email delivered is not about transmitting it — it&#39;s about being <em>trusted</em> enough that the receiving server puts it in the inbox instead of the spam folder or the trash. That trust is a system: your domain needs authentication records (SPF, DKIM, DMARC) that prove you&#39;re allowed to send as your domain; your sending IP needs a <em>reputation</em> built over time; you need to handle bounces and complaints so you stop mailing dead addresses; and you need to not trip the spam filters that treat unknown senders with suspicion. Running your own mail server means owning all of that — reputation, blacklist monitoring, authentication, the works — and it is a full-time infrastructure job that has nothing to do with your product.</p>
<p>This is why transactional email services — <a href="https://resend.com">Resend</a>, Postmark, SendGrid — exist, and why using one isn&#39;t laziness but the correct architecture. They&#39;ve spent years building sender reputation, they handle the authentication protocols, they manage bounces and deliverability, and they give you a clean API. Your backend calls that API; the hard, invisible work of <em>actually getting delivered</em> is theirs.</p>
<pre><code class="language-ts">// Your server. The service owns deliverability; you own intent.
import { Resend } from &#39;resend&#39;;
const resend = new Resend(process.env.RESEND_API_KEY); // server-only secret

app.post(&#39;/api/emails/welcome&#39;, async (req, res) =&gt; {
  const user = await getUser(req.body.userId);
  await resend.emails.send({
    from: &#39;hello@yourdomain.com&#39;,
    to: user.email,
    subject: &#39;Welcome aboard&#39;,
    react: &lt;WelcomeEmail name={user.name} /&gt;, // more on this below
  });
  res.json({ queued: true });
});
</code></pre>
<p>Same lesson as every article in this series: the raw capability (SMTP) is a footgun, and the value is a service that owns the messy, reputation-dependent edges. You are buying deliverability, not a send button.</p>
<h2>The Genuinely Weird Part: Email Rendering Is Stuck in 2005</h2>
<p>Here&#39;s the one place your React skills partly transfer, and it comes with a nasty surprise. You&#39;d assume you can build an email template with normal components and modern CSS. You cannot, and this trips up every frontend developer exactly once.</p>
<p>Email clients — Outlook especially, but not only — render HTML with engines that are decades behind browsers. No flexbox, no grid, no modern CSS, inconsistent support for things you consider baseline. The reliable way to lay out an email is <em>tables</em>, the way the web worked in 2005, with inlined styles because <code>&lt;style&gt;</code> blocks get stripped. Hand-writing that is miserable and easy to get wrong across the dozens of clients your users actually use.</p>
<p>The modern rescue is that you <em>can</em> write email templates as React components — using a library like <a href="https://react.email">React Email</a> — and it compiles them down to the table-based, inline-styled HTML that survives Outlook. You get to think in components; the library deals with the 2005 rendering reality underneath.</p>
<pre><code class="language-tsx">import { Html, Button, Text } from &#39;@react-email/components&#39;;

export function WelcomeEmail({ name }: { name: string }) {
  return (
    &lt;Html&gt;
      &lt;Text&gt;Hi {name}, glad you&#39;re here.&lt;/Text&gt;
      &lt;Button href=&quot;https://yourdomain.com/start&quot;&gt;Get started&lt;/Button&gt;
    &lt;/Html&gt;
  );
}
</code></pre>
<p>That looks like a normal component, but <code>&lt;Button&gt;</code> and <code>&lt;Text&gt;</code> aren&#39;t rendering <code>&lt;button&gt;</code> and <code>&lt;p&gt;</code> — they emit the gnarly, table-wrapped, inline-styled markup that renders consistently from Gmail to a decade-old Outlook. It&#39;s the same pattern once more: a library absorbing an ugly reality so you can work in a sane abstraction. You write React; it ships something that would make you cry if you had to write it by hand.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>The client never sends email.</strong> It asks the backend. The API key is a server-only secret — leaking it burns your domain&#39;s reputation.</li>
<li><strong>Don&#39;t run your own mail server.</strong> Deliverability is a reputation-and-authentication system, not a transmission problem. Use a transactional email service.</li>
<li><strong>You&#39;re buying deliverability, not a send button.</strong> SPF/DKIM/DMARC, bounce handling, and IP reputation are the actual product these services sell.</li>
<li><strong>Email rendering is stuck in the past.</strong> Write templates as React components via React Email; let it compile down to the table-based HTML that survives Outlook.</li>
</ul>
<p>The reason &quot;just send an email&quot; turns into a week is that the sentence hides the entire iceberg. The tip — trigger a send — is the trivial thing everyone pictures. The mass underneath is deliverability, authentication, reputation, and a rendering environment that predates most of the developers working on it. The good news is that the whole iceberg is a solved, buyable problem: a transactional service for delivery, React Email for templates, and a hard rule that none of it touches the client. Get the boundary right and email goes back to being the five-minute task everyone assumed it was — because someone else already did the hard 95%.</p>
<p>Next, the series shifts from wiring things up to thinking about them differently: <a href="https://ma-x.im/blog/react-playbook-design-prototyping">design prototyping for React</a> — where design work stops being visual and starts being architectural, and why that handoff matters more than people admit.</p>
<p>If you&#39;ve got emails landing in spam, or a template that looks perfect everywhere except Outlook, those are the two classic email potholes — deliverability and rendering — and both have known fixes. Tell me which one&#39;s biting you and I&#39;ll point you at the layer to check.</p>
]]></content:encoded>
      <pubDate>Tue, 26 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Email</category>
      <category>Backend</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>File Upload in React: The Input Is Easy, the File Is Not</title>
      <link>https://ma-x.im/blog/react-playbook-file-upload</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-file-upload</guid>
      <description>An upload looks like a one-liner — a file input, a POST, done. Then someone uploads a two-gigabyte video on hotel wifi and the whole illusion collapses. The hard part was never the input. It&apos;s moving a large, unreliable blob of bytes from a browser to somewhere it can live. Here&apos;s how I actually handle it.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-file-upload/cover.png" alt="File Upload in React: The Input Is Easy, the File Is Not" /></p>
<p>Adding a file upload feels like a solved problem. You drop an <code>&lt;input type=&quot;file&quot;&gt;</code> on the page, grab the file in an <code>onChange</code>, POST it to an endpoint. Ten minutes of work, and on your machine — with a 40KB avatar and localhost — it works flawlessly. You move on.</p>
<p>Then it meets reality. Someone uploads a two-gigabyte video from hotel wifi. Someone&#39;s connection drops at 90%. Someone picks a 500MB file and your server, which happily buffered the 40KB avatar into memory, tries to do the same with half a gigabyte and falls over. The file input was never the hard part. The hard part is that a file is a <em>large, unreliable stream of bytes</em> that has to travel from an untrusted browser, across a flaky network, to somewhere it can actually live — and every one of those words is a problem.</p>
<p>So this article isn&#39;t about the input element. It&#39;s about the thing underneath: moving real files, at real sizes, over real networks, without melting your server or lying to your user about what&#39;s happening.</p>
<h2>The First Decision: Your Server Shouldn&#39;t Touch the File</h2>
<p>Here&#39;s the instinct that causes most upload pain: the file goes browser → your server → storage. Your React app POSTs to your API, your API receives the bytes and forwards them to S3 or wherever. It&#39;s the obvious shape, and it&#39;s the wrong one for anything but tiny files.</p>
<p>Why it&#39;s wrong: your server now has to <em>hold</em> the file — in memory or on disk — while it passes through. A handful of users uploading large files at once and your server is doing nothing but shuttling bytes, burning memory and connection time on work that isn&#39;t yours. You&#39;ve turned your application server into a file relay, and file relays are exactly what dedicated storage services already are, better than you.</p>
<p>The pattern that fixes it is <strong>presigned uploads</strong>, and it&#39;s worth understanding because it reshapes the whole flow. The file goes browser → storage <em>directly</em>, and your server&#39;s only job is to hand out permission. The client asks your API &quot;I want to upload this file,&quot; your API asks the storage service for a short-lived, single-purpose URL, and hands that URL back. The browser then uploads straight to storage. Your bytes never touch your server.</p>
<pre><code class="language-ts">// Your server: it never sees the file. It only grants permission.
app.post(&#39;/api/uploads/sign&#39;, async (req, res) =&gt; {
  const { filename, contentType } = req.body;

  // Ask storage for a short-lived URL scoped to this one upload.
  const uploadUrl = await storage.createPresignedUrl({
    key: `uploads/${crypto.randomUUID()}/${filename}`,
    contentType,
    expiresIn: 60, // seconds — the URL is useless after that
  });

  res.json({ uploadUrl });
});
</code></pre>
<pre><code class="language-ts">// The browser: uploads the bytes directly to storage, not to you.
async function uploadFile(file: File) {
  const { uploadUrl } = await fetch(&#39;/api/uploads/sign&#39;, {
    method: &#39;POST&#39;,
    body: JSON.stringify({ filename: file.name, contentType: file.type }),
  }).then((r) =&gt; r.json());

  await fetch(uploadUrl, { method: &#39;PUT&#39;, body: file }); // straight to storage
}
</code></pre>
<p>Look at how the responsibilities split. Your server does a tiny, fast, cheap thing — validate the request, mint a scoped URL, respond. The multi-gigabyte transfer happens between the browser and a service built for exactly that. This is the same <a href="https://ma-x.im/blog/react-playbook-authentication">server-is-the-authority, client-does-the-work</a> split from the auth and payments articles: your backend controls <em>permission</em>, but it doesn&#39;t do the heavy lifting it has no business doing.</p>
<h2>The Second Reality: The Upload Is a Process, Not a Request</h2>
<p>Once the file is going straight to storage, the next thing to accept is that a large upload is not an event that happens — it&#39;s a process that <em>unfolds over time</em>, and your UI has to treat it that way.</p>
<p>A 40KB avatar uploads instantly, so you can pretend it&#39;s a normal request. A 500MB file takes a minute or more, and during that minute the user is staring at your interface wondering if it&#39;s working. If you show them a frozen spinner, they&#39;ll assume it hung and refresh — killing the upload. So progress isn&#39;t a nice-to-have here; it&#39;s the difference between a feature that works and one users abort halfway. You have to report how far along the bytes are:</p>
<pre><code class="language-ts">function uploadWithProgress(url: string, file: File, onProgress: (pct: number) =&gt; void) {
  const xhr = new XMLHttpRequest();
  xhr.upload.addEventListener(&#39;progress&#39;, (e) =&gt; {
    if (e.lengthComputable) onProgress(Math.round((e.loaded / e.total) * 100));
  });
  xhr.open(&#39;PUT&#39;, url);
  xhr.send(file);
}
</code></pre>
<p>That <code>xhr.upload.progress</code> event — one of the few times raw <code>XMLHttpRequest</code> still earns its place over <code>fetch</code>, which can&#39;t report upload progress — is what lets you draw a real progress bar. And a real progress bar changes user behavior: people wait for a bar that moves, and they abandon a spinner that doesn&#39;t. The technical work of measuring progress is small; the UX consequence of showing it is large.</p>
<h2>The Third Reality: Big Files Need to Survive a Dropped Connection</h2>
<p>Now the hardest layer, and the one that separates a toy from a product: what happens at 90% when the wifi blinks?</p>
<p>With a single PUT of the whole file, the answer is brutal — the upload fails and starts over from zero. For a 40KB file, who cares. For a 2GB file on a shaky connection, &quot;start over from zero&quot; may mean it <em>never</em> completes, because the odds of an uninterrupted multi-gigabyte transfer on bad wifi are genuinely low. The single-request model has a size ceiling baked in, and above it, uploads just don&#39;t finish.</p>
<p>The answer is <strong>chunked (multipart) uploads</strong>: split the file into pieces, upload each piece independently, and reassemble them in storage. Now a dropped connection costs you one chunk, not the whole file — you retry that chunk and continue. Uploads become resumable, and the size ceiling effectively disappears.</p>
<p>I want to be honest about the tradeoff, because this is where the &quot;it&#39;s just an input&quot; fantasy fully dies: chunking is genuinely more complex. You&#39;re tracking which chunks succeeded, retrying the ones that didn&#39;t, handling out-of-order completion, and telling storage to stitch them back together. This is real coordination logic, and it&#39;s exactly why you don&#39;t hand-roll it. Storage services expose multipart upload APIs, and libraries like <a href="https://uppy.io">Uppy</a> wrap the whole thing — chunking, retries, progress, resumability — behind a component. The same lesson as every article in this series: the raw capability is a footgun, and the value is a library that owns the messy edges so you spend your attention on your product, not on retry bookkeeping.</p>
<h2>Don&#39;t Forget: The File Is Untrusted</h2>
<p>One security note that&#39;s too important to skip, because uploads are a classic attack surface. A file arriving from a browser is <em>user input</em>, and the same rule from the <a href="https://ma-x.im/blog/react-playbook-payments">payments</a> and <a href="https://ma-x.im/blog/react-playbook-authentication">auth</a> articles applies: never trust it.</p>
<p>The client can claim a file is a 2MB image and actually send a 2GB executable. It can lie about the content type. So validation belongs on the boundary you control — size limits, allowed types, and ideally scanning — enforced when you mint the presigned URL and verified again on the storage side, never solely in the browser where a hostile user can bypass it. Client-side checks are for UX (tell the user &quot;that&#39;s too big&quot; before they wait); server-side checks are for safety. You need both, and you must never mistake the first for the second.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Your server hands out permission, not bytes.</strong> Use presigned URLs so the file goes browser → storage directly. Don&#39;t turn your API into a file relay.</li>
<li><strong>Treat the upload as a process.</strong> Show real progress from <code>xhr.upload</code>. A moving bar is why users wait instead of refreshing.</li>
<li><strong>Chunk large files.</strong> Multipart uploads make transfers resumable and remove the size ceiling. Don&#39;t hand-roll it — use the storage API via a library.</li>
<li><strong>The file is untrusted input.</strong> Validate size and type on the server boundary, not just in the browser. Client checks are UX; server checks are security.</li>
</ul>
<p>The reason file upload is a rite of passage is that the easy version and the real version look identical right up until the file gets big or the network gets bad. The input, the POST, the success toast — all of that is the 10% you can write in your sleep. The 90% is respecting that you&#39;re moving a large, untrusted, interruptible thing across a connection you don&#39;t control, to a service that should be doing the heavy carrying instead of your server. Get that shape right — permission from your backend, bytes to real storage, progress and resumability for the user — and uploads stop being the feature that falls over in production.</p>
<p>Next, another piece of infrastructure that looks trivial and isn&#39;t: <a href="https://ma-x.im/blog/react-playbook-mails">sending email from a React app</a> — why &quot;just send an email&quot; is a backend problem wearing a frontend costume, and where the real work hides.</p>
<p>If you&#39;ve got an upload that works for small files and dies on big ones, that&#39;s the single-request ceiling, and it&#39;s the most common upload bug there is. Tell me where it breaks — the server memory, the timeout, the 90% drop — and I&#39;ll tell you which layer is missing.</p>
]]></content:encoded>
      <pubDate>Fri, 22 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>File Upload</category>
      <category>Storage</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Time in React: The Bugs That Only Show Up at 2 AM</title>
      <link>https://ma-x.im/blog/react-playbook-time</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-time</guid>
      <description>Dates look like the most boring data type in your app. They are also the source of the subtlest, most embarrassing bugs — the meeting that shows an hour off, the &apos;today&apos; that&apos;s tomorrow in Tokyo, the date that shifts a day every time you save it. Here&apos;s how I stop time from quietly breaking a React app.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-time/cover.png" alt="Time in React: The Bugs That Only Show Up at 2 AM" /></p>
<p>A date is just a value. It sits there in your data looking as harmless as a string or a number, and for a while it behaves. Then one day a user in another timezone reports that their meeting shows an hour off, or a &quot;created today&quot; badge appears on something made yesterday, or a date field creeps backward by one day every single time someone saves the form. Suddenly the most boring data type in your app is the source of the weirdest bug in your tracker.</p>
<p>Time is deceptive because it looks simple and is not. The complexity is real and mostly invisible on the machine you develop on, because your machine has one timezone, and so the entire category of problems — timezones, daylight saving, what &quot;midnight&quot; even means — stays hidden until someone in a different part of the world hits it. And they always hit it, usually in production, usually at an hour that makes the bug&#39;s name feel personal.</p>
<p>So this article is about the small number of ideas that keep time from breaking a React app. Not a date-library tour — a way of thinking about time that makes the whole class of bugs stop happening.</p>
<h2>The One Rule That Prevents Most of It</h2>
<p>If you take nothing else from this: <strong>store and transmit time in UTC. Convert to the user&#39;s local timezone only for display.</strong> That&#39;s it. That single discipline, applied without exception, eliminates the majority of time bugs before they can form.</p>
<blockquote>
<p>A timestamp is a fact — the exact instant something happened. A timezone is a lens for <em>viewing</em> that fact. Store the fact once, in UTC. Choose the lens at the last possible moment, at render.</p>
</blockquote>
<p>Here&#39;s why this works. An instant in time is absolute — the moment a user clicked &quot;submit&quot; is the same moment everywhere on Earth. What differs is how you <em>describe</em> it: 3 PM in London, 10 AM in New York, same instant. The bug factory is storing or comparing times in local terms, because then &quot;the same moment&quot; has many different labels and they drift against each other. Store the absolute fact in UTC, keep it in UTC through your database and your API, and only bend it into a human&#39;s local timezone when you&#39;re about to show it to that specific human. Timezone becomes a <em>display</em> concern, which is the only place it belongs.</p>
<p>The mirror-image mistake — the one that produces the &quot;date shifts by a day every save&quot; bug — is round-tripping a date through local time on the way in <em>and</em> out, so each save nudges it by the offset. Keep the storage UTC and the conversion display-only, and that whole genre of bug can&#39;t happen.</p>
<h2>Why <code>new Date()</code> Keeps Betraying You</h2>
<p>The native <code>Date</code> object is where good intentions go to die, and it&#39;s worth being specific about why, because the failure is silent.</p>
<p><code>Date</code> is implicitly local. The moment you parse or construct one, it quietly adopts the runtime&#39;s timezone — which is your laptop in development and the user&#39;s machine (or worse, a server in yet another zone) in production. So code that looks correct on your screen produces different results for different users, and you won&#39;t see it, because your machine only ever shows you your own timezone&#39;s answer.</p>
<pre><code class="language-ts">// Looks innocent. Behaves differently depending on where it runs.
const d = new Date(&#39;2026-07-21&#39;); // midnight UTC... interpreted as LOCAL
d.getDate(); // 21 in London. 20 in New York. Same code, different day.
</code></pre>
<p>That <code>getDate()</code> returning 20 for a New York user is the entire &quot;off by one day&quot; bug in miniature — the string is midnight UTC, but <code>Date</code> shows it through local eyes, and New York&#39;s local eyes are still on the 20th. Nothing threw an error. The value is just wrong, for some people, some of the time, which is the worst kind of wrong.</p>
<h2>Reach for a Real Library — and Watch Temporal</h2>
<p>Because <code>Date</code> is a minefield, the long-standing answer is a library that makes timezone handling explicit instead of implicit. <a href="https://date-fns.org">date-fns</a> is my usual pick — modular, tree-shakeable, and its companion <code>date-fns-tz</code> makes conversions something you <em>state</em> rather than something that happens to you:</p>
<pre><code class="language-ts">import { formatInTimeZone } from &#39;date-fns-tz&#39;;

// The instant is stored in UTC. Display it in the user&#39;s zone, explicitly.
const utcInstant = &#39;2026-07-21T23:30:00Z&#39;;
formatInTimeZone(utcInstant, &#39;America/New_York&#39;, &#39;yyyy-MM-dd HH:mm&#39;); // &quot;2026-07-21 19:30&quot;
formatInTimeZone(utcInstant, &#39;Asia/Tokyo&#39;, &#39;yyyy-MM-dd HH:mm&#39;);      // &quot;2026-07-22 08:30&quot;
</code></pre>
<p>Notice the same UTC instant renders as the 21st in New York and the 22nd in Tokyo — correctly, because the conversion is <em>explicit</em> and timezone-aware, not left to whatever <code>Date</code> guesses. You&#39;re stating the lens instead of inheriting it.</p>
<p>The genuinely good news is that the language itself is fixing this. <strong>Temporal</strong> — the new built-in date/time API — is designed specifically to end the <code>Date</code> era: immutable objects, explicit timezone handling, no more implicit-local footguns. It&#39;s landing in engines now, and it&#39;s the direction everything moves. If you&#39;re starting fresh, it&#39;s worth watching closely and reaching for as it stabilizes; it makes the &quot;be explicit about timezones&quot; discipline the <em>default</em> instead of something you bolt on with a library.</p>
<h2>The Traps That Remain</h2>
<p>Even with the UTC rule and a real library, a few sharp edges stay sharp, and they&#39;re worth naming because each has bitten real apps.</p>
<p>Daylight saving time means some days don&#39;t have 24 hours and some local times don&#39;t exist at all — the clock jumps forward and 2:30 AM simply never happened on that date. Any code doing time math by adding <code>24 * 60 * 60 * 1000</code> milliseconds and calling it &quot;a day&quot; is wrong twice a year; a real library that adds <em>calendar</em> days handles it, hand-rolled arithmetic does not.</p>
<p>&quot;Midnight&quot; and &quot;today&quot; are not absolute either — they&#39;re timezone-bound. &quot;Show me everything from today&quot; means a different range of instants for a user in Los Angeles than one in Berlin, and if your backend computes &quot;today&quot; in the server&#39;s timezone, half your users see the wrong day&#39;s data. &quot;Today&quot; is always <em>someone&#39;s</em> today, and you have to decide whose.</p>
<p>And serialization is where discipline leaks: the safe way to put a date on the wire is ISO 8601 in UTC (that trailing <code>Z</code>), because it&#39;s unambiguous. A date formatted for humans — or worse, a bare <code>new Date().toString()</code> — is a guessing game for whatever parses it next. Keep the API boundary UTC and ISO, and convert to human-readable strictly at the edges, at display.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Store and send UTC. Convert only for display.</strong> The single rule that prevents most time bugs.</li>
<li><strong>Distrust <code>new Date()</code>.</strong> It&#39;s implicitly local, so it&#39;s implicitly wrong for someone. Don&#39;t do timezone logic with it.</li>
<li><strong>Use a real library</strong> (date-fns today) and make conversions explicit. <strong>Watch Temporal</strong> and adopt it as it lands.</li>
<li><strong>Respect DST and &quot;today.&quot;</strong> A day isn&#39;t always 24 hours, and &quot;today&quot; is always somebody&#39;s local today.</li>
<li><strong>Serialize as ISO 8601 UTC.</strong> Unambiguous on the wire; humanize only at the edge.</li>
</ul>
<p>The reason time bugs are so embarrassing is that they&#39;re invisible right up until they&#39;re not. Everything works on your machine, in your timezone, during the months without a DST transition — and then a user in Tokyo, or a clock change in November, or a &quot;today&quot; filter computed in the wrong zone quietly serves the wrong answer, and it looks like the code was never tested. It was tested. It was just tested in one timezone. Treating time as an absolute UTC fact that you view through a local lens only at the very end is the habit that makes those 2 AM bugs stop being yours.</p>
<p>Next, a different kind of unglamorous-but-treacherous: <a href="https://ma-x.im/blog/react-playbook-file-upload">file uploads in React</a> — progress, chunking, and the surprisingly deep problem of moving a large file from a browser to somewhere it can live.</p>
<p>If you&#39;ve got a date that shifts by a day, a badge that says &quot;today&quot; when it isn&#39;t, or a meeting an hour off for some users, that&#39;s almost always a UTC-vs-local leak. Tell me where the wrong value shows up and I&#39;ll tell you which conversion is doing it.</p>
]]></content:encoded>
      <pubDate>Tue, 19 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Dates</category>
      <category>Time</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Payments in React: The Client Never Decides You Got Paid</title>
      <link>https://ma-x.im/blog/react-playbook-payments</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-payments</guid>
      <description>Taking a payment looks like a form submission and is nothing like one. The card number is radioactive, the confirmation is asynchronous, and the one thing your React app must never do is believe it got paid because a callback said so. Here&apos;s where the real line sits between the browser, your server, and Stripe.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-payments/cover.png" alt="Payments in React: The Client Never Decides You Got Paid" /></p>
<p>On the surface, taking a payment looks like the most ordinary thing in the world: a form with some fields, a submit button, a success message. You&#39;ve built a hundred forms. This is just a form where one of the fields happens to be a card number.</p>
<p>It is not just a form, and the card number is exactly why. That field is radioactive. The moment a real card number touches your servers, you&#39;ve walked into PCI compliance — a wall of security obligations that exists for very good reasons and that you do not want to be responsible for. And the &quot;success message&quot; hides the second trap: unlike every other form you&#39;ve built, you don&#39;t actually know the outcome when the user clicks submit. The payment confirms <em>asynchronously</em>, somewhere you can&#39;t see, and the single most expensive mistake in this entire domain is letting your React app believe it got paid because a client-side callback said so.</p>
<p>So this article isn&#39;t a Stripe tutorial. It&#39;s about the two lines that matter — the one that keeps the card number away from you, and the one that decides where &quot;you got paid&quot; is actually true — because getting those two lines right is the whole job, and getting either wrong is how you end up either liable or robbed.</p>
<h2>The First Line: Never Touch the Card Number</h2>
<p>Start with the rule that shapes everything else: <strong>the raw card number must never reach your server.</strong> Not to &quot;just pass it through,&quot; not to log it, not for a millisecond. The instant it does, you&#39;re in PCI-compliance scope, and that is a burden measured in audits and dread, not code.</p>
<p>The whole architecture of a modern payments provider exists to keep you out of that scope, and the mechanism is worth understanding because it&#39;s genuinely clever. You don&#39;t build the card input. Stripe does — via <a href="https://stripe.com/docs/payments/elements">Stripe Elements</a>, an iframe-based field that Stripe hosts and controls. The card number is typed into <em>their</em> iframe, on <em>your</em> page, and it goes straight to Stripe. Your JavaScript can&#39;t read it. Your server never sees it. What you get back instead is a token — a harmless reference that means &quot;Stripe is holding a card for you&quot; — and <em>that</em> is what flows through your system.</p>
<pre><code class="language-tsx">import { PaymentElement, useStripe, useElements } from &#39;@stripe/react-stripe-js&#39;;

export function CheckoutForm() {
  const stripe = useStripe();
  const elements = useElements();

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault();
    if (!stripe || !elements) return;

    // Stripe collects the card in its own iframe and confirms directly.
    // The card number never touches your JS or your server.
    const { error } = await stripe.confirmPayment({
      elements,
      confirmParams: { return_url: &#39;https://example.com/order/complete&#39; },
    });

    if (error) {
      // Show the user the reason. This is the &quot;did it start&quot; answer,
      // NOT the &quot;did it succeed&quot; answer.
    }
  }

  return (
    &lt;form onSubmit={handleSubmit}&gt;
      &lt;PaymentElement /&gt;
      &lt;button type=&quot;submit&quot;&gt;Pay&lt;/button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>Look at what your code never handles: the card number itself. <code>&lt;PaymentElement /&gt;</code> is Stripe&#39;s iframe living inside your form. You render it, you style around it, and you stay completely out of scope for the one piece of data that would otherwise make you liable. This is the <a href="https://ma-x.im/blog/react-playbook-authentication">buy-don&#39;t-build</a> principle from the auth article at its most extreme — the risk here is so high that building it yourself isn&#39;t a trade-off, it&#39;s a mistake.</p>
<h2>The Second Line: The Client Doesn&#39;t Decide</h2>
<p>Now the subtler and more dangerous part. The payment is initiated in the browser, but the browser is the last place that should decide whether it <em>succeeded</em>.</p>
<p>Here&#39;s why this bites people. The naive flow: user pays, Stripe&#39;s client call resolves, you show &quot;Payment successful!&quot; and mark the order as paid in your UI. It demos perfectly. And it&#39;s exploitable and unreliable, because the client is an environment you don&#39;t control. The user closes the tab the instant before the callback fires — the card was charged, your app never knew. The network drops after the charge but before the response — same. And a hostile user can straight up call your &quot;mark as paid&quot; endpoint without paying at all, because anything the client asserts, a client can fake.</p>
<p>The answer is the one that runs through this whole series: <strong>the client is untrusted; the server is the source of truth.</strong> The real confirmation doesn&#39;t come from your React callback. It comes from Stripe, server-to-server, via a <strong>webhook</strong> — Stripe calls <em>your backend</em> to say &quot;this payment actually succeeded,&quot; and that server-side event is the only thing allowed to mark an order as paid.</p>
<pre><code class="language-ts">// On your server — the ONLY place an order becomes &quot;paid&quot;.
app.post(&#39;/webhooks/stripe&#39;, async (req, res) =&gt; {
  const event = stripe.webhooks.constructEvent(
    req.body,
    req.headers[&#39;stripe-signature&#39;],
    WEBHOOK_SECRET, // proves the call really came from Stripe
  );

  if (event.type === &#39;payment_intent.succeeded&#39;) {
    const intent = event.data.object;
    await markOrderPaid(intent.metadata.orderId); // trusted truth
  }

  res.json({ received: true });
});
</code></pre>
<p>So the client&#39;s job and the server&#39;s job split cleanly. The client <em>starts</em> the payment and shows a hopeful &quot;we&#39;re processing your order.&quot; The server, via the webhook, <em>confirms</em> it and flips the order to paid. Those are two different facts arriving through two different channels, and conflating them — treating the client&#39;s optimistic result as the settled truth — is the bug that gets apps double-charging, under-charging, or handing out product for free.</p>
<blockquote>
<p>The client can tell you a payment <em>started</em>. Only the server, via the provider&#39;s webhook, can tell you it <em>succeeded</em>. Never let the browser be the thing that says &quot;paid.&quot;</p>
</blockquote>
<p>This maps exactly onto the <a href="https://ma-x.im/blog/react-playbook-authentication">server-state idea from the auth article</a>: the important truth lives on the server, and the client holds a hopeful, possibly-stale view of it. Payments are the highest-stakes version of that same pattern, which is why the discipline matters more here than anywhere.</p>
<h2>So How Hard Is It, Really?</h2>
<p>Honestly? Less than the fear suggests, <em>if</em> you respect the two lines. Stripe has done the genuinely hard parts — the card handling, the PCI scope, the fraud tooling. What&#39;s left for you is smaller than people expect, but it&#39;s unforgiving about correctness.</p>
<p>Your real work is three things. Render Stripe&#39;s Element so the card never touches you — mechanical, and Stripe hands you the components. Kick off the payment from the client and, critically, treat the client-side result as &quot;started,&quot; not &quot;done.&quot; And build the webhook handler on your backend as the sole authority that marks an order paid, verifying the signature so nobody can forge that call. That&#39;s the shape of it. It&#39;s not a huge amount of code. It&#39;s a small amount of code where being casually wrong costs actual money, which is a very different kind of hard than &quot;lots of code.&quot;</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>The card number is radioactive.</strong> Use Stripe Elements so it never touches your JS or server. Don&#39;t negotiate with PCI scope — avoid it.</li>
<li><strong>The client starts the payment; it does not confirm it.</strong> Show &quot;processing,&quot; never &quot;paid,&quot; from a client callback.</li>
<li><strong>The webhook is the source of truth.</strong> An order becomes paid on your server, when Stripe tells your server it succeeded — and never before.</li>
<li><strong>Verify the webhook signature.</strong> Otherwise anyone can call your endpoint and claim they paid.</li>
<li><strong>Buy, don&#39;t build.</strong> This is the domain where rolling your own is not clever, it&#39;s negligent.</li>
</ul>
<p>The reason payments feel scarier than they are is that they punish the exact habit web development otherwise rewards: trusting the client. Everywhere else, an optimistic UI that assumes success is good UX. Here, an optimistic UI that <em>records</em> success is a liability. Once you internalize that the browser can only ever say &quot;I tried&quot; and the server is the only thing that can say &quot;it worked,&quot; the whole domain stops being frightening and becomes just careful — two clean lines, respected every time.</p>
<p>The next article stays with the values that are deceptively easy to get wrong and quietly ruin things: <a href="https://ma-x.im/blog/react-playbook-time">time and dates in React</a> — timezones, storage, and the bugs that only surface at the worst possible hour.</p>
<p>If you&#39;ve got a checkout that marks orders paid from a client callback, that&#39;s the second line crossed, and it&#39;s worth fixing before it costs you. Tell me how your success path works and I&#39;ll tell you where the hole is.</p>
]]></content:encoded>
      <pubDate>Fri, 15 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Payments</category>
      <category>Stripe</category>
      <category>Security</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Internationalization Isn&apos;t Translation: What Actually Breaks</title>
      <link>https://ma-x.im/blog/react-playbook-i18n</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-i18n</guid>
      <description>Everyone thinks i18n means swapping English strings for a dictionary lookup. That&apos;s the 10% that&apos;s easy. The 90% is formatting, pluralization, text that changes direction, and language data you can&apos;t afford to ship all at once. Here&apos;s what internationalizing a React app actually involves once you get past the word &apos;translation&apos;.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-i18n/cover.png" alt="Internationalization Isn&apos;t Translation: What Actually Breaks" /></p>
<p>Ask most developers what internationalization means and you&#39;ll get the same answer: instead of writing &quot;Save&quot; in your JSX, you look up a key in a dictionary and get &quot;Save&quot; or &quot;Enregistrer&quot; depending on the user&#39;s language. And they&#39;re right — that <em>is</em> part of it. It&#39;s also the easy 10%, and it&#39;s the part that makes people underestimate the whole thing.</p>
<p>Because a translated string is not a translated interface. The moment you actually ship to another language, the interesting failures show up, and none of them are about the words. A date renders as <code>7/18/2026</code> to an American and <code>18/07/2026</code> to nearly everyone else. A price needs a different currency symbol, in a different position, with a comma where you had a period. &quot;1 item&quot; versus &quot;5 items&quot; is trivial in English and a genuine grammar problem in languages with six plural forms. And in Arabic or Hebrew the entire layout flips direction. None of that is a dictionary lookup.</p>
<p>So this article is about the 90% — what internationalizing a React app actually involves once you stop thinking of it as translation and start thinking of it as <em>formatting and loading</em>, which is what it really is.</p>
<h2>The Reframe: It&#39;s Formatting, Not Words</h2>
<p>Here&#39;s the shift that made i18n click for me. Stop picturing a dictionary. Picture a formatting engine.</p>
<p>The hardest parts of internationalization aren&#39;t the strings you wrote — they&#39;re the <em>values</em> you render. A date is a value that has to be formatted for a locale. A number is a value. A currency amount is a value. A count that decides between &quot;item&quot; and &quot;items&quot; is a value running through a locale&#39;s plural rules. The English strings are static and easy; the dynamic values are where every locale disagrees, and where a naive app quietly produces wrong output that looks fine to you and broken to them.</p>
<blockquote>
<p>Translation handles the words you wrote. Internationalization handles the values you render. The second one is the hard part, and it&#39;s the part people skip.</p>
</blockquote>
<p>The good news is you don&#39;t implement any of this yourself. The browser ships a full formatting engine — the <code>Intl</code> API — and the React libraries are wrappers that make it ergonomic.</p>
<h2>Formatting Values the Right Way</h2>
<p>The instinct, when you need a date or a price in the UI, is to build the string by hand — grab the month, slap it together, append a currency symbol. That instinct is exactly the bug, because the <em>format itself</em> is locale-dependent, not just the numbers inside it.</p>
<p>The <code>Intl</code> API already knows every locale&#39;s rules. You give it a value and a locale; it gives you correct output:</p>
<pre><code class="language-ts">// Dates: same instant, formatted for the user&#39;s locale.
new Intl.DateTimeFormat(&#39;en-US&#39;).format(date); // &quot;7/18/2026&quot;
new Intl.DateTimeFormat(&#39;de-DE&#39;).format(date); // &quot;18.7.2026&quot;

// Currency: symbol, position, and separators all differ by locale.
new Intl.NumberFormat(&#39;en-US&#39;, { style: &#39;currency&#39;, currency: &#39;USD&#39; })
  .format(1234.5); // &quot;$1,234.50&quot;
new Intl.NumberFormat(&#39;de-DE&#39;, { style: &#39;currency&#39;, currency: &#39;EUR&#39; })
  .format(1234.5); // &quot;1.234,50 €&quot;
</code></pre>
<p>Look at what you&#39;re <em>not</em> deciding: where the currency symbol goes, whether the thousands separator is a comma or a period, the order of day and month. You hand over the value and the locale and get correct output. Building any of that by hand isn&#39;t just more code — it&#39;s code that&#39;s confidently wrong in locales you don&#39;t personally read.</p>
<p>Then there&#39;s pluralization, which English makes look trivial and other languages do not. &quot;1 item&quot; / &quot;2 items&quot; is a two-case rule; Polish and Arabic have far more, and a hardcoded <code>count === 1 ? &#39;item&#39; : &#39;items&#39;</code> is simply broken there. This is why the string libraries use the ICU message format, which encodes plural rules per locale:</p>
<pre><code>{count, plural, one {# item} other {# items}}
</code></pre>
<p>You write the rule once; the library picks the right form for whatever locale is active, using the same <code>Intl</code> plural data. You are never in the business of knowing Polish grammar — you&#39;re in the business of not hardcoding English grammar.</p>
<h2>The React Side: Loading, Not Bundling</h2>
<p>Now the part that&#39;s specifically a React and architecture problem, and the one that separates a demo from a real app: <strong>you cannot ship every language in your bundle.</strong></p>
<p>Here&#39;s the trap. You wire up <a href="https://react.i18next.com">react-i18next</a> or <a href="https://formatjs.io">FormatJS</a>, it works, and to make it work you import all your translation files at the top. Now every user downloads English, French, German, Japanese, and Arabic just to see the one language they actually use. Your bundle balloons with text 90% of users will never read, and it gets worse with every locale you add. A feature meant to serve more users made the app slower for all of them.</p>
<p>The right shape is to treat translation data as something you <em>load on demand</em>, the same way you&#39;d <a href="https://ma-x.im/blog/react-playbook-code-structure">code-split</a> a route. The active locale&#39;s messages load when needed; the rest stay on the server until someone actually switches:</p>
<pre><code class="language-ts">// Load only the active locale&#39;s messages, not all of them.
async function loadMessages(locale: string) {
  const messages = await import(`../locales/${locale}.json`);
  return messages.default;
}
</code></pre>
<p>That dynamic <code>import</code> is the whole difference. A user on French downloads French. Switch to German and German loads then — a small, one-time fetch — instead of everyone paying for every language up front. The i18n libraries support this directly; the mistake is not the library, it&#39;s statically importing everything because the demo did.</p>
<p>This is the same instinct the whole series keeps returning to: the naive version loads everything eagerly, and the production version loads what&#39;s needed when it&#39;s needed. Translations are just data, and data that most users won&#39;t touch belongs behind a lazy load.</p>
<h2>One More Thing: Direction</h2>
<p>A short but real one, because it&#39;s the part that turns a &quot;translated&quot; app back into an untranslated-looking one. Some languages — Arabic, Hebrew, Persian — read right-to-left, and that&#39;s not a font setting, it&#39;s a layout inversion. Your sidebar moves to the other side, your icons mirror, your text aligns the other way.</p>
<p>The workable answer is to stop thinking in &quot;left&quot; and &quot;right&quot; and start thinking in &quot;start&quot; and &quot;end.&quot; Modern CSS logical properties — <code>margin-inline-start</code> instead of <code>margin-left</code>, <code>padding-inline-end</code> instead of <code>padding-right</code> — flip automatically with the document direction. Set <code>dir=&quot;rtl&quot;</code> on the root for RTL locales and a layout built on logical properties reorients itself. Build it on hardcoded <code>left</code>/<code>right</code> and you get to audit every component by hand. It&#39;s cheap if you do it from the start and expensive if you retrofit — which is a good reason to reach for logical properties by default even in a single-language app.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>It&#39;s formatting, not translation.</strong> The strings are easy; the values — dates, numbers, currency, plurals — are where locales actually disagree.</li>
<li><strong>Never format values by hand.</strong> Use <code>Intl</code> (via your i18n library). It already knows every locale&#39;s rules; you never do.</li>
<li><strong>Use ICU message format for plurals.</strong> Don&#39;t hardcode English&#39;s two-case grammar into a multi-locale app.</li>
<li><strong>Load locales on demand, don&#39;t bundle them all.</strong> Dynamic-import the active language; leave the rest on the server.</li>
<li><strong>Build layout on logical properties.</strong> <code>start</code>/<code>end</code>, not <code>left</code>/<code>right</code>, so RTL is a setting and not a rewrite.</li>
</ul>
<p>I used to think of i18n as a chore you tack on at the end — extract the strings, hand them to translators, done. That framing is why so many &quot;internationalized&quot; apps still show a German user an American date and an Arabic user a left-to-right layout. The strings were never the hard part. The hard part is that every locale formats values differently and reads in its own direction, and the app has to be built to bend to that instead of assuming the world works like the machine you developed on. Get the formatting and loading right and translation is the small, boring finish — which is exactly where it should sit.</p>
<p>Speaking of values that quietly differ everywhere and ruin your day when you get them wrong — the next article is about <a href="https://ma-x.im/blog/react-playbook-payments">handling money in React</a>: what taking a payment actually requires, and why the client should never be the thing that decides you got paid.</p>
<p>If you&#39;ve got an app that &quot;supports&quot; multiple languages but still leaks the developer&#39;s locale — an English date here, a broken plural there — that&#39;s the formatting-not-translation gap. Tell me what&#39;s showing wrong and I&#39;ll point you at which piece is missing.</p>
]]></content:encoded>
      <pubDate>Tue, 12 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>i18n</category>
      <category>Internationalization</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The Smeared Component: When Architecture Scatters What Should Stay Together</title>
      <link>https://ma-x.im/blog/smeared-component</link>
      <guid isPermaLink="true">https://ma-x.im/blog/smeared-component</guid>
      <description>FSD is one of the best frontend methodologies out there. Used without thinking, it turns a complex component into a scavenger hunt across eight folders. Here&apos;s what I learned.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/smeared-component/cover.png" alt="The Smeared Component: When Architecture Scatters What Should Stay Together" /></p>
<p>I&#39;ve been writing code since 2003. That&#39;s over twenty years of watching patterns come and go — and occasionally making the same mistakes the patterns were supposed to prevent.</p>
<p>My first serious FSD project was one of those mistakes.</p>
<p>Feature-Sliced Design is one of the most popular frontend architectures today. The docs are clear, the community is active, the rules make sense on paper. You read through it and something clicks: pages here, features there, entities in their own layer, shared utilities at the bottom. Clean. Logical. Structured.</p>
<p>So I followed the rules. Meticulously.</p>
<p>And I ended up with a codebase where a single button — one button that validated a form field — lived across ten folders. Its logic was in <code>features/</code>. Its type was in <code>entities/</code>. Its hook was in <code>shared/hooks/</code>. Its analytics call was somewhere in <code>features/analytics/</code>. Its test was buried in <code>__tests__/features/</code>. To understand what that button did, you had to open half the project.</p>
<p>I was frustrated. For a while, I blamed myself — maybe I didn&#39;t understand FSD well enough. Maybe I was doing it wrong. It took me some time to realize: the problem wasn&#39;t the architecture. The problem was that I had stopped thinking.</p>
<h2>The Smeared Component</h2>
<p>There&#39;s a pattern I&#39;ve started calling the <em>smeared component</em>. You&#39;ve probably seen it. You might have built it.</p>
<p>It happens when a single feature — a payment form, an order widget, a settings panel — gets distributed across the project&#39;s folder structure &quot;correctly.&quot; Each piece goes to its designated layer. Hooks go to <code>hooks/</code>. Types go to <code>types/</code>. API calls go to <code>api/</code>. Validators to <code>validators/</code>. Stories to <code>stories/</code>. Tests to <code>__tests__/</code>.</p>
<p>The result looks organized from the outside. Every folder has the right things in it.</p>
<p>But try to understand the component. Try to fix a bug in it. Try to onboard a new developer onto it. You&#39;ll open one file, realize you need another, follow an import, land in a third folder, lose track of the original file, open a fourth — and fifteen minutes later you&#39;re not sure you&#39;ve seen all the pieces yet.</p>
<blockquote>
<p>Code is read far more often than it is written. Architecture should optimize for understanding — not just separation.</p>
</blockquote>
<p>The smeared component optimizes for separation. It&#39;s perfectly layered. And it&#39;s nearly impossible to hold in your head.</p>
<h2>A Real Example: PaymentMethodSelector</h2>
<p>Let me show you what this looks like in practice.</p>
<p>Imagine a <code>PaymentMethodSelector</code> component — the kind you see on checkout pages. It fetches available payment methods from an API, displays them with icons and descriptions, handles selection state, validates the selected method against order constraints, shows a modal for adding a new card, fires analytics events on selection change, and stores the result in global state.</p>
<p>A well-intentioned FSD implementation might distribute it like this:</p>
<pre><code>src/
  features/
    payment-method/
      ui/
        PaymentMethodSelector.tsx     ← main component
        PaymentMethodOption.tsx
        AddPaymentModal.tsx
  entities/
    payment/
      model/
        payment.store.ts              ← Zustand store
        payment.selectors.ts
      api/
        payment.api.ts                ← API calls
      types/
        payment.types.ts              ← types and interfaces
  shared/
    hooks/
      usePaymentValidation.ts         ← validation logic
    analytics/
      payment.analytics.ts            ← analytics events
    lib/
      formatters/
        payment.formatters.ts         ← formatting helpers
  __tests__/
    features/
      PaymentMethodSelector.test.tsx
  stories/
    PaymentMethodSelector.stories.tsx
</code></pre>
<p>Eight locations. For one component.</p>
<p>Now imagine the validation logic needs to change — a new payment method has different constraints. You update <code>usePaymentValidation.ts</code>. The type needs updating — <code>payment.types.ts</code>. The API response shape changed — <code>payment.api.ts</code>. The analytics event needs a new field — <code>payment.analytics.ts</code>. Update the test — somewhere in <code>__tests__/features/</code>. Update the story. Six folders. One change.</p>
<p>Every bug fix is an archaeological dig. Every refactor is a leap of faith.</p>
<p>Here&#39;s what the same component looks like with colocation:</p>
<pre><code>src/
  features/
    payment-method/
      PaymentMethodSelector.tsx
      PaymentMethodSelector.types.ts
      PaymentMethodSelector.hooks.ts
      PaymentMethodSelector.api.ts
      PaymentMethodSelector.store.ts
      PaymentMethodSelector.analytics.ts
      PaymentMethodSelector.utils.ts
      PaymentMethodSelector.test.tsx
      PaymentMethodSelector.stories.tsx
      ui/
        PaymentMethodOption.tsx
        AddPaymentModal.tsx
</code></pre>
<p>Everything that changes together lives together. One folder. One mental unit. One place to look.</p>
<h2>&quot;But FSD Doesn&#39;t Allow That&quot;</h2>
<p>I can already hear the objection: &quot;FSD has rules. Types belong in entities. Hooks belong in shared. You can&#39;t just put everything in features.&quot;</p>
<p>Here&#39;s the thing: FSD is a tool. Not a commandment.</p>
<p>The methodology was designed for the common case — and the common case is a typical CRUD feature with thin logic and shared primitives. For a simple <code>UserAvatar</code> component, fine — put the type in entities, the API call in shared. The overhead is minimal because the component is minimal.</p>
<p>But for a complex, domain-rich feature like <code>PaymentMethodSelector</code>? The cost changes. Every &quot;correct&quot; placement is a navigation tax you pay every time you touch the component.</p>
<p>FSD actually allows adaptation. You can add a <code>core/</code> layer for truly foundational cross-cutting concerns. You can extend <code>shared/</code> with domain-specific subfolders. You can colocate everything within a feature when that feature is complex enough to justify it. The methodology explicitly says the structure should serve the project — not the other way around.</p>
<p>I&#39;ve worked on a project where the team extended FSD&#39;s layer model to add a <code>core/</code> layer for infrastructure-level services: logging, auth tokens, global config. Things that don&#39;t belong to any feature but are too specific for <code>shared/</code>. It worked cleanly. No FSD rule was violated — because the rule was never &quot;these are the only layers you can have.&quot; The rule was &quot;maintain the import direction and keep dependencies explicit.&quot;</p>
<blockquote>
<p>Architecture is not what you do by the rules. It&#39;s how you solve the problem in front of you — in a way that stays solvable tomorrow.</p>
</blockquote>
<h2>Why We Keep Doing This</h2>
<p>The reason we smear components is usually good intentions. We read the docs. We follow the patterns. We want the project to be &quot;clean.&quot; We&#39;ve been burned by messy codebases before and we never want to go back.</p>
<p>So we apply the architecture top to bottom, folder by folder, layer by layer. And it works. Until the project grows. Until the component gets complex. Until someone new joins and spends three days just finding where things are.</p>
<p>I&#39;ve worked in legacy codebases where the original authors followed every rule. The folder structure is immaculate. And the cognitive load of working in it is brutal — because everything <em>looks</em> right but nothing <em>reads</em> right. Opening a component means opening a new tab for the hook, a new tab for the type, a new tab for the API call. Your brain is juggling six contexts at once.</p>
<p>I&#39;ve been that original author too, on my first FSD project. I was so focused on placing files correctly that I stopped asking whether the placement made the code better.</p>
<h2>What Architecture Actually Optimizes For</h2>
<p>Good architecture optimizes for:</p>
<ul>
<li><strong>Locality</strong> — related things are close to each other</li>
<li><strong>Discoverability</strong> — you can find what you&#39;re looking for without a map</li>
<li><strong>Changeability</strong> — when requirements change, the blast radius is predictable</li>
<li><strong>Cognitive simplicity</strong> — a developer can hold the relevant context in their head</li>
</ul>
<p>Less useful to optimize for:</p>
<ul>
<li>Folder aesthetics</li>
<li>Layer purity</li>
<li>DRY at the cost of everything else</li>
</ul>
<p>These aren&#39;t opposites. A well-applied FSD project can nail all the good ones. But &quot;well-applied&quot; requires judgment, not just rule-following.</p>
<p>The principle I keep coming back to: <strong>things that change together should live together.</strong> It sounds simple. It&#39;s not always easy to apply. But it&#39;s the right question to ask before you decide where a file goes.</p>
<h2>A Practical Test</h2>
<p>Before placing a file in its &quot;architecturally correct&quot; location, ask yourself:</p>
<ol>
<li>If I need to change this file, what other files will I need to open at the same time?</li>
<li>Are those files nearby — or spread across the project?</li>
<li>If a new developer needed to understand this feature, how many folders would they need to open?</li>
</ol>
<p>If the answers make you uncomfortable, you&#39;re probably optimizing for layer purity instead of developer experience. Reconsider the placement.</p>
<p>This isn&#39;t &quot;ignore architecture.&quot; It&#39;s the opposite — it&#39;s using the architecture deliberately. FSD, done thoughtfully, is excellent. Applied mechanically — rule by rule, folder by folder, without judgment — it produces smeared components and frustrated developers.</p>
<p>I&#39;ve worked on both kinds of projects. The difference wasn&#39;t the architecture they used. It was the thinking behind it.</p>
]]></content:encoded>
      <pubDate>Sat, 09 May 2026 00:00:00 GMT</pubDate>
      <category>Architecture</category>
      <category>React</category>
      <category>FSD</category>
      <category>DX</category>
      <category>Frontend</category>
    </item>
    <item>
      <title>Real-Time React: When the Server Has Something to Say</title>
      <link>https://ma-x.im/blog/react-playbook-websockets</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-websockets</guid>
      <description>Every data pattern so far has the client asking and the server answering. Real-time inverts that — the server speaks first, and your UI has to be listening. The mistake is treating that live connection as a second, parallel source of truth. Here&apos;s how I keep a React app live without ending up with two states that disagree.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-websockets/cover.png" alt="Real-Time React: When the Server Has Something to Say" /></p>
<p>Everything in this series so far has had the same conversation shape: the client asks a question, the server answers. You call <code>useQuery</code>, the request goes out, the response comes back. The client always speaks first. It&#39;s a comfortable model, and it&#39;s most of what a web app does.</p>
<p>Real-time flips the direction. Now the server speaks first — a new message lands, a price ticks, another user drags a card across the board — and your UI has to already be listening, with no request that triggered it. That inversion is the whole thing that makes real-time feel different to build, and it&#39;s where people reach for a WebSocket and immediately make the mistake that poisons the rest of the feature.</p>
<p>The mistake is treating the live connection as a <em>second source of truth</em>. You already have server state, cached and managed. Then you open a socket, start catching messages, and stuff them into a separate piece of React state. Now you have two stores that are supposed to represent the same reality, updated through two different channels, and they drift. This article is about not doing that — how I wire a live connection into the state I already have, so the app stays live <em>and</em> consistent.</p>
<h2>First: Do You Even Need a WebSocket?</h2>
<p>Before the socket, a fork in the road, because &quot;real-time&quot; gets reached for reflexively and WebSockets are the heaviest option.</p>
<p>There are really three tools, and they&#39;re not interchangeable. <strong>Polling</strong> — just refetch on an interval — is the humble one everyone forgets. If &quot;real-time&quot; means &quot;within thirty seconds,&quot; a <code>refetchInterval</code> on an existing query is the entire feature, no new infrastructure, and you should feel no shame about it. <strong>Server-Sent Events (SSE)</strong> are a one-way stream from server to client over plain HTTP: perfect when the server needs to push but the client never pushes back — notifications, a live feed, status updates. <strong>WebSockets</strong> are the full bidirectional pipe, and you want them when <em>both</em> sides talk continuously: chat, collaborative editing, multiplayer anything.</p>
<p>The honest default is to start at the top of that list and only move down when the requirement forces it. A surprising number of &quot;we need WebSockets&quot; features are actually a polling interval or an SSE stream wearing a costume. Reach for the bidirectional socket when you genuinely have bidirectional, continuous communication — not before.</p>
<h2>The Core Idea: One Source of Truth</h2>
<p>Here&#39;s the principle that makes the rest of this easy, and it&#39;s worth stating before any code:</p>
<blockquote>
<p>The live connection is a delivery mechanism, not a place to store state. Messages arrive on the socket and are handed to your existing cache. They don&#39;t get their own store.</p>
</blockquote>
<p>You spent the <a href="https://ma-x.im/blog/react-playbook-data-fetching">data fetching article</a> setting up TanStack Query as the manager of server state. Real-time doesn&#39;t replace that — it feeds it. When a WebSocket message arrives, its job isn&#39;t to become a new piece of state you render directly. Its job is to <em>update the cache that&#39;s already the source of truth</em>. The socket is a courier. It delivers news to the store; it isn&#39;t the store.</p>
<p>Get this one idea right and the two-sources-of-truth bug simply can&#39;t happen, because there&#39;s still only one source of truth. The socket just has a new way of writing to it.</p>
<h2>Wiring a Socket Into the Cache</h2>
<p>Concretely, there are two moves when a message arrives, and choosing between them is most of the skill.</p>
<p>The blunt move is <strong>invalidate</strong>: a message says &quot;something changed over here,&quot; and you tell TanStack Query to refetch that query. You don&#39;t trust the message to carry the new data — you use it purely as a signal, and the refetch pulls fresh truth from the server.</p>
<pre><code class="language-tsx">import { useQueryClient } from &#39;@tanstack/react-query&#39;;
import { useEffect } from &#39;react&#39;;

function useTicketUpdates(socket: WebSocket) {
  const queryClient = useQueryClient();

  useEffect(() =&gt; {
    function handleMessage(event: MessageEvent) {
      const msg = JSON.parse(event.data);
      if (msg.type === &#39;ticket.updated&#39;) {
        // The socket is just a signal. Refetch the real data.
        queryClient.invalidateQueries({ queryKey: [&#39;tickets&#39;, msg.ticketId] });
      }
    }

    socket.addEventListener(&#39;message&#39;, handleMessage);
    return () =&gt; socket.removeEventListener(&#39;message&#39;, handleMessage);
  }, [socket, queryClient]);
}
</code></pre>
<p>Invalidation is safe and simple: the server stays authoritative, and you never render data the socket made up. The cost is an extra round trip per message, which is fine for low-frequency events and wrong for a firehose.</p>
<p>The sharp move is <strong>write the message straight into the cache</strong> with <code>setQueryData</code>, when the message already carries the full new value and refetching would be wasteful — a chat message, a price tick:</p>
<pre><code class="language-tsx">function handleMessage(event: MessageEvent) {
  const msg = JSON.parse(event.data);
  if (msg.type === &#39;chat.message&#39;) {
    queryClient.setQueryData([&#39;chat&#39;, msg.roomId], (old: Message[] = []) =&gt; [
      ...old,
      msg.message,
    ]);
  }
}
</code></pre>
<p>Same principle either way — the socket updates the one cache — but <code>setQueryData</code> skips the round trip by trusting the payload. My rule of thumb: <strong>invalidate for &quot;something changed,&quot; write directly for &quot;here is exactly what changed.&quot;</strong> High-frequency streams where a refetch-per-message would melt your server are the clear case for writing directly; everything else, start with invalidate because it&#39;s harder to get wrong.</p>
<h2>The Part Everyone Underestimates: The Connection Lifecycle</h2>
<p>Opening a socket is the easy 20% again. The other 80% is that connections are <em>not reliable</em>, and a real-time feature is only as good as how it handles a connection that isn&#39;t there.</p>
<p>The network drops. The laptop sleeps and wakes. A deploy restarts the server and every socket dies at once. If your feature assumes a persistent, healthy connection, it silently stops updating and the user has no idea — which is worse than a visible error, because they&#39;re now looking at stale data they believe is live. So the lifecycle work is not optional polish; it&#39;s the feature. You need <strong>reconnection</strong> with backoff so a blip heals itself instead of ending the session. You need to <strong>resync on reconnect</strong> — when the socket comes back, invalidate the relevant queries and refetch, because you almost certainly missed messages while it was down, and the socket can&#39;t replay history it never delivered. And you owe the user <strong>connection status</strong> in the UI, even something quiet, so &quot;live&quot; and &quot;trying to reconnect&quot; don&#39;t look identical.</p>
<p>This is exactly why not hand-rolling the raw <code>WebSocket</code> for anything serious is the right call. A library like <a href="https://socket.io">Socket.IO</a> exists to handle reconnection, fallbacks, and connection state for you — the same trade as every other article in this series, where the value isn&#39;t the raw capability but the library that manages its messy edges. Raw <code>WebSocket</code> is fine to learn on and fine for a toy. For a feature people depend on, let something else own the reconnection logic, and spend your attention on what the messages <em>mean</em>.</p>
<h2>How I&#39;d Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Don&#39;t reach for WebSockets reflexively.</strong> Polling and SSE cover more cases than people admit. Use the bidirectional socket only for genuinely bidirectional, continuous features.</li>
<li><strong>The connection is a courier, not a store.</strong> One source of truth stays your TanStack Query cache; the socket writes to it.</li>
<li><strong>Invalidate for &quot;something changed,&quot; <code>setQueryData</code> for &quot;here&#39;s exactly what changed.&quot;</strong> Start with invalidate; reach for direct writes when the frequency demands it.</li>
<li><strong>The lifecycle is the feature.</strong> Reconnect with backoff, resync on reconnect, show connection status. A silently dead socket is worse than an error.</li>
<li><strong>Let a library own the connection.</strong> Hand-rolled raw sockets are for learning, not for things people rely on.</li>
</ul>
<p>The thing that used to make real-time feel hard, for me, was imagining it as a whole separate system bolted onto the app — its own state, its own rules, its own bugs. It isn&#39;t. It&#39;s one new way for data to arrive into the store you already have. Once the socket stops being a source of truth and becomes a courier delivering to your cache, the &quot;two states that disagree&quot; class of bug disappears, and what&#39;s left is just careful handling of a connection that can drop. That part takes discipline, but it&#39;s a known problem with known answers.</p>
<p>Next up, a different kind of &quot;the same app, but harder&quot;: <a href="https://ma-x.im/blog/react-playbook-i18n">internationalization in React</a> — which turns out to be far less about translating strings and far more about formatting, loading, and everything that quietly breaks when your app leaves one language.</p>
<p>If you&#39;ve got a real-time feature where the UI occasionally shows stale data or two views disagree, that&#39;s almost always a two-sources-of-truth problem or a missed-resync-on-reconnect problem. Tell me the symptom and I&#39;ll tell you which.</p>
]]></content:encoded>
      <pubDate>Thu, 07 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>WebSockets</category>
      <category>Real-time</category>
      <category>TanStack Query</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>AI in a React App: Past the Demo, Into the Product</title>
      <link>https://ma-x.im/blog/react-playbook-ai</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-ai</guid>
      <description>The demos are intoxicating — a chat box, a streaming response, thirty lines of code and it feels like magic. Then you try to ship it and the hard parts show up all at once: streaming UI, structured output you can trust, state that doesn&apos;t fight you. Here&apos;s what building AI into a real React app actually takes.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-ai/cover.png" alt="AI in a React App: Past the Demo, Into the Product" /></p>
<p>The first AI demo you build feels like cheating. A text input, a fetch to a model, a response on screen — thirty lines, maybe less, and suddenly your app <em>talks</em>. It&#39;s genuinely exciting, and that excitement is exactly the trap. Because the demo is the easy 20%. The moment you try to put that chat box in front of real users, the other 80% arrives all at once, and none of it is about the model.</p>
<p>The response doesn&#39;t appear all at once — it streams, token by token, and your UI has to handle that gracefully. You don&#39;t just want text back, you want <em>structured</em> data you can render into a component, and the model doesn&#39;t always cooperate. The request takes seconds, sometimes fails halfway, and your app has to stay honest about loading and error states the whole time. This is the part the demos skip, and it&#39;s the entire job.</p>
<p>So this article isn&#39;t &quot;how to call an LLM.&quot; Calling the model is trivial. This is about the React-side reality of turning that call into something you&#39;d actually ship — the streaming, the structure, the state — and where the current tools genuinely help versus where you&#39;re still on your own.</p>
<h2>Why AI Broke My Normal Data Layer</h2>
<p>Up to this point in the series, every data problem had the same shape. You fire a request, you wait, you get a response, <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a> caches it and hands it to your component. Request, response, done. It&#39;s a clean model and it covers almost everything.</p>
<p>AI breaks it in one specific way: the response isn&#39;t a single event, it&#39;s a <em>stream</em>. The model produces its answer incrementally, and the whole point — the thing that makes it feel alive — is showing those tokens as they arrive. A spinner that sits for eight seconds and then dumps a wall of text feels broken, even when it&#39;s technically correct. The perceived quality of an AI feature is almost entirely about the streaming.</p>
<p>That&#39;s a different primitive than request/response. You&#39;re not awaiting a value; you&#39;re consuming a sequence over time and rendering each chunk. Trying to force that into a normal <code>useQuery</code> fights the grain of the tool. This is why AI-specific React libraries exist — not because calling a model is hard, but because <em>streaming a model&#39;s output into a UI</em> is a genuinely different problem, and doing it by hand means manually managing readers, buffers, partial state, and cancellation.</p>
<h2>The Streaming Chat, Handled For You</h2>
<p>The good news is that this exact problem is now well-solved. The <a href="https://ai-sdk.dev">Vercel AI SDK</a> (and the TanStack-flavored patterns around it) exists precisely to make streaming chat a solved problem on the React side. Its <code>useChat</code> hook is the <code>useQuery</code> of this world — it owns the messy part so you don&#39;t.</p>
<pre><code class="language-tsx">import { useChat } from &#39;ai/react&#39;;

export function SupportChat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading } =
    useChat({ api: &#39;/api/chat&#39; });

  return (
    &lt;div className=&quot;support-chat&quot;&gt;
      {messages.map((m) =&gt; (
        &lt;div key={m.id} data-role={m.role}&gt;
          {m.content}
        &lt;/div&gt;
      ))}

      &lt;form onSubmit={handleSubmit}&gt;
        &lt;input value={input} onChange={handleInputChange} disabled={isLoading} /&gt;
        &lt;button type=&quot;submit&quot; disabled={isLoading}&gt;Send&lt;/button&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>Look at what you <em>didn&#39;t</em> write. No stream reader. No manual buffering of partial tokens. No hand-rolled &quot;append this chunk to the last message&quot; logic. <code>messages</code> updates as tokens arrive, so the response types itself onto the screen. <code>isLoading</code> gives you the state to disable the input and show activity. The entire streaming problem — the thing that would take a day to get right by hand and stay subtly buggy — is inside the hook.</p>
<p>This is the same lesson as every other article in this series: the value isn&#39;t the raw capability, it&#39;s a library that takes the awkward part and gives you a clean React interface for it. <code>useChat</code> is to streaming what TanStack Query is to caching.</p>
<h2>The Part That Actually Trips People Up: Structured Output</h2>
<p>Streaming text is solved. Here&#39;s where it gets real: most of the time you don&#39;t want a paragraph of prose, you want <em>data</em>. You want the model to extract three fields from an invoice, or classify a support ticket, or return a list of suggested tags — and then you want to render that into an actual component, not a string.</p>
<p>The naive approach is to ask the model for JSON in the prompt and <code>JSON.parse</code> the result. Do this in a demo and it works. Do it in production and it breaks, because the model is a text generator, not an API. Sometimes it wraps the JSON in ````json fences. Sometimes it adds a friendly &quot;Sure, here&#39;s the data:&quot; preamble. Sometimes it hallucinates a field. <code>JSON.parse</code> throws, and now your feature is down because the model was chatty.</p>
<p>The current answer is to stop hoping and start enforcing. You define a <a href="https://ma-x.im/blog/react-playbook-form-libraries">Zod</a> schema for the shape you need and let the SDK constrain the model to it:</p>
<pre><code class="language-ts">import { generateObject } from &#39;ai&#39;;
import { z } from &#39;zod&#39;;

const ticketSchema = z.object({
  category: z.enum([&#39;billing&#39;, &#39;technical&#39;, &#39;account&#39;, &#39;other&#39;]),
  priority: z.enum([&#39;low&#39;, &#39;medium&#39;, &#39;high&#39;]),
  summary: z.string(),
});

const { object } = await generateObject({
  model,
  schema: ticketSchema,
  prompt: `Classify this support ticket: ${ticketText}`,
});

// object is typed as { category: ..., priority: ..., summary: string }
// and validated — not a hopeful JSON.parse.
</code></pre>
<p>The difference is the difference between a demo and a product. <code>object</code> isn&#39;t a string you&#39;re praying parses — it&#39;s a validated, typed value the SDK guarantees matches your schema, or the call fails loudly instead of feeding garbage into your UI. Notice this is the <em>exact same Zod schema pattern</em> from the forms article, pointed at a new problem. You already know this tool. The model becomes just another untrusted input source you validate at the boundary — which, if you&#39;ve read the <a href="https://ma-x.im/blog/react-playbook-authentication">authentication</a> piece, is exactly how you should treat everything crossing into your app.</p>
<blockquote>
<p>Treat model output like user input, not like an API response. It is text that happens to look structured. Validate it, or it will embarrass you in production.</p>
</blockquote>
<h2>Where the Tools Stop and You Start</h2>
<p>I want to be honest about the edges, because the ecosystem is young and the demos oversell.</p>
<p>The streaming and structured-output tools are solid. What they don&#39;t solve is everything <em>around</em> the call. Cost — every token is money, and a chat feature with no limits is a bill waiting to happen; you need throttling and usage caps, and those are your problem, not the SDK&#39;s. Latency — responses take seconds, and no amount of UI polish makes a slow model fast; you design around the wait, you don&#39;t remove it. Failure — models time out, hit rate limits, and return nonsense, so every AI feature needs the same <a href="https://ma-x.im/blog/react-playbook-error-handling">error handling</a> discipline as any other flaky network dependency, plus a graceful answer for &quot;the model gave me something useless.&quot;</p>
<p>And the honest constraint underneath all of it: the model is non-deterministic. The same input can produce different output. That breaks a core assumption every other part of your app relies on. You can&#39;t snapshot-test it the normal way, you can&#39;t guarantee a given response, and you have to design UI that stays usable when the answer is wrong. That&#39;s not a library gap that&#39;ll be patched next quarter. That&#39;s the nature of the thing, and building AI features well means designing for it rather than pretending it away.</p>
<h2>How I&#39;d Actually Approach It</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>Streaming is the product, not a detail.</strong> The feel of an AI feature is the streaming. Don&#39;t fake it with a spinner.</li>
<li><strong>Don&#39;t hand-roll the stream.</strong> Use <code>useChat</code> / the AI SDK. It&#39;s the <code>useQuery</code> of streaming — it owns the messy part.</li>
<li><strong>Never trust free-form JSON.</strong> Use <code>generateObject</code> with a Zod schema. Treat model output as untrusted input, validated at the boundary.</li>
<li><strong>Budget for cost, latency, and failure from day one.</strong> They&#39;re your responsibility, not the SDK&#39;s, and they don&#39;t go away.</li>
<li><strong>Design for non-determinism.</strong> The answer can be wrong. The UI has to survive that.</li>
</ul>
<p>The demos make AI look like a feature you drop in. It isn&#39;t. It&#39;s a new kind of data source — streaming, untrusted, non-deterministic, and metered — and the work is treating it with the same engineering seriousness you&#39;d give any other unreliable dependency. The tools have gotten genuinely good at the streaming and the structure. The judgment — where to use it, how to fail, what to guarantee — is still entirely yours, and that&#39;s the part worth getting right.</p>
<p>The next article stays in real-time territory but drops the AI: <a href="https://ma-x.im/blog/react-playbook-websockets">WebSockets and real-time in React</a> — how to keep a UI live when the server has something to say, and how it fits with the TanStack Query cache you already have.</p>
<p>If you&#39;re building an AI feature and it works beautifully in the demo but falls apart with real traffic, that gap is almost always one of these — streaming, structure, or the metered/non-deterministic reality underneath. Tell me where it breaks and I&#39;ll tell you which one it is.</p>
]]></content:encoded>
      <pubDate>Mon, 04 May 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>AI</category>
      <category>TanStack</category>
      <category>Streaming</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Testing React Without Drowning: What Each Test Actually Buys You</title>
      <link>https://ma-x.im/blog/react-playbook-testing</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-testing</guid>
      <description>Most testing advice is either &quot;100% coverage&quot; zealotry or &quot;tests slow you down&quot; cynicism. Both miss the point. A test is a trade — effort now for confidence later — and the skill is knowing which trades pay off. Here&apos;s how I test React without a brittle suite that everyone hates.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-testing/cover.png" alt="Testing React Without Drowning: What Each Test Actually Buys You" /></p>
<p>Two developers look at the same untested codebase and reach opposite conclusions. One says &quot;this is reckless, we need 100% coverage before we ship anything else.&quot; The other says &quot;tests are why we&#39;re slow, they break every time we change a line, let&#39;s just be careful instead.&quot; I&#39;ve been in rooms with both, and here&#39;s the uncomfortable thing: they&#39;re both partly right, and both are optimizing the wrong variable.</p>
<p>Because a test isn&#39;t a virtue and it isn&#39;t a tax. It&#39;s a trade — effort and speed now, in exchange for confidence and safety later. Some of those trades are wildly worth it. Some cost more than they&#39;ll ever return. The whole skill of testing isn&#39;t writing tests; it&#39;s knowing which trade you&#39;re making and whether it pays off.</p>
<p>So this article isn&#39;t a testing tutorial and it definitely isn&#39;t a coverage sermon. It&#39;s how I actually decide what to test in a React app, so I end up with a suite that catches real bugs without becoming the brittle, resented thing that everyone works around.</p>
<h2>The Pain Each Kind of Test Removes</h2>
<p>Forget the pyramid diagram for a second. The useful question isn&#39;t &quot;what are the types of tests,&quot; it&#39;s &quot;what specific pain does each one remove.&quot; Because if a test isn&#39;t removing pain, you&#39;re paying for nothing.</p>
<p><strong>Unit tests</strong> remove the pain of a single piece of logic being subtly wrong. A pricing calculation, a date formatter, a reducer with tricky edge cases. Fast, focused, and they pin down the exact spot a bug lives.</p>
<p><strong>Component tests</strong> remove the pain of a UI piece behaving wrong when a user touches it. Does the form show the error, does the button disable while submitting, does the list render what it&#39;s handed. This is where most React testing value actually lives, and we&#39;ll spend the most time here.</p>
<p><strong>End-to-end (E2E) tests</strong> remove the pain of the whole thing being broken even though every piece works alone. The classic disaster: every unit passes, and login is still broken because two correct parts don&#39;t fit together. E2E drives a real browser through a real flow and catches exactly that.</p>
<p>Each targets a different fear. Unit: &quot;is this logic right?&quot; Component: &quot;does this behave when used?&quot; E2E: &quot;does the actual journey work?&quot; You don&#39;t need all three everywhere. You need each one where its specific fear is real.</p>
<h2>The One Rule That Fixed My Component Tests</h2>
<p>For a long time my React tests were brittle. I&#39;d change an implementation detail — rename a state variable, restructure a handler, swap a class name — and a dozen tests would go red without a single actual bug. That&#39;s the experience that makes people hate testing, and it turns out it was my fault, not testing&#39;s.</p>
<p>The rule that fixed it: <strong>test behavior, not implementation. Test what the user experiences, not how the component achieves it.</strong></p>
<p><a href="https://testing-library.com">React Testing Library</a> is built entirely around this idea, and once it clicked, my tests stopped breaking for stupid reasons. You don&#39;t query for internal state or component instances. You query the DOM the way a user perceives it — by text, by role, by label — and you interact the way a user does.</p>
<pre><code class="language-tsx">import { render, screen } from &#39;@testing-library/react&#39;;
import userEvent from &#39;@testing-library/user-event&#39;;

test(&#39;shows an error when submitting an empty policy holder name&#39;, async () =&gt; {
  render(&lt;PolicyForm /&gt;);

  // Interact like a user: find the button by its accessible role.
  await userEvent.click(screen.getByRole(&#39;button&#39;, { name: /save/i }));

  // Assert on what the user actually sees.
  expect(screen.getByText(/holder name is required/i)).toBeInTheDocument();
});
</code></pre>
<p>Look at what that test doesn&#39;t know. It doesn&#39;t know if <code>PolicyForm</code> uses <code>useState</code> or a form library, whether the error lives in local state or context, what the handler is called. I can rewrite the entire internals of that component and, as long as an empty submit still shows that error, the test stays green. That&#39;s the point: the test is coupled to the <em>behavior the user relies on</em>, not the code I happened to write. Implementation-coupled tests break when you refactor; behavior-coupled tests break when you regress. Only one of those is useful.</p>
<p>This connects straight back to the <a href="https://ma-x.im/blog/react-playbook-form-libraries">forms article</a>: the reason I could confidently recommend a specific form library is that behavior tests let me swap implementations without rewriting the test suite. Good tests make good architecture decisions cheaper to reverse.</p>
<h2>Vitest, Quickly</h2>
<p>A note on tooling, because it changed and the old advice lingers. For years Jest was the default. Today, for a Vite-based app — which is most new React apps — <strong>Vitest</strong> is the natural pick. It reuses your existing Vite config and transforms, so there&#39;s no separate parallel build setup to maintain, and it&#39;s fast. The API is close enough to Jest that everything you know transfers. Pair it with React Testing Library and you have the whole component-testing stack. I won&#39;t belabor it: if you&#39;re on Vite, use Vitest, and move on to the part that matters, which is deciding what to test.</p>
<h2>What I Actually Test, and What I Don&#39;t</h2>
<p>Here&#39;s where the &quot;it&#39;s a trade&quot; framing turns into decisions. Not everything deserves a test, and pretending otherwise is how you get a slow, resented suite.</p>
<p><strong>I test the things that are painful when wrong.</strong> Business logic with real consequences — pricing, permissions, anything money- or data-integrity-related. Complex conditional UI, the kind with enough branches that I can&#39;t hold them in my head. Bugs I&#39;ve already fixed once, which get a regression test so they can&#39;t come back. Custom hooks with real logic. These earn their keep because the cost of them silently breaking is high.</p>
<p><strong>I don&#39;t test the things that are cheap to verify by looking.</strong> A component that renders a prop into a heading. Trivial passthrough. Third-party libraries — that&#39;s their job, not mine. Static markup with no logic. Writing a test for <code>&lt;h1&gt;{title}&lt;/h1&gt;</code> doesn&#39;t remove any pain; it just adds a file that has to change every time the markup does.</p>
<p>The honest heuristic I use: <strong>before writing a test, I ask what bug it would catch and how bad that bug would be.</strong> If I can&#39;t name a realistic bug, or the bug would be obvious and harmless, I skip it. If the bug would be subtle, expensive, or embarrassing, I write the test first. Coverage percentage never enters the decision — a codebase at 100% coverage full of <code>&lt;h1&gt;</code> tests is worse than one at 60% that covers every pricing path, because the first one is mostly noise you have to maintain.</p>
<blockquote>
<p>Coverage measures how much code ran during tests. It says nothing about whether the tests would catch a bug that matters. Chasing the number optimizes the wrong thing.</p>
</blockquote>
<h2>E2E: A Little Goes a Long Way</h2>
<p>End-to-end tests are the most expensive to write and maintain and the slowest to run, so I&#39;m deliberately stingy with them — but the few I keep are the ones I&#39;d least want to live without.</p>
<p>I don&#39;t E2E every screen. I E2E the handful of flows where &quot;broken and nobody noticed&quot; would be a genuine disaster: the signup and login path, the checkout or core conversion flow, the one or two journeys the business actually runs on. Tools like Playwright drive a real browser through those, catching the integration failures that unit and component tests structurally can&#39;t see.</p>
<pre><code class="language-ts">import { test, expect } from &#39;@playwright/test&#39;;

test(&#39;a user can sign in and reach the dashboard&#39;, async ({ page }) =&gt; {
  await page.goto(&#39;/login&#39;);
  await page.getByLabel(&#39;Email&#39;).fill(&#39;user@example.com&#39;);
  await page.getByLabel(&#39;Password&#39;).fill(&#39;correct-horse&#39;);
  await page.getByRole(&#39;button&#39;, { name: /sign in/i }).click();

  await expect(page.getByRole(&#39;heading&#39;, { name: /dashboard/i })).toBeVisible();
});
</code></pre>
<p>Notice it&#39;s the same philosophy as the component test — interact by role and label, assert on what the user sees — just scaled up to the whole journey through a real browser. A small number of these, guarding the flows that matter most, buys an enormous amount of confidence for the cost. Try to E2E everything and you get a slow, flaky suite people disable. Guard the critical few and you get a safety net people trust.</p>
<h2>How This All Fits Together</h2>
<p>Strip it to decisions:</p>
<ul>
<li><strong>A test is a trade, not a virtue.</strong> Effort now for confidence later — only worth it when the confidence is worth more than the effort.</li>
<li><strong>Match the test to the fear.</strong> Unit for logic, component for behavior, E2E for the critical journey.</li>
<li><strong>Test behavior, not implementation.</strong> Query and interact like a user, so refactors don&#39;t turn your suite red for no reason.</li>
<li><strong>On Vite, use Vitest</strong> with React Testing Library, and stop thinking about tooling.</li>
<li><strong>Ask &quot;what bug would this catch, and how bad is it?&quot;</strong> before writing any test. Skip the ones with no honest answer.</li>
<li><strong>Ignore the coverage number.</strong> Cover what hurts when it breaks.</li>
</ul>
<p>I used to think the goal of testing was a high coverage number, and I wrote a lot of useless tests chasing it. The goal is confidence — the specific, earned feeling that you can change this code and not break something that matters. A suite that gives you that at 60% honest coverage is worth more than one that gives you false comfort at 100%. Tests aren&#39;t there to make a dashboard green. They&#39;re there so you can move fast without being afraid, and every test that doesn&#39;t serve that is just weight you&#39;re carrying.</p>
<p>This is the last piece of the &quot;build a complete app&quot; arc — start a project, structure it, fetch data, route, manage state, style, build forms and charts, authenticate, talk to a backend, render on the server, store data, deploy, and now test it. From here the series turns to the sharper edges: the next article is about <a href="https://ma-x.im/blog/react-playbook-ai">React and AI</a> — what the current tools actually make possible in a real app, past the demos.</p>
<p>If you&#39;ve got a test suite that everyone quietly works around — slow, brittle, red for the wrong reasons — that&#39;s usually a &quot;testing implementation instead of behavior&quot; problem, and it&#39;s fixable. Tell me what breaks when you refactor, and I&#39;ll tell you what I&#39;d change.</p>
]]></content:encoded>
      <pubDate>Thu, 30 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Testing</category>
      <category>Vitest</category>
      <category>Testing Library</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Hosting a React App in 2026: From DevOps Project to Git Push</title>
      <link>https://ma-x.im/blog/react-playbook-hosting-railway</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-hosting-railway</guid>
      <description>Deployment used to be a separate discipline — servers, pipelines, a person whose whole job was getting code to production. Modern platforms compressed it into a git push. Here&apos;s how to think about hosting a React app, what the tiers actually mean, and where the magic stops.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-hosting-railway/cover.png" alt="Hosting a React App in 2026: From DevOps Project to Git Push" /></p>
<p>The first time I deployed something to production, it took a week and a person whose entire job was making deployments happen. There was a server to configure, a pipeline to wire, environment variables to get wrong in six interesting ways, and a deploy checklist that read like a pre-flight sequence. Shipping was an event.</p>
<p>The last thing I deployed took a git push. No server, no pipeline I wrote, no checklist. I pushed to <code>main</code>, and a minute later it was live with a URL and HTTPS I didn&#39;t configure. Same person, same nervousness the first time — and almost none of the work.</p>
<p>That compression is the story of modern hosting, and it&#39;s easy to take for granted until you realize it quietly changed who gets to ship things. When deployment stops being a discipline, a single developer can put a real, full-stack app in front of real users without a DevOps team standing behind them. So this article is about how to think about hosting a React app now — not a tutorial for one platform, but the mental model, the tiers, and the place where the magic politely stops.</p>
<h2>First, What Are You Actually Deploying?</h2>
<p>The most common hosting confusion isn&#39;t about platforms. It&#39;s that &quot;deploy my React app&quot; means two completely different things depending on what you built, and picking the wrong kind of host is where people get stuck.</p>
<p><strong>A static site.</strong> If your app is client-side rendered or statically generated — this very blog is a static export — then &quot;deploying&quot; is just serving files. HTML, CSS, JS, sitting on a CDN. There&#39;s no server running your code; there&#39;s a very fast file server near your users. This is the cheapest, simplest, most durable thing you can ship, and platforms like GitHub Pages, Netlify, Cloudflare Pages, and Vercel do it beautifully, often free.</p>
<p><strong>An app with a server.</strong> The moment you have <a href="https://ma-x.im/blog/react-playbook-ssr-tanstack-start">SSR</a>, server functions, or a <a href="https://ma-x.im/blog/react-playbook-database-turso">backend and database</a>, there&#39;s actual code that has to run on demand, somewhere, all the time. That&#39;s not a file server. That&#39;s a running process, and it needs a host that keeps it alive, scales it, and gives it access to your database.</p>
<p>Almost every hosting mistake I see is someone trying to fit one of these into the other&#39;s home — pushing a full-stack app onto a static-only host and wondering why the server code never runs, or paying for an always-on server to deliver what is really just a folder of files. Know which one you have first. Everything else follows from that.</p>
<h2>The Static Path: Basically Solved</h2>
<p>If you&#39;re shipping a static site, the good news is you barely have to think. Connect your Git repo to Netlify, Cloudflare Pages, Vercel, or GitHub Pages; every push to your main branch builds and deploys automatically; you get a global CDN, HTTPS, and preview URLs for pull requests without configuring any of it.</p>
<p>This series&#39; own site is the proof — a Next.js static export pushed to a branch, built by CI, served from a CDN, on a custom domain. There is no server. There is nothing to keep running. And that&#39;s exactly why it&#39;s the most robust option: the thing that can&#39;t crash at 3am is the thing that isn&#39;t running. If your app can be static, let it be static, and enjoy how little there is to worry about.</p>
<h2>The Full-Stack Path: Where Railway Comes In</h2>
<p>The interesting decisions start when you have a server to run. Now you need a platform that takes your code, containerizes it, keeps the process alive, connects it to a database, manages environment variables and secrets, and scales it when traffic climbs — all without you learning Kubernetes.</p>
<p>This is the category <strong>Railway</strong> lives in, alongside Render, Fly.io, and the more serverless-shaped Vercel functions model. What they share is the same compression that hit static hosting, now applied to running servers: you connect a repo, and a git push builds and deploys a live process.</p>
<pre><code class="language-bash"># The entire &quot;deploy a full-stack app&quot; story, conceptually:
git push            # push to the connected branch
# platform detects the app, builds a container,
# injects env vars, runs it, gives it a URL and HTTPS
</code></pre>
<p>What I like about this generation of platforms is that they collapse the app <em>and</em> its dependencies into one place. On Railway you can provision your database right next to your app and wire them together in the dashboard, so the <a href="https://ma-x.im/blog/react-playbook-database-turso">Turso-or-Postgres decision from the last article</a> and the hosting decision stop being two separate projects. The whole backend — process, database, environment — lives in one connected picture instead of a pile of services you glued together by hand. For a solo developer or a small team, that consolidation is the actual product.</p>
<p>I&#39;m not going to crown one winner. Railway is excellent for full-stack apps that want a database and a server in one tidy place; Fly.io leans toward running close to users at the edge; Vercel is the smoothest possible home for a Next.js app specifically. The right pick depends on your stack, and the honest advice is to match the platform to the shape of what you built, not to the loudest brand.</p>
<h2>Environment Variables: The One Thing to Get Right</h2>
<p>If there&#39;s a single operational skill that separates a smooth deploy from a painful one, it&#39;s handling configuration correctly — and it&#39;s where security quietly rides along.</p>
<p>The rule is simple and non-negotiable: <strong>secrets go in the platform&#39;s environment variables, never in your code, never in your repo.</strong> Your database URL, your auth tokens, your API keys — those live in Railway&#39;s (or whoever&#39;s) settings, injected into the process at runtime.</p>
<p>And there&#39;s a React-specific trap worth calling out, because it burns people. Anything your frontend bundle can read is public — it ships to the browser, where anyone can open it and look. Frameworks make this explicit with prefixes: a variable exposed to the client is deliberately marked as such.</p>
<pre><code class="language-bash"># Safe on the server only — never reaches the browser:
DATABASE_URL=...
AUTH_SECRET=...

# Deliberately public — bundled into the client, so treat as visible:
NEXT_PUBLIC_ANALYTICS_ID=...
VITE_PUBLIC_APP_URL=...
</code></pre>
<p>That prefix is a security boundary, not a naming convention. A secret with a public prefix is a secret you&#39;ve published. This connects straight back to the <a href="https://ma-x.im/blog/react-playbook-authentication">authentication article</a>: the same discipline of knowing what the browser can and cannot see applies to every environment variable you set. Get this one habit right and you&#39;ve avoided the most common way small teams leak credentials.</p>
<h2>Where the Magic Stops</h2>
<p>I&#39;ve spent this article celebrating how easy hosting got, so let me be honest about where the smooth part ends, because pretending it doesn&#39;t is how people get surprised.</p>
<p>Auto-scaling is real, but it isn&#39;t free or infinite — traffic spikes cost money, and a runaway process or a bad loop can run up a bill while you sleep. Cold starts are real on serverless tiers: a function that hasn&#39;t run in a while takes a beat to wake, and for some apps that latency matters. Databases have connection limits that serverless functions, which spin up many instances, can exhaust in ways that surprise you. And &quot;it deployed&quot; is not &quot;it works&quot; — you still own monitoring, logging, and knowing when production is unhappy.</p>
<p>None of this brings back the week-long deploy. It just means the modern platform moved your attention from <em>how do I ship this</em> to <em>how does this behave once it&#39;s shipped</em>, which is a much better problem to have. The floor got higher. The ceiling is still yours to worry about.</p>
<h2>The Takeaway</h2>
<p>Boil it down:</p>
<ul>
<li><strong>Know what you&#39;re deploying.</strong> Static site or running server — that single fact chooses your host.</li>
<li><strong>If it can be static, let it be static.</strong> The cheapest, most durable option is the one with nothing running.</li>
<li><strong>For full-stack, use a platform that consolidates app and database.</strong> Railway, Render, Fly.io — a git push to a live process, database next door.</li>
<li><strong>Secrets in env vars, and respect the public prefix.</strong> It&#39;s a security boundary, not a style choice.</li>
<li><strong>The magic stops at runtime behavior.</strong> Scaling cost, cold starts, connection limits, monitoring — still yours.</li>
</ul>
<p>I started this series&#39; thread on infrastructure by admitting the database used to scare me. Hosting used to scare me for the same reason — it looked like a separate profession guarding the door to production. It isn&#39;t anymore. The tools compressed a week of DevOps into a git push, and that didn&#39;t just save time; it moved the power to ship real software into the hands of individual developers. You can build the app, store its data, and put it in front of the world, alone, in an afternoon. That&#39;s genuinely new, and it&#39;s worth not taking for granted.</p>
<p>The app is built, stored, and deployed. Now we make sure it stays working — the next article is about <a href="https://ma-x.im/blog/react-playbook-testing">testing in React</a>: the kinds of tests, the pain each one actually removes, and how to test without drowning in brittle suites.</p>
<p>If you&#39;ve got an app that&#39;s stuck at the &quot;how do I actually put this online&quot; step, that used to be a wall and now it&#39;s a git push — tell me what you built and I&#39;ll point you at where it should live.</p>
]]></content:encoded>
      <pubDate>Mon, 27 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Hosting</category>
      <category>Deployment</category>
      <category>Railway</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Databases for React Developers: What Serverless Actually Changed</title>
      <link>https://ma-x.im/blog/react-playbook-database-turso</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-database-turso</guid>
      <description>&quot;Add a database&quot; used to mean provisioning a server, managing connections, and running migrations by hand. Serverless quietly rewrote that whole sentence. Here&apos;s what a React developer actually needs to know about picking and using a database in 2026.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-database-turso/cover.png" alt="Databases for React Developers: What Serverless Actually Changed" /></p>
<p>There&#39;s a specific fear a lot of frontend developers have, and I had it too: the database is where I&#39;m not supposed to go. Tables, indexes, connection pools, migrations, the DBA who guards it all — it reads like someone else&#39;s territory, and the safe move is to stay in the browser where I belong.</p>
<p>That fear made sense a decade ago. It doesn&#39;t anymore, and holding onto it now quietly caps what you can build alone. Because &quot;add a database&quot; used to be a genuinely heavy sentence — provision a server, keep it running, manage a pool of connections, hand-run migrations, worry about it at 3am. Serverless rewrote every clause of that sentence, and most of the weight is just gone.</p>
<p>So this article is the database explainer I wish someone had handed me when I still thought data storage was off-limits. Not how to be a DBA — how to be a React developer who can pick a database, reason about it, and ship a full app without asking permission.</p>
<h2>The Fear Was Really About Operations</h2>
<p>Here&#39;s the thing I got wrong for years: my fear of databases wasn&#39;t about SQL. SQL is learnable in a weekend. It was about <em>operations</em> — the running, scaling, backing-up, not-losing-everyone&#39;s-data part. That&#39;s the genuinely hard, genuinely scary part, and it&#39;s exactly the part serverless takes off your plate.</p>
<p>A serverless database is one you don&#39;t run. No server to provision, no connection pool to babysit, no capacity to plan. It scales when you&#39;re busy, costs almost nothing when you&#39;re idle, and someone else owns the 3am pager. What&#39;s left for you is the part that was never scary: describing your data and querying it.</p>
<p>Once I understood that the wall was operational, not intellectual, databases stopped being forbidden and started being just another tool I could reach for. That reframe is the whole point of this article.</p>
<h2>The Types, Quickly and Honestly</h2>
<p>You need a rough map before names mean anything. There are more categories than this, but for building a React app, three matter.</p>
<p><strong>Relational (SQL)</strong> — Postgres, MySQL, SQLite. Data in tables with defined relationships. This is the default, and it should be. It&#39;s proven, it&#39;s flexible, and the guarantees are strong. When you don&#39;t have a specific reason to pick something else, pick this.</p>
<p><strong>Document (NoSQL)</strong> — MongoDB, Firestore. Data as flexible JSON-ish documents, no rigid schema up front. Genuinely useful when your data is loosely structured or you&#39;re moving fast before the shape settles. My honest take: teams reach for it to &quot;avoid schemas&quot; and later discover the schema didn&#39;t disappear, it just moved into their application code where the database can&#39;t help enforce it.</p>
<p><strong>Key-value</strong> — Redis. Dead-simple pairs, blazing fast, usually a <em>complement</em> to a real database for caching and sessions, not your source of truth.</p>
<p>If you&#39;re building a typical app and unsure, the boring answer is right: a relational database. The interesting question in 2026 isn&#39;t SQL versus NoSQL — it&#39;s what serverless did to the relational option.</p>
<h2>Why SQLite Came Back</h2>
<p>For years SQLite was filed under &quot;the little embedded database&quot; — great for a phone app or a local file, not something you&#39;d run a real web product on. That mental filing is now out of date, and it&#39;s worth understanding why, because it&#39;s the shift this whole article turns on.</p>
<p>SQLite is spectacularly simple and fast — the entire database is a file. What it historically lacked was a way to be that simple <em>and</em> live in the cloud serving a distributed app. That&#39;s the gap platforms like <strong>Turso</strong> fill: SQLite&#39;s simplicity, rebuilt for the edge, replicated close to your users, with the operational weight removed.</p>
<pre><code class="language-ts">import { createClient } from &#39;@libsql/client&#39;;

const db = createClient({
  url: process.env.DATABASE_URL!,
  authToken: process.env.DATABASE_AUTH_TOKEN,
});

// This is the whole &quot;connect to your production database&quot; story.
const policies = await db.execute(&#39;SELECT * FROM policies WHERE user_id = ?&#39;, [userId]);
</code></pre>
<p>Look at what isn&#39;t there. No pool configuration, no connection lifecycle, no server to have provisioned first. A URL and a token, and you&#39;re talking to a replicated production database. The thing that scared me — the operations — has been compressed into two environment variables. That&#39;s the serverless shift made concrete.</p>
<p>I&#39;m not claiming Turso is the only right answer; serverless Postgres options like Neon do the same trick for the Postgres world, and if your heart is set on Postgres, reach for one of those instead. The point isn&#39;t the specific vendor. It&#39;s that the operational barrier that made databases feel off-limits has genuinely fallen, whichever ecosystem you pick.</p>
<h2>Don&#39;t Write SQL Strings by Hand — Use an ORM</h2>
<p>The one place I&#39;ll push you toward a specific practice: put a typed query layer between your React-adjacent code and the raw database. In the TypeScript world that means an ORM like <strong>Drizzle</strong> or <strong>Prisma</strong>, and the reason is the same thread running through this entire series — types.</p>
<p>Raw SQL strings are stringly-typed. A typo in a column name is a runtime error you find in production. A typed ORM turns that into a compile error you find while typing.</p>
<pre><code class="language-ts">import { drizzle } from &#39;drizzle-orm/libsql&#39;;
import { eq } from &#39;drizzle-orm&#39;;

const db = drizzle(client);

// Fully typed: &#39;policies.userId&#39; is checked, the result is typed,
// a renamed column breaks the build, not production.
const rows = await db.select().from(policies).where(eq(policies.userId, userId));
//    ^? Policy[] — inferred from the schema
</code></pre>
<p>This should feel deeply familiar by now. It&#39;s the exact move from the <a href="https://ma-x.im/blog/react-playbook-backend">tRPC and server-functions articles</a>: define the shape once, let the compiler carry it everywhere. Your database schema becomes a TypeScript source of truth, the same types flow from the table into your query into your <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a> cache and finally into your component. One definition, enforced end to end, from the database row to the rendered pixel. That&#39;s the coherence this series keeps chasing, now extended all the way down to storage.</p>
<h2>Migrations Aren&#39;t Scary Either</h2>
<p>The last piece of the old fear: migrations — changing your schema without breaking the running app. This used to be hand-written SQL run carefully against production, and it&#39;s where &quot;I might delete everything&quot; lives.</p>
<p>Modern ORMs largely defuse it. You change your schema definition in TypeScript, and the tool generates the migration for you.</p>
<pre><code class="language-bash"># Change the schema in code, then:
npx drizzle-kit generate   # generates the migration from the diff
npx drizzle-kit migrate    # applies it
</code></pre>
<p>You still have to <em>think</em> — a migration that drops a column is still destructive, and no tool saves you from a bad decision. But the mechanical, error-prone part of hand-writing schema changes is handled, and the migration lives in your repo as a reviewable, version-controlled file instead of a command someone typed into a production shell and forgot.</p>
<h2>What a React Developer Should Actually Take Away</h2>
<p>Strip it down:</p>
<ul>
<li><strong>The wall was operational, not intellectual.</strong> Serverless removed the running-and-scaling part that was actually hard.</li>
<li><strong>Default to relational.</strong> Reach for document or key-value only when you have a specific reason.</li>
<li><strong>SQLite is a real option now.</strong> Turso (or serverless Postgres like Neon) gives you production data storage behind a URL and a token.</li>
<li><strong>Use a typed ORM.</strong> Drizzle or Prisma make your schema a TypeScript source of truth, and the types flow all the way to your components.</li>
<li><strong>Migrations are generated, not hand-written</strong> — but you still own the judgment.</li>
</ul>
<p>I spent too long treating the database as someone else&#39;s job, and all it did was make me dependent on someone else to ship anything real. The serverless shift didn&#39;t just make databases easier — it made them <em>mine</em>, and yours. You don&#39;t need a DBA and a provisioning ticket to store data anymore. You need a schema, a typed client, and the willingness to walk through a wall that isn&#39;t there. That, more than any single tool, is what changed.</p>
<p>With storage handled, the app needs to actually run somewhere the world can reach it. The next article is about <a href="https://ma-x.im/blog/react-playbook-hosting-railway">hosting a React app</a> — how modern platforms turned deployment from a DevOps project into a git push, and where the limits still bite.</p>
<p>If you&#39;re sitting on an app idea and the database is the part that&#39;s stopping you, that&#39;s the exact fear I&#39;m describing — tell me what you&#39;re trying to store and I&#39;ll help you pick where it goes.</p>
]]></content:encoded>
      <pubDate>Thu, 23 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Database</category>
      <category>Turso</category>
      <category>SQLite</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>SSR in React: What TanStack Start Actually Changes</title>
      <link>https://ma-x.im/blog/react-playbook-ssr-tanstack-start</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-ssr-tanstack-start</guid>
      <description>Server rendering isn&apos;t a performance checkbox — it collapses the wall between frontend and backend into one framework. Here&apos;s what SSR really buys you, why the client/server line stops being where you think it is, and where TanStack Start fits next to Next.js.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-ssr-tanstack-start/cover.png" alt="SSR in React: What TanStack Start Actually Changes" /></p>
<p>For most of this series I&#39;ve drawn a clean line: React runs in the browser, the backend runs on a server, and the <a href="https://ma-x.im/blog/react-playbook-backend">contract between them</a> is where the interesting decisions live. That line is useful. It&#39;s also about to dissolve.</p>
<p>Server-side rendering is usually sold as a performance trick — render HTML on the server so the first paint is faster and Google can read your page. That&#39;s true, and it&#39;s the least interesting thing about it. The real change is architectural: SSR takes the wall between &quot;frontend&quot; and &quot;backend&quot; and turns it into a seam <em>inside a single framework</em>. Suddenly some of your React code runs on a server, some runs in a browser, and the hardest part is knowing which is which — and why it matters.</p>
<p>So this article isn&#39;t a list of SSR benefits. It&#39;s about what actually shifts in how you think when rendering moves to the server, and where TanStack Start lands in a world Next.js has owned for years.</p>
<h2>What SSR Actually Buys You</h2>
<p>Let me be fair to the standard pitch first, because the benefits are real. Server rendering gives you a faster first contentful paint — the user sees content before a single kilobyte of JavaScript executes. It gives you SEO that actually works, because crawlers get real HTML instead of an empty <code>&lt;div id=&quot;root&quot;&gt;</code>. And it gives you a better story on slow devices, because the server does the heavy lifting once instead of every phone doing it again.</p>
<p>If that were all SSR did, it&#39;d be a performance optimization you turn on and forget. But turning it on changes the shape of your code, and that&#39;s the part nobody warns you about until you hit it.</p>
<h2>The Line Moves, and You Have to Know Where</h2>
<p>Here&#39;s the thing that trips everyone up, and it&#39;s worth slowing down on.</p>
<p>In a pure client-side app, all your code runs in one place: the browser. <code>window</code> exists. <code>localStorage</code> exists. Every component runs after the user&#39;s device has loaded it. Simple mental model.</p>
<p>The moment you add SSR, your components can run in <em>two</em> environments — once on the server to produce the initial HTML, and again (or partially) in the browser to make it interactive. And the server doesn&#39;t have <code>window</code>. It doesn&#39;t have <code>localStorage</code>. Code that assumed the browser now runs somewhere the browser&#39;s globals don&#39;t exist.</p>
<pre><code class="language-tsx">function Sidebar() {
  // Runs on the server first — where &#39;window&#39; is undefined.
  // This throws during SSR, and the error is confusing the first time.
  const collapsed = window.localStorage.getItem(&#39;sidebar-collapsed&#39;);
  return collapsed ? &lt;Collapsed /&gt; : &lt;Expanded /&gt;;
}
</code></pre>
<p>That crashes on the server, and the first time it happens the error message sends you on a goose chase, because it&#39;s failing in a place you didn&#39;t know your component ran. This is the tax of SSR: you now have to know, for every piece of code, <em>where it executes</em>. Browser-only work — reading <code>localStorage</code>, touching the DOM, using <code>window</code> — has to be deferred to after hydration, usually in an effect. It&#39;s not hard once you have the mental model. It&#39;s brutal before you do.</p>
<blockquote>
<p>With SSR, &quot;where does this run?&quot; stops being a trivia question and becomes the question. Half of SSR debugging is just answering it correctly.</p>
</blockquote>
<p>That&#39;s the real cost, and no framework fully removes it — it&#39;s inherent to running the same code in two places. What a good framework does is make the boundary <em>visible</em> instead of something you discover by crashing.</p>
<h2>Hydration: The Weird Middle Step</h2>
<p>There&#39;s a moment between &quot;server sent HTML&quot; and &quot;app is interactive&quot; that deserves a name, because it&#39;s the source of the strangest SSR bugs: hydration.</p>
<p>The server sends fully-formed HTML so the user sees content immediately. But that HTML is inert — no event handlers, no state. Then the JavaScript loads and React &quot;hydrates&quot; it: it walks the server-rendered DOM and attaches all the interactivity, expecting the DOM it finds to match the DOM it would have rendered.</p>
<p>When those two don&#39;t match — because you rendered a timestamp, or a random value, or something that read <code>window</code> — you get a hydration mismatch, and React complains. The fix is conceptual, not mechanical: anything that differs between server and client has to wait until after hydration. Once you internalize that server and client must agree on the first render, the whole class of hydration bugs stops being mysterious and starts being obvious.</p>
<h2>Where TanStack Start Comes In</h2>
<p>Everything so far is true of any SSR framework. So why TanStack Start, in a world where Next.js is the default answer?</p>
<p>Because of what this series has already built. If you&#39;ve been following along, you&#39;re using <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a> for server state and <a href="https://ma-x.im/blog/react-playbook-tanstack-router">TanStack Router</a> for type-safe routing. TanStack Start is a full-stack framework built <em>on that same router</em> — so SSR, server functions, and data loading arrive as a natural extension of tools you already use, not a new paradigm you adopt wholesale.</p>
<p>The piece that matters most is <strong>server functions</strong> — typed functions you write once and call from the client, where the network boundary between them is handled for you.</p>
<pre><code class="language-ts">import { createServerFn } from &#39;@tanstack/start&#39;;

const getPolicy = createServerFn(&#39;GET&#39;, async (id: number) =&gt; {
  // This body runs ONLY on the server — DB access is safe here.
  return db.policy.findUnique({ where: { id } });
});

// Called from a component; the type flows across the boundary,
// and &#39;db&#39; never ships to the browser.
const policy = await getPolicy(42);
//    ^? Policy — inferred end to end
</code></pre>
<p>If that feels familiar, it should — it&#39;s the <a href="https://ma-x.im/blog/react-playbook-backend">tRPC idea from the backend article</a>, folded directly into the framework. You write a function that runs on the server, call it from the client with full type safety, and the framework draws the network line for you. The <code>db</code> import never reaches the browser. The type reaches everything. That coherence — one router, one type system, server and client speaking the same language — is the reason Start fits this series so naturally.</p>
<h2>The Honest Comparison with Next.js</h2>
<p>I&#39;m not going to pretend TanStack Start dethrones Next.js. It doesn&#39;t, and picking it over Next.js is a real trade-off, not a free upgrade.</p>
<p>Next.js is mature, enormous, and battle-tested at every scale, with the deepest ecosystem in React and a hosting story that&#39;s effectively frictionless on Vercel. Its App Router and React Server Components are a genuinely different, powerful model. If your team wants the safest, most-documented, most-hireable-for choice, Next.js is it, and I&#39;d never argue someone out of it on the merits.</p>
<p>What TanStack Start offers is a different center of gravity. It&#39;s router-first and type-obsessed, and if your app is already built on TanStack&#39;s pieces, Start is the option where <em>everything agrees</em> — the same router types flow from your routes into your loaders into your server functions with no seams. Next.js asks you to adopt its model of the world; Start extends the model you already chose. For the kind of deeply-typed, client-rich application this series keeps building, that consistency is worth a lot. For a content site that wants RSC and edge rendering out of the box, Next.js is probably still the sharper tool.</p>
<p>That&#39;s the honest split: Next.js for maturity and reach, Start for coherence with a TanStack-native stack. Neither is a mistake.</p>
<h2>The Real Takeaway</h2>
<p>Strip away the framework names and here&#39;s what SSR actually asks of you:</p>
<ul>
<li><strong>Know where every piece of code runs.</strong> Server or browser is now a question you answer constantly, not never.</li>
<li><strong>Defer browser-only work past hydration.</strong> <code>window</code>, <code>localStorage</code>, the DOM — none of it exists during the server render.</li>
<li><strong>Make server and client agree on the first render.</strong> Mismatches are the price of getting this wrong.</li>
<li><strong>Let the framework draw the boundary.</strong> Server functions turn &quot;which side does this run on&quot; from a guess into a typed contract.</li>
</ul>
<p>I opened by saying the line between frontend and backend was about to dissolve, and this is what I meant. SSR doesn&#39;t move your code to the server — it makes &quot;the server&quot; and &quot;the browser&quot; two places your <em>same</em> code lives, and the skill it demands is holding both in your head at once. That&#39;s harder than it sounds and more valuable than it looks. The developers I trust most with a large React app aren&#39;t the ones who memorized a framework. They&#39;re the ones who always know where their code is running.</p>
<p>With rendering settled, the app needs somewhere to keep its data. The next article is about <a href="https://ma-x.im/blog/react-playbook-database-turso">databases for React developers</a> — the kinds, the trade-offs, and why the modern serverless options change what &quot;add a database&quot; even means.</p>
<p>If you&#39;ve fought a hydration mismatch that made no sense, or you&#39;re weighing Start against Next.js for a real project, I&#39;d like to hear the specifics — those decisions are all about your exact stack, and that&#39;s where the interesting part lives.</p>
]]></content:encoded>
      <pubDate>Mon, 20 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>SSR</category>
      <category>TanStack Start</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The Backend a React Developer Actually Needs to Understand</title>
      <link>https://ma-x.im/blog/react-playbook-backend</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-backend</guid>
      <description>You don&apos;t have to write the server. But the shape of the contract between your React app and the backend — REST, tRPC, or a BFF — decides how much of your frontend is real work versus glue code. Here&apos;s the part of the backend that&apos;s actually yours.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-backend/cover.png" alt="The Backend a React Developer Actually Needs to Understand" /></p>
<p>&quot;That&#39;s a backend concern&quot; is one of the most expensive sentences a frontend developer can say.</p>
<p>I understand the instinct. There&#39;s a clean line in most people&#39;s heads: the backend team owns the server, we own the browser, and the API is the wall between us. Someone hands you an endpoint, you fetch from it, you render. Not your problem what happens on the other side.</p>
<p>Except the shape of that wall decides how much of your job is real work versus glue code. I&#39;ve spent weeks of my life writing frontend that existed only to paper over an awkward API — flattening nested responses, stitching three calls into one screen, re-deriving fields the server could have just sent. That&#39;s not frontend work. That&#39;s tax, paid in your codebase, because of a decision made on the other side of a wall you decided wasn&#39;t yours.</p>
<p>So this article isn&#39;t about writing servers. It&#39;s about the part of the backend that&#39;s genuinely yours as a React developer: the contract. How you talk to the server — REST, tRPC, or a BFF — shapes your frontend more than most of us are willing to admit.</p>
<h2>The Contract Is the Product</h2>
<p>Here&#39;s the reframe that changed how I work: from the frontend, the backend <em>is</em> its API. You don&#39;t experience the database, the services, the queues. You experience the contract — the shape of the requests and responses. And a good contract makes the frontend small, while a bad one makes it a translation layer.</p>
<p>That&#39;s the lens for everything below. Not &quot;which technology is trendy,&quot; but &quot;which contract leaves the least incidental work in my React code.&quot; Because every gap between what the server sends and what the UI needs gets filled by <em>you</em>, in components that should be about rendering and instead are about reshaping data.</p>
<h2>REST: The Default, and Its Real Cost</h2>
<p>REST is the lingua franca. Everything speaks it, every tool supports it, and for a lot of applications it&#39;s completely fine. When someone hands me a well-designed REST API — sensible resources, predictable shapes, the fields I need where I expect them — I have no complaints. It pairs perfectly with <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a>, which was practically built for the REST request/response model.</p>
<p>The costs of REST are real but they&#39;re not &quot;REST is bad.&quot; They&#39;re two specific frictions.</p>
<p>The first is <strong>over- and under-fetching</strong>. An endpoint returns a fixed shape. Sometimes that&#39;s more than your screen needs, so you ship kilobytes you throw away. More often it&#39;s less than you need, so you make a second call, then a third, and now one screen is a waterfall of requests you have to orchestrate and cache.</p>
<p>The second, and the one that actually costs me time, is <strong>types</strong>. A REST response is JSON — untyped by nature. On the client it arrives as <code>any</code> unless you do something about it. Which means the boundary between &quot;typed React app&quot; and &quot;untyped network&quot; runs right through your data layer, and you spend effort re-establishing types the backend already knew.</p>
<pre><code class="language-ts">// The gap, made concrete:
const res = await fetch(&#39;/api/policies/42&#39;);
const policy = await res.json(); // any — the types stop here

// so you rebuild what the server already knew:
const policy = policySchema.parse(await res.json()); // Zod, by hand
</code></pre>
<p>That <code>policySchema</code> is you, manually, re-declaring a shape the backend already has in its own code. It works — I do it, and validating at the boundary with Zod is genuinely good practice. But notice what it is: the same truth, written twice, on two sides of the wall, kept in sync by discipline. Hold that thought, because the next option deletes it.</p>
<h2>tRPC: When You Own Both Sides</h2>
<p>If your backend is TypeScript, and especially if it&#39;s the same repo or monorepo as your frontend, tRPC changes the entire equation. It gives you end-to-end type safety with no schema, no code generation, no manual sync.</p>
<p>You define a procedure on the server, and the client just <em>knows</em> its types — inputs, outputs, everything — inferred straight across the boundary.</p>
<pre><code class="language-ts">// server: the procedure
const policyRouter = router({
  byId: publicProcedure
    .input(z.object({ id: z.number() }))
    .query(({ input }) =&gt; getPolicy(input.id)), // returns Policy
});

// client: fully typed, no codegen, no fetch wiring
const policy = await trpc.policy.byId.query({ id: 42 });
//    ^? Policy — inferred end to end
</code></pre>
<p>Look at what disappeared. No <code>policySchema</code> written twice. No <code>any</code> at the network boundary. Rename a field on the server and your React code fails to compile <em>before</em> you run it — the contract enforces itself. This is the same instinct the whole series keeps returning to: one source of truth, let the compiler carry it. tRPC just extends that line across the network, which is the one place REST couldn&#39;t.</p>
<p>I&#39;ll be honest about the constraints, because they&#39;re firm. tRPC needs TypeScript on both ends, and it works best when the same team owns both. It&#39;s not for a public API consumed by clients you don&#39;t control, and it&#39;s not what you reach for when a separate backend team ships you REST or GraphQL. But for the shape this series keeps building — a TypeScript frontend and backend under one roof — it removes an entire category of busywork. When I control both sides, it&#39;s my default, for the same reason I pick every other tool in this series: it makes the frontend smaller.</p>
<h2>GraphQL: The Over/Under-Fetching Answer</h2>
<p>GraphQL deserves a fair mention, because it targets REST&#39;s first friction directly. The client asks for exactly the fields it needs — no more, no less — in one request, even across what would have been several REST resources. For a complex UI assembling data from many places, that&#39;s a real answer to the waterfall problem, and the typed schema helps on the client too.</p>
<p>I reach for it less than I used to, and it&#39;s a cost-benefit call, not a dismissal. GraphQL brings its own server, its own caching model, its own operational weight. On a large product with many clients and genuinely graph-shaped data, that weight pays for itself. On the mid-size TypeScript app this series targets, tRPC gets me most of the type-safety win for a fraction of the machinery. Right tool, right scale.</p>
<h2>The BFF: A Backend That Works for the Frontend</h2>
<p>There&#39;s one pattern worth knowing by name, because at some point you&#39;ll need it and it helps to know it has one: the <strong>Backend for Frontend</strong>.</p>
<p>The situation is familiar. You don&#39;t control the backend. It&#39;s a separate team, or several, exposing APIs designed for their convenience, not your screen&#39;s. Your dashboard needs data from three services in shapes that don&#39;t match what any of them return. The naive fix is to do all that stitching in React — three calls, merge, reshape, re-derive — and now your components are a data-integration layer wearing a UI costume.</p>
<p>A BFF is a thin server layer that belongs to the frontend and sits between your React app and those messy upstream services. Its whole job is to talk to them and hand your UI exactly the shape it wants, in one call.</p>
<pre><code class="language-ts">// BFF endpoint: one call for the frontend,
// three upstream calls hidden behind it
async function getDashboard(userId: string) {
  const [profile, policies, claims] = await Promise.all([
    profileService.get(userId),
    policyService.listFor(userId),
    claimService.recentFor(userId),
  ]);
  return shapeForDashboard(profile, policies, claims); // exactly what the UI renders
}
</code></pre>
<p>The reason I like the BFF isn&#39;t just tidiness. It moves the stitching to where it belongs. Reshaping data, aggregating services, adapting an awkward upstream contract — that&#39;s backend work, and doing it in a Node BFF is cheaper, more testable, and more cacheable than doing it in React components. Your frontend goes back to rendering. And if you&#39;re already in a <a href="https://ma-x.im/blog/react-playbook-tanstack-router">TanStack Start</a> or Next.js world, you have a natural place to put a BFF without standing up a whole separate service.</p>
<p>The through-line to the whole article is right here: whenever you find your React code reshaping data, ask whether that reshaping belongs on the server instead. Usually it does.</p>
<h2>How I Actually Decide</h2>
<p>Strip it to the decision:</p>
<ul>
<li><strong>A separate team ships you REST or GraphQL</strong> → consume it with TanStack Query, validate at the boundary with Zod, and consider a BFF the moment your components start stitching.</li>
<li><strong>You own a TypeScript backend, same repo or org</strong> → tRPC. End-to-end types, no manual sync, the frontend stays small.</li>
<li><strong>Complex graph-shaped data, many clients, real scale</strong> → GraphQL earns its weight.</li>
<li><strong>You don&#39;t own the upstream and the shapes fight your UI</strong> → a BFF, so the reshaping lives on the server where it&#39;s cheap.</li>
</ul>
<p>None of this requires you to write a real backend. It requires you to have an opinion about the contract, and to push on it when it&#39;s making your frontend do work it shouldn&#39;t. That&#39;s the part that was always yours.</p>
<p>I used to think staying out of backend decisions kept my work clean. It did the opposite — it let other people&#39;s convenience become my complexity. The most leverage I&#39;ve found as a frontend developer isn&#39;t a faster component or a cleverer hook. It&#39;s a better contract, argued for early, that makes half the frontend code simply unnecessary. You don&#39;t have to build the server. You do have to care what it sends you.</p>
<p>The next article goes one layer deeper into that same server: <a href="https://ma-x.im/blog/react-playbook-ssr-tanstack-start">SSR and React with TanStack Start</a> — where &quot;frontend&quot; and &quot;backend&quot; stop being two places and start being one framework, and what that does to everything we&#39;ve built so far.</p>
<p>If you&#39;re staring at a React codebase that&#39;s mostly reshaping awkward API responses, that&#39;s usually a contract problem wearing a frontend costume — tell me what the API looks like and I&#39;ll tell you where I&#39;d put the seam.</p>
]]></content:encoded>
      <pubDate>Thu, 16 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Backend</category>
      <category>tRPC</category>
      <category>API Design</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Authentication in React: Buy It, and Where the Line Really Is</title>
      <link>https://ma-x.im/blog/react-playbook-authentication</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-authentication</guid>
      <description>Auth looks like a login form and turns out to be an architecture decision. Where do tokens live, who guards a route, what happens when the session dies mid-request? Here&apos;s why I buy auth instead of building it, and the parts that stay yours no matter what you buy.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-authentication/cover.png" alt="Authentication in React: Buy It, and Where the Line Really Is" /></p>
<p>Authentication always starts as a login form. Two fields and a button. You could build that in an afternoon, and that afternoon is exactly the trap.</p>
<p>Because the form isn&#39;t the work. The work is everything the form implies. Where does the token live once the server hands it back? How does every subsequent request prove who you are? What happens when the token expires halfway through a multi-step action the user has been filling out for ten minutes? Who decides that this route is off-limits to this person, and where does that decision live so it can&#39;t be bypassed? None of that is visible in the login form, and all of it is auth.</p>
<p>So this article isn&#39;t about building a login screen. It&#39;s about the decision underneath it — buy versus build — and about the parts that stay your responsibility no matter which way you go. Because there&#39;s a security floor here that a nice-looking form quietly hides, and getting it wrong doesn&#39;t throw an error. It just leaves a door open.</p>
<h2>The Honest Default: Don&#39;t Build It</h2>
<p>I&#39;ll put my conclusion first, because I hold it strongly: for almost every application, you should not roll your own authentication.</p>
<p>This is not the usual &quot;don&#39;t reinvent the wheel&quot; reflex. Auth is a category where the cost of a subtle mistake is not a bug — it&#39;s a breach. Password hashing done slightly wrong. A token stored somewhere a script can read it. A session that doesn&#39;t actually invalidate on logout. A password-reset flow with a timing leak. These aren&#39;t exotic; they&#39;re the standard ways homegrown auth fails, and you usually find out about them from someone you&#39;d rather not hear from.</p>
<p>Providers like Auth0, Clerk, and Supabase Auth — and the framework-native options like NextAuth/Auth.js — exist because a lot of very focused people have already made those mistakes so you don&#39;t have to. They handle the hashing, the token lifecycle, the reset flows, the MFA, the OAuth dance with Google and GitHub. Buying that isn&#39;t laziness. It&#39;s routing your effort to where your product is actually different, which is almost never &quot;we hash passwords in a special way.&quot;</p>
<p>So my real advice is narrow: <strong>use a provider, and spend your energy integrating it correctly</strong> — because the integration is where the interesting decisions still live, and where you can still get it wrong.</p>
<h2>Where Tokens Live Is the Whole Game</h2>
<p>Here&#39;s the decision that matters most on the client, and the one people get wrong most often: once you&#39;re authenticated, where does the credential live in the browser?</p>
<p>The tempting answer is <code>localStorage</code>. It&#39;s easy, it persists across tabs and reloads, and the token is right there when you need it.</p>
<p>It&#39;s also readable by any JavaScript running on your page. That&#39;s the problem. If an attacker gets a script onto your site — a compromised dependency, a bad ad, an XSS hole — <code>localStorage</code> hands them the token directly. This is the single most common serious mistake I see in hand-rolled React auth, and it looks completely fine until the day it isn&#39;t.</p>
<p>The safer model is an <strong>httpOnly cookie</strong>: the server sets it, the browser sends it automatically, and JavaScript literally cannot read it. That removes the entire class of &quot;a script stole the token&quot; attacks. The trade-off you take on in return is CSRF — because the cookie is sent automatically, you now need CSRF protection — but that&#39;s a well-understood problem with standard defenses, and it&#39;s a better problem to own than token theft.</p>
<pre><code class="language-ts">// The shape of the good answer, conceptually:
// - server sets an httpOnly, Secure, SameSite cookie on login
// - the browser attaches it to every request automatically
// - the React app never sees, stores, or touches the raw token
// - CSRF protection guards the state-changing requests
</code></pre>
<p>Notice what this does to your React code: the app stops managing tokens at all. It doesn&#39;t read them, store them, or attach them to requests. That&#39;s the goal. The less your frontend knows about the raw credential, the fewer ways it can leak one. A good provider integration leans exactly this way — and this is precisely the kind of thing you get for free by buying instead of building.</p>
<h2>Auth State Is Just State — Treat It That Way</h2>
<p>On the client, &quot;am I logged in, and who am I?&quot; is a piece of state like any other. The mistake is treating it as special and inventing a bespoke system for it, when the tools this series has already chosen handle it cleanly.</p>
<p>The current user is server state — it lives on the server, you fetch it, it can go stale. That makes it a <a href="https://ma-x.im/blog/react-playbook-data-fetching">TanStack Query</a> concern, not a hand-built context with manual loading flags.</p>
<pre><code class="language-tsx">function useCurrentUser() {
  return useQuery({
    queryKey: [&#39;currentUser&#39;],
    queryFn: fetchCurrentUser,
    staleTime: 5 * 60 * 1000,
    retry: false,
  });
}
</code></pre>
<p>Now the whole app has one honest source of truth for identity. Logging out is a cache invalidation. A 401 from any request can trigger a refetch that flips the user to logged-out everywhere at once. You didn&#39;t build an auth state machine — you reused the data layer you already have, and it behaves consistently because it&#39;s the same machinery as the rest of your data. That consistency is the point I keep coming back to in this series: identity isn&#39;t a special island, it&#39;s server state, so it uses the server-state tool.</p>
<h2>Guarding Routes: A Convenience, Not a Wall</h2>
<p>This is the part where a dangerous misconception hides, so I want to be blunt about it.</p>
<p>Protecting a route on the client — redirecting away from <code>/dashboard</code> when there&#39;s no user — is a <strong>UX feature, not a security control.</strong> With <a href="https://ma-x.im/blog/react-playbook-tanstack-router">TanStack Router</a> you can gate a route in its <code>beforeLoad</code>, and you should, because showing a logged-out person a broken dashboard is bad UX.</p>
<pre><code class="language-tsx">export const Route = createFileRoute(&#39;/dashboard&#39;)({
  beforeLoad: ({ context }) =&gt; {
    if (!context.auth.user) {
      throw redirect({ to: &#39;/login&#39; });
    }
  },
});
</code></pre>
<p>But understand exactly what that does and does not do. It stops a <em>browser</em> from <em>showing</em> a page. It does nothing to stop a <em>request</em> from <em>reaching your data</em>. Anyone can open dev tools, read your bundle, and call your API directly — the client-side guard is irrelevant to them.</p>
<blockquote>
<p>Client-side route guards decide what the UI shows. The server decides what the user can actually access. Only one of those is security.</p>
</blockquote>
<p>The real protection lives on the server: every endpoint validates the session and authorizes the specific action, every time, as if the client did not exist. The React route guard makes the app pleasant for honest users. The server check makes it safe from everyone else. If you only build one, build the server one — an app with server checks and no client guard is merely awkward; an app with client guards and no server checks is wide open.</p>
<h2>The Part No Provider Handles: The Expiry Moment</h2>
<p>Buy the provider, store the token safely, guard your routes and your server — and there&#39;s still one moment that stays yours, because it&#39;s a UX problem, not a security one: the session dies while the user is in the middle of something.</p>
<p>Tokens expire. The user was filling out a long form (the exact painful form from the <a href="https://ma-x.im/blog/react-playbook-form-libraries">forms article</a>), stepped away for coffee, came back, hit save — and the request comes back 401. What now?</p>
<p>This is where thoughtful apps separate from careless ones, and no library decides it for you. The naive answer is to hard-redirect to login and throw away everything they typed, which is how you make a user hate your product. The considered answer is a layer that catches the 401, attempts a silent token refresh if the provider supports it, retries the original request, and only if that genuinely fails preserves their work and prompts a re-login without discarding the form.</p>
<pre><code class="language-ts">// Conceptual interceptor, not a full implementation:
// 1. request returns 401
// 2. attempt a silent refresh (if the provider offers refresh tokens)
// 3. on success -&gt; retry the original request, user notices nothing
// 4. on failure -&gt; keep their unsaved work, prompt a gentle re-login
</code></pre>
<p>The provider hands you the refresh capability. What you do with it — how gracefully you handle the moment a session dies under a user&#39;s hands — is product design, and it&#39;s entirely yours. This is the through-line of the whole article: buying auth doesn&#39;t remove your judgment, it relocates it. You stop deciding how to hash a password and start deciding how it feels when a session expires. The second decision is the one your users actually experience.</p>
<h2>Where the Line Really Is</h2>
<p>So the buy-versus-build question has a cleaner answer than it looks:</p>
<ul>
<li><strong>Hashing, token issuance, OAuth, MFA, password resets</strong> → buy. This is the security floor, and the cost of building it wrong is a breach, not a bug.</li>
<li><strong>Where the token lives in the browser</strong> → yours, and httpOnly cookies over <code>localStorage</code> is close to non-negotiable.</li>
<li><strong>Auth state in the app</strong> → not special; it&#39;s server state, so it&#39;s a TanStack Query concern.</li>
<li><strong>Route guarding</strong> → client-side for UX, server-side for security, and never confuse the two.</li>
<li><strong>The expiry-mid-action moment</strong> → yours, and it&#39;s the difference between an app people trust and one they resent.</li>
</ul>
<p>I buy authentication, and I&#39;d tell you to as well — not because integrating it is trivial, but because buying it moves the hard part to where it belongs. The interesting work was never the login form. It&#39;s the token storage, the guard boundary, the graceful death of a session. That&#39;s the part that shapes the architecture, and that&#39;s the part that&#39;s still yours after you&#39;ve paid someone else for the rest.</p>
<p>The next article follows this thread straight down: <a href="https://ma-x.im/blog/react-playbook-backend">what a React developer actually needs to know about the backend</a> — REST versus tRPC versus a BFF — because how you talk to your server decides more about your frontend than most of us admit.</p>
<p>If you&#39;ve had a session expire under a user in a way that cost them real work — or if you&#39;re running <code>localStorage</code> tokens right now and want to talk through moving off them — reach out. That migration is one of the higher-leverage things you can do for an app&#39;s security, and I&#39;m happy to compare notes.</p>
]]></content:encoded>
      <pubDate>Mon, 13 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Authentication</category>
      <category>Security</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>The AI Agent Memory System I Built Was Broken — Here Is the Redesign</title>
      <link>https://ma-x.im/blog/ai-agent-memory-redesign</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-agent-memory-redesign</guid>
      <description>Append-only markdown memory works great — until it doesn&apos;t. After watching the system degrade in production, our team redesigned it from scratch with strict principles: small, high-signal, controlled, and self-maintaining.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-agent-memory-redesign/cover.png" alt="The AI Agent Memory System I Built Was Broken — Here Is the Redesign" /></p>
<p>A few weeks ago I wrote about <a href="https://ma-x.im/blog/give-your-ai-agent-a-memory">giving your AI agent a persistent memory</a> — a simple system of markdown files the agent reads and writes across sessions. I was excited about it. It worked. I rolled it out on my personal project, recommended it to colleagues, and eventually submitted a pull request adding the memory system to our main company repository.</p>
<p>That PR triggered something I did not expect.</p>
<p>A manager reached out and suggested organizing a meeting — not to approve or reject the PR, but because several people at the company had already been experimenting with similar ideas independently. Before the meeting even happened, there was a chat where people started sharing their experiences. Engineers who had been quietly running local memory systems of their own, not as a global company-wide thing, just something they had hacked together for themselves. They shared what worked and, more importantly, what did not.</p>
<p>What I heard in that chat changed how I think about this entirely.</p>
<h2>What Broke</h2>
<p>The first version of the system had one rule: append. Learned something? Append it. Fixed a bug? Append it. Session ended? Append a summary. Append, append, append.</p>
<p>This works fine for the first few days. Then the files get long. Then they get contradictory. Then they become a liability.</p>
<p>One story from the chat stuck with me. A colleague had been working in a long session — the kind where you get deep into something and lose track of time. The agent was faithfully logging each action to a sessions file, just as instructed. Then midway through, he asked it to do something simple. Small task. Nothing complicated.</p>
<p>The agent picked up the session logs, scanned the history, and concluded that several things from earlier in the session had not been completed yet. So it started doing them. All of them. On top of the actual task. Everything tangled together. The agent was trying to be helpful by using the context it had been given — and it made a mess precisely because of that context.</p>
<p>He rolled everything back and started a fresh session. Simple task, no history, done in two minutes.</p>
<p>That story captures the failure mode better than any metric I could give you. The agent did exactly what you told it to do. It dutifully loaded the history, applied it to the task, and produced chaos. The problem was not the agent. The problem was that we had built a system that grew without any mechanism for forgetting.</p>
<p>Here is what we also observed across different setups:</p>
<p><strong>Context explosion.</strong> <code>lessons.md</code> hit 200 lines in three weeks. The agent was loading all of it into context on every session. Most of it was stale or irrelevant to the current task. We were paying context budget for noise.</p>
<p><strong>Drift and contradictions.</strong> Decisions change. The agent would append a new decision without removing the old one. Now both exist. Which is current? Depends on where in the file the agent looks.</p>
<p><strong>Entropy wins.</strong> The longer a system runs without maintenance discipline, the worse it gets. Append-only systems drift toward entropy by design.</p>
<p>We realized the core mistake: <strong>we treated memory like a log, not like a knowledge base.</strong></p>
<h2>The Redesign Principles</h2>
<p>The meeting itself was candid in a way that work meetings rarely are. People with months of hands-on experience with these systems, and everyone had the same conclusion: the idea is right, but the implementation costs too much to maintain. The systems degraded. They required manual cleanup. They occasionally made things worse. The good news was that everyone had independently arrived at similar fixes — which meant the fixes were probably right.</p>
<p>The rewrite started with constraints rather than architecture. We asked: what properties does memory need to have to not degrade?</p>
<p><strong>Memory is not a log.</strong> Do not store what happened. Store what matters. A session where nothing changed — nothing gets written.</p>
<p><strong>If memory grows continuously, it is a bug.</strong> Not a feature, not normal accumulation. A symptom of missing update rules.</p>
<p><strong>Overwrite, do not append.</strong> When knowledge changes, replace the old entry. The file should represent current state, not a history of states.</p>
<p><strong>Load index first, fetch content lazily.</strong> The agent should never blindly load all memory into context. It should load a small index, decide what is relevant, then fetch only that.</p>
<p><strong>One source of truth per topic.</strong> No two entries should describe the same thing differently. If they do, one is wrong and should be removed.</p>
<h2>The New File Structure</h2>
<pre><code>.ai-memory/
  index.md          — What exists and when to load it
  state.md          — Current project state (short, always overwritten)
  decisions.md      — Architecture and product decisions (overwritten, not appended)
  facts.md          — Stable technical facts (rarely changes)
  tasks.md          — Active tasks only (no completed tasks)
</code></pre>
<p>Five files. No sessions folder. No history.</p>
<p>Notice what is gone: the sessions directory. We removed it. It was generating the most noise for the least value. If you need an audit trail, use git — that is what git is for. Memory is not git.</p>
<h3>index.md</h3>
<p>This is the only file the agent loads unconditionally at the start of a session. Everything else is fetched on demand.</p>
<pre><code class="language-markdown"># Memory Index

## Always load
- state.md — current project state, active focus

## Load when relevant
- decisions.md — when making architecture or tech stack decisions
- facts.md — when working with the API, data schema, or deployment
- tasks.md — when planning work or checking what is in progress

## Topics → files
- auth flow → facts.md#authentication
- API endpoints → facts.md#api
- styling rules → decisions.md#styling
- current sprint → tasks.md
</code></pre>
<p>The index is a routing table. The agent reads it, decides what it needs, loads only that.</p>
<h3>state.md</h3>
<p>One file, always overwritten. Never appended.</p>
<pre><code class="language-markdown"># Current State

Updated: 2026-04-11

## Active focus
Redesigning the memory system after observing degradation in production.
Building the hardened v2. Targeting minimal file count and no append logic.

## Recent context
- Completed: initial audit of degraded memory files
- Completed: team discussion and principles alignment
- In progress: implementing new structure across active projects

## Known blockers
None currently.
</code></pre>
<p>This is a snapshot, not a timeline. Next session, the agent rewrites it. Old state disappears. That is by design.</p>
<h3>decisions.md</h3>
<p>Decisions are updated in place. When a decision changes, the entry is rewritten — not a new entry added below.</p>
<pre><code class="language-markdown"># Decisions

## Styling approach
**Decision:** Tailwind CSS utility classes only.
**Reasoning:** Utility-first is fast for a small team, eliminates CSS naming arguments.
**Updated:** 2026-03-10

## Blog post storage
**Decision:** TypeScript files with markdown in template literals.
**Reasoning:** Type-safe content, no CMS needed, works with SSG.
**Updated:** 2026-03-15
</code></pre>
<p>Note the <code>Updated</code> field on every entry. This is the anti-drift mechanism. When you see a date from six months ago, you know to verify it is still current. When you update a decision, you update the date. Stale dates are a signal to review.</p>
<h3>facts.md</h3>
<p>Stable technical facts that rarely change. API shapes, schema, deployment configuration, environment behavior. Written once, updated when things change.</p>
<pre><code class="language-markdown"># Facts

## Authentication
- JWT tokens, 15-minute expiry
- Refresh tokens stored in httpOnly cookies
- Endpoint: POST /api/auth/refresh

## Deployment
- Static export via GitHub Actions
- Deployed to GitHub Pages
- Custom domain: ma-x.im
- Build command: next build (output: export)

## Data schema
- Blog posts: TypeScript files in src/data/posts/
- Each post exports { post: BlogPost }
- Registered in src/data/posts/index.ts
</code></pre>
<h3>tasks.md</h3>
<p>Active tasks only. Completed tasks are deleted, not archived.</p>
<pre><code class="language-markdown"># Active Tasks

## In progress
- Implement hardened memory structure across ma-x.im project

## Blocked
(none)

## Next
- Write documentation for the new structure
- Review and prune decisions.md across older projects

---
No completed tasks in this file. Completed = deleted.
</code></pre>
<p>This is a working board, not a history. Once a task is done, it disappears.</p>
<h2>Update Rules: When to Write and When to Stay Silent</h2>
<p>This was the most important thing we formalized. The first system had vague rules: &quot;record things when appropriate.&quot; Agents interpreted this as &quot;record everything always.&quot; The new rules are explicit about silence:</p>
<p><strong>Write when:</strong></p>
<ul>
<li>A decision was made that did not exist before</li>
<li>An existing decision was changed</li>
<li>A new stable fact was discovered</li>
<li>The current focus shifted significantly</li>
<li>A bug was encountered that reveals a non-obvious constraint</li>
</ul>
<p><strong>Do NOT write when:</strong></p>
<ul>
<li>You completed a routine task (the task file handles this, no memoir needed)</li>
<li>You repeated something already in memory</li>
<li>The session produced no new knowledge</li>
<li>You would only be adding a timestamp without changing content</li>
</ul>
<p><strong>Overwrite rules:</strong></p>
<ul>
<li><code>state.md</code> — always overwrite entirely</li>
<li><code>tasks.md</code> — remove completed tasks, add new ones</li>
<li><code>decisions.md</code> — edit the existing entry, update the date</li>
<li><code>facts.md</code> — edit in place, same as decisions</li>
</ul>
<p>If you cannot identify which existing entry to update — stop and check if the new information belongs in memory at all.</p>
<h2>Consolidation: What We Called &quot;Dreaming&quot;</h2>
<p>One thing we kept from the meeting discussion was the idea of an explicit consolidation step. Call it memory maintenance, pruning, or — more evocatively — dreaming. The agent is not doing real work during this step. It is reviewing its own memory for quality.</p>
<p>The consolidation prompt looks like this:</p>
<pre><code class="language-markdown">Review all memory files. For each file:
1. Remove entries that are no longer current or relevant
2. Merge entries that describe the same thing
3. Resolve contradictions — keep the most recent or most accurate entry
4. Compress verbose entries into concise ones
5. Check that facts.md and decisions.md have Updated dates
6. Verify tasks.md contains only active tasks

Do not add new information during consolidation.
Output a summary of what was changed and why.
</code></pre>
<p>Run this manually every two to four weeks, or when memory files feel like they are growing again. It takes a few minutes and resets the quality level. Think of it as code review, but for your agent&#39;s knowledge.</p>
<h2>Loading Strategy: Index First, Lazy Fetch</h2>
<p>The old system: agent reads everything, every session. Expensive and noisy.</p>
<p>The new system: agent reads <code>index.md</code> first, then decides what to load.</p>
<p>Pseudo-logic the agent follows:</p>
<pre><code>session_start:
  load index.md
  load state.md  (always — it is small and always relevant)

  if task involves technology decisions:
    load decisions.md
  
  if task involves API, schema, or deployment:
    load facts.md
  
  if task involves planning or sprint work:
    load tasks.md

session_end:
  if state has changed → overwrite state.md
  if a decision was made or changed → update decisions.md
  if new stable fact was discovered → update facts.md
  if tasks changed → update tasks.md
  if nothing materially changed → write nothing
</code></pre>
<p>The index also contains a topic-to-file routing table. If the task is about authentication, the agent looks up &quot;auth&quot; in the index, sees it maps to <code>facts.md#authentication</code>, and loads only that section if the file supports section loading.</p>
<h2>Good Memory vs Bad Memory</h2>
<p>Here is what the degraded system looked like, and what the hardened version looks like for the same information.</p>
<p><strong>Bad — lessons.md after three weeks:</strong></p>
<pre><code class="language-markdown">- Fixed a bug with template literals and backticks - need to escape them
- Tailwind is our CSS approach
- Remember to update the blog index when adding posts
- Spent 2 hours debugging the sitemap — turned out to be a missing trailing slash
- The agent keeps using CSS modules even though we use Tailwind — told it again
- Fixed template literal backtick issue again in a different file
- Reminder: blog posts need to be registered in index.ts
- Auth tokens expire after 15 min — need refresh logic
- AGAIN: escape backticks in template literals
</code></pre>
<p>Three separate entries about the same backtick bug. Two reminders about the blog index. This is what append-only looks like in practice.</p>
<p><strong>Good — facts.md with a single consolidated entry:</strong></p>
<pre><code class="language-markdown">## Blog post authoring
- Posts are TypeScript files in src/data/posts/, registered in index.ts
- Content is a template literal — backticks inside content must be escaped as \`
- Dates use format: &quot;Apr 11, 2026&quot;
</code></pre>
<p>One entry. Everything relevant. Zero repetition.</p>
<h2>Honest Trade-offs</h2>
<p>This system is better, but it is not free:</p>
<p><strong>You lose history.</strong> There is no record of what was decided and then changed. If you care about that trail, decisions.md has <code>Updated</code> dates, but it does not track the previous value. For audit purposes, commit your memory files to version control — git gives you the history.</p>
<p><strong>Consolidation requires effort.</strong> The dreaming step will not happen automatically unless you explicitly schedule it. Without it, even this system will degrade eventually — just much more slowly.</p>
<p><strong>The agent still needs good instructions.</strong> Vague instructions produce vague memory behavior. The more specific your rules (especially the &quot;do NOT write&quot; cases), the more disciplined the output.</p>
<p><strong>Index routing is manual.</strong> The topic-to-file mappings in <code>index.md</code> need to be maintained by hand. A stale index routes the agent to the wrong file. Keep it accurate.</p>
<h2>Summary</h2>
<p>I submitted that PR thinking I was sharing something solid. The pre-meeting chat humbled me a little — people had hit the same walls I had not hit yet, they just had not written about it publicly.</p>
<p>The honest conclusion from all of that experience: the naive version works until it does not, and when it stops working it does so quietly. You do not get an error. You get an agent that does almost the right thing, confidently, based on stale context you forgot was there. That is harder to debug than a crash.</p>
<p>The hardened version applies one core idea: <strong>memory should represent current knowledge, not accumulated history.</strong> Everything else follows from that — overwrite instead of append, index-based loading instead of bulk dumps, explicit silence rules, no sessions folder.</p>
<p>The agent does not need a journal. It needs a working knowledge base that stays reliable over time.</p>
<p>Build that instead.</p>
]]></content:encoded>
      <pubDate>Sat, 11 Apr 2026 00:00:00 GMT</pubDate>
      <category>AI</category>
      <category>Architecture</category>
      <category>DX</category>
      <category>Workflow</category>
    </item>
    <item>
      <title>Charts in React: Who Renders the Pixels</title>
      <link>https://ma-x.im/blog/react-playbook-chart-libraries</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-chart-libraries</guid>
      <description>&quot;Just use a chart library&quot; hides a real decision. Do you want charts handed to you, or do you want to control every pixel? That choice — and your data size — decides between Recharts, visx, and raw D3. Here&apos;s how I pick, and where charts actually get hard.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-chart-libraries/cover.png" alt="Charts in React: Who Renders the Pixels" /></p>
<p>The chart looked perfect in the demo. Clean line, smooth animation, a tooltip that felt expensive. Then real data showed up — forty thousand points instead of the twenty I&#39;d tested with — and the tab froze solid every time you hovered.</p>
<p>That&#39;s the moment you learn charts aren&#39;t a UI problem. They&#39;re a rendering problem wearing a UI costume.</p>
<p>&quot;Just use a chart library&quot; is the advice everyone gives, and it&#39;s not wrong, exactly. It&#39;s just hiding the actual decision. There are a dozen chart libraries and they are not interchangeable. Picking one commits you to a rendering model, a customization ceiling, and a performance profile — and you usually discover all three at the worst possible time, which is after the design changes or the data grows.</p>
<p>So this article is about how I actually choose, and the question underneath the choice is the same one that decided the <a href="https://ma-x.im/blog/react-playbook-ui-libraries">UI library article</a> and the <a href="https://ma-x.im/blog/react-playbook-form-libraries">forms article</a>: who owns the thing being rendered — the library, or me?</p>
<h2>The Two Questions That Decide Everything</h2>
<p>Before any library names, two questions do most of the sorting.</p>
<p><strong>How much do you need to control the visuals?</strong> A dashboard with standard bar and line charts is a different problem from a bespoke visualization no chart library has a preset for. The first wants defaults. The second wants primitives.</p>
<p><strong>How big is your data, and how interactive is it?</strong> A hundred points rendered as SVG is nothing. A hundred thousand points, hovering and zooming, is where SVG dies and you need Canvas or WebGL. This one question quietly eliminates half the options before you&#39;ve read their docs.</p>
<p>Everything below is really just these two axes: control vs done-for-you, and data size vs interactivity. Hold them in your head and the landscape stops looking like a dozen equivalent choices.</p>
<h2>Recharts: The Sensible Default</h2>
<p>For the majority of what people actually build — dashboards, admin panels, the CRM-shaped screens this series keeps returning to — Recharts is where I start. It&#39;s React-native and composable: charts are components, and you build them the way you build the rest of your UI.</p>
<pre><code class="language-tsx">import { LineChart, Line, XAxis, YAxis, Tooltip } from &#39;recharts&#39;;

function RevenueChart({ data }: { data: MonthlyRevenue[] }) {
  return (
    &lt;LineChart width={640} height={320} data={data}&gt;
      &lt;XAxis dataKey=&quot;month&quot; /&gt;
      &lt;YAxis /&gt;
      &lt;Tooltip /&gt;
      &lt;Line type=&quot;monotone&quot; dataKey=&quot;revenue&quot; stroke=&quot;#22c55e&quot; /&gt;
    &lt;/LineChart&gt;
  );
}
</code></pre>
<p>Look at what that is: no imperative setup, no lifecycle wiring, no D3 selections. Axes are components. The line is a component. You compose a chart the way you compose a form or a page, and it fits a React codebase without friction. For standard chart types it&#39;s readable, quick, and easy to hand to another developer.</p>
<p>The ceiling is real, though, and worth naming. Recharts is great until your design asks for something outside its component vocabulary — a custom axis behavior, an unusual interaction, a shape it doesn&#39;t ship. Then you&#39;re either fighting its internals or reaching for something lower-level. For 80% of dashboards that ceiling is far above what you need. For the other 20%, you feel it early.</p>
<p>There are close siblings here — Nivo, Victory, react-chartjs-2 — and I won&#39;t pretend to rank them precisely. Nivo&#39;s defaults are beautiful and it has a Canvas escape hatch for larger datasets. Chart.js (via react-chartjs-2) renders to Canvas out of the box, which helps with volume but makes it feel less React-native. They cluster in the same place: high-level, done-for-you, great until the design goes off-script.</p>
<h2>visx: Primitives, Not Charts</h2>
<p>When the design <em>does</em> go off-script — when you&#39;re building a visualization rather than picking a chart — visx is what I reach for.</p>
<p>visx (from Airbnb) is a set of low-level primitives built on D3&#39;s math, wrapped so React owns the rendering. D3 does the scales, the shapes, the layout calculations; React draws the actual SVG. You don&#39;t get a <code>&lt;BarChart&gt;</code>. You get scales, axes, shapes, gradients — the pieces — and you assemble the exact chart you want.</p>
<pre><code class="language-tsx">import { scaleLinear } from &#39;@visx/scale&#39;;
import { Bar } from &#39;@visx/shape&#39;;

const xScale = scaleLinear({ domain: [0, maxValue], range: [0, width] });

function CustomBars({ data }: { data: Point[] }) {
  return data.map((d) =&gt; (
    &lt;Bar key={d.id} x={0} y={yScale(d.label)} width={xScale(d.value)} height={18} /&gt;
  ));
}
</code></pre>
<p>This is the same move as Radix and shadcn/ui from the earlier articles: the library gives you behavior and math, and hands the pixels back to you. The cost is honest — you write more, you think about scales and margins yourself, and there&#39;s no &quot;just drop in a chart.&quot; The payoff is that there&#39;s no ceiling. If you can describe it, you can draw it, and React stays in control of every element.</p>
<p>I don&#39;t reach for visx for a revenue line chart. That would be building a table saw to cut one board. I reach for it when the visualization <em>is</em> the product feature, not a readout beside it.</p>
<h2>Raw D3: Powerful, and at Odds with React</h2>
<p>D3 is the foundation under most of this, and you can absolutely use it directly. I mostly don&#39;t, and the reason is architectural, not a knock on D3.</p>
<p>D3 wants to own the DOM. Its whole model is selecting elements and mutating them imperatively — enter, update, exit. React also wants to own the DOM, through its virtual model. Point them at the same nodes and you get two systems fighting over who&#39;s in charge, which shows up as subtle bugs where React re-renders away what D3 just drew, or vice versa.</p>
<p>The clean pattern is to split the roles: <strong>let D3 do the math, let React do the rendering.</strong> Use D3 for scales, layouts, and path generators — pure functions that take data and return numbers or SVG path strings — and let React turn those into elements. That&#39;s exactly what visx formalizes, which is why I&#39;d rather use visx than wire that boundary by hand. If you do reach for raw D3 inside React, keep it to the calculation layer and let React render. The moment D3 starts calling <code>.append()</code> on nodes React manages, you&#39;ve bought yourself a class of bugs that&#39;s genuinely hard to reason about.</p>
<h2>The Thing Nobody Warns You About: SVG vs Canvas</h2>
<p>Here&#39;s the constraint that ambushed me in the opening story, and it&#39;s independent of which library you pick.</p>
<p>Most chart libraries render to SVG, and SVG is wonderful — every point is a DOM node you can style, inspect, and attach handlers to. Until there are too many of them. Ten thousand DOM nodes is a slow page. Fifty thousand is a frozen one. The browser simply wasn&#39;t built to keep that many elements interactive.</p>
<p>Past that threshold you need <strong>Canvas</strong> (one element, drawn imperatively, no per-point DOM) or <strong>WebGL</strong> for the truly massive. This changes what you&#39;re allowed to pick. If you know up front you&#39;re plotting a hundred thousand time-series points with hover and zoom, that fact chooses your library before any aesthetic preference does — you filter to the ones with a Canvas renderer and decide from there. Choosing a beautiful SVG library and <em>then</em> discovering the data size is how you end up rewriting the chart layer under deadline.</p>
<p>So I&#39;ve learned to ask the data-size question first, not last. It&#39;s the least glamorous input and the most decisive one.</p>
<h2>How I Actually Decide</h2>
<p>Strip it down and it&#39;s short:</p>
<ul>
<li><strong>Standard dashboard charts, reasonable data size</strong> → Recharts. React-native, composable, done. This is most cases.</li>
<li><strong>Beautiful defaults, occasionally large data</strong> → Nivo, for its presets and Canvas option.</li>
<li><strong>A custom visualization that&#39;s a real product feature</strong> → visx. Own the pixels, no ceiling.</li>
<li><strong>Genuinely huge datasets, heavy interaction</strong> → a Canvas/WebGL renderer, and let data size lead the choice.</li>
<li><strong>Raw D3</strong> → for the math, behind a React rendering layer. Rarely for the DOM directly.</li>
</ul>
<p>For the Playbook, Recharts is the default and visx is the escape hatch — the same shape as every other choice in this series. Start with the composable, React-native option that agrees with the rest of the stack; drop to primitives only when the design demands it.</p>
<p>But I want to end where the real difficulty lives, because it isn&#39;t the library. The hard part of a chart is never the rendering — it&#39;s the data. Getting your series into the exact shape the chart wants, aggregating and bucketing correctly, choosing the visualization that answers the actual question instead of the one that looks impressive. A library renders whatever you hand it. It has no opinion on whether the chart is honest, or clear, or answering the right question. That part is yours, and no dependency will do it for you.</p>
<p>Data going in, data coming out — we&#39;ve covered both. The next article turns to something every real app needs and everyone underestimates: <a href="https://ma-x.im/blog/react-playbook-authentication">authentication</a>. Rolling your own versus buying it, and where that decision quietly reshapes your whole architecture.</p>
<p>If you&#39;ve hit the SVG performance wall, or fought D3 and React over the same DOM, I&#39;d like to hear how it went — those are the war stories where the interesting trade-offs actually surface, and I&#39;m always collecting the ones I haven&#39;t run into yet.</p>
]]></content:encoded>
      <pubDate>Thu, 09 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Data Visualization</category>
      <category>Charts</category>
      <category>Recharts</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Forms in React: Where TanStack Form Earns Its Place</title>
      <link>https://ma-x.im/blog/react-playbook-form-libraries</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-form-libraries</guid>
      <description>Forms are where clean React apps quietly fall apart. Conditional fields, async validation, a submit button that has to know about everything — it adds up fast. Here&apos;s why I reach for TanStack Form, where it beats Formik and React Hook Form, and where a form library still can&apos;t save you.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-form-libraries/cover.png" alt="Forms in React: Where TanStack Form Earns Its Place" /></p>
<p>Every codebase has one file everyone is a little afraid of. In the apps I&#39;ve worked on, it&#39;s usually a form.</p>
<p>Not a login form. Those are fine. I mean the real one — the checkout, the policy application, the onboarding flow with the field that only appears when you pick &quot;business account,&quot; which itself unlocks a tax ID field, which has to be validated against a server, but only after the user stops typing, and only if the country is one of four. That form. The one where every new requirement lands as another <code>if</code> somewhere in a 600-line component.</p>
<p>Forms are where clean React quietly falls apart. Everywhere else you can lean on good structure and it holds. Forms fight back. They mix local state, async work, cross-field logic, and user timing into one knot, and the naive version — a pile of <code>useState</code> and a submit handler that reads all of them — works right up until the requirements get real.</p>
<p>So this article is about the tool I reach for when a form stops being trivial, and, just as importantly, about the moment you actually need one. Because reaching for a form library too early is its own mistake.</p>
<h2>When You Don&#39;t Need a Form Library</h2>
<p>Let me start against my own topic. A lot of forms don&#39;t need a library at all.</p>
<pre><code class="language-tsx">function NewsletterSignup() {
  const [email, setEmail] = useState(&#39;&#39;);

  return (
    &lt;form onSubmit={handleSubmit}&gt;
      &lt;input value={email} onChange={(e) =&gt; setEmail(e.target.value)} /&gt;
      &lt;button type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>That&#39;s a form. It&#39;s fine. One field, no cross-field rules, validation you can do in a single line. Adding a library here buys you nothing but an import. I&#39;ve seen people install a form framework for a search box, and it always reads like insecurity — reaching for structure the problem doesn&#39;t have yet.</p>
<p>The line I use is simple: <strong>the moment fields start depending on each other, or validation has to talk to a server, or the same form has more than a handful of inputs, the hand-rolled version starts costing more than it saves.</strong> Below that line, <code>useState</code> is the right answer. Above it, you want a real tool. Most interesting forms sit above it.</p>
<h2>What Actually Makes Forms Hard</h2>
<p>Before the library, it&#39;s worth naming what we&#39;re fighting, because the pain isn&#39;t &quot;typing out inputs.&quot; It&#39;s four things that stack:</p>
<ul>
<li><strong>State that spreads.</strong> Every field is state, but so is <em>touched</em>, <em>dirty</em>, <em>validating</em>, <em>error</em>, <em>submitting</em>. Multiply by twenty fields.</li>
<li><strong>Cross-field logic.</strong> Field B is required only if field A has a value. Field C&#39;s options depend on field D. The rules form a little graph.</li>
<li><strong>Async validation.</strong> &quot;Is this username taken?&quot; means a server round-trip, debounced, cancelable, with a pending state the UI has to show.</li>
<li><strong>Re-render cost.</strong> The naive approach lifts everything to one state object, so one keystroke re-renders the entire form. On a big form you feel it.</li>
</ul>
<p>A good form library is really a tool for managing that specific mess. That&#39;s the lens I judge them through — not &quot;does it render inputs,&quot; but &quot;does it make those four things smaller.&quot;</p>
<h2>Why TanStack Form</h2>
<p>There are three names worth taking seriously: Formik, React Hook Form, and TanStack Form. I&#39;ll be fair to all of them, because none is a bad tool.</p>
<p><strong>Formik</strong> was the default for years. It&#39;s stable and widely known. But its model re-renders broadly by default, and its TypeScript story shows its age — types feel bolted on rather than designed in. On a large, dynamic form you end up working around it.</p>
<p><strong>React Hook Form</strong> is genuinely good, and if you&#39;re happy with it I won&#39;t try to talk you out of it. It&#39;s fast, because it leans on uncontrolled inputs and refs, and it&#39;s the right answer for a lot of teams. My hesitation is narrow: that ref-based, uncontrolled model gets awkward exactly where my forms get hard — deeply dynamic fields, tightly controlled components, cross-field reactivity. It optimizes for the common case in a way that can fight the uncommon one.</p>
<p><strong>TanStack Form</strong> is the one I default to in this series, and the reason is consistency more than superiority. It&#39;s type-safe to the core — your field names, values, and errors are all inferred, so renaming a field is a compiler problem, not a runtime surprise. It&#39;s granularly reactive: you subscribe to the exact slice of form state a component cares about, and only that component re-renders. And it&#39;s headless and framework-agnostic, which means it makes no styling decisions and hands control back to you — the same principle that decided the <a href="https://ma-x.im/blog/react-playbook-ui-libraries">UI library article</a>.</p>
<p>If you&#39;ve followed this series, that reactivity model should feel familiar. It&#39;s the same instinct behind the <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">Reatom choice</a>: fine-grained subscriptions instead of one big blob that re-renders on every change. A form is just reactive state with a lot of edges, and TanStack Form treats it that way.</p>
<h2>The Shape of It</h2>
<p>Here&#39;s the same policy form, the kind this series keeps circling, wired with TanStack Form.</p>
<pre><code class="language-tsx">import { useForm } from &#39;@tanstack/react-form&#39;;

function PolicyApplication() {
  const form = useForm({
    defaultValues: {
      accountType: &#39;personal&#39;,
      holderName: &#39;&#39;,
      taxId: &#39;&#39;,
    },
    onSubmit: async ({ value }) =&gt; {
      await submitApplication(value);
    },
  });

  return (
    &lt;form
      onSubmit={(e) =&gt; {
        e.preventDefault();
        form.handleSubmit();
      }}
    &gt;
      &lt;form.Field name=&quot;holderName&quot;&gt;
        {(field) =&gt; (
          &lt;input
            value={field.state.value}
            onBlur={field.handleBlur}
            onChange={(e) =&gt; field.handleChange(e.target.value)}
          /&gt;
        )}
      &lt;/form.Field&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>The verbosity is real, and I won&#39;t pretend otherwise — the render-prop <code>Field</code> component is more ceremony than React Hook Form&#39;s <code>register</code>. But look at what you get for it: <code>field.state.value</code> is typed from <code>defaultValues</code>, <code>name=&quot;holderName&quot;</code> is checked against the same shape, and everything inside that <code>Field</code> re-renders in isolation. The ceremony is buying type safety and surgical re-renders. On a small form that&#39;s a bad trade. On the 600-line one, it&#39;s the whole point.</p>
<h2>Conditional Fields Without the Spaghetti</h2>
<p>This is the part that sold me. Remember the &quot;business account unlocks a tax ID field&quot; requirement — the one that becomes an <code>if</code> in a naive form. Here it&#39;s just reading one field&#39;s value to decide whether to render another.</p>
<pre><code class="language-tsx">&lt;form.Subscribe selector={(state) =&gt; state.values.accountType}&gt;
  {(accountType) =&gt;
    accountType === &#39;business&#39; ? (
      &lt;form.Field
        name=&quot;taxId&quot;
        validators={{
          onChange: ({ value }) =&gt;
            value.length === 0 ? &#39;Tax ID is required for business accounts&#39; : undefined,
        }}
      &gt;
        {(field) =&gt; (
          &lt;input
            value={field.state.value}
            onChange={(e) =&gt; field.handleChange(e.target.value)}
          /&gt;
        )}
      &lt;/form.Field&gt;
    ) : null
  }
&lt;/form.Subscribe&gt;
</code></pre>
<p>Two things matter here. First, <code>form.Subscribe</code> with a <code>selector</code> means only <em>this</em> branch re-renders when <code>accountType</code> changes — the rest of the form doesn&#39;t flinch. Second, the validation rule lives <em>on the field that owns it</em>, not in a giant central schema that has to know about every conditional. The tax-ID rule exists only when the tax-ID field exists. When the field unmounts, its rule goes with it. That co-location is what keeps dynamic forms from rotting.</p>
<h2>Async Validation That Doesn&#39;t Fight You</h2>
<p>The &quot;is this taken?&quot; problem is where hand-rolled forms accumulate the most incidental complexity — debouncing, cancellation, a pending flag, a race condition you didn&#39;t see coming. TanStack Form has a slot for exactly this.</p>
<pre><code class="language-tsx">&lt;form.Field
  name=&quot;username&quot;
  validators={{
    onChangeAsyncDebounceMs: 400,
    onChangeAsync: async ({ value }) =&gt; {
      const taken = await checkUsernameTaken(value);
      return taken ? &#39;That username is already in use&#39; : undefined;
    },
  }}
&gt;
  {(field) =&gt; (
    &lt;&gt;
      &lt;input
        value={field.state.value}
        onChange={(e) =&gt; field.handleChange(e.target.value)}
      /&gt;
      {field.state.meta.isValidating &amp;&amp; &lt;span&gt;Checking…&lt;/span&gt;}
      {field.state.meta.errors[0] &amp;&amp; &lt;span&gt;{field.state.meta.errors[0]}&lt;/span&gt;}
    &lt;/&gt;
  )}
&lt;/form.Field&gt;
</code></pre>
<p>The library owns the parts that are easy to get subtly wrong: the debounce, the in-flight tracking via <code>isValidating</code>, cancelling a stale check when the value changes again. You own the one thing that&#39;s actually your business logic — the <code>checkUsernameTaken</code> call. That&#39;s the right division of labor. I don&#39;t want to write another debounced-async-with-cancellation dance by hand; I&#39;ve written it enough times to know I&#39;ll get the edge case wrong.</p>
<h2>Let the Schema Do the Talking</h2>
<p>Inline validators are fine for one-off rules, but real forms usually already have a schema — and TanStack Form is happy to defer to it. Point a field&#39;s validator at a Zod schema and the types and the rules come from one source.</p>
<pre><code class="language-tsx">import { z } from &#39;zod&#39;;

const holderName = z.string().min(2, &#39;Name is too short&#39;);

&lt;form.Field name=&quot;holderName&quot; validators={{ onChange: holderName }}&gt;
  {(field) =&gt; (/* … */)}
&lt;/form.Field&gt;
</code></pre>
<p>Now the validation rule and the TypeScript type live in the same place, and neither can drift from the other. This is the same instinct as everything else in the series: one source of truth, let the compiler enforce it. A form is often where a Zod schema you already have — for your API payload — gets a second job validating the UI. Reuse it. Don&#39;t write the shape twice.</p>
<h2>Where a Form Library Still Can&#39;t Save You</h2>
<p>I want to end honestly, because it&#39;s easy to oversell this. A form library manages state, validation, and re-renders. It does not manage <em>product complexity</em>, and most painful forms are painful because the product is.</p>
<p>Multi-step wizards with a back button that has to preserve state. Fields whose valid options depend on an API call that depends on another field. Save-as-draft that has to serialize a half-finished form. A submit that&#39;s really five API calls with partial-failure handling. TanStack Form gives you a clean foundation for all of it — but the wizard&#39;s flow, the dependency graph, the draft format, the failure strategy are yours to design. The library keeps the state honest. It doesn&#39;t tell you what the state should be.</p>
<p>That&#39;s the realistic version. The right tool removes the incidental pain — the re-renders, the type drift, the debounce dance — so that what&#39;s left is the <em>essential</em> pain, the actual product logic. That&#39;s the most any library can do, and it&#39;s a lot. A form that only hurts where the problem is genuinely hard is a form you can live with.</p>
<p>For the Playbook, TanStack Form is the choice, for the same reason shadcn/ui and Reatom were: it&#39;s headless, type-safe, and granularly reactive, so it points the same direction as everything else we&#39;ve built. Coherence over any single feature.</p>
<p>Forms handle the data going <em>in</em>. The next article is about making sense of data coming <em>out</em> — <a href="https://ma-x.im/blog/react-playbook-chart-libraries">visualization and charting in React</a>, and why &quot;just use a chart library&quot; is a much bigger decision than it sounds.</p>
<p>If you&#39;ve got a form in your codebase that everyone tiptoes around, I&#39;d genuinely like to hear what makes it hard — the specific requirement that broke the clean version. That&#39;s usually where the interesting design problems live, and I&#39;m always looking for the ones I haven&#39;t hit yet.</p>
]]></content:encoded>
      <pubDate>Mon, 06 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Forms</category>
      <category>TanStack Form</category>
      <category>Validation</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>React UI Libraries: Who Owns the Styling Decides Everything</title>
      <link>https://ma-x.im/blog/react-playbook-ui-libraries</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-ui-libraries</guid>
      <description>Picking a component library feels like picking buttons and modals. It isn&apos;t. You&apos;re deciding who controls your styling — you or the library — and that single choice shapes how much you&apos;ll fight it for years. Here&apos;s how I weigh MUI, Ant Design, Radix, and shadcn/ui.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-ui-libraries/cover.png" alt="React UI Libraries: Who Owns the Styling Decides Everything" /></p>
<p>The demo always goes well. That&#39;s the trap.</p>
<p>You&#39;re evaluating a component library. You drop in their date picker, their data table, their modal. In twenty minutes you have something that looks polished, and the decision feels made. Ship it.</p>
<p>The problems don&#39;t show up in the demo. They show up eight months later, when a designer hands you a spec that doesn&#39;t match the library&#39;s defaults, and you discover that changing one border radius means fighting three layers of the library&#39;s own styling system. That&#39;s when you learn what you actually signed up for.</p>
<p>So I&#39;ve stopped evaluating UI libraries by how good the demo looks. The question I ask now is different, and it&#39;s uncomfortable:</p>
<blockquote>
<p>When my design diverges from the library&#39;s defaults — and it will — who wins, me or the library?</p>
</blockquote>
<p>That question splits the entire landscape cleanly. And it connects straight back to the <a href="https://ma-x.im/blog/react-playbook-css-styling">styling decision from the previous article</a>: once you&#39;ve committed to Tailwind and a token system, a component library that brings its <em>own</em> styling engine isn&#39;t a neat addition. It&#39;s a second opinion arguing with your first one.</p>
<p>Let me walk through the four I actually consider — MUI, Ant Design, Radix, and shadcn/ui — grouped not by popularity, but by who holds the pen.</p>
<h2>Two Families, Not Four Libraries</h2>
<p>Before the individual names, the split that matters:</p>
<ul>
<li><strong>Batteries-included, styled libraries</strong> — MUI, Ant Design. They give you finished, good-looking components <em>and</em> the styling system that drives them. You adopt their look and their theming model.</li>
<li><strong>Headless / you-own-the-styles</strong> — Radix, shadcn/ui. They give you behavior and accessibility, and hand the styling back to you.</li>
</ul>
<p>Almost everything else follows from which family you pick. The first family gets you to &quot;looks done&quot; fastest. The second gets you to &quot;looks like <em>ours</em>&quot; without a fight. Neither is wrong. They&#39;re answers to different questions.</p>
<h2>MUI: Powerful, Complete, and Opinionated</h2>
<p>MUI (Material UI) is the library I&#39;d hand someone who needs a lot of surface area, fast, and doesn&#39;t have a strong custom design language to protect.</p>
<pre><code class="language-tsx">import { Button, TextField } from &#39;@mui/material&#39;;

function PolicyForm() {
  return (
    &lt;form&gt;
      &lt;TextField label=&quot;Holder name&quot; variant=&quot;outlined&quot; /&gt;
      &lt;Button variant=&quot;contained&quot;&gt;Save&lt;/Button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>What&#39;s genuinely great: the component coverage is enormous. Data grids, date pickers, autocomplete, everything. The accessibility is solid. For an internal tool or a dashboard where &quot;clean and consistent&quot; beats &quot;uniquely ours,&quot; MUI gets you there remarkably fast.</p>
<p>Here&#39;s my reservation, and it&#39;s the same one that decides most of this article. MUI brings its own styling engine — historically Emotion, a runtime CSS-in-JS system. You saw in the last article why I&#39;ve cooled on runtime CSS-in-JS: the cost moved, and it sits awkwardly with Server Components. MUI inherits that tension. And it carries a point of view — Material Design — that you either want, or spend a long time theming your way out of. Overriding MUI deeply means learning MUI&#39;s override system, which is its own body of knowledge.</p>
<p>MUI isn&#39;t a bad choice. It&#39;s a <em>committing</em> choice. You&#39;re adopting a design language and a styling runtime along with the components.</p>
<h2>Ant Design: Enterprise Density Out of the Box</h2>
<p>Ant Design is MUI&#39;s closest sibling in philosophy, aimed at a slightly different target: dense, data-heavy enterprise applications.</p>
<p>If you&#39;re building an admin panel with complex tables, filters, and forms — the CRM-shaped software this series keeps circling back to — Ant&#39;s defaults are tuned for exactly that. The tables alone save weeks.</p>
<p>The trade-off is the same shape as MUI&#39;s, just with a different accent. You&#39;re adopting Ant&#39;s visual language, which is strong and distinctly <em>Ant</em> — apps built with it tend to look like they&#39;re built with it. Deep customization means working within Ant&#39;s theming system, and stepping outside its defaults gets progressively harder the further you go. Honestly, if your product needs to look like your brand rather than like an Ant demo, this is the friction you&#39;ll feel most.</p>
<p>Both MUI and Ant answer the question &quot;how do I get a lot of good components quickly?&quot; Well. The next two answer a different question: &quot;how do I keep control of how everything looks?&quot;</p>
<h2>Radix: Behavior Without the Costume</h2>
<p>Radix Primitives took the part that&#39;s genuinely hard — accessible, correct interactive behavior — and shipped <em>only</em> that. No visual design at all.</p>
<pre><code class="language-tsx">import * as Dialog from &#39;@radix-ui/react-dialog&#39;;

function ConfirmDialog() {
  return (
    &lt;Dialog.Root&gt;
      &lt;Dialog.Trigger&gt;Delete policy&lt;/Dialog.Trigger&gt;
      &lt;Dialog.Portal&gt;
        &lt;Dialog.Overlay className=&quot;fixed inset-0 bg-black/40&quot; /&gt;
        &lt;Dialog.Content className=&quot;fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-lg bg-white p-6&quot;&gt;
          &lt;Dialog.Title className=&quot;text-sm font-semibold&quot;&gt;Are you sure?&lt;/Dialog.Title&gt;
        &lt;/Dialog.Content&gt;
      &lt;/Dialog.Portal&gt;
    &lt;/Dialog.Root&gt;
  );
}
</code></pre>
<p>Look at what Radix gives you and what it doesn&#39;t. It gives you the dialog&#39;s focus trapping, escape-to-close, scroll locking, ARIA wiring, portal handling — all the things that are tedious and easy to get subtly wrong. It gives you <em>no</em> appearance. Every <code>className</code> there is mine, and it&#39;s Tailwind, and Radix doesn&#39;t care.</p>
<p>That&#39;s the whole point. Radix is what you reach for when accessibility matters and you refuse to inherit someone else&#39;s look. The cost is honest: you build the visual layer yourself. For a one-off that&#39;s work. Which is exactly the gap the last option fills.</p>
<h2>shadcn/ui: The One That Fits This Series</h2>
<p>shadcn/ui isn&#39;t really a library, and that&#39;s the clever part. It&#39;s a collection of components built <em>on</em> Radix, styled <em>with</em> Tailwind, that you copy into your own codebase.</p>
<p>You don&#39;t <code>npm install</code> a black box. You run a command, and the component&#39;s source lands in your project as your code:</p>
<pre><code class="language-bash">npx shadcn@latest add dialog
</code></pre>
<p>Now <code>components/ui/dialog.tsx</code> is <em>yours</em>. Radix underneath for behavior, Tailwind classes for the look, and every line editable because it lives in your repo. There&#39;s no override system to learn — if you want a different border radius, you change the border radius, because it&#39;s just a file you own.</p>
<p>I&#39;ll be direct about why this fits the Playbook so well. We chose Tailwind and a token system in the last article. shadcn/ui is built on exactly that foundation — its components read from your Tailwind tokens, so they inherit your design system instead of imposing one. Radix gives the behavior I&#39;d otherwise have to build by hand. And because the code is mine, the library can never become the thing I&#39;m fighting. There&#39;s no black box to fight <em>with</em>.</p>
<p>The trade-off is real and worth stating: you own more code. Updates aren&#39;t an <code>npm update</code> — you re-pull components or merge changes by hand. For some teams that ownership is a burden. For a team that wants its product to look like itself and never wants to be trapped by a vendor&#39;s styling decisions, it&#39;s the feature, not the cost.</p>
<h2>The Decision, Honestly</h2>
<p>Strip away the popularity contests and it comes down to that one question — who owns the styling:</p>
<ul>
<li><strong>You need enormous component coverage fast, and a strong custom look isn&#39;t the priority</strong> → MUI. Adopt its language on purpose.</li>
<li><strong>You&#39;re building dense, data-heavy enterprise screens</strong> → Ant Design. Its defaults are tuned for exactly that.</li>
<li><strong>You need accessible behavior but insist on owning every pixel</strong> → Radix primitives, styled yourself.</li>
<li><strong>You want owned, Tailwind-native components with Radix behavior underneath</strong> → shadcn/ui. This is my default for the kind of app this series builds.</li>
</ul>
<p>Notice that the &quot;right&quot; answer flips entirely based on one thing: how much your product needs to look like <em>yours</em> versus how fast it needs to look <em>done</em>. There&#39;s no library that wins both, and anyone selling you one hasn&#39;t hit month eight yet.</p>
<p>For the Playbook, I&#39;ll build on shadcn/ui, because it&#39;s the choice that agrees with every decision we&#39;ve made so far instead of arguing with one of them. That coherence — styling, tokens, components all pointing the same way — matters more than any single component&#39;s polish.</p>
<p>Components give you the pieces. The next article is about the hardest piece to get right: forms. We&#39;ll build real ones with <a href="https://ma-x.im/blog/react-playbook-form-libraries">TanStack Form</a>, the kind with conditional fields, async validation, and the messy requirements that make forms the place UIs go to die.</p>
<p>If you&#39;re weighing a component library right now, do one thing before you commit: try to restyle its busiest component to look nothing like the default. However that fight goes is your real answer. And if you&#39;ve made peace with MUI or Ant at scale and found the customization ceiling higher than I&#39;m describing, tell me — I&#39;d genuinely like to know where the line actually sits.</p>
]]></content:encoded>
      <pubDate>Thu, 02 Apr 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>UI Libraries</category>
      <category>shadcn/ui</category>
      <category>Radix</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Styling React in 2026: How I Actually Choose</title>
      <link>https://ma-x.im/blog/react-playbook-css-styling</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-css-styling</guid>
      <description>Styling is the most argued-about, least reasoned-about decision in a React app. The real question isn&apos;t which library looks nicest in a demo — it&apos;s which one survives a growing codebase. Here&apos;s how I weigh Tailwind, CSS Modules, CSS-in-JS, and zero-runtime options, and where each one earns its place.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-css-styling/cover.png" alt="Styling React in 2026: How I Actually Choose" /></p>
<p>Ask ten React developers how to style a component and you&#39;ll start a fight.</p>
<p>Not a discussion. A fight. Tailwind people and CSS-in-JS people don&#39;t disagree about syntax — they disagree about identity. Somewhere along the way, &quot;how do we apply styles&quot; turned into a values debate, and the actual engineering question got buried under it.</p>
<p>Here&#39;s the thing that bugs me about that debate: almost none of it is about the question that matters. It&#39;s about which approach looks cleanest in a ten-line demo. But nobody styles a ten-line demo for a living. You style an application that grows, gets handed to new people, adds a design system halfway through, and has to survive a framework upgrade.</p>
<p>So the question I actually ask isn&#39;t &quot;which one is prettiest.&quot; It&#39;s:</p>
<blockquote>
<p>When this codebase triples in size and I wasn&#39;t the one who wrote half of it, which styling approach still makes sense?</p>
</blockquote>
<p>That reframes everything. Let me walk through the real options the way I weigh them — Tailwind, CSS Modules, CSS-in-JS, and the zero-runtime newcomers — and where each one genuinely fits.</p>
<h2>First, The Question Under The Question</h2>
<p>Every styling approach is really answering three separate questions, and people conflate them:</p>
<ol>
<li><strong>Where does the style live?</strong> Same file as the component, a co-located file, or a global sheet.</li>
<li><strong>When is the style computed?</strong> At build time, or at runtime in the browser.</li>
<li><strong>How do you stay consistent?</strong> Ad-hoc values, or tokens and constraints.</li>
</ol>
<p>Most &quot;Tailwind vs styled-components&quot; arguments are really arguments about question 2 without anyone saying so. Runtime cost is the thing that quietly decides a lot of this in 2026, especially with React Server Components in the picture. Keep those three questions in mind — they&#39;ll do more for you than any hot take.</p>
<h2>Tailwind: The Default I Keep Coming Back To</h2>
<p>I&#39;ll be honest about my bias up front: Tailwind is my default. Not because it&#39;s fashionable, and not without reservations. Because of what it does to a <em>team</em>, not to a single component.</p>
<pre><code class="language-tsx">function PolicyCard({ policy }: { policy: Policy }) {
  return (
    &lt;article className=&quot;rounded-lg border border-slate-200 p-4 hover:border-slate-300&quot;&gt;
      &lt;h3 className=&quot;text-sm font-semibold text-slate-900&quot;&gt;{policy.name}&lt;/h3&gt;
      &lt;p className=&quot;mt-1 text-xs text-slate-500&quot;&gt;{policy.status}&lt;/p&gt;
    &lt;/article&gt;
  );
}
</code></pre>
<p>The first reaction most people have is &quot;that&#39;s ugly, the markup is noisy.&quot; Fair. I had the same reaction. Then I worked in codebases where Tailwind had been the standard for a year, and the noise stopped mattering. Here&#39;s what I noticed instead:</p>
<ul>
<li><strong>Nobody invents new spacing values.</strong> <code>p-4</code> is <code>p-4</code> everywhere. The design constraints are baked into the class names, so drift is hard.</li>
<li><strong>Deleting a component deletes its styles.</strong> No orphaned CSS rotting in a global file because everyone&#39;s afraid to remove it.</li>
<li><strong>No naming tax.</strong> I don&#39;t spend energy inventing <code>.policy-card__status--pending</code>. The cognitive cost of good CSS class names is real, and Tailwind just removes it.</li>
<li><strong>It compiles away.</strong> The output is a static stylesheet. Zero runtime style computation, which means it plays nicely with Server Components.</li>
</ul>
<p>The trade-offs are real too, and I won&#39;t pretend otherwise. The markup is genuinely harder to scan at first. Long class strings need something like <code>clsx</code> or <code>tailwind-merge</code> once conditions appear. And onboarding someone who&#39;s never used it costs a day or two of &quot;why is everything a class name.&quot;</p>
<p>The reservation I take most seriously: Tailwind is weak when a style is genuinely <em>dynamic</em> — driven by a value only known at runtime, like a chart bar&#39;s height from data. You don&#39;t want <code>h-[347px]</code> generated on the fly. For that, an inline style or a CSS variable is the right tool, and mixing them in is fine. Tailwind for the static 95%, a CSS variable for the dynamic 5%.</p>
<p>That&#39;s my honest position: Tailwind wins on team-scale maintainability, and that&#39;s the axis I care about most. But &quot;it wins for me&quot; is not &quot;it wins for everyone,&quot; so let me give the alternatives a real hearing.</p>
<h2>CSS Modules: The Boring, Correct Answer</h2>
<p>If Tailwind&#39;s class soup is a dealbreaker for you, CSS Modules is the option I respect the most.</p>
<pre><code class="language-tsx">import styles from &#39;./PolicyCard.module.css&#39;;

function PolicyCard({ policy }: { policy: Policy }) {
  return (
    &lt;article className={styles.card}&gt;
      &lt;h3 className={styles.title}&gt;{policy.name}&lt;/h3&gt;
      &lt;p className={styles.status}&gt;{policy.status}&lt;/p&gt;
    &lt;/article&gt;
  );
}
</code></pre>
<pre><code class="language-css">/* PolicyCard.module.css */
.card {
  border-radius: 0.5rem;
  border: 1px solid var(--color-border);
  padding: 1rem;
}
</code></pre>
<p>What I like: it&#39;s just CSS. No new mental model, no runtime, locally scoped class names so you can&#39;t leak styles across the app. It compiles to a static stylesheet, so like Tailwind it&#39;s Server Components-friendly. If a team has strong CSS skills and hates utility classes, this is not a compromise — it&#39;s a genuinely solid choice.</p>
<p>What pushes me away from it at scale: it doesn&#39;t enforce consistency on its own. Nothing stops one file from using <code>padding: 16px</code> and another using <code>padding: 1rem</code> and a third using <code>padding: 14px</code> because someone eyeballed it. You get scoping for free, but you have to build the discipline — the tokens, the shared variables — yourself. Tailwind ships that discipline in the box. That&#39;s the whole difference, and for some teams CSS Modules plus a well-guarded token file is exactly right.</p>
<h2>CSS-in-JS: Why I&#39;m Walking Away From It</h2>
<p>This is the one where I&#39;ve changed my mind, so I&#39;ll say it plainly.</p>
<p>I used to reach for styled-components and Emotion without a second thought. The developer experience is lovely: styles co-located with the component, full access to props, dynamic styling that feels natural.</p>
<pre><code class="language-tsx">const Card = styled.article&lt;{ $pending: boolean }&gt;`
  border-radius: 0.5rem;
  padding: 1rem;
  border: 1px solid ${(p) =&gt; (p.$pending ? &#39;#f59e0b&#39; : &#39;#e2e8f0&#39;)};
`;
</code></pre>
<p>That reads great. So why am I walking away?</p>
<p>Because the cost moved. Runtime CSS-in-JS computes styles in the browser, on render, and serializes them into the DOM as the app runs. For years that cost was acceptable. Then React Server Components arrived, and runtime CSS-in-JS sits awkwardly with them — these libraries lean on runtime context and client-side execution, which is exactly what Server Components are trying to avoid. The React team&#39;s own guidance nudged people toward compile-time styling for a reason.</p>
<p>I don&#39;t say this to bury a whole category — if you&#39;re on a heavily client-rendered app and the DX makes your team faster, runtime CSS-in-JS is not a crime. But for new work in 2026, especially anything touching Server Components, I no longer start here. The direction of the ecosystem is away from runtime style computation, and I&#39;d rather not build on the side that&#39;s swimming against it.</p>
<p>Which leads directly to the interesting middle ground.</p>
<h2>Zero-Runtime CSS-in-JS: The Best of Both, Mostly</h2>
<p>Tools like vanilla-extract keep the part of CSS-in-JS people actually love — writing styles in TypeScript, with type-safe tokens — while throwing away the part that hurts: the runtime.</p>
<pre><code class="language-ts">// PolicyCard.css.ts
import { style } from &#39;@vanilla-extract/css&#39;;
import { tokens } from &#39;../theme.css&#39;;

export const card = style({
  borderRadius: tokens.radius.md,
  border: `1px solid ${tokens.color.border}`,
  padding: tokens.space.md,
});
</code></pre>
<p>You author in TypeScript, get autocomplete and type-checking on your design tokens, and it all compiles to a static stylesheet at build time. Zero runtime. Server Components-friendly. Honestly, this is the most intellectually satisfying option on the list — it fixes the exact thing that made me leave runtime CSS-in-JS.</p>
<p>So why isn&#39;t it my default? Two reasons, and neither is technical. The ecosystem and hiring pool around Tailwind are enormous, and the token-based type safety that vanilla-extract gives you, Tailwind approximates well enough through its config. If your team already thinks in TypeScript tokens and wants that safety enforced by the compiler, vanilla-extract is a genuinely excellent pick — arguably the &quot;correct&quot; one on paper. I just find Tailwind gets a team to the same maintainability with less setup.</p>
<h2>The Thread That Ties It Together: Tokens</h2>
<p>Notice what quietly showed up in almost every example above — <code>var(--color-border)</code>, <code>tokens.space.md</code>, Tailwind&#39;s <code>slate-200</code>. That&#39;s the part that actually matters, and it&#39;s independent of which library you pick.</p>
<blockquote>
<p>The styling library is a delivery mechanism. Design tokens are the actual system.</p>
</blockquote>
<p>Whatever you choose, the thing that keeps a large app coherent is a single source of truth for color, spacing, radius, and typography — and the discipline to never bypass it with a magic number. Tailwind gives you tokens through its config. Vanilla-extract gives you tokens as typed objects. CSS Modules gives you tokens through CSS custom properties. The delivery differs; the principle doesn&#39;t. If you get tokens right, you can survive changing your styling library later. If you get them wrong, no library saves you.</p>
<h2>So What Do I Actually Pick?</h2>
<p>Stripped of tribalism, here&#39;s my honest decision path:</p>
<ul>
<li><strong>New app, normal team, wants to move fast and stay consistent</strong> → Tailwind. It ships the constraints in the box.</li>
<li><strong>Team with strong CSS culture that dislikes utility classes</strong> → CSS Modules plus a guarded token file. Boring and correct.</li>
<li><strong>Team that lives in TypeScript and wants compiler-enforced tokens</strong> → vanilla-extract. The most &quot;principled&quot; pick.</li>
<li><strong>Heavily client-rendered app where runtime DX wins</strong> → runtime CSS-in-JS is still defensible, eyes open about Server Components.</li>
</ul>
<p>There&#39;s no universally right answer, and anyone who tells you there is hasn&#39;t worked in enough different codebases. For this series I&#39;ll use Tailwind, because it fits the kind of app we&#39;re building and it pairs cleanly with what comes next.</p>
<p>Because styling rarely lives alone. The moment you have a real app, you reach for a component library — and how it&#39;s styled decides whether you can theme it, override it, or fight it. That&#39;s the next article: <a href="https://ma-x.im/blog/react-playbook-ui-libraries">choosing a React UI library</a>, and why the styling decision you just made quietly constrains that choice.</p>
<p>If your team is mid-argument about this right now, try changing the question. Stop asking which one is best and start asking which one your codebase can live with in two years. And if you&#39;ve landed somewhere I&#39;d disagree with — especially if you&#39;re still happily on runtime CSS-in-JS — I genuinely want to hear the case. I&#39;ve changed my mind on this before.</p>
]]></content:encoded>
      <pubDate>Mon, 30 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>CSS</category>
      <category>Tailwind</category>
      <category>Styling</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Immutable Data in React: The Rule I Stopped Arguing About</title>
      <link>https://ma-x.im/blog/react-playbook-immutable-data-structures</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-immutable-data-structures</guid>
      <description>A module I was told not to over-engineer grew into an entire application. Most of what let it survive was boring — and one of the most boring rules was never mutating state in place. Here&apos;s how immutability actually works in React, when I reach for Immer, and why I skip Immutable.js.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-immutable-data-structures/cover.png" alt="Immutable Data in React: The Rule I Stopped Arguing About" /></p>
<p>The company had a legacy PHP application, and we were not brought in to rebuild it. We were brought in to add one module.</p>
<p>One module. A self-contained piece of the product that a couple of teams needed. Nobody expected it to matter much beyond that.</p>
<p>So when I pushed for structure — a real data model, immutable updates, clear boundaries — there was pushback. Fair pushback. &quot;It&#39;s one module. Why the ceremony? Why can&#39;t we just mutate the thing and move on?&quot;</p>
<p>I understood the argument. On a small, disposable module, discipline can look like pure overhead. You feel every extra rule slowing you down long before it pays you back.</p>
<p>Then the module did the thing small modules occasionally do. It got good. It got used. Other teams started building against it. Over a couple of years it quietly stopped being a module and became the main application, with almost everything bolted onto the shape we set at the start. I still talk to the people there. They are still building on that foundation today.</p>
<p>I&#39;m not telling this to claim we were geniuses. We weren&#39;t. Most of what held up under that growth was boring. And one of the most boring rules of all was this: we never mutated state in place.</p>
<p>That&#39;s what this article is about. Immutable data in React — not the purity lecture, but the practical discipline. Where it bites you if you skip it, when the native approach stops being pleasant, and how it connects to the reactive model from the <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">Reatom article</a> earlier in this series.</p>
<p>If you know JavaScript, you already know how to copy an object. That&#39;s not the hard part. The hard part is understanding <em>why</em> React, memoization, and reactive stores punish you the moment you forget.</p>
<h2>What Mutation Actually Breaks</h2>
<p>React doesn&#39;t deeply compare your state. It compares references.</p>
<p>That single sentence is the whole thing. Everything else is a consequence of it.</p>
<p>Here&#39;s a bug I&#39;ve watched people write, debug for twenty minutes, and never fully explain afterward:</p>
<pre><code class="language-tsx">const [draft, setDraft] = useState&lt;PolicyDraft&gt;(initialDraft);

function addBeneficiary(beneficiary: BeneficiaryDraft) {
  draft.beneficiaries.push(beneficiary); // mutation
  setDraft(draft);                        // same reference
}
</code></pre>
<p>You added a beneficiary. The data changed. The screen didn&#39;t.</p>
<p>React ran <code>Object.is(previousDraft, nextDraft)</code>, saw the same object reference, and decided nothing changed. Your <code>push</code> mutated the array in place, so the outer <code>draft</code> object is still the exact same reference it was before. As far as React is concerned, you called <code>setDraft</code> with the value it already had.</p>
<p>The fix isn&#39;t a trick. It&#39;s a new reference:</p>
<pre><code class="language-tsx">function addBeneficiary(beneficiary: BeneficiaryDraft) {
  setDraft({
    ...draft,
    beneficiaries: [...draft.beneficiaries, beneficiary],
  });
}
</code></pre>
<p>New object, new array. React compares references, sees a difference, re-renders.</p>
<p>This is the React nuance the JS-fundamentals tutorials skip. Immutability in React isn&#39;t about being functionally pure for its own sake. It&#39;s that the entire rendering and memoization model is built on the assumption that <em>a change produces a new reference</em>. Break that assumption and everything downstream — <code>React.memo</code>, <code>useMemo</code>, dependency arrays, reactive stores — quietly stops working. Not with an error. With silence.</p>
<blockquote>
<p>Mutation doesn&#39;t crash. It just makes your UI lie about the data.</p>
</blockquote>
<h2>The Rule: A New Reference On Every Change</h2>
<p>The rule is boring, and boring is the point:</p>
<p><strong>Never change data in place. Produce a new value.</strong></p>
<p>For flat, shallow state, the native tools are enough, and I reach for them first.</p>
<p>Add an item:</p>
<pre><code class="language-ts">const next = [...beneficiaries, newBeneficiary];
</code></pre>
<p>Remove by id:</p>
<pre><code class="language-ts">const next = beneficiaries.filter((b) =&gt; b.id !== removedId);
</code></pre>
<p>Update one item by id — the operation people get wrong most often:</p>
<pre><code class="language-ts">const next = beneficiaries.map((b) =&gt;
  b.id === targetId ? { ...b, share: newShare } : b,
);
</code></pre>
<p>Notice what <code>map</code> does here. Every item that didn&#39;t change keeps its original reference. Only the one you touched becomes a new object. That&#39;s not an accident — it&#39;s exactly what you want. Components rendering the untouched rows can skip re-rendering, because their props are referentially identical.</p>
<p>Update a field on an object:</p>
<pre><code class="language-ts">const next = { ...draft, holderName: &#39;Updated Name&#39; };
</code></pre>
<p>None of this is clever. That&#39;s the appeal. Anyone reading it can see what changed and what stayed the same.</p>
<h2>Where Native Updates Get Painful</h2>
<p>Then the state gets nested, and the spreads start to hurt.</p>
<p>Say the policy draft looks like this:</p>
<pre><code class="language-ts">interface PolicyDraft {
  holderName: string;
  sections: {
    coverage: {
      type: &#39;standard&#39; | &#39;premium&#39;;
      limits: {
        medical: number;
        liability: number;
      };
    };
  };
}
</code></pre>
<p>Now update <code>limits.medical</code> immutably with native syntax:</p>
<pre><code class="language-ts">const next = {
  ...draft,
  sections: {
    ...draft.sections,
    coverage: {
      ...draft.sections.coverage,
      limits: {
        ...draft.sections.coverage.limits,
        medical: newValue,
      },
    },
  },
};
</code></pre>
<p>Technically correct. Practically miserable. You changed one number and wrote a five-level spread pyramid to do it. Worse, in a code review nobody can see at a glance <em>what actually changed</em> — the signal is buried under structural boilerplate. And every one of those spread levels is a place to make a typo that silently drops a branch of state.</p>
<p>This is the moment people conclude &quot;immutability is exhausting&quot; and start looking for shortcuts. Some of those shortcuts are good. One of them is a mistake.</p>
<h2>Immer: The Escape Hatch for Deep Updates</h2>
<p>When updates get deep, I reach for <a href="https://immerjs.github.io/immer/">Immer</a>.</p>
<p>Immer lets you write code that <em>looks</em> like mutation and hands you an immutable result:</p>
<pre><code class="language-ts">import { produce } from &#39;immer&#39;;

const next = produce(draft, (d) =&gt; {
  d.sections.coverage.limits.medical = newValue;
});
</code></pre>
<p>That&#39;s the same nested update as above. One line. You mutate a temporary draft object; Immer records the changes and produces a new immutable value, reusing every branch you didn&#39;t touch. That last part matters: the <code>holderName</code> string and the untouched <code>liability</code> limit keep their references, so referential-equality checks and memoization still work. You get the ergonomics of mutation with the guarantees of immutability.</p>
<p>My rule of thumb is narrow on purpose:</p>
<ul>
<li><strong>Flat or shallow state → native spreads.</strong> Don&#39;t pull in Immer to change one field on a flat object. It&#39;s noise.</li>
<li><strong>Deep or awkwardly nested updates → Immer.</strong> The moment the spread pyramid appears, <code>produce</code> earns its place.</li>
</ul>
<p>Immer isn&#39;t free. It wraps your drafts in proxies and freezes output in development, which is a real runtime cost. For application state — forms, drafts, workflow models — that cost is invisible. I would not reach for it inside a hot render loop or a tight animation frame. Right tool, right layer.</p>
<h2>Why I Skip Immutable.js</h2>
<p>Every time this topic comes up, someone asks about <a href="https://immutable-js.com/">Immutable.js</a>. It gives you <code>Map</code>, <code>List</code>, and structural sharing baked in, so surely it&#39;s the &quot;serious&quot; choice?</p>
<p>Honestly, no. Not in a modern React codebase, and not for me.</p>
<p>The problem isn&#39;t the data structures. They&#39;re well made. The problem is that Immutable.js introduces <em>a parallel type system</em> that doesn&#39;t speak plain JavaScript:</p>
<pre><code class="language-ts">const draft = Map({ holderName: &#39;Ann&#39;, beneficiaries: List() });

const name = draft.get(&#39;holderName&#39;);        // not draft.holderName
const next = draft.set(&#39;holderName&#39;, &#39;Bob&#39;); // not { ...draft }
const plain = draft.toJS();                  // needed at every boundary
</code></pre>
<p>That <code>toJS()</code> at the boundary is the tell. Your API returns plain objects. TanStack Query caches plain objects. TanStack Form, most component libraries, your logging, your TypeScript types — all assume plain objects. So you spend your life converting in and out, and every conversion is a place for bugs and lost type information. The TypeScript ergonomics are genuinely worse than working with plain interfaces.</p>
<p>Native immutable updates plus Immer for the deep cases cover everything Immutable.js was solving, without forcing the entire codebase to adopt a second notion of what &quot;an object&quot; is. I stopped considering it years ago and haven&#39;t missed it once.</p>
<h2>Immutability and Reatom</h2>
<p>This is where the previous article and this one meet.</p>
<p>In the <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">Reatom piece</a>, the whole model was a graph: small atoms, derived atoms, actions as transitions. That graph runs on the exact same assumption React does — <strong>a change is a new reference.</strong> Reatom decides what to recompute and what to re-render by comparing atom values by reference. Mutate an atom&#39;s value in place and you reproduce the React bug one layer down: derived atoms don&#39;t recompute, subscribed components don&#39;t update.</p>
<p>So the wrong version:</p>
<pre><code class="language-ts">export const addBeneficiary = action((ctx, beneficiary: BeneficiaryDraft) =&gt; {
  const draft = ctx.get(policyDraftAtom);
  draft.beneficiaries.push(beneficiary); // mutation
  policyDraftAtom(ctx, draft);           // same reference — nothing reacts
}, &#39;addBeneficiary&#39;);
</code></pre>
<p>And the right one:</p>
<pre><code class="language-ts">export const addBeneficiary = action((ctx, beneficiary: BeneficiaryDraft) =&gt; {
  const draft = ctx.get(policyDraftAtom);

  policyDraftAtom(ctx, {
    ...draft,
    beneficiaries: [...draft.beneficiaries, beneficiary],
  });
}, &#39;addBeneficiary&#39;);
</code></pre>
<p>And when the transition is deep, Immer lives comfortably inside an action:</p>
<pre><code class="language-ts">import { produce } from &#39;immer&#39;;

export const setMedicalLimit = action((ctx, medical: number) =&gt; {
  const draft = ctx.get(policyDraftAtom);

  policyDraftAtom(ctx, produce(draft, (d) =&gt; {
    d.sections.coverage.limits.medical = medical;
  }));
}, &#39;setMedicalLimit&#39;);
</code></pre>
<p>Immutability isn&#39;t a nice-to-have bolted onto the state graph. It&#39;s the thing that makes the graph work at all. The reactivity you get from Reatom is <em>paid for</em> by never mutating in place.</p>
<h2>The Payoff</h2>
<p>Everything so far sounds like rules. Here&#39;s what the rules buy you, and it&#39;s the reason that small module survived becoming a large application.</p>
<p><strong>Memoization actually works.</strong> Because unchanged branches keep their references, <code>React.memo</code> and derived atoms skip work honestly. You don&#39;t fight spurious re-renders, because your data stops lying about what changed.</p>
<p><strong>Undo becomes boring.</strong> The <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">undo history from the Reatom article</a> only works because every previous draft is a distinct, frozen-in-time reference. If you mutated in place, &quot;history&quot; would just be a list of pointers to the same object, all mutating together. Immutability is what makes a snapshot an actual snapshot.</p>
<pre><code class="language-ts">const history: PolicyDraft[] = [];

// every entry is a real, independent snapshot — because we never mutate
history.push(currentDraft);
policyDraftAtom(ctx, nextDraft);
</code></pre>
<p><strong>Optimistic updates are safe.</strong> You apply the optimistic value as a new reference, keep the previous one, and roll back to it if the request fails. No cloning gymnastics, no fear that the rollback value mutated underneath you.</p>
<p><strong>Debugging tells the truth.</strong> A logged state object stays what it was when you logged it. No more &quot;it was correct in the console but wrong in the UI&quot; — the classic symptom of an alias mutating after you inspected it.</p>
<p>None of these are exotic. They&#39;re the everyday properties that let a codebase absorb requirements it wasn&#39;t designed for. Which is exactly what that module had to do.</p>
<h2>The Rule I Stopped Arguing About</h2>
<p>The early pushback was really about ceremony. Immutability <em>looked</em> like ceremony — extra rules, extra keystrokes, extra discipline for a module nobody thought would grow.</p>
<p>But the module grew, and the ceremony turned out to be the cheapest insurance we bought. Not because immutability is profound. Because it removed an entire category of bug — the silent, intermittent, &quot;works until it doesn&#39;t&quot; kind — right at the point where scale makes those bugs most expensive to find.</p>
<p>I don&#39;t argue about immutability anymore. When someone asks whether it&#39;s worth the trouble on a small piece of code, I&#39;ve stopped debating it. I just don&#39;t mutate. The habit costs almost nothing, and it holds up under exactly the pressure you can&#39;t predict when you&#39;re writing &quot;just one module.&quot;</p>
<p>Next in the series, we move from data to what the user actually sees: <a href="https://ma-x.im/blog/react-playbook-css-styling">styling in React</a>, and why I keep coming back to Tailwind after trying most of the alternatives.</p>
<p>If you&#39;ve got a state bug that only shows up sometimes — the kind that vanishes the moment you add a <code>console.log</code> — go looking for something mutating in place. And if you want to compare notes on where Immer genuinely earns its keep versus where it&#39;s overkill, reach out. I have opinions, and I&#39;m happy to hear yours.</p>
]]></content:encoded>
      <pubDate>Fri, 27 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Immutability</category>
      <category>Reatom</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Give Your AI Agent a Memory — It Will Stop Disappointing You</title>
      <link>https://ma-x.im/blog/give-your-ai-agent-a-memory</link>
      <guid isPermaLink="true">https://ma-x.im/blog/give-your-ai-agent-a-memory</guid>
      <description>A simple file-based memory system that turns AI agents from forgetful interns into reliable collaborators. Works for engineers, designers, writers, and anyone who uses AI tools daily.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/give-your-ai-agent-a-memory/cover.png" alt="Give Your AI Agent a Memory — It Will Stop Disappointing You" /></p>
<p>I was mass-scrolling LinkedIn and almost missed it. A <a href="https://www.linkedin.com/posts/dmytro-serg-909385b5_%D1%89%D0%BE%D1%80%D0%B0%D0%B7%D1%83-%D0%BF%D0%BE%D1%8F%D1%81%D0%BD%D1%8E%D1%94%D1%88-ai-%D0%B0%D0%B3%D0%B5%D0%BD%D1%82%D1%83-%D0%BE%D0%B4%D0%BD%D0%B5-%D0%B9-%D1%82%D0%B5-%D1%81%D0%B0%D0%BC%D0%B5-activity-7440664293827567616-i37k/?utm_source=ma-x.im&utm_medium=member_desktop&rcm=ACoAAARw5RMBaP7bUQuqKf2SLQvB7dtX0aJT9Fo">post from Dmytro Sergiev</a> — a Product Manager, not an engineer — about how he gives his AI agent a persistent memory using plain folders and markdown files. And something clicked.</p>
<p>I had been annoyed for months. Every new chat session with an AI agent — same questions, same wrong assumptions, same &quot;what framework are you using?&quot; dance. I would explain my project setup, my conventions, my stack. The agent would do decent work. Next day — blank slate. All of it, gone. I knew the problem was not the model itself — I understood that AI needs context to be useful. But I could not figure out a sustainable way to give it that context. The problem was amnesia.</p>
<p>Dmytro&#39;s idea was dead simple: give the agent a folder it can read and write between sessions. I grabbed this concept, adapted it for my frontend engineering workflow, and started rolling it out across every project I work on. It has been a few weeks now, and honestly — I cannot believe I worked without this for so long.</p>
<blockquote>
<p><strong>Update — April 2026:</strong> This post describes v1 of the system. After about three weeks of real use — across multiple projects and a team discussion — I ran into the limits of the append-only approach and redesigned it from scratch. The core idea here still holds and is worth reading for context. The hardened version is in <a href="https://ma-x.im/blog/ai-agent-memory-redesign">The AI Agent Memory System I Built Was Broken — Here Is the Redesign</a>.</p>
</blockquote>
<h2>The Problem Is Not the Model — It Is the Amnesia</h2>
<p>Think about what happens when you open a fresh chat and say &quot;add a blog post to my site.&quot; The agent has no idea what framework you use. It does not know how your content is structured. It does not know you spent two hours last Tuesday figuring out that your blog posts are TypeScript files with markdown in a template literal. It does not know that you tried CSS-in-JS once and it was a disaster.</p>
<p>So it guesses. You correct. It guesses again. You correct again. Twenty minutes pass before any real work happens. And tomorrow? Same thing. From zero.</p>
<p>I always understood that the model needs context. That was never the question. The question was — how do you actually give it enough? Context is not a document you write once. It is years of decisions, conventions, mistakes, preferences — all living in your head. You cannot dump all of that into a prompt. And even if you could, it would be out of date by next week. I needed a system, not a one-time fix.</p>
<h2>The Fix: A Folder the Agent Can Read and Write</h2>
<p>Create a <code>.ai-memory/</code> folder in your project root. Put a few markdown files in it. Tell the agent to read them at the start of every session and update them as it learns. Seriously, that is the whole idea.</p>
<pre><code class="language-markdown">.ai-memory/
  context.md       — What this project is, who works on it, goals, constraints
  decisions.md     — Key decisions made, with reasoning
  patterns.md      — Conventions, naming rules, recurring patterns
  lessons.md       — Mistakes, gotchas, things to avoid
  sessions/
    2026-03-25.md  — What was done today, what was learned
    2026-03-24.md  — Yesterday&#39;s session
</code></pre>
<p>Five files and a folder. No database. No API. No npm packages. Just markdown files that a human can read too.</p>
<h2>What Goes in Each File</h2>
<p><strong>context.md</strong> is the project&#39;s identity card. Who you are, what the project does, the tech stack, the constraints. For a React project it might say &quot;Next.js 15, App Router, Tailwind, deployed to Vercel.&quot; For a marketing project it might say &quot;Brand guidelines in /docs, tone is professional but warm, target audience is B2B SaaS.&quot; For an accounting workflow it could describe the tools, the report formats, the compliance rules.</p>
<p><strong>decisions.md</strong> captures the &quot;why&quot; behind choices. Not just &quot;we use Tailwind&quot; but &quot;we use Tailwind because utility-first is fast for a small team and avoids CSS naming debates.&quot; When the agent encounters a similar decision later, it does not propose something contradictory.</p>
<p><strong>patterns.md</strong> records how things are done in this specific project. File naming conventions. Component structure. How blog posts are stored. Where images go. The things a new team member would ask on their first day — except the agent finds them on its own.</p>
<p><strong>lessons.md</strong> is the file that surprised me the most. I did not expect it to matter this much. Every bug, every gotcha, every &quot;do not do this because...&quot; entry prevents the agent from repeating the same mistake. I had a bug in one of my projects where backticks inside a template literal were breaking the build. I fixed it, moved on, and the next day the agent generated the exact same bug. After I added &quot;backticks inside template literal content must be escaped&quot; to lessons.md — never happened again. Three words in a markdown file saved me from debugging the same issue three times.</p>
<p><strong>sessions/</strong> logs keep a running history. After each session the agent writes what it did, what decisions were made, what was learned. These logs feed future sessions with recent context.</p>
<h2>Why This Works for Everyone — Not Just Engineers</h2>
<p>Here is what struck me about Dmytro&#39;s original post: he is not a developer. He is a Product Manager who works with data, strategy, and AI-assisted research. He does not write React components. He uses VS Code as a workspace for documents and workflows. And the memory system works just as well for him — maybe better, because his context is even harder for an agent to guess.</p>
<p>Think about an analyst who works with multiple data sources, specific report formats, metric definitions that differ between clients, and compliance rules that change quarterly. Every time they start a new AI session, they would have to re-explain all of that. With a memory folder, the agent already knows: &quot;Client X uses quarterly cohort analysis, reports go in /reports/YYYY-QN/, retention is measured as 30-day active, not 30-day login.&quot; That is not code. That is just domain knowledge written in plain text. And it makes the agent dramatically more useful.</p>
<p>The same applies to product managers tracking sprint context, designers recording spacing conventions and color tokens, writers maintaining tone guidelines. The files are plain markdown. No code required. If you can write a bullet point, you can give your agent a memory.</p>
<h2>The Practical Difference</h2>
<p>Let me give you a real example. Last week I came back to a project I had not touched in six days. Old me would have spent the first 15 minutes re-explaining the setup: &quot;This is a Next.js site, blog posts are TypeScript files, we use Tailwind, content goes in src/data/posts...&quot; Instead, I just said &quot;create a new blog post about agent memory.&quot; The agent read context.md and patterns.md, understood the file structure, the naming conventions, the BlogPost type — and produced a working post file on the first try. Fifteen minutes of setup reduced to zero.</p>
<p>Or this one: I kept correcting the agent — &quot;we use Tailwind, not CSS modules.&quot; Every. Single. Session. After I wrote it in decisions.md, that correction disappeared permanently. A one-line entry fixed a daily annoyance.</p>
<p>The thing that gets me is how it compounds. After a week, the agent knows your project better than a new team member after onboarding. After a month, going back to a project without memory feels broken — like opening a codebase with no README, no comments, nothing. You realize how much invisible context you were carrying in your head and re-typing every day.</p>
<h2>How to Set This Up in Any Project</h2>
<p>Here is the step-by-step. Takes about five minutes.</p>
<p><strong>Step 1:</strong> Create the folder structure in your project root:</p>
<pre><code class="language-bash">mkdir .ai-memory
mkdir .ai-memory/sessions
</code></pre>
<p><strong>Step 2:</strong> Create the instruction file. If you use VS Code with GitHub Copilot, put this in <code>.github/copilot-instructions.md</code>. If you use Claude/Cursor, put it in <code>.cursorrules</code> or the equivalent config file.</p>
<p>Here is the full prompt you can copy:</p>
<pre><code class="language-markdown"># Agent Memory System

You have access to a persistent memory system stored in .ai-memory/
at the root of this project. Use it to maintain context across sessions,
avoid asking repeated questions, and make better decisions over time.

## Memory Structure

.ai-memory/
  context.md       — Project identity, tech stack, goals, constraints
  decisions.md     — Key technical decisions with reasoning
  patterns.md      — Conventions, naming patterns, project-specific rules
  lessons.md       — Mistakes, gotchas, things to remember and avoid
  sessions/
    YYYY-MM-DD.md  — Session log: what was done, decided, learned

## Rules

### At the Start of Every Session
1. Read context.md, decisions.md, patterns.md, and lessons.md.
2. Use this information to inform your work. Do not ask questions
   already answered in these files.
3. If a file does not exist yet, create it when you have information
   worth recording.

### During Work
- Non-trivial decisions -&gt; record in decisions.md
- Discovered conventions -&gt; record in patterns.md
- Bugs, gotchas, mistakes -&gt; record in lessons.md
- Project goals or constraints -&gt; update context.md

### Mandatory Memory Checkpoints
You MUST update memory at these moments. Do not skip. Do not defer.
1. After fixing a bug or discovering a gotcha -&gt; add to lessons.md immediately.
2. After completing a multi-step task -&gt; update relevant memory files right away.
3. When the user shares a preference or constraint -&gt; update context.md or patterns.md within the same response.
4. Before responding with &quot;done&quot; -&gt; check if anything should be in memory first.

### After Significant Work
- Create or append to sessions/YYYY-MM-DD.md with:
  what was done, key decisions, lessons learned.

### Writing Style
- Concise: 1-3 sentences per entry
- Factual: what was decided, not how you feel
- Append-friendly: new entries at the bottom
- No duplication: update existing entries, do not contradict

### If .ai-memory/ Does Not Exist
Create the folder and initial files by scanning the project:
package.json, README, configs -&gt; context.md and patterns.md.
Leave decisions.md and lessons.md mostly empty — they grow over time.
</code></pre>
<p><strong>Step 3:</strong> Start a new session. The agent will read the instruction, create the initial memory files by scanning your project, and begin maintaining them automatically.</p>
<p><strong>Step 4:</strong> Decide where memory lives.</p>
<p>If this is your personal project — commit <code>.ai-memory/</code> to the repo. You are the only one working here, so your memory is the project&#39;s memory. That is how I do it on ma-x.im.</p>
<p>If you work in a team — even three developers — add <code>.ai-memory/</code> to <code>.gitignore</code>. Memory is personal. Your thinking, your mistakes, your patterns — they are different from your colleague&#39;s. General project knowledge (architecture, API conventions, component rules) belongs in shared context files like <code>.github/copilot-instructions.md</code>. But memory is your private notebook. The instruction prompt stays in the repo so every developer&#39;s agent knows to create and maintain its own local memory — but the memory content itself stays local.</p>
<p>There is a possible third path: shared team memory where everyone writes to the same folder. I can see how it could work in theory, but I have not seen a strong use case for it yet. Shared context plus individual memory covers most scenarios.</p>
<h2>Honest Limitations</h2>
<p>This is not magic. A few things to keep in mind:</p>
<ul>
<li><strong>The agent forgets to update memory.</strong> This was my biggest surprise. I wrote clear instructions: &quot;after fixing a bug, record it in lessons.md.&quot; The agent would fix the bug, confirm it was done, and move on without writing anything down. Hours of work, zero memory updates. The fix was adding explicit mandatory checkpoints to the instructions — not &quot;record things when appropriate&quot; but &quot;you MUST update memory BEFORE telling me you are done.&quot; Passive instructions get ignored during complex multi-step work. Forceful, specific checkpoints work.</li>
<li><strong>Files can get bloated.</strong> If lessons.md grows past 100 lines, split it or prune old entries. The agent reads these files every session — keep them focused.</li>
<li><strong>It depends on the agent following instructions.</strong> Some AI tools respect system prompts better than others. GitHub Copilot in VS Code and Claude in Cursor both handle this well. Other tools may vary.</li>
<li><strong>It is not a replacement for proper documentation.</strong> This is a working notebook, not a wiki. Keep real docs in real docs.</li>
</ul>
<h2>Context vs Memory — Why You Need Both</h2>
<p>This is the distinction I wish someone had explained to me earlier.</p>
<p><strong>Context</strong> is what describes the project: architecture, API structure, component conventions, CSS rules, TypeScript config, deployment pipeline. It is documentation. You write it once, maintain it as the project evolves, and every developer on the team reads the same version. Context lives in shared files — README, copilot-instructions.md, architecture docs.</p>
<p><strong>Memory</strong> is what describes the developer: your decisions, your mistakes, your patterns, your working style. It grows organically, session by session. What you did today, what went wrong yesterday, what you learned this week — this is what makes the agent smarter over time. Context is static knowledge. Memory is accumulated experience.</p>
<p>In an ideal project, you need both. Context so the agent understands the codebase. Memory so it understands you. Without context, the agent guesses about your stack. Without memory, it repeats the same mistakes and asks the same questions every day.</p>
<p>The real shift for me was understanding that context alone is never enough. There is always more knowledge in your head than you can write down — it forms over months and years of working on a project. You cannot capture all of it upfront. The memory approach solves this differently: instead of trying to dump everything at once, it lets knowledge accumulate naturally. Every session teaches the agent something new. After a week it knows your project better than a new hire after onboarding. After a month, working without it feels broken.</p>
<h2>Summary</h2>
<p>I always knew the model needed context. That was never the mystery. What I could not figure out was how to give it enough context without spending half my day writing documentation that would be outdated by Friday. The memory approach solved this — not by asking me to write everything upfront, but by letting knowledge accumulate naturally, session by session.</p>
<p>Five markdown files and a prompt. Five minutes of setup. The return is immediate and it compounds — every session makes the next one smarter.</p>
<p>The people chasing the newest model are solving the wrong problem. The leverage is not in the model. It is in what the model knows about you and your project.</p>
<p>If you have feedback, alternative approaches, or just want to discuss this topic — feel free to reach out.</p>
]]></content:encoded>
      <pubDate>Thu, 26 Mar 2026 00:00:00 GMT</pubDate>
      <category>AI</category>
      <category>Productivity</category>
      <category>DX</category>
      <category>Workflow</category>
    </item>
    <item>
      <title>React State Management with Reatom: When useState Stops Being Enough</title>
      <link>https://ma-x.im/blog/react-playbook-reatom-state-management</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-reatom-state-management</guid>
      <description>Most React state doesn&apos;t need a library. Then one workflow grows into drafts, derived values, undo, optimistic steps, and cross-component coordination. This is where I reach for Reatom instead of turning TanStack Query into a client-state store.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-reatom-state-management/cover.png" alt="React State Management with Reatom: When useState Stops Being Enough" /></p>
<p>The policy builder did not look dangerous at first.</p>
<p>It was &quot;just a form.&quot; A few sections, some conditional fields, a Save Draft button, a final Submit action. We already had TanStack Query for server state and TanStack Form for field state. It felt like the rest could live in component state.</p>
<p>Then the requirements arrived one by one.</p>
<p>If the user changed coverage type, three sections had to recalculate. If they removed a beneficiary, dependent warnings had to disappear. If they switched tabs, the draft had to stay alive. If the API returned a validation warning, it had to attach to a section that might not be mounted. If they clicked Review, the app had to derive a summary from fields, rules, permissions, and unsaved changes.</p>
<p>Nothing was individually hard. Together, it became a graph.</p>
<p>I&#39;ve been in this room before. Different company, different domain, same shape. A workflow starts as local state. Then you add a context. Then a reducer. Then a few selectors. Then effects hidden in components. Eventually nobody can answer a simple question: <em>when this value changes, what else reacts?</em></p>
<p>That is the point where I want a state management library.</p>
<p>Not for every boolean. Not for every modal. For the moment client state becomes a reactive model instead of a component detail.</p>
<blockquote>
<p>The question is not &quot;do we need global state?&quot; The question is &quot;does this workflow have a state graph?&quot;</p>
</blockquote>
<p>This is article #7 in the <strong>React Playbook</strong> series. We already covered TanStack Query and TanStack Router. Now we need the missing piece: client state that does not belong to the server, but has outgrown <code>useState</code>.</p>
<h2>Query Is Not Your Client Store</h2>
<p>TanStack Query owns server state.</p>
<p>Policies from the API. Customer records. Coverage types. Search results. Anything fetched, cached, invalidated, and synchronized with a backend.</p>
<p>Client state is different:</p>
<ul>
<li>which policy sections are expanded</li>
<li>current unsaved draft</li>
<li>selected rows in a table</li>
<li>multi-step wizard progress</li>
<li>temporary validation warnings</li>
<li>optimistic local operations before submit</li>
<li>undo history</li>
<li>derived review summary</li>
</ul>
<p>The mistake is pushing these into Query because Query is already installed.</p>
<pre><code class="language-ts">queryClient.setQueryData([&#39;policy-builder-draft&#39;], draft);
</code></pre>
<p>This works mechanically. It is also the wrong abstraction. Query cache is built around remote data identity, freshness, invalidation, refetching, and synchronization. A local draft is not stale. It should not refetch. It does not have server freshness semantics.</p>
<p>Use Query for server state.</p>
<p>Use a client-state tool for client state.</p>
<h2>Why Not Just useState?</h2>
<p><code>useState</code> is still the default.</p>
<p>For component-local state, it is perfect:</p>
<pre><code class="language-tsx">const [isOpen, setIsOpen] = useState(false);
const [selectedTab, setSelectedTab] = useState&lt;&#39;details&#39; | &#39;activity&#39;&gt;(&#39;details&#39;);
</code></pre>
<p>Do not install a state library for that.</p>
<p><code>useReducer</code> is good when one component owns a local state machine:</p>
<pre><code class="language-tsx">const [state, dispatch] = useReducer(policyWizardReducer, initialWizardState);
</code></pre>
<p>Still fine.</p>
<p>The trouble starts when state has to be shared, derived, observed, and changed from multiple places.</p>
<p>In the policy builder:</p>
<ul>
<li>The wizard footer needs to know whether any section is dirty.</li>
<li>The review page needs a derived summary.</li>
<li>Section components need local edits.</li>
<li>Autosave needs to react to draft changes.</li>
<li>Permission logic needs to disable actions.</li>
<li>Validation warnings need to attach to sections.</li>
</ul>
<p>You can put all of this in context. But context broadcasts. Every consumer becomes part of the same render conversation. You can split contexts. Then you manage context architecture. You can write selectors. Then you are building a state library slowly, without the debugging tools or mental model.</p>
<p>That is where Reatom enters.</p>
<h2>Why Reatom</h2>
<p>I like Reatom for workflows where state is a graph.</p>
<p>Small atoms. Derived atoms. Actions. Effects. Explicit dependencies. React components subscribe to the pieces they read, not to a giant provider value.</p>
<p>The important thing is not the library name. The important thing is the model:</p>
<blockquote>
<p>Break client state into small atoms, derive what can be derived, and make actions the place where transitions happen.</p>
</blockquote>
<p>That model fits complex frontend workflows better than a single reducer object or a large Zustand store.</p>
<p>Zustand is excellent for simple global stores. I use it happily for small apps: auth-ish UI state, sidebar preferences, feature flags, lightweight cross-component state.</p>
<p>But for CRM-scale workflows with derived state, undo, chained reactions, and local process logic, I prefer a graph. Reatom makes the graph visible.</p>
<h2>A Small Policy Builder Model</h2>
<p>Imagine the policy builder feature:</p>
<pre><code>src/
  features/
    build-policy/
      model/
        policy-builder.atoms.ts
        policy-builder.actions.ts
        policy-builder.selectors.ts
      ui/
        PolicyBuilderPage.tsx
        CoverageSection.tsx
        BeneficiariesSection.tsx
        ReviewPanel.tsx
      index.ts
</code></pre>
<p>The model belongs to the feature because the state exists for this workflow. It is not an entity. It is not shared. It is not a generic app store.</p>
<p>Start with atoms:</p>
<pre><code class="language-ts">// src/features/build-policy/model/policy-builder.atoms.ts
import { atom } from &#39;@reatom/core&#39;;

export interface PolicyDraft {
  holderName: string;
  coverageType: &#39;standard&#39; | &#39;premium&#39;;
  beneficiaries: BeneficiaryDraft[];
}

export const policyDraftAtom = atom&lt;PolicyDraft&gt;(
  {
    holderName: &#39;&#39;,
    coverageType: &#39;standard&#39;,
    beneficiaries: [],
  },
  &#39;policyDraftAtom&#39;,
);

export const activeSectionAtom = atom&lt;PolicySection&gt;(&#39;coverage&#39;, &#39;activeSectionAtom&#39;);

export const dirtySectionsAtom = atom&lt;Set&lt;PolicySection&gt;&gt;(
  new Set(),
  &#39;dirtySectionsAtom&#39;,
);
</code></pre>
<p>Small atoms. Named atoms. No giant <code>policyBuilderStore</code> object with everything inside.</p>
<p>Why?</p>
<p>Because different components care about different parts. The coverage section should not rerender because the beneficiaries list changed. The review panel might care about the full draft, but the active tab indicator only cares about <code>activeSectionAtom</code>.</p>
<h2>Derived State Should Be Derived</h2>
<p>Dirty state often gets stored badly.</p>
<pre><code class="language-ts">const [canSubmit, setCanSubmit] = useState(false);
</code></pre>
<p>Then you remember to update it after every draft change, every validation change, every permission change. Or you forget, and the button lies.</p>
<p>Derived state should be computed from source state:</p>
<pre><code class="language-ts">// src/features/build-policy/model/policy-builder.selectors.ts
import { atom } from &#39;@reatom/core&#39;;
import { dirtySectionsAtom, policyDraftAtom } from &#39;./policy-builder.atoms&#39;;

export const hasUnsavedChangesAtom = atom((ctx) =&gt; {
  return ctx.spy(dirtySectionsAtom).size &gt; 0;
}, &#39;hasUnsavedChangesAtom&#39;);

export const reviewSummaryAtom = atom((ctx) =&gt; {
  const draft = ctx.spy(policyDraftAtom);

  return {
    holder: draft.holderName || &#39;Missing holder name&#39;,
    coverage: draft.coverageType,
    beneficiaryCount: draft.beneficiaries.length,
  };
}, &#39;reviewSummaryAtom&#39;);
</code></pre>
<p>The exact Reatom API can vary by package setup, but the principle is stable: derived atoms observe source atoms and recompute when dependencies change.</p>
<p>No manual synchronization.</p>
<p>No effect that says &quot;when A changes, update B.&quot;</p>
<p>B is a function of A.</p>
<h2>Actions Are Transitions</h2>
<p>I do not want random components mutating atoms however they like.</p>
<p>For meaningful workflow changes, define actions:</p>
<pre><code class="language-ts">// src/features/build-policy/model/policy-builder.actions.ts
import { action } from &#39;@reatom/core&#39;;
import { dirtySectionsAtom, policyDraftAtom } from &#39;./policy-builder.atoms&#39;;

export const updateCoverageType = action(
  (ctx, coverageType: PolicyDraft[&#39;coverageType&#39;]) =&gt; {
    const draft = ctx.get(policyDraftAtom);

    policyDraftAtom(ctx, {
      ...draft,
      coverageType,
    });

    const dirtySections = new Set(ctx.get(dirtySectionsAtom));
    dirtySections.add(&#39;coverage&#39;);
    dirtySectionsAtom(ctx, dirtySections);
  },
  &#39;updateCoverageType&#39;,
);
</code></pre>
<p>The coverage type change is not just a setter. It changes the draft and marks a section dirty. Later it might clear dependent fields. Later it might add a warning. The action is where that transition belongs.</p>
<p>This is the main reason I like action-based state models. They create names for product events:</p>
<ul>
<li><code>updateCoverageType</code></li>
<li><code>removeBeneficiary</code></li>
<li><code>restoreDraft</code></li>
<li><code>markSectionReviewed</code></li>
<li><code>resetBuilder</code></li>
</ul>
<p>Those names are easier to reason about than a trail of <code>setState</code> calls.</p>
<h2>React Components Stay Small</h2>
<p>The component should read state and dispatch intent.</p>
<pre><code class="language-tsx">// src/features/build-policy/ui/CoverageSection.tsx
import { reatomComponent } from &#39;@reatom/npm-react&#39;;
import { policyDraftAtom } from &#39;../model/policy-builder.atoms&#39;;
import { updateCoverageType } from &#39;../model/policy-builder.actions&#39;;

export const CoverageSection = reatomComponent(({ ctx }) =&gt; {
  const draft = ctx.spy(policyDraftAtom);

  return (
    &lt;section&gt;
      &lt;h2&gt;Coverage&lt;/h2&gt;
      &lt;CoverageTypeSelect
        value={draft.coverageType}
        onChange={(coverageType) =&gt; updateCoverageType(ctx, coverageType)}
      /&gt;
    &lt;/section&gt;
  );
}, &#39;CoverageSection&#39;);
</code></pre>
<p>The component does not know how dirty state works. It does not know which dependent values reset. It says: the user changed coverage type.</p>
<p>The model handles the transition.</p>
<p>That separation is the payoff.</p>
<h2>Where Reatom Meets TanStack Query</h2>
<p>Query loads the server state. Reatom owns the local workflow.</p>
<p>When opening an existing policy builder, seed the draft from Query data:</p>
<pre><code class="language-tsx">export function PolicyBuilderRoute({ policyId }: { policyId: string }) {
  const policyQuery = useQuery(policyDetailQueryOptions(policyId));

  if (policyQuery.isPending) return &lt;PolicyBuilderSkeleton /&gt;;
  if (policyQuery.isError) return &lt;PolicyBuilderError onRetry={() =&gt; policyQuery.refetch()} /&gt;;

  return &lt;PolicyBuilderPage initialPolicy={policyQuery.data} /&gt;;
}
</code></pre>
<p>Then initialize the feature model at the boundary:</p>
<pre><code class="language-tsx">export function PolicyBuilderPage({ initialPolicy }: { initialPolicy: PolicyDetail }) {
  const draft = mapPolicyToDraft(initialPolicy);

  return (
    &lt;PolicyBuilderScope initialDraft={draft}&gt;
      &lt;CoverageSection /&gt;
      &lt;BeneficiariesSection /&gt;
      &lt;ReviewPanel /&gt;
    &lt;/PolicyBuilderScope&gt;
  );
}
</code></pre>
<p>The important boundary: server data becomes an initial draft. After that, the draft is client state.</p>
<p>When the user saves:</p>
<pre><code class="language-ts">export const savePolicyDraft = action(async (ctx) =&gt; {
  const draft = ctx.get(policyDraftAtom);

  await updatePolicyDraft(draft);

  dirtySectionsAtom(ctx, new Set());
}, &#39;savePolicyDraft&#39;);
</code></pre>
<p>Then invalidate Query data if needed:</p>
<pre><code class="language-ts">queryClient.invalidateQueries({ queryKey: policyKeys.detail(policyId) });
</code></pre>
<p>Do not keep the Query cache and draft atom synchronized on every keystroke. That turns two clear models into one blurry model.</p>
<h2>Effects Need Discipline</h2>
<p>Autosave is a good example.</p>
<p>It is tempting to put autosave in a component:</p>
<pre><code class="language-tsx">useEffect(() =&gt; {
  const id = setTimeout(() =&gt; saveDraft(draft), 1000);
  return () =&gt; clearTimeout(id);
}, [draft]);
</code></pre>
<p>That works until the component unmounts, remounts, gets split, or the draft changes from somewhere else.</p>
<p>For serious workflows, autosave should live near the state model. It reacts to the model, not to a particular component being mounted.</p>
<p>The exact Reatom effect API depends on setup, but the shape is what matters:</p>
<pre><code class="language-ts">export const autosaveDraft = action(async (ctx) =&gt; {
  const draft = ctx.get(policyDraftAtom);
  const hasChanges = ctx.get(hasUnsavedChangesAtom);

  if (!hasChanges) return;

  await savePolicyDraftToServer(draft);
  dirtySectionsAtom(ctx, new Set());
}, &#39;autosaveDraft&#39;);
</code></pre>
<p>Then schedule or trigger it from the feature boundary, not from every field.</p>
<p>Effects should be named. Effects should be cancellable when needed. Effects should not hide in five components.</p>
<h2>Undo and History</h2>
<p>This is where graph-based state starts to feel worth it.</p>
<p>Undo is awkward when state is scattered across components. It is manageable when workflow state has a model.</p>
<pre><code class="language-ts">export const draftHistoryAtom = atom&lt;PolicyDraft[]&gt;([], &#39;draftHistoryAtom&#39;);

export const commitDraftChange = action((ctx, nextDraft: PolicyDraft) =&gt; {
  const currentDraft = ctx.get(policyDraftAtom);
  const history = ctx.get(draftHistoryAtom);

  draftHistoryAtom(ctx, [...history, currentDraft]);
  policyDraftAtom(ctx, nextDraft);
}, &#39;commitDraftChange&#39;);

export const undoDraftChange = action((ctx) =&gt; {
  const history = ctx.get(draftHistoryAtom);
  const previousDraft = history.at(-1);

  if (!previousDraft) return;

  draftHistoryAtom(ctx, history.slice(0, -1));
  policyDraftAtom(ctx, previousDraft);
}, &#39;undoDraftChange&#39;);
</code></pre>
<p>Would I add undo to every form? No.</p>
<p>But when the product needs it, I want the state model to make it boring.</p>
<h2>What Not to Put in Reatom</h2>
<p>Reatom is not a dumping ground.</p>
<p>Do not put this in Reatom by default:</p>
<ul>
<li>fetched server data that belongs in TanStack Query</li>
<li>simple component booleans</li>
<li>URL state that belongs in TanStack Router search params</li>
<li>form field state already owned cleanly by TanStack Form</li>
<li>one-off UI state that never leaves a component</li>
</ul>
<p>The fact that a library can hold state does not mean it should hold all state.</p>
<p>My default split:</p>
<ul>
<li><strong>TanStack Query:</strong> server state</li>
<li><strong>TanStack Router:</strong> URL/navigation state</li>
<li><strong>TanStack Form:</strong> form field state and validation flow</li>
<li><strong>Reatom:</strong> complex client workflows and reactive graphs</li>
<li><strong>useState:</strong> local component details</li>
</ul>
<p>This split keeps each tool honest.</p>
<h2>The Review Checklist</h2>
<p>When I review client state, I ask:</p>
<ul>
<li>Is this state server-owned, URL-owned, form-owned, or client-owned?</li>
<li>Can it stay local with <code>useState</code>?</li>
<li>Is this derived value being stored manually?</li>
<li>Are components changing atoms directly, or dispatching named actions?</li>
<li>Does the feature model live near the feature?</li>
<li>Are effects named and centralized, or hidden in component trees?</li>
<li>Is Query cache being abused as a local store?</li>
<li>Can a new developer see the state graph without opening ten components?</li>
</ul>
<p>The last question matters most. State management is not about reducing lines. It is about making change propagation understandable.</p>
<h2>What Comes Next</h2>
<p>Reatom gives us a way to model complex client workflows. The next article goes one layer deeper: <a href="https://ma-x.im/blog/react-playbook-immutable-data-structures">immutable data structures in React</a>. Not as a purity lecture, but as the practical reason state graphs, memoization, optimistic updates, and undo histories stay predictable.</p>
<hr>
<p>The policy builder eventually stopped feeling like a form and started feeling like a small application. That was the important realization. Treating it as &quot;just component state&quot; made every requirement feel like an exception. Treating it as a workflow model made the requirements fit somewhere.</p>
<p>Not because Reatom was magic.</p>
<p>Because the state graph finally had a shape.</p>
<p>If you&#39;re working on a React feature where every small change triggers three unrelated effects, reach out. The problem may not be your components. It may be that the workflow wants a model of its own.</p>
]]></content:encoded>
      <pubDate>Mon, 23 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Reatom</category>
      <category>State Management</category>
      <category>Architecture</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Stop Quizzing Senior Engineers — Start Talking to Them (TL;DR)</title>
      <link>https://ma-x.im/blog/interviewing-senior-engineers-short</link>
      <guid isPermaLink="true">https://ma-x.im/blog/interviewing-senior-engineers-short</guid>
      <description>The condensed version: why conversational interviews beat trivia quizzes for senior roles, with practical examples and actionable advice.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/interviewing-senior-engineers-short/cover.png" alt="Stop Quizzing Senior Engineers — Start Talking to Them (TL;DR)" /></p>
<p>I recently wrote a <a href="https://ma-x.im/blog/interviewing-senior-engineers">detailed article</a> on this topic. It came out long because I wanted to cover every nuance — the full story, detailed interview examples, the AI angle, common mistakes, and practical formats. But I realize not everyone has 15 minutes to spare, so here is the short version covering only the essentials.</p>
<h2>The Problem</h2>
<p>Experienced engineers fail interviews. Not because they lack skill — because the interview tests the wrong thing.</p>
<p>I have over a decade of production frontend experience. TypeScript, React, enterprise products, design systems, team leadership. And I have failed interviews because I could not recall the exact behavior of <code>Promise.allSettled</code> under pressure or write a tree traversal algorithm from memory. These are things I almost never do by hand in real work.</p>
<p>The trivia-based interview format creates false negatives (rejecting experienced builders who do not drill interview prep) and false positives (passing candidates who memorized answers but cannot make architecture decisions). It tests recall under stress — a scenario that never occurs in actual engineering work.</p>
<h2>What Works Instead</h2>
<p>Talk to the candidate like a colleague. Ask about real scenarios and follow up on every answer.</p>
<p>The best interview I ever had was with a guy named John at a large Canadian company in the automotive industry. Two hours. No algorithms, no trivia. Just questions like:</p>
<ul>
<li>&quot;You inherit a React app with 200+ components and no design system. What is your first week like?&quot;</li>
<li>&quot;Two teams depend on the same component library but ship on different schedules. How do you handle versioning?&quot;</li>
<li>&quot;A junior on your team writes overly abstract code. How do you handle that conversation?&quot;</li>
</ul>
<p>Every answer led to a follow-up: &quot;What if the team pushes back?&quot; &quot;What if the deadline is two weeks?&quot; He was mapping how I think, not checking what I have memorized.</p>
<p>I got the job and stayed for three years. No whiteboard session would have predicted that outcome better.</p>
<h2>How Simple Questions Reveal Deep Expertise</h2>
<p>You do not need complex scenarios. Start simple and follow the thread.</p>
<p><strong>Example: &quot;Your team needs to support dark mode across the product. How do you approach it?&quot;</strong></p>
<p>A junior talks about CSS variables. A senior asks: &quot;How big is the product? Is there a design system? Does design have dark mode tokens ready? Is this a gradual rollout?&quot; Then follow up: &quot;Design only covers 40% of components — the rest is on you.&quot; Then: &quot;A senior dev wants to refactor all CSS first. What do you do?&quot; Then: &quot;Product wants to A/B test with 10% of users.&quot;</p>
<p>One question. Fifteen minutes. You see problem decomposition, ambiguity handling, leadership instincts, and technical depth. No algorithm required.</p>
<p><strong>Example: &quot;Users say the app feels slow. No specific page, just... slow. What do you do?&quot;</strong></p>
<p>A senior starts with: &quot;Before I touch code — what does slow mean to users? Initial load? Navigation? Interactions? Do we have metrics? Is it all users or specific segments?&quot; Then you push: &quot;Mainly mobile users on slow networks.&quot; Then: &quot;VP wants a fix in two weeks for a board meeting.&quot; Then: &quot;Backend says APIs are fine, it is purely frontend.&quot;</p>
<p>Two questions. Thirty minutes total. You know how this person operates in the real world.</p>
<h2>Ask About AI</h2>
<p>In 2026, skipping AI in a senior interview means missing critical signal. Some seniors write ten lines by hand per week and orchestrate AI agents for the rest. Others barely use AI. Both can be effective.</p>
<p>The issue: candidates will not volunteer their AI usage honestly unless you make it safe. There is still stigma.</p>
<p>Ask openly:</p>
<ul>
<li>&quot;Walk me through how you built the last feature you shipped — tools, process, everything.&quot;</li>
<li>&quot;When you use AI-generated code, what is your review process?&quot;</li>
<li>&quot;A developer submits a PR that is entirely AI-generated. They understand it, but did not write it by hand. How do you feel as a reviewer?&quot;</li>
</ul>
<p>You are evaluating judgment about tools and quality — not whether they use AI.</p>
<h2>Where This Breaks</h2>
<p><strong>Requires skilled interviewers.</strong> You cannot run this from an answer sheet. The interviewer needs domain depth and the ability to improvise follow-ups.</p>
<p><strong>Consistency is hard.</strong> Different interviewers, different questions, different evaluations. Shared rubrics and calibration sessions are essential.</p>
<p><strong>Charisma bias.</strong> Open conversations favor articulate, sociable candidates. Evaluate substance of answers, not delivery style.</p>
<p><strong>Takes more time.</strong> You need 60–90 minutes. This does not work for high-volume initial screening — use it in final rounds.</p>
<h2>Common Mistakes</h2>
<ul>
<li>Having a &quot;correct&quot; answer in your head. You are evaluating reasoning, not checking solutions.</li>
<li>Being too casual. &quot;Just a chat&quot; produces no signal. You need evaluation criteria.</li>
<li>Not following up. The first answer is surface level. Push with &quot;why?&quot;, &quot;what if?&quot;, &quot;what could go wrong?&quot;</li>
<li>Not calibrating across interviewers. Without shared rubrics, you get incomparable evaluations.</li>
<li>Treating AI usage as a red flag. The right follow-up to &quot;I use AI for 80% of code&quot; is &quot;how do you ensure quality?&quot; — not a concerned look.</li>
</ul>
<h2>Quick Recommendations</h2>
<p><strong>Interviewers:</strong> Build 5–8 scenario questions from real team challenges. Write a rubric with clear dimensions (problem decomposition, trade-off awareness, communication, technical depth, collaboration, leadership). Run calibration sessions quarterly. Ask about AI openly.</p>
<p><strong>Candidates:</strong> When you do not remember an API detail — say so and explain the concept. Prepare stories about real projects, not rehearsed definitions. Be honest about your AI workflow. Ask your own questions aggressively — they signal seniority more than your answers do. If the interview feels like an exam, that is a signal about the culture.</p>
<h2>Summary</h2>
<p>Senior interviews should test judgment, not recall. Simple scenario questions with deep follow-ups reveal more about an engineer&#39;s real capability than any algorithm or trivia quiz. It takes better interviewers and more time, but you hire people who can actually do the job. In an era where AI handles routine code generation, the quality of decisions is what makes a senior engineer valuable. Test for that.</p>
<p>For the full deep-dive with complete interview formats and extended examples, read the <a href="https://ma-x.im/blog/interviewing-senior-engineers">long version</a>.</p>
<hr>
<p><em>If you have thoughts on this or want to share your own interview experience — <a href="https://ma-x.im/contact">reach out</a>.</em></p>
]]></content:encoded>
      <pubDate>Sat, 21 Mar 2026 00:00:00 GMT</pubDate>
      <category>Interviews</category>
      <category>Engineering Culture</category>
      <category>Leadership</category>
      <category>Career</category>
      <category>TL;DR</category>
    </item>
    <item>
      <title>Stop Quizzing Senior Engineers — Start Talking to Them</title>
      <link>https://ma-x.im/blog/interviewing-senior-engineers</link>
      <guid isPermaLink="true">https://ma-x.im/blog/interviewing-senior-engineers</guid>
      <description>Why conversational interviews produce better hiring signal than algorithmic puzzles and API trivia — and how one interview with a guy named John changed how I think about hiring.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/interviewing-senior-engineers/cover.png" alt="Stop Quizzing Senior Engineers — Start Talking to Them" /></p>
<p>I have failed interviews. Multiple times. Not because I did not know my craft — I have been shipping production frontend for well over a decade. I failed because I could not recite the exact behavior of <code>Promise.allSettled</code> under pressure, or because I blanked on a tree traversal algorithm I had not hand-written since university. Meanwhile, the things I actually do every day — making architecture decisions, untangling legacy code, mentoring developers, designing systems that survive contact with real users — none of that was being tested.</p>
<p>This article is about what a good senior interview looks like. I know because I experienced one, and it changed everything.</p>
<h2>Context: The Interview Problem Nobody Talks About</h2>
<p>There is an uncomfortable truth in our industry. Many experienced engineers — people who have built and maintained complex production systems for years — routinely fail technical interviews.</p>
<p>Not because they are bad engineers. Because the interview tests a different skill than the job requires.</p>
<p>I work primarily with TypeScript and React. Promises? I use them every day — through async/await, through React Query, through framework abstractions. But when someone puts me in front of a whiteboard and says &quot;chain three promises, handle partial failures, and explain the microtask queue,&quot; I stumble. Not because I do not understand the concept. Because I do not carry that specific API surface in my active memory. I have not needed to.</p>
<p>Tree traversals and graph algorithms? Honestly — I do not enjoy them and I rarely encounter them in my daily work. When I need something like that, I reach for a library or write a straightforward recursive approach. I do not sit and think about BFS vs DFS complexity tradeoffs while building a dashboard for an enterprise client.</p>
<p>Logical puzzles? Same story. I can solve them given time and a calm environment. Under interview pressure, with someone watching me and a timer ticking? I freeze. And I know I am not alone.</p>
<p>Here is what bothers me most: I have seen engineers who drill LeetCode for months pass these interviews with flying colors, then struggle to make a single meaningful architecture decision in their first sprint. The interview selected for the wrong thing. It tested preparation discipline, not engineering capability.</p>
<p>I am not saying algorithmic knowledge is useless. I am saying that for a senior role, it should not be the primary filter. And yet — at most companies — it still is.</p>
<h2>The Interview with John</h2>
<p>Let me tell you about the best technical interview I have ever experienced.</p>
<p>It was at a large Canadian company. Well-known products — automotive industry. The kind of place where the frontend work is complex, the user base is massive, and the decisions you make on Monday are still affecting the product two years later.</p>
<p>My interviewer was John. I remember his name because what happened in that interview stuck with me permanently. I still feel a kind of gratitude toward him years later — not because he went easy on me, but because he showed me what a respectful, effective technical interview looks like.</p>
<p>We were scheduled for ninety minutes. We went over two hours. I did not notice the time passing. Not once did I feel like I was being tested. It felt like I was having a deep technical conversation with a colleague I respected.</p>
<p>John did not ask me what a closure is. He did not ask me to reverse a linked list. He did not ask me to explain the event loop.</p>
<p>Instead, he asked things like:</p>
<ul>
<li>&quot;You join a team and inherit a React app with 200+ components and no design system. What is your first week like?&quot;</li>
<li>&quot;The product team wants to support three different brands from the same codebase. How would you approach the frontend?&quot;</li>
<li>&quot;You notice the CI pipeline takes 40 minutes and developers are complaining. Where do you start?&quot;</li>
<li>&quot;Two teams depend on the same component library but ship on different schedules. How would you handle versioning?&quot;</li>
<li>&quot;A junior on your team writes overly abstract code — creates a generic wrapper for everything. How do you handle that conversation?&quot;</li>
</ul>
<p>Every answer I gave led somewhere. John would nod and then say, &quot;Okay, but what if the team pushes back on that approach?&quot; or &quot;What if the deadline is in two weeks?&quot; or &quot;What happens when the third team joins and they use a different state management solution?&quot; He was not looking for a specific answer. He was mapping how I think.</p>
<p>I got the job. I stayed for about three years. The team was strong, the work was meaningful, and as far as I could tell, the company did not regret the hire. That interview — with no algorithms, no trivia, no trick questions — predicted my actual job performance far better than any whiteboard session ever could.</p>
<p>That experience left me with a conviction: you learn more about a senior engineer in thirty minutes of genuine conversation than in two hours of trivia.</p>
<h2>Why Trivia-Based Senior Interviews Are Fundamentally Broken</h2>
<p>Let me be specific about the failure modes.</p>
<p><strong>The wrong skill is being tested.</strong> When you ask a senior engineer to write a <code>debounce</code> function from memory, you are testing recall. But a senior&#39;s actual value lies in knowing <em>when</em> to debounce, <em>where</em> it matters for user experience, and <em>whether</em> the framework already provides a better abstraction. The API signature is a Google search away. The judgment is not.</p>
<p><strong>False negatives destroy your pipeline.</strong> The most experienced candidates — the ones who have been building real systems instead of doing interview prep — are the ones most likely to fail trivia questions. They have been too busy shipping code to memorize it. You are systematically filtering out the people you want most.</p>
<p><strong>False positives are expensive.</strong> A candidate who spent three months on an interview prep platform will ace your quiz. They might know every closure edge case, every prototype chain gotcha, every <code>this</code> binding trap. But put them in front of a real architecture decision — &quot;should we build a micro-frontend or keep the monolith?&quot; — and they might have nothing to say. That mismatch costs you months of onboarding and potentially a bad hire.</p>
<p><strong>Stress is not signal.</strong> There is a pervasive belief that &quot;if they can perform under pressure, they will be good in production.&quot; This is wrong. Interview stress and production pressure are fundamentally different experiences. In production, you have context, tools, colleagues, and time to think. In an interview, you have a stranger staring at you while you try to remember if <code>Array.prototype.flat</code> takes a depth argument. These are not comparable situations. You are measuring anxiety tolerance, not engineering skill.</p>
<p><strong>It does not match how we actually work.</strong> In my daily work, I look things up constantly. I read documentation. I discuss approaches in Slack. I use AI to scaffold boilerplate. I do code reviews where I catch things I would not have remembered in an interview. The isolated, no-tools, no-references interview environment tests a scenario that literally never occurs in the actual job.</p>
<h2>The Conversational Interview: What It Looks Like in Practice</h2>
<p>The alternative is not &quot;just chatting.&quot; A conversational interview has structure, evaluation criteria, and clear goals. But it evaluates what matters: how the candidate thinks about problems, makes decisions, and handles complexity.</p>
<p>Let me walk through two complete examples. These are realistic — the kind of conversations you could have with a senior frontend candidate next week.</p>
<h3>Example 1: The Innocent Question That Reveals Everything</h3>
<p>Imagine you start the interview with this:</p>
<p><strong>&quot;Hey, so — your team has just been told that you need to support dark mode across the entire product. How do you approach this?&quot;</strong></p>
<p>This sounds almost too simple. Dark mode. Every junior knows about <code>prefers-color-scheme</code>. But this question is a depth charge. Watch what happens when you follow the thread.</p>
<p>A junior will talk about CSS variables and a toggle button. A mid-level will mention design tokens, maybe theming with styled-components or Tailwind.</p>
<p>A senior? A senior will start asking questions back:</p>
<ul>
<li>&quot;How big is the product? How many pages, how many components?&quot;</li>
<li>&quot;Is there a design system, or are styles scattered?&quot;</li>
<li>&quot;Does the design team have dark mode designs ready, or are we deriving them?&quot;</li>
<li>&quot;What is the CSS architecture — utility-first, CSS modules, CSS-in-JS?&quot;</li>
<li>&quot;Are there third-party components that do not support theming?&quot;</li>
<li>&quot;Is this a gradual rollout or a big-bang release?&quot;</li>
</ul>
<p>Now you are in a real conversation. Follow up with:</p>
<ul>
<li><p><strong>&quot;The design team says they will provide dark mode tokens, but only for the core components. The rest — about 60% of the UI — you have to figure out.&quot;</strong> This tests whether they can handle ambiguity and make pragmatic decisions about imperfect situations.</p>
</li>
<li><p><strong>&quot;One of the senior developers on the team insists on doing a full CSS refactor before implementing dark mode. He says the current styles are too messy. What do you do?&quot;</strong> This is no longer a technical question — it is a leadership and collaboration question. How do they handle disagreement? Do they see the tradeoff between ideal and practical?</p>
</li>
<li><p><strong>&quot;Product says they want to A/B test dark mode with 10% of users first.&quot;</strong> Now you are in feature flags, runtime theming, and state management territory. Do they think about persistence? Server-side rendering? Flash of unstyled content?</p>
</li>
</ul>
<p>One question. Fifteen minutes. You have learned more about this person&#39;s seniority than any algorithm challenge could reveal. You have seen how they decompose problems, how they handle incomplete information, how they think about people and process alongside code.</p>
<h3>Example 2: The Deceptively Simple Scenario</h3>
<p>Here is another one:</p>
<p><strong>&quot;A product manager comes to you and says: users are complaining that the app feels slow. No specific page, just... slow. What do you do?&quot;</strong></p>
<p>Again — sounds easy. But a senior engineer&#39;s answer to this question reveals an enormous amount about their experience.</p>
<p>The junior answer: &quot;I would use Lighthouse and fix the scores.&quot;</p>
<p>The mid-level answer: &quot;I would profile the app, check bundle size, look at network requests.&quot;</p>
<p>The senior answer starts with: &quot;Before I touch any code, I need to understand what &#39;slow&#39; means to the users.&quot;</p>
<ul>
<li>&quot;Is it initial load time? Navigation between pages? Interaction responsiveness? All of the above?&quot;</li>
<li>&quot;Do we have any metrics — Core Web Vitals, custom performance tracking, session recordings?&quot;</li>
<li>&quot;Is it slow for all users or specific segments — mobile, specific regions, specific browsers?&quot;</li>
<li>&quot;When did it start feeling slow? Was there a recent deployment that correlates?&quot;</li>
</ul>
<p>Follow up:</p>
<ul>
<li><p><strong>&quot;Turns out it is mainly mobile users in regions with slower networks. Desktop users are fine.&quot;</strong> This pushes toward real-world constraints — code splitting, server-side rendering, image optimization, CDN strategy. Do they think about the network layer, or only about JavaScript?</p>
</li>
<li><p><strong>&quot;The VP of Engineering wants a fix in two weeks because there is a board meeting.&quot;</strong> Now you are testing prioritization under pressure. Do they panic and promise everything? Do they push back with data? Do they suggest a phased approach — quick wins first, structural changes later?</p>
</li>
<li><p><strong>&quot;The backend team says the APIs are fine and the problem is purely on the frontend.&quot;</strong> How do they handle cross-team accountability? Do they accept that at face value, or do they know how to prove or disprove it with data?</p>
</li>
</ul>
<p>Two questions. Two conversations. Thirty minutes total. You now have a detailed picture of how this person operates in the real world. You know their technical depth, their communication style, their leadership instincts, and their ability to work with ambiguity. No whiteboard required.</p>
<h3>What Makes These Questions Work</h3>
<p>Notice what both examples have in common:</p>
<ul>
<li><strong>The initial question is approachable.</strong> It does not trigger &quot;I need to remember the right answer&quot; anxiety. It triggers &quot;let me think about this problem.&quot;</li>
<li><strong>Depth comes from follow-ups, not from the question itself.</strong> The interviewer&#39;s skill matters. You need to know where to push.</li>
<li><strong>There is no single correct answer.</strong> Multiple good approaches exist. What you evaluate is reasoning, awareness of tradeoffs, and the ability to adapt when the situation changes.</li>
<li><strong>They test real-world skills.</strong> Ambiguity handling, stakeholder management, prioritization, team dynamics — these are what senior engineers actually deal with every day.</li>
</ul>
<h2>The AI Question Every Senior Interview Needs in 2026</h2>
<p>Let me say something that might be controversial: if you are interviewing a senior engineer in 2026 and you do not ask about AI, you are missing a critical data point.</p>
<p>Here is the reality. I know engineers — good ones, experienced ones — who write maybe ten lines of code by hand per week. The rest is orchestrating AI agents. They prompt, they review, they refine, they iterate. And they are productive. They ship features, maintain quality, and make good architectural decisions. They are still seniors — arguably more effective seniors than before.</p>
<p>I also know engineers who barely use AI. They write everything by hand, they are meticulous, and they produce excellent work. Also valid.</p>
<p>The interesting question is not &quot;do you use AI?&quot; It is everything that comes after.</p>
<p>But here is the problem: candidates will not talk about this honestly unless you make it safe. There is still a stigma. Engineers worry that admitting heavy AI usage will make them look lazy or less skilled. So they downplay it. And you miss the real picture.</p>
<p>How to ask:</p>
<ul>
<li><strong>&quot;Walk me through how you built the last feature you shipped. Tools, process, everything.&quot;</strong> This is open-ended enough that they can naturally mention AI without feeling judged.</li>
<li><strong>&quot;When you use AI-generated code, what is your review process? What do you look for?&quot;</strong> This tells you whether they are using AI thoughtfully or blindly.</li>
<li><strong>&quot;Have you seen AI-generated code cause a problem in production? What happened?&quot;</strong> A senior who uses AI regularly will have a story. And how they handled it tells you about their quality standards.</li>
<li><strong>&quot;A developer on your team submits a PR that is entirely AI-generated. They understand it, but they did not write any of it by hand. How do you feel about that as a reviewer?&quot;</strong> This tests their philosophy about code ownership, maintainability, and team standards.</li>
</ul>
<p>What you are really evaluating is judgment. Does this person use AI as a power tool while maintaining engineering responsibility? Or are they outsourcing their thinking? There is a big difference, and a conversational interview is the only format that can surface it.</p>
<h2>Where Conversational Interviews Break Down</h2>
<p>I am not going to pretend this approach is perfect. It has real limitations.</p>
<p><strong>It requires skilled interviewers.</strong> Running a conversational interview well is hard. You need someone who knows the domain deeply enough to improvise follow-ups, challenge weak answers, and recognize strong ones even when they differ from what they expected. A trivia quiz can be run by anyone with an answer sheet. A conversational interview needs a senior engineer who is also a good communicator. Not every team has that.</p>
<p><strong>Consistency is genuinely difficult.</strong> When different interviewers ask different questions in different ways, comparing candidates becomes harder. Two candidates might both be strong, but the scores look different because one interviewer went deep on architecture while another focused on team dynamics. You need a shared rubric with explicit evaluation dimensions to mitigate this.</p>
<p><strong>It takes more time.</strong> A meaningful conversational interview needs 60 to 90 minutes. You cannot rush it to 30. For companies doing high-volume hiring, that is a real constraint.</p>
<p><strong>Charisma bias is a real risk.</strong> Open-ended conversations naturally favor people who are articulate, confident, and personable. Brilliant engineers who are introverted, or who communicate better in writing than in speech, or who think slowly and carefully, can get undervalued. Your rubric needs to account for this — evaluate the substance of answers, not the delivery.</p>
<p><strong>Some candidates genuinely prefer structured tests.</strong> Not everyone thrives in ambiguity. Some great engineers feel more comfortable with a clear problem, a clear solution, and a clear evaluation. Dismissing their preference entirely is its own form of bias.</p>
<h2>Common Mistakes When Running Conversational Interviews</h2>
<p><strong>Asking scenario questions but having a &quot;correct&quot; answer in your head.</strong> If you already decided that the right approach to the dark mode question is &quot;use CSS custom properties with a ThemeProvider,&quot; you are just running trivia with extra steps. The goal is to evaluate reasoning, not to check if they chose the same solution you would.</p>
<p><strong>Being too casual.</strong> &quot;Let&#39;s just chat&quot; produces zero usable signal. You need a plan: which scenarios, which follow-ups, what evaluation criteria. The conversation should feel informal. The evaluation should be rigorous.</p>
<p><strong>Not going deep enough.</strong> The first answer to any question is the surface layer. The signal is in the second and third follow-ups. &quot;Why that approach?&quot; &quot;What could go wrong?&quot; &quot;What would you do differently with more time?&quot; If you accept the first answer and move on, you are wasting the format.</p>
<p><strong>Failing to calibrate.</strong> Three interviewers running three different conversational interviews without shared criteria will produce three incomparable evaluations and an argument in the debrief meeting. Calibration sessions — where interviewers evaluate the same recorded interview and compare notes — are not optional.</p>
<p><strong>Testing outside the candidate&#39;s experience.</strong> If someone has spent their career in React and you ask them to design a distributed backend system, you will get a weak answer that tells you nothing. Tailor your scenarios to the role and the candidate&#39;s actual background.</p>
<p><strong>Treating AI usage as a negative signal.</strong> This one deserves its own bullet point because it is still happening everywhere. If a candidate openly says &quot;I use AI for 80% of my initial code generation,&quot; the correct follow-up is &quot;how do you ensure quality and maintainability?&quot; Not a raised eyebrow.</p>
<h2>When NOT To Use Conversational Interviews</h2>
<p>This format works best for senior, staff, and principal-level roles where judgment, systems thinking, and leadership matter more than implementation speed.</p>
<p>For <strong>junior roles</strong>, you need to verify fundamental skills. A coding exercise — even a simple one — is reasonable. You need to know they can write a function, debug an error, use basic data structures. A conversational interview would not produce enough signal at this level because junior candidates might not have enough experience to discuss.</p>
<p>For <strong>mid-level roles</strong>, a hybrid works well. A short practical task — maybe a small feature implementation or a debugging exercise — followed by a conversational segment about their approach and decisions.</p>
<p>For <strong>high-volume pipelines</strong> processing hundreds of applicants per week, conversational interviews do not work for initial screening. Use them in final rounds where signal quality matters most and the candidate pool is already filtered.</p>
<p>And if you honestly cannot train your interviewers to run these well — if they will default to &quot;just chatting&quot; without structure or evaluation criteria — then a structured technical assessment with a clear rubric will give you more consistent, if less insightful, results. A mediocre conversational interview is worse than a well-run structured one.</p>
<h2>How I Apply This</h2>
<p>When I interview senior candidates now, the first thing I say is: &quot;This is not a test. I want to understand how you think about problems.&quot; You can visibly see people relax. The dynamic shifts from &quot;exam&quot; to &quot;conversation.&quot; And that shift is where the good signal lives.</p>
<p>I pick two or three scenarios from projects I have actually worked on — problems where I already know the trade-offs, the failure modes, the &quot;it depends&quot; factors. This lets me push on follow-ups authentically instead of working from a script.</p>
<p>I pay close attention to how candidates handle what they do not know. Do they acknowledge the gap and think through it? Do they ask clarifying questions? Do they try to bluff? That single behavioral signal — comfort with uncertainty — correlates more strongly with real-world senior performance than any technical test I have seen.</p>
<p>I always ask about AI. Always. The answer tells me whether they are adapting to how our craft is evolving or clinging to how it used to be. Both types of engineers can be effective, but I want to know which one I am hiring.</p>
<p>The best senior hires I have been part of all came through conversational interviews. The worst mismatch I ever witnessed was someone who aced every technical quiz but could not have a productive discussion about trade-offs in their first sprint. They knew all the answers. They just could not apply them to real problems.</p>
<h2>Practical Recommendations</h2>
<p><strong>If you are an interviewer or hiring manager:</strong></p>
<ul>
<li>Build a library of 5–8 scenario questions drawn from real challenges your team has faced. Rotate them across interviews.</li>
<li>Write a rubric with clear dimensions: problem decomposition, trade-off awareness, communication clarity, technical depth, collaboration instincts, leadership signal. Score each dimension independently.</li>
<li>Run calibration sessions at least quarterly. Have two or three interviewers evaluate the same mock candidate (or a recorded session) and compare scores. Discuss disagreements.</li>
<li>Let interviews run long if the conversation is productive. Cutting off a great discussion at exactly 60 minutes because the calendar says so is counterproductive.</li>
<li>Ask about AI openly. Normalize it. The candidates who are honest about their AI workflow are giving you more useful information than those who claim they write everything by hand.</li>
</ul>
<p><strong>If you are a candidate:</strong></p>
<ul>
<li>When you do not remember an exact API or detail — say so, explain the concept, and describe how you would find it. This is what you would do at work. A good interviewer will not hold it against you. A bad one will, and that tells you something about the company.</li>
<li>Prepare stories, not answers. Think about real projects: problems you encountered, decisions you made, trade-offs you accepted, things you would do differently. Stories are memorable and specific. Rehearsed definitions are not.</li>
<li>Be honest about your AI workflow. If you use Copilot, Cursor, Claude, or any other tool heavily — own it. Explain how you review, validate, and maintain what gets generated. Hiding your actual workflow means the company is evaluating a version of you that does not exist.</li>
<li>Pay attention to how you are being interviewed. If it feels like a university exam — question after question, right or wrong, no discussion — that is a signal about the engineering culture. Use that information when you decide whether to accept or decline.</li>
<li>Ask your own questions aggressively. What is the team&#39;s architecture like? How are decisions made? What was the last big technical bet that did not work out? Your questions reveal your seniority more than your answers do.</li>
</ul>
<h2>Summary</h2>
<p>The best technical interview I ever had involved zero algorithms, zero API trivia, and zero whiteboard coding. It was a two-hour conversation with a guy named John at a large Canadian automotive company, where he asked me real questions about real problems and followed every answer with &quot;what if?&quot; I learned more about how interviews should work from that single experience than from dozens of standard ones on both sides of the table.</p>
<p>Senior interviews should test judgment. They should surface how a candidate thinks about ambiguity, trade-offs, team dynamics, and the realities of production systems. Conversational formats — scenario-based discussions, simple-sounding questions with deep follow-ups, collaborative problem-solving — do this better than any quiz.</p>
<p>They require better interviewers. They require shared rubrics and calibration. They take more time. But the trade-off is clear: you hire people who can actually do the job, not just people who can pass the test.</p>
<p>In an era where AI writes a significant chunk of our code, the thing that makes a senior engineer valuable is not what they can recite from memory. It is the quality of the decisions they make. Test for that.</p>
<hr>
<p><em>If you have thoughts on how senior interviews should work — or horror stories from the other side — <a href="https://ma-x.im/contact">I&#39;d love to hear them</a>.</em></p>
]]></content:encoded>
      <pubDate>Fri, 20 Mar 2026 00:00:00 GMT</pubDate>
      <category>Interviews</category>
      <category>Engineering Culture</category>
      <category>Leadership</category>
      <category>Career</category>
      <category>Long Read</category>
    </item>
    <item>
      <title>Routing with TanStack Router: Treat the URL Like Application State</title>
      <link>https://ma-x.im/blog/react-playbook-tanstack-router</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-tanstack-router</guid>
      <description>Routing is not just rendering a page for a path. In serious React apps, the URL carries product state: filters, tabs, pagination, selected records, redirects, and preload decisions. TanStack Router makes that state typed instead of hopeful.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-tanstack-router/cover.png" alt="Routing with TanStack Router: Treat the URL Like Application State" /></p>
<p>The policy list bug started as a support ticket.</p>
<p>An agent filtered policies by status, page, and renewal date. She copied the URL and sent it to another agent. The second agent opened it and saw a different list.</p>
<p>Same page. Same product. Different state.</p>
<p>The filters were not in the URL. The page number lived in local component state. The selected tab lived in a store. The search box was debounced into a fetch call, but never synchronized back to navigation. The route looked shareable. It wasn&#39;t.</p>
<p>Honestly, that bug changed how I think about routing.</p>
<p>Routing is not just &quot;which component renders for this path.&quot; That is the small version. In product software, routing is about preserving intent. What screen is the user on? Which record? Which filters? Which tab? Which page? Can they reload and get the same view? Can they send the link to someone else and trust it?</p>
<p>I&#39;ve seen teams treat the URL as decoration. Then they spend months rebuilding browser behavior manually.</p>
<p>TanStack Router gives React apps a better option: typed routes, typed params, typed search, loaders, redirects, preloading, and integration with TanStack Query. The point is not fancy routing. The point is making navigation state explicit.</p>
<blockquote>
<p>A URL is not a string you push into history. It is part of your application&#39;s state model.</p>
</blockquote>
<p>This is article #6 in the <strong>React Playbook</strong> series. We already covered the stack, TypeScript patterns, code structure, error handling, and TanStack Query. Now we connect those pieces through routing.</p>
<h2>Why TanStack Router</h2>
<p>React Router is familiar. It works. I used it for years.</p>
<p>The reason I reach for TanStack Router in new TypeScript-heavy apps is not that React Router is bad. It is that TanStack Router makes more route mistakes impossible.</p>
<p>With TanStack Router, routes are registered into a typed route tree. That means:</p>
<ul>
<li><code>&lt;Link to=&quot;/policies/$policyId&quot;&gt;</code> knows which params are required.</li>
<li><code>useParams({ from: &#39;/policies/$policyId&#39; })</code> returns the right param type.</li>
<li><code>useSearch({ from: &#39;/policies&#39; })</code> returns validated search state.</li>
<li><code>navigate({ to, params, search })</code> is checked against the actual route.</li>
<li>Loaders can use route context and params safely.</li>
</ul>
<p>The difference is subtle until a route changes.</p>
<p>Rename <code>$policyId</code> to <code>$id</code>, and TypeScript tells you everywhere the old param is still used. Remove a search param, and navigation calls fail to compile. That is the kind of friction I want.</p>
<p>Not because I enjoy types for their own sake.</p>
<p>Because broken links and stale URL assumptions are production bugs.</p>
<h2>File-Based or Code-Based Routes?</h2>
<p>TanStack Router supports both file-based and code-based routing.</p>
<p>For large teams, I like file-based routing. It creates a predictable structure, works well with route code splitting, and keeps route files close to pages.</p>
<p>For teaching and small early projects, code-based routing is easier to see in one place.</p>
<p>This article uses a file-based shape because it is what I would use once the app grows:</p>
<pre><code>src/
  routes/
    __root.tsx
    index.tsx
    policies/
      index.tsx
      $policyId.tsx
      $policyId.edit.tsx
  routeTree.gen.ts
</code></pre>
<p>In an FSD-style project, I still keep page UI in <code>pages/</code> and let route files wire it:</p>
<pre><code>src/
  routes/
    policies/
      index.tsx
      $policyId.tsx
  pages/
    policy-list/
      PolicyListPage.tsx
    policy-detail/
      PolicyDetailPage.tsx
</code></pre>
<p>Route files are integration points. They connect URL shape, loader behavior, search validation, and page components. They should not become business logic containers.</p>
<h2>The Root Route</h2>
<p>The root route is where app-level layout and route context begin.</p>
<pre><code class="language-tsx">// src/routes/__root.tsx
import { Outlet, createRootRouteWithContext } from &#39;@tanstack/react-router&#39;;
import type { QueryClient } from &#39;@tanstack/react-query&#39;;
import { RootLayout } from &#39;@/app/RootLayout&#39;;

interface RouterContext {
  queryClient: QueryClient;
  auth: AuthSession | null;
}

export const Route = createRootRouteWithContext&lt;RouterContext&gt;()({
  component: RootRoute,
});

function RootRoute() {
  return (
    &lt;RootLayout&gt;
      &lt;Outlet /&gt;
    &lt;/RootLayout&gt;
  );
}
</code></pre>
<p>That context matters. Later, route loaders can access <code>queryClient</code> without importing a singleton. Auth checks can use the current session. Tests can provide a different context.</p>
<p>Then create the router:</p>
<pre><code class="language-ts">// src/app/router.ts
import { createRouter } from &#39;@tanstack/react-router&#39;;
import { routeTree } from &#39;@/routeTree.gen&#39;;
import { queryClient } from &#39;./query-client&#39;;
import { authSession } from &#39;./auth-session&#39;;

export const router = createRouter({
  routeTree,
  context: {
    queryClient,
    auth: authSession,
  },
});

declare module &#39;@tanstack/react-router&#39; {
  interface Register {
    router: typeof router;
  }
}
</code></pre>
<p>The <code>Register</code> declaration is the bridge that gives the rest of the app type-safe navigation. Skip it and you lose much of the point.</p>
<h2>Route Params Should Be Boring</h2>
<p>A policy detail route:</p>
<pre><code class="language-tsx">// src/routes/policies/$policyId.tsx
import { createFileRoute } from &#39;@tanstack/react-router&#39;;
import { PolicyDetailPage } from &#39;@/pages/policy-detail&#39;;

export const Route = createFileRoute(&#39;/policies/$policyId&#39;)({
  component: PolicyDetailRoute,
});

function PolicyDetailRoute() {
  const { policyId } = Route.useParams();

  return &lt;PolicyDetailPage policyId={policyId} /&gt;;
}
</code></pre>
<p><code>policyId</code> is typed from the route path. No manual generic. No <code>as string</code>. No &quot;I hope this param exists.&quot;</p>
<p>Navigation is typed too:</p>
<pre><code class="language-tsx">import { Link } from &#39;@tanstack/react-router&#39;;

export function PolicyRow({ policy }: { policy: Policy }) {
  return (
    &lt;Link to=&quot;/policies/$policyId&quot; params={{ policyId: policy.id }}&gt;
      {policy.policyNumber}
    &lt;/Link&gt;
  );
}
</code></pre>
<p>If you forget <code>policyId), TypeScript complains. If you pass </code>id<code>instead of</code>policyId`, TypeScript complains. That is boring in the best possible way.</p>
<h2>Search Params Are Product State</h2>
<p>The policy list route is where TanStack Router starts to feel different.</p>
<p>Filters belong in the URL:</p>
<pre><code>/policies?status=active&amp;page=2&amp;query=miller
</code></pre>
<p>Not because URLs are pretty. Because users reload pages, bookmark views, share links, and go back in browser history.</p>
<p>Define the search shape:</p>
<pre><code class="language-tsx">// src/routes/policies/index.tsx
import { createFileRoute } from &#39;@tanstack/react-router&#39;;
import { z } from &#39;zod&#39;;
import { PolicyListPage } from &#39;@/pages/policy-list&#39;;

const policyListSearchSchema = z.object({
  status: z.enum([&#39;active&#39;, &#39;expired&#39;, &#39;pending&#39;]).optional(),
  page: z.number().int().positive().catch(1),
  query: z.string().catch(&#39;&#39;),
});

export const Route = createFileRoute(&#39;/policies/&#39;)({
  validateSearch: policyListSearchSchema,
  component: PolicyListRoute,
});

function PolicyListRoute() {
  const search = Route.useSearch();

  return &lt;PolicyListPage filters={search} /&gt;;
}
</code></pre>
<p>Now <code>search.page</code> is a number. Not a string you parsed manually. Not <code>string | undefined</code>. A number.</p>
<p>This matters because search params are the easiest place for type lies to enter an app. The browser gives you strings. Your product wants domain state. TanStack Router gives you a validation boundary.</p>
<h2>Updating Search Without Losing State</h2>
<p>When the user changes a filter:</p>
<pre><code class="language-tsx">import { useNavigate } from &#39;@tanstack/react-router&#39;;

export function PolicyFilters({ filters }: { filters: PolicyListFilters }) {
  const navigate = useNavigate({ from: &#39;/policies/&#39; });

  return (
    &lt;StatusSelect
      value={filters.status}
      onChange={(status) =&gt; {
        navigate({
          search: (prev) =&gt; ({
            ...prev,
            status,
            page: 1,
          }),
        });
      }}
    /&gt;
  );
}
</code></pre>
<p>Notice the <code>page: 1</code>.</p>
<p>Changing status should reset pagination. This is a product rule. Put it where navigation state changes. Otherwise you end up on page 7 of a filter that only has one page and someone files a &quot;missing policies&quot; bug.</p>
<p>For pagination:</p>
<pre><code class="language-tsx">export function PolicyPagination({ page }: { page: number }) {
  const navigate = useNavigate({ from: &#39;/policies/&#39; });

  return (
    &lt;Pagination
      page={page}
      onPageChange={(nextPage) =&gt; {
        navigate({
          search: (prev) =&gt; ({
            ...prev,
            page: nextPage,
          }),
        });
      }}
    /&gt;
  );
}
</code></pre>
<p>No local <code>useState</code>. The URL is the state.</p>
<h2>Connecting Router to Query</h2>
<p>Article #5 covered query options. Routing is where they pay off.</p>
<pre><code class="language-tsx">// src/routes/policies/index.tsx
export const Route = createFileRoute(&#39;/policies/&#39;)({
  validateSearch: policyListSearchSchema,
  loaderDeps: ({ search }) =&gt; ({ search }),
  loader: ({ context, deps }) =&gt;
    context.queryClient.ensureQueryData(policiesQueryOptions(deps.search)),
  component: PolicyListRoute,
});
</code></pre>
<p>The route validates search. The loader uses validated search to warm the query cache. The page subscribes to the same query:</p>
<pre><code class="language-tsx">function PolicyListRoute() {
  const search = Route.useSearch();
  const policiesQuery = useQuery(policiesQueryOptions(search));

  return &lt;PolicyListPage filters={search} policiesQuery={policiesQuery} /&gt;;
}
</code></pre>
<p>One data definition. Two usages.</p>
<p>That is the shape I want: route loaders handle navigation-level data readiness; components still use Query for cache subscription, background refetching, and UI state.</p>
<p>Don&#39;t fetch in the loader and pass raw data into the component while also using Query elsewhere. That creates two data paths. Use the loader to fill the cache.</p>
<h2>Detail Routes and Not Found</h2>
<p>For detail pages:</p>
<pre><code class="language-tsx">// src/routes/policies/$policyId.tsx
import { createFileRoute, notFound } from &#39;@tanstack/react-router&#39;;
import { policyDetailQueryOptions } from &#39;@/entities/policy&#39;;
import { isNotFoundError } from &#39;@/shared/api&#39;;
import { PolicyDetailPage } from &#39;@/pages/policy-detail&#39;;

export const Route = createFileRoute(&#39;/policies/$policyId&#39;)({
  loader: async ({ context, params }) =&gt; {
    try {
      return await context.queryClient.ensureQueryData(
        policyDetailQueryOptions(params.policyId),
      );
    } catch (error) {
      if (isNotFoundError(error)) throw notFound();
      throw error;
    }
  },
  component: PolicyDetailRoute,
  notFoundComponent: PolicyNotFound,
  errorComponent: PolicyRouteError,
});

function PolicyDetailRoute() {
  const { policyId } = Route.useParams();
  const policyQuery = useQuery(policyDetailQueryOptions(policyId));

  return &lt;PolicyDetailPage policy={policyQuery.data} /&gt;;
}
</code></pre>
<p>The route decides whether the page can exist. A missing policy is not the same as a temporary server failure. This lines up with the error-handling model from article #4.</p>
<h2>Route Guards with beforeLoad</h2>
<p>Some routes require authentication or permissions.</p>
<p>Use <code>beforeLoad</code> for route-level guards:</p>
<pre><code class="language-tsx">// src/routes/admin.tsx
import { createFileRoute, redirect } from &#39;@tanstack/react-router&#39;;
import { AdminPage } from &#39;@/pages/admin&#39;;

export const Route = createFileRoute(&#39;/admin&#39;)({
  beforeLoad: ({ context, location }) =&gt; {
    if (!context.auth) {
      throw redirect({
        to: &#39;/login&#39;,
        search: {
          redirectTo: location.href,
        },
      });
    }

    if (context.auth.role !== &#39;admin&#39;) {
      throw redirect({ to: &#39;/policies&#39; });
    }
  },
  component: AdminPage,
});
</code></pre>
<p>This is not a replacement for backend authorization. Never trust the frontend for security.</p>
<p>But it is the right place to keep users out of UI they cannot use. It also preserves intent: after login, send them back to where they tried to go.</p>
<h2>Preloading on Intent</h2>
<p>Routing should feel instant when intent is clear.</p>
<p>TanStack Router can preload routes through links. TanStack Query can prefetch query data. Use both deliberately.</p>
<pre><code class="language-tsx">export function PolicyRow({ policy }: { policy: Policy }) {
  return (
    &lt;Link
      to=&quot;/policies/$policyId&quot;
      params={{ policyId: policy.id }}
      preload=&quot;intent&quot;
    &gt;
      {policy.policyNumber}
    &lt;/Link&gt;
  );
}
</code></pre>
<p>When the user hovers or focuses the link, the router can prepare the destination. If your route loader calls <code>ensureQueryData</code>, the query cache warms too.</p>
<p>That is the clean integration: link intent -&gt; route preload -&gt; loader -&gt; query cache.</p>
<p>The user clicks, and the page is already halfway there.</p>
<h2>Use Route APIs Inside Route Components</h2>
<p>TanStack Router gives each route a <code>Route</code> object. Use it.</p>
<pre><code class="language-tsx">function PolicyListRoute() {
  const search = Route.useSearch();
  const navigate = Route.useNavigate();

  // ...
}
</code></pre>
<p>This is more precise than global hooks when you know which route you are in. The types are anchored to that route. Refactors stay local.</p>
<p>In leaf page components, I still prefer passing explicit props:</p>
<pre><code class="language-tsx">function PolicyListRoute() {
  const search = Route.useSearch();
  const policiesQuery = useQuery(policiesQueryOptions(search));

  return &lt;PolicyListPage filters={search} policiesQuery={policiesQuery} /&gt;;
}
</code></pre>
<p>Why not let <code>PolicyListPage</code> call <code>Route.useSearch()</code> directly?</p>
<p>Sometimes that is fine. But I like keeping page components easier to test and less coupled to router internals. The route knows about routing. The page knows about rendering the product view.</p>
<h2>Don&#39;t Hide Navigation in Random Helpers</h2>
<p>One pattern I avoid:</p>
<pre><code class="language-ts">export function openPolicy(policyId: string) {
  router.navigate({ to: &#39;/policies/$policyId&#39;, params: { policyId } });
}
</code></pre>
<p>It looks convenient. It spreads navigation decisions into random helpers. Over time, you get hidden redirects, surprising history behavior, and navigation that bypasses route-local search state.</p>
<p>Prefer explicit links and route-local navigation. If a navigation pattern repeats, create a component:</p>
<pre><code class="language-tsx">export function PolicyLink({
  policyId,
  children,
}: {
  policyId: string;
  children: React.ReactNode;
}) {
  return (
    &lt;Link to=&quot;/policies/$policyId&quot; params={{ policyId }} preload=&quot;intent&quot;&gt;
      {children}
    &lt;/Link&gt;
  );
}
</code></pre>
<p>Now navigation stays visible in the UI tree.</p>
<h2>The Review Checklist</h2>
<p>When I review routing code, I ask:</p>
<ul>
<li>Is shareable product state in the URL?</li>
<li>Are search params validated at the route boundary?</li>
<li>Do search changes preserve or reset related state intentionally?</li>
<li>Do route loaders reuse TanStack Query options instead of inventing separate fetch paths?</li>
<li>Are detail routes handling not-found separately from server errors?</li>
<li>Are protected routes guarded in <code>beforeLoad</code>?</li>
<li>Are links using typed <code>to</code>, <code>params</code>, and <code>search</code> instead of string concatenation?</li>
<li>Is preloading used where user intent is clear?</li>
<li>Are page components receiving explicit props when that keeps them easier to test?</li>
</ul>
<p>Most routing bugs are not about paths. They are about state that should have been in the URL but wasn&#39;t.</p>
<h2>What Comes Next</h2>
<p>With Query and Router in place, the next question is client state: the data that does not live on the server but still becomes too complex for <code>useState</code>.</p>
<p><strong>Article #7:</strong> <a href="https://ma-x.im/blog/react-playbook-reatom-state-management">state management with Reatom</a> — why I use it in CRM-scale React apps, where it fits next to TanStack Query, and why I don&#39;t reach for Zustand by default in this particular stack.</p>
<hr>
<p>After the policy-list support ticket, we moved filters, page, and search into route search params. The bug disappeared. More importantly, the product started behaving like a web app again. Refresh worked. Back worked. Shared links worked. QA could send exact states to developers instead of screenshots and vague reproduction steps.</p>
<p>That is what routing should do.</p>
<p>Not just render components.</p>
<p>Preserve intent.</p>
<p>If your React app has pages that look shareable but aren&#39;t, start with the URL. The fix is often not another store. It is admitting that the route already owns more state than your code says it does.</p>
]]></content:encoded>
      <pubDate>Thu, 19 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TanStack Router</category>
      <category>Routing</category>
      <category>TypeScript</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>React Data Fetching with TanStack Query: The Patterns That Keep Apps Calm</title>
      <link>https://ma-x.im/blog/react-playbook-data-fetching</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-data-fetching</guid>
      <description>TanStack Query is not just a better useEffect. It is the server-state layer of your React app. The hard part is not fetching data — it is designing keys, freshness, invalidation, pagination, and prefetching so the UI stays predictable.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-data-fetching/cover.png" alt="React Data Fetching with TanStack Query: The Patterns That Keep Apps Calm" /></p>
<p>Three months into the insurance CRM, we had a page that felt haunted.</p>
<p>The policy list would load. Then it would blink. Then it would refetch after switching tabs. Then a status change would update one row but leave another count stale. Sometimes the detail page showed fresh data. Sometimes it showed yesterday&#39;s version until someone manually refreshed the browser.</p>
<p>Nobody had written obviously bad code.</p>
<p>There was a <code>useEffect</code> here, a loading boolean there, a bit of local state for filters, a manual refetch after save, and a few defensive checks around undefined data. Each decision made sense in isolation. Together, they formed the kind of frontend state machine nobody wants to debug.</p>
<p>I&#39;ve seen this pattern more times than I can count. Teams think their data-fetching problem is &quot;how do we call the API?&quot; That is the easy part. The real problem is ownership: who decides whether data is fresh, where it is cached, what invalidates it, what happens during pagination, and what the user sees while the network is moving.</p>
<p>That is why TanStack Query matters.</p>
<p>Not because it fetches.</p>
<p>Because it gives server state a home.</p>
<blockquote>
<p>The hardest part of data fetching is not the request. It is the lifecycle around the request.</p>
</blockquote>
<p>This is article #5 in the <strong>React Playbook</strong> series. We already covered the starting stack, TypeScript patterns, code structure, and error handling. Now we get to the layer that makes most React apps feel either calm or twitchy: TanStack Query.</p>
<h2>Server State Is Not Client State</h2>
<p>The first mental shift is simple but important.</p>
<p>Server state is not yours.</p>
<p>It lives somewhere else. It can change without your app knowing. It can be stale. It can fail. It can be shared by multiple users. It has a network boundary, a cache lifetime, and a synchronization problem.</p>
<p>Client state is different. A dialog is open or closed. A sidebar is collapsed. A draft field has local input. That state belongs to the browser session.</p>
<p>The mistake is mixing the two.</p>
<pre><code class="language-tsx">const [policies, setPolicies] = useState&lt;Policy[]&gt;([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState&lt;Error | null&gt;(null);

useEffect(() =&gt; {
  setIsLoading(true);

  fetchPolicies()
    .then(setPolicies)
    .catch(setError)
    .finally(() =&gt; setIsLoading(false));
}, []);
</code></pre>
<p>This looks harmless. It is fine for a demo. In production, it immediately raises questions:</p>
<ul>
<li>What happens when another component needs the same policies?</li>
<li>How long is this data fresh?</li>
<li>Who retries?</li>
<li>Who cancels the request if the component unmounts?</li>
<li>What invalidates this list after a mutation?</li>
<li>What happens when filters change?</li>
<li>What happens when the user leaves and comes back?</li>
</ul>
<p>You can answer all of those manually. You can also spend the next year rediscovering why caching is hard.</p>
<p>TanStack Query is the place where those answers live.</p>
<h2>Start with Query Options, Not Custom Hooks Everywhere</h2>
<p>My default pattern in larger apps is to define query options at the entity boundary.</p>
<pre><code class="language-ts">// src/entities/policy/api/policy.queries.ts
import { queryOptions } from &#39;@tanstack/react-query&#39;;
import { fetchPolicies, fetchPolicyById } from &#39;./policy.api&#39;;
import type { PolicyListFilters } from &#39;../model/policy.types&#39;;

export const policyKeys = {
  all: [&#39;policies&#39;] as const,
  lists: () =&gt; [...policyKeys.all, &#39;list&#39;] as const,
  list: (filters: PolicyListFilters) =&gt; [...policyKeys.lists(), filters] as const,
  details: () =&gt; [...policyKeys.all, &#39;detail&#39;] as const,
  detail: (policyId: string) =&gt; [...policyKeys.details(), policyId] as const,
};

export function policiesQueryOptions(filters: PolicyListFilters) {
  return queryOptions({
    queryKey: policyKeys.list(filters),
    queryFn: () =&gt; fetchPolicies(filters),
    staleTime: 1000 * 60 * 2,
  });
}

export function policyDetailQueryOptions(policyId: string) {
  return queryOptions({
    queryKey: policyKeys.detail(policyId),
    queryFn: () =&gt; fetchPolicyById(policyId),
    staleTime: 1000 * 60 * 5,
  });
}
</code></pre>
<p>Then components use the options:</p>
<pre><code class="language-tsx">// src/pages/policy-list/PolicyListPage.tsx
import { useQuery } from &#39;@tanstack/react-query&#39;;
import { policiesQueryOptions } from &#39;@/entities/policy&#39;;

export function PolicyListPage() {
  const filters = usePolicyListFilters();
  const policiesQuery = useQuery(policiesQueryOptions(filters));

  if (policiesQuery.isPending) return &lt;PolicyListSkeleton /&gt;;
  if (policiesQuery.isError) return &lt;PolicyListError onRetry={() =&gt; policiesQuery.refetch()} /&gt;;

  return &lt;PolicyTable policies={policiesQuery.data.items} /&gt;;
}
</code></pre>
<p>Why not just create <code>usePoliciesQuery(filters)</code>?</p>
<p>Sometimes I do. But query options compose better. The same object can be used by <code>useQuery</code>, route loaders, prefetching, tests, and invalidation helpers. It keeps the query definition in one place without forcing every usage through a hook.</p>
<p>The hook becomes optional sugar, not the source of truth.</p>
<h2>Query Keys Are Architecture</h2>
<p>Query keys look like small arrays. They are not small.</p>
<p>They are the identity of your server state.</p>
<p>If your keys are inconsistent, invalidation becomes guesswork:</p>
<pre><code class="language-ts">useQuery({ queryKey: [&#39;policy&#39;, policyId], queryFn: ... });
useQuery({ queryKey: [&#39;policies&#39;, &#39;detail&#39;, id], queryFn: ... });
queryClient.invalidateQueries({ queryKey: [&#39;policy&#39;] });
</code></pre>
<p>This is how stale UI survives after a successful mutation.</p>
<p>Use factories:</p>
<pre><code class="language-ts">export const policyKeys = {
  all: [&#39;policies&#39;] as const,
  lists: () =&gt; [...policyKeys.all, &#39;list&#39;] as const,
  list: (filters: PolicyListFilters) =&gt; [...policyKeys.lists(), filters] as const,
  details: () =&gt; [...policyKeys.all, &#39;detail&#39;] as const,
  detail: (policyId: string) =&gt; [...policyKeys.details(), policyId] as const,
};
</code></pre>
<p>This gives you hierarchy:</p>
<pre><code class="language-ts">queryClient.invalidateQueries({ queryKey: policyKeys.lists() });
queryClient.invalidateQueries({ queryKey: policyKeys.detail(policyId) });
queryClient.invalidateQueries({ queryKey: policyKeys.all });
</code></pre>
<p>Invalidate all policies. Or only lists. Or one detail. The key shape makes the choice explicit.</p>
<p>The rule is straightforward:</p>
<blockquote>
<p>If a variable changes what the query fetches, that variable belongs in the query key.</p>
</blockquote>
<p>Filters, page number, cursor, search term, sort order, preview mode, tenant id. Put them in the key. TanStack Query cannot cache what you do not identify.</p>
<h2>Freshness Is a Product Decision</h2>
<p>TanStack Query&#39;s defaults are intentionally active: cached data is stale by default, stale queries refetch on mount, window focus, and reconnect, and inactive queries are garbage-collected after a default cache window.</p>
<p>Those defaults are good for learning and safe for many apps. They are not a substitute for product thinking.</p>
<p>In a CRM, different data has different freshness needs.</p>
<pre><code class="language-ts">export function policyDetailQueryOptions(policyId: string) {
  return queryOptions({
    queryKey: policyKeys.detail(policyId),
    queryFn: () =&gt; fetchPolicyById(policyId),
    staleTime: 1000 * 60 * 5,
  });
}

export function policyActivityQueryOptions(policyId: string) {
  return queryOptions({
    queryKey: [...policyKeys.detail(policyId), &#39;activity&#39;],
    queryFn: () =&gt; fetchPolicyActivity(policyId),
    staleTime: 1000 * 30,
  });
}

export function coverageTypesQueryOptions() {
  return queryOptions({
    queryKey: [&#39;coverage-types&#39;],
    queryFn: fetchCoverageTypes,
    staleTime: 1000 * 60 * 60,
    gcTime: 1000 * 60 * 60,
  });
}
</code></pre>
<p>Policy detail can be fresh for five minutes. Activity should refresh more aggressively. Coverage types barely change during a session.</p>
<p>This is the kind of decision I want visible in code. If every query inherits one global <code>staleTime</code>, you are pretending all data has the same volatility.</p>
<p>It doesn&#39;t.</p>
<h2>Background Fetching Without UI Jank</h2>
<p>One of TanStack Query&#39;s best features is that it separates initial loading from background fetching.</p>
<pre><code class="language-tsx">export function PolicyListPage() {
  const filters = usePolicyListFilters();
  const policiesQuery = useQuery(policiesQueryOptions(filters));

  if (policiesQuery.isPending) return &lt;PolicyListSkeleton /&gt;;
  if (policiesQuery.isError) return &lt;PolicyListError onRetry={() =&gt; policiesQuery.refetch()} /&gt;;

  return (
    &lt;&gt;
      {policiesQuery.isFetching &amp;&amp; &lt;RefreshingIndicator /&gt;}
      &lt;PolicyTable policies={policiesQuery.data.items} /&gt;
    &lt;/&gt;
  );
}
</code></pre>
<p><code>isPending</code> means there is no data yet.</p>
<p><code>isFetching</code> means a request is currently happening.</p>
<p>Those are not the same UX. If you show a full-page skeleton every time <code>isFetching</code> is true, your app will blink constantly. Keep the old data visible. Show a small refreshing indicator if the user needs to know.</p>
<p>This is one of the places where React apps start feeling calm.</p>
<h2>Pagination Without Flicker</h2>
<p>Pagination is where naive data fetching becomes obvious.</p>
<pre><code class="language-tsx">const policiesQuery = useQuery({
  queryKey: [&#39;policies&#39;, page],
  queryFn: () =&gt; fetchPolicies({ page }),
});
</code></pre>
<p>Technically correct. Bad experience.</p>
<p>Every page is a different query key, so moving from page 1 to page 2 can bounce the UI back into a pending state. For tables, that feels like the screen is being rebuilt under the user&#39;s hands.</p>
<p>Use <code>placeholderData: keepPreviousData</code>:</p>
<pre><code class="language-tsx">import { keepPreviousData, useQuery } from &#39;@tanstack/react-query&#39;;

export function PolicyListPage() {
  const [page, setPage] = useState(1);
  const filters = usePolicyListFilters();

  const policiesQuery = useQuery({
    ...policiesQueryOptions({ ...filters, page }),
    placeholderData: keepPreviousData,
  });

  if (policiesQuery.isPending) return &lt;PolicyListSkeleton /&gt;;
  if (policiesQuery.isError) return &lt;PolicyListError onRetry={() =&gt; policiesQuery.refetch()} /&gt;;

  return (
    &lt;&gt;
      &lt;PolicyTable
        policies={policiesQuery.data.items}
        faded={policiesQuery.isPlaceholderData}
      /&gt;
      &lt;Pagination
        page={page}
        hasNextPage={policiesQuery.data.hasNextPage}
        disabled={policiesQuery.isPlaceholderData}
        onPageChange={setPage}
      /&gt;
    &lt;/&gt;
  );
}
</code></pre>
<p>Now the previous page stays visible while the next page loads. When the new data arrives, it swaps in. The UI doesn&#39;t fall through loading states just because the query key changed.</p>
<p>Small detail. Huge difference.</p>
<h2>Dependent Queries</h2>
<p>Some data needs another piece of data first.</p>
<p>The policy detail page loads the policy. Then it may load documents for the policy&#39;s customer account. You can&#39;t fetch those documents until you know the account id.</p>
<pre><code class="language-tsx">export function PolicyDetailPage() {
  const { policyId } = Route.useParams();

  const policyQuery = useQuery(policyDetailQueryOptions(policyId));

  const documentsQuery = useQuery({
    ...customerDocumentsQueryOptions(policyQuery.data?.customerAccountId ?? &#39;&#39;),
    enabled: Boolean(policyQuery.data?.customerAccountId),
  });

  // ...
}
</code></pre>
<p>The <code>enabled</code> flag is the important part. It says: this query exists, but it should not run until its input exists.</p>
<p>Don&#39;t put conditional hooks around this:</p>
<pre><code class="language-tsx">if (policyQuery.data) {
  const documentsQuery = useQuery(...);
}
</code></pre>
<p>React hooks don&#39;t work that way. Keep the hook unconditional. Control execution with <code>enabled</code>.</p>
<h2>Prefetch at the Route Boundary</h2>
<p>TanStack Router and TanStack Query fit together cleanly because route loaders can ensure query data before the page renders.</p>
<pre><code class="language-ts">// src/pages/policy-detail/policy-detail.route.ts
export const policyDetailRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies/$policyId&#39;,
  loader: ({ context, params }) =&gt;
    context.queryClient.ensureQueryData(policyDetailQueryOptions(params.policyId)),
  component: PolicyDetailPage,
});
</code></pre>
<p>Then inside the page:</p>
<pre><code class="language-tsx">export function PolicyDetailPage() {
  const { policyId } = Route.useParams();
  const policyQuery = useQuery(policyDetailQueryOptions(policyId));

  return &lt;PolicyDetailView policy={policyQuery.data} /&gt;;
}
</code></pre>
<p>The page still uses <code>useQuery</code>. That is good. The loader warms the cache; the component subscribes to it. You get route-level loading behavior without inventing a second data path.</p>
<p>For list-to-detail navigation, prefetch on intent:</p>
<pre><code class="language-tsx">function PolicyRow({ policy }: { policy: Policy }) {
  const queryClient = useQueryClient();

  return (
    &lt;Link
      to=&quot;/policies/$policyId&quot;
      params={{ policyId: policy.id }}
      onMouseEnter={() =&gt; {
        queryClient.prefetchQuery(policyDetailQueryOptions(policy.id));
      }}
    &gt;
      {policy.policyNumber}
    &lt;/Link&gt;
  );
}
</code></pre>
<p>Hover is not the only signal. Focus works for keyboard users. Intersection works for visible rows. The point is the same: fetch before the click when intent is clear enough.</p>
<h2>Mutations Should Invalidate Narrowly</h2>
<p>Mutations deserve their own full article later, but invalidation belongs here.</p>
<p>After editing a policy status:</p>
<pre><code class="language-ts">export function useEditPolicyStatus() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: updatePolicyStatus,
    onSuccess: (updatedPolicy) =&gt; {
      queryClient.setQueryData(
        policyKeys.detail(updatedPolicy.id),
        updatedPolicy,
      );

      queryClient.invalidateQueries({ queryKey: policyKeys.lists() });
    },
  });
}
</code></pre>
<p>Two things happen:</p>
<ol>
<li>The detail cache gets the exact updated policy immediately.</li>
<li>Policy lists are invalidated because that status might affect filters, counts, and visible rows.</li>
</ol>
<p>I prefer this over invalidating everything:</p>
<pre><code class="language-ts">queryClient.invalidateQueries();
</code></pre>
<p>That is the data-fetching equivalent of turning the lights off and on again. Sometimes useful during development. Rarely the right production behavior.</p>
<p>Be specific. Your users can feel the difference.</p>
<h2>Don&#39;t Mirror Query Data Into Local State</h2>
<p>This is the smell I look for in reviews:</p>
<pre><code class="language-tsx">const policiesQuery = useQuery(policiesQueryOptions(filters));
const [policies, setPolicies] = useState&lt;Policy[]&gt;([]);

useEffect(() =&gt; {
  if (policiesQuery.data) {
    setPolicies(policiesQuery.data.items);
  }
}, [policiesQuery.data]);
</code></pre>
<p>Now there are two sources of truth. Query cache and local state. They will drift.</p>
<p>Usually the fix is simple:</p>
<pre><code class="language-tsx">const policies = policiesQuery.data?.items ?? [];
</code></pre>
<p>If you need derived data, derive it:</p>
<pre><code class="language-tsx">const activePolicies = useMemo(
  () =&gt; policiesQuery.data?.items.filter((policy) =&gt; policy.status === &#39;active&#39;) ?? [],
  [policiesQuery.data?.items],
);
</code></pre>
<p>If you need editable draft state, make that explicit:</p>
<pre><code class="language-tsx">const policyQuery = useQuery(policyDetailQueryOptions(policyId));
const form = useForm({
  defaultValues: policyQuery.data,
});
</code></pre>
<p>Server data can seed a draft. Once it becomes a draft, it is client state. Name the transition. Don&#39;t hide it in a <code>useEffect</code>.</p>
<h2>The Review Checklist</h2>
<p>When I review TanStack Query code, I ask:</p>
<ul>
<li>Does every query key include every variable used by the query function?</li>
<li>Are keys created through a factory instead of hand-written arrays everywhere?</li>
<li>Does each query have a deliberate <code>staleTime</code>?</li>
<li>Is pagination using <code>placeholderData</code> when flicker would hurt?</li>
<li>Are dependent queries controlled with <code>enabled</code>?</li>
<li>Are route loaders using the same query options as components?</li>
<li>Do mutations invalidate the narrowest useful key?</li>
<li>Is query data being mirrored into local state unnecessarily?</li>
<li>Does the UI distinguish first load from background refetch?</li>
</ul>
<p>That checklist catches most of the data bugs I see in React applications.</p>
<h2>What Comes Next</h2>
<p>TanStack Query is the server-state layer. Once that layer is stable, routing becomes much easier to reason about: routes can preload the same query options, search params can drive query keys, and navigation can become type-safe instead of stringly typed.</p>
<p><strong>Article #6:</strong> <a href="https://ma-x.im/blog/react-playbook-tanstack-router">routing with TanStack Router</a> — route trees, typed params, search validation, loaders, navigation, and how Router and Query should meet without turning every page into glue code.</p>
<hr>
<p>The haunted policy list eventually became boring. Not because we added more spinners. Because the data model became explicit. Query keys had hierarchy. Freshness was intentional. Mutations invalidated the right caches. Pagination stopped flickering. Route loaders warmed the detail page before the user landed there.</p>
<p>That is what I want from data fetching in React.</p>
<p>Not cleverness. Calm.</p>
<p>If your React app has started to feel like the UI is arguing with the network, reach out. The fix is often not another loading state. It is giving server state one clear owner.</p>
]]></content:encoded>
      <pubDate>Tue, 17 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TanStack Query</category>
      <category>Data Fetching</category>
      <category>TypeScript</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Error Handling in React: What Users Should See When Things Break</title>
      <link>https://ma-x.im/blog/react-playbook-error-handling</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-error-handling</guid>
      <description>Error handling is not a catch block sprinkled at the end. In a React + TanStack app, it is a product decision: what fails locally, what fails at the route level, what can retry, and what the user needs to do next.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-error-handling/cover.png" alt="Error Handling in React: What Users Should See When Things Break" /></p>
<p>The first serious error-handling conversation on the insurance CRM happened after a demo.</p>
<p>We had built the policy detail page. It fetched the policy, loaded the customer, showed coverage sections, rendered a timeline, and pulled a list of recent changes. The happy path looked good. Fast enough. Clean enough. The kind of screen that makes a product manager relax for about five minutes.</p>
<p>Then the staging API returned a 500 for the timeline.</p>
<p>The whole page disappeared.</p>
<p>Not the timeline. Not the lower section. The entire policy detail page. A blank error screen replaced the policy number, the customer information, the coverage details, everything the agent actually needed to keep working.</p>
<p>The backend bug was real. But the frontend decision was worse: we had treated one failed widget as if the whole route was unusable.</p>
<p>I&#39;ve watched this happen in multiple React apps. Error handling gets added late, after the components are built and the data flow already exists. Someone wraps the app in an Error Boundary, adds a few <code>isError</code> checks, maybe logs to Sentry, and calls it done.</p>
<p>It is not done.</p>
<p>Error handling is not just catching failures. It is deciding the blast radius of a failure.</p>
<blockquote>
<p>A good error boundary is not a wall around your app. It is a circuit breaker around the smallest thing that can safely fail.</p>
</blockquote>
<p>This article continues the <strong>React Playbook</strong> foundation. We already covered the stack, TypeScript patterns, and code structure. Now we need to talk about what happens when the happy path breaks: React Error Boundaries, TanStack Query error states, TanStack Router route errors, mutation failures, and the messages users actually deserve.</p>
<h2>First, Separate Error Types</h2>
<p>Most frontend error handling gets messy because teams put every failure in the same mental bucket.</p>
<p>They are not the same.</p>
<p>In a React app, I separate five categories:</p>
<ol>
<li><strong>Render errors</strong> — a component throws while rendering.</li>
<li><strong>Route loading errors</strong> — a route cannot load the data required to show the page.</li>
<li><strong>Query errors</strong> — a specific async resource failed.</li>
<li><strong>Mutation errors</strong> — a user action failed.</li>
<li><strong>Validation errors</strong> — user input is invalid before the request should happen.</li>
</ol>
<p>Each one needs a different UX.</p>
<p>A render error is usually unexpected. You need containment, logging, and a recovery path.</p>
<p>A route loading error might mean the page cannot exist. If <code>/policies/$policyId</code> cannot load the policy at all, the page cannot show much.</p>
<p>A query error might only affect part of the screen. If recent activity fails, the user can still read the policy.</p>
<p>A mutation error happens after intent. The user clicked Save, Submit, Assign, Delete. The UI needs to explain what happened and preserve their work.</p>
<p>A validation error is not exceptional at all. It is part of the form flow.</p>
<p>When you model these separately, the code gets simpler. More importantly, the product gets calmer.</p>
<h2>App-Level Error Boundary</h2>
<p>React Error Boundaries catch errors thrown during rendering, lifecycle methods, and constructors of child components. They do not catch async errors from promises by default. TanStack Query and Router have their own integration points, which we&#39;ll get to.</p>
<p>For the app shell, you still want a top-level boundary:</p>
<pre><code class="language-tsx">// src/app/AppErrorBoundary.tsx
import { ErrorBoundary } from &#39;react-error-boundary&#39;;

interface AppErrorFallbackProps {
  error: Error;
  resetErrorBoundary: () =&gt; void;
}

function AppErrorFallback({ error, resetErrorBoundary }: AppErrorFallbackProps) {
  return (
    &lt;main className=&quot;mx-auto flex min-h-screen max-w-xl flex-col justify-center p-6&quot;&gt;
      &lt;h1 className=&quot;text-2xl font-semibold&quot;&gt;Something went wrong&lt;/h1&gt;
      &lt;p className=&quot;mt-3 text-muted-foreground&quot;&gt;
        The application hit an unexpected error. You can try again, or refresh the page if the
        problem continues.
      &lt;/p&gt;
      &lt;pre className=&quot;mt-4 rounded-md bg-muted p-3 text-sm&quot;&gt;{error.message}&lt;/pre&gt;
      &lt;button className=&quot;mt-6&quot; onClick={resetErrorBoundary}&gt;
        Try again
      &lt;/button&gt;
    &lt;/main&gt;
  );
}

export function AppErrorBoundary({ children }: { children: React.ReactNode }) {
  return (
    &lt;ErrorBoundary
      FallbackComponent={AppErrorFallback}
      onError={(error, info) =&gt; {
        reportError(error, { componentStack: info.componentStack });
      }}
    &gt;
      {children}
    &lt;/ErrorBoundary&gt;
  );
}
</code></pre>
<p>Then wire it at the application edge:</p>
<pre><code class="language-tsx">// src/app/providers.tsx
import { QueryClientProvider } from &#39;@tanstack/react-query&#39;;
import { AppErrorBoundary } from &#39;./AppErrorBoundary&#39;;
import { queryClient } from &#39;./query-client&#39;;

export function AppProviders({ children }: { children: React.ReactNode }) {
  return (
    &lt;AppErrorBoundary&gt;
      &lt;QueryClientProvider client={queryClient}&gt;{children}&lt;/QueryClientProvider&gt;
    &lt;/AppErrorBoundary&gt;
  );
}
</code></pre>
<p>This is the last line of defense. It should almost never be the boundary your users see during normal failures.</p>
<p>If a single chart crashes, don&#39;t replace the entire app. If a sidebar widget crashes, don&#39;t hide the working page. Put smaller boundaries around risky areas.</p>
<h2>Section-Level Boundaries</h2>
<p>The policy detail page had several independent sections:</p>
<ul>
<li>policy header</li>
<li>coverage summary</li>
<li>beneficiaries</li>
<li>recent activity timeline</li>
<li>document attachments</li>
</ul>
<p>The timeline was useful, but not essential. A timeline failure should not kill the route.</p>
<p>So give it a local boundary:</p>
<pre><code class="language-tsx">// src/widgets/recent-policy-activity/RecentPolicyActivityBoundary.tsx
import { ErrorBoundary } from &#39;react-error-boundary&#39;;
import { RecentPolicyActivity } from &#39;./RecentPolicyActivity&#39;;

function RecentPolicyActivityFallback() {
  return (
    &lt;section className=&quot;rounded-md border p-4&quot;&gt;
      &lt;h2 className=&quot;font-medium&quot;&gt;Recent activity&lt;/h2&gt;
      &lt;p className=&quot;mt-2 text-sm text-muted-foreground&quot;&gt;
        Activity could not be loaded right now. The policy details are still available.
      &lt;/p&gt;
    &lt;/section&gt;
  );
}

export function RecentPolicyActivityBoundary({ policyId }: { policyId: string }) {
  return (
    &lt;ErrorBoundary FallbackComponent={RecentPolicyActivityFallback}&gt;
      &lt;RecentPolicyActivity policyId={policyId} /&gt;
    &lt;/ErrorBoundary&gt;
  );
}
</code></pre>
<p>The key sentence in the fallback is not &quot;An error occurred.&quot; The key sentence is: <strong>the policy details are still available.</strong></p>
<p>Users need to know scope. What broke? What still works? What can they do next?</p>
<h2>TanStack Query: Local Error States First</h2>
<p>TanStack Query gives you the query state directly:</p>
<pre><code class="language-tsx">const policyQuery = useQuery(policyDetailQueryOptions(policyId));

if (policyQuery.isPending) return &lt;PolicyDetailSkeleton /&gt;;
if (policyQuery.isError) return &lt;PolicyDetailError error={policyQuery.error} /&gt;;

return &lt;PolicyDetailView policy={policyQuery.data} /&gt;;
</code></pre>
<p>This is the right pattern when the query owns the whole component. The component has one main job: load policy detail and render it. If that query fails, the component cannot do its job.</p>
<p>Make the error component specific:</p>
<pre><code class="language-tsx">// src/entities/policy/ui/PolicyDetailError.tsx
interface PolicyDetailErrorProps {
  error: Error;
  onRetry: () =&gt; void;
}

export function PolicyDetailError({ error, onRetry }: PolicyDetailErrorProps) {
  return (
    &lt;div className=&quot;rounded-md border border-destructive/30 p-4&quot;&gt;
      &lt;h2 className=&quot;font-medium&quot;&gt;Policy could not be loaded&lt;/h2&gt;
      &lt;p className=&quot;mt-2 text-sm text-muted-foreground&quot;&gt;
        The policy may have been removed, or the server may be temporarily unavailable.
      &lt;/p&gt;
      &lt;p className=&quot;mt-2 text-xs text-muted-foreground&quot;&gt;{error.message}&lt;/p&gt;
      &lt;button className=&quot;mt-4&quot; onClick={onRetry}&gt;
        Retry
      &lt;/button&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>Then pass the retry:</p>
<pre><code class="language-tsx">if (policyQuery.isError) {
  return &lt;PolicyDetailError error={policyQuery.error} onRetry={() =&gt; policyQuery.refetch()} /&gt;;
}
</code></pre>
<p>That <code>refetch()</code> matters. A dead error state with no recovery action forces the user to refresh the browser. That is not error handling. That is surrender.</p>
<h2>Configure Query Defaults Deliberately</h2>
<p>In article #1, we started with:</p>
<pre><code class="language-ts">// src/app/query-client.ts
import { QueryClient } from &#39;@tanstack/react-query&#39;;

export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 1000 * 60 * 5,
      retry: 1,
    },
  },
});
</code></pre>
<p><code>retry: 1</code> is not random. The TanStack Query default is three retries with exponential backoff. For some apps, that is fine. For internal CRM workflows, I usually prefer one retry.</p>
<p>Why?</p>
<p>Because three retries can make an error feel like a frozen screen. The user clicked a policy. The API is failing. The UI waits, retries, waits, retries, waits again, and only then shows an error. Technically resilient. Product-wise frustrating.</p>
<p>But not every query deserves the same retry behavior.</p>
<pre><code class="language-ts">export function policyDetailQueryOptions(policyId: string) {
  return queryOptions({
    queryKey: policyKeys.detail(policyId),
    queryFn: () =&gt; fetchPolicyById(policyId),
    retry: (failureCount, error) =&gt; {
      if (isNotFoundError(error)) return false;
      return failureCount &lt; 1;
    },
  });
}
</code></pre>
<p>Don&#39;t retry 404s. The policy isn&#39;t going to appear after a second attempt. Do retry one transient network failure. That is a reasonable default.</p>
<p>This is where typed API errors help.</p>
<h2>Typed API Errors</h2>
<p>In article #2, we talked about typing API responses at the boundary. Do the same for errors.</p>
<pre><code class="language-ts">// src/shared/api/api-error.ts
export class ApiError extends Error {
  constructor(
    message: string,
    public readonly status: number,
    public readonly code?: string,
  ) {
    super(message);
    this.name = &#39;ApiError&#39;;
  }
}

export function isApiError(error: unknown): error is ApiError {
  return error instanceof ApiError;
}

export function isNotFoundError(error: unknown) {
  return isApiError(error) &amp;&amp; error.status === 404;
}
</code></pre>
<p>Use it in the HTTP client:</p>
<pre><code class="language-ts">// src/shared/api/http-client.ts
import { ApiError } from &#39;./api-error&#39;;

export async function getJson&lt;T&gt;(url: string): Promise&lt;T&gt; {
  const response = await fetch(url);

  if (!response.ok) {
    const body = await response.json().catch(() =&gt; null);
    throw new ApiError(body?.message ?? `GET ${url} failed`, response.status, body?.code);
  }

  return response.json() as Promise&lt;T&gt;;
}
</code></pre>
<p>Now your UI can make product decisions:</p>
<pre><code class="language-tsx">function PolicyDetailError({ error, onRetry }: PolicyDetailErrorProps) {
  if (isNotFoundError(error)) {
    return (
      &lt;EmptyState
        title=&quot;Policy not found&quot;
        description=&quot;This policy may have been deleted or you may not have access to it.&quot;
      /&gt;
    );
  }

  return (
    &lt;ErrorState
      title=&quot;Policy could not be loaded&quot;
      description=&quot;The server did not return the policy. Try again in a moment.&quot;
      action={{ label: &#39;Retry&#39;, onClick: onRetry }}
    /&gt;
  );
}
</code></pre>
<p>A 404 and a 500 should not have the same message. One is probably permanent. One may be temporary. The UI should reflect that.</p>
<h2>Route-Level Errors with TanStack Router</h2>
<p>Some data is required before a route can render.</p>
<p>For a policy detail page, the policy itself is required. Recent activity is not. Attachments are not. Recommendations are not.</p>
<p>TanStack Router lets you load required data at the route level:</p>
<pre><code class="language-ts">// src/pages/policy-detail/policy-detail.route.ts
export const policyDetailRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies/$policyId&#39;,
  loader: ({ context, params }) =&gt;
    context.queryClient.ensureQueryData(policyDetailQueryOptions(params.policyId)),
  errorComponent: PolicyDetailRouteError,
  component: PolicyDetailPage,
});
</code></pre>
<p>Then the route error component owns route-level failures:</p>
<pre><code class="language-tsx">// src/pages/policy-detail/PolicyDetailRouteError.tsx
import { useRouter } from &#39;@tanstack/react-router&#39;;
import { isNotFoundError } from &#39;@/shared/api&#39;;

export function PolicyDetailRouteError({ error }: { error: unknown }) {
  const router = useRouter();

  if (isNotFoundError(error)) {
    return (
      &lt;RouteErrorLayout
        title=&quot;Policy not found&quot;
        description=&quot;The policy does not exist, was removed, or is not available to your account.&quot;
        action={{ label: &#39;Back to policies&#39;, to: &#39;/policies&#39; }}
      /&gt;
    );
  }

  return (
    &lt;RouteErrorLayout
      title=&quot;Policy could not be opened&quot;
      description=&quot;Something failed while loading this policy. You can retry without leaving the page.&quot;
      action={{ label: &#39;Retry&#39;, onClick: () =&gt; router.invalidate() }}
    /&gt;
  );
}
</code></pre>
<p><code>router.invalidate()</code> is the route-level retry. It re-runs loaders and gives the route a chance to recover.</p>
<p>The important design choice is deciding what belongs in the loader. If every widget query goes into the loader, every widget failure becomes a route failure. That is exactly how we blanked the policy detail page in the demo.</p>
<p>My rule:</p>
<blockquote>
<p>Load the minimum data required to make the route meaningful. Everything else should fail locally.</p>
</blockquote>
<h2>Mutation Errors Are Different</h2>
<p>Query errors happen while reading.</p>
<p>Mutation errors happen after the user did something.</p>
<p>That difference matters. A failed mutation should preserve the user&#39;s input, explain what happened, and make retry obvious.</p>
<pre><code class="language-tsx">// src/features/edit-policy-status/ui/EditPolicyStatusForm.tsx
export function EditPolicyStatusForm({ policyId }: { policyId: string }) {
  const editStatus = useEditPolicyStatus();

  return (
    &lt;form
      onSubmit={(event) =&gt; {
        event.preventDefault();
        editStatus.mutate({ policyId, status: &#39;active&#39; });
      }}
    &gt;
      {editStatus.isError &amp;&amp; (
        &lt;InlineError&gt;
          Status could not be updated. Your change was not saved. Try again in a moment.
        &lt;/InlineError&gt;
      )}

      &lt;button disabled={editStatus.isPending}&gt;
        {editStatus.isPending ? &#39;Saving...&#39; : &#39;Save status&#39;}
      &lt;/button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<p>Notice the wording: <strong>Your change was not saved.</strong></p>
<p>Users need to know whether the action happened. Ambiguous mutation errors are dangerous. Did the payment submit? Did the policy update? Did the customer receive the email? If the system cannot guarantee success, say so clearly.</p>
<p>For destructive actions, be even more explicit:</p>
<pre><code class="language-tsx">&lt;InlineError&gt;
  The document was not deleted. The server rejected the request, so it is still attached to the
  policy.
&lt;/InlineError&gt;
</code></pre>
<p>That kind of sentence sounds small. In operational software, it matters.</p>
<h2>Optimistic Updates Need Rollback</h2>
<p>TanStack Query makes optimistic updates straightforward, but optimistic error handling has to be designed.</p>
<pre><code class="language-ts">// src/features/edit-policy-status/api/edit-policy-status.mutation.ts
export function useEditPolicyStatus() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: updatePolicyStatus,
    onMutate: async (variables) =&gt; {
      await queryClient.cancelQueries({ queryKey: policyKeys.detail(variables.policyId) });

      const previousPolicy = queryClient.getQueryData&lt;PolicyDetail&gt;(
        policyKeys.detail(variables.policyId),
      );

      queryClient.setQueryData&lt;PolicyDetail&gt;(policyKeys.detail(variables.policyId), (policy) =&gt;
        policy ? { ...policy, status: variables.status } : policy,
      );

      return { previousPolicy };
    },
    onError: (_error, variables, context) =&gt; {
      queryClient.setQueryData(policyKeys.detail(variables.policyId), context?.previousPolicy);
    },
    onSettled: (_data, _error, variables) =&gt; {
      queryClient.invalidateQueries({ queryKey: policyKeys.detail(variables.policyId) });
    },
  });
}
</code></pre>
<p>Optimistic UI is a promise: &quot;we&#39;ll show the result now and clean it up if the server disagrees.&quot;</p>
<p>If you don&#39;t roll back, it becomes a lie.</p>
<p>Honestly, I use optimistic updates less often than people expect. They are excellent for low-risk interactions: toggles, lightweight status changes, local ordering. For payments, legal records, irreversible actions, or anything with compliance implications, I prefer boring pending states and clear confirmation.</p>
<p>Fast is good. Correct is better.</p>
<h2>Validation Errors Are Not Exceptions</h2>
<p>Form validation errors should not go through Error Boundaries, route error components, or global toasts.</p>
<p>They belong next to the field or section that needs correction.</p>
<pre><code class="language-tsx">// src/features/create-policy/ui/CreatePolicyForm.tsx
&lt;form.Field
  name=&quot;holderName&quot;
  validators={{
    onBlur: ({ value }) =&gt; {
      if (!value.trim()) return &#39;Policy holder name is required&#39;;
      return undefined;
    },
  }}
&gt;
  {(field) =&gt; (
    &lt;TextField
      label=&quot;Policy holder&quot;
      value={field.state.value}
      onBlur={field.handleBlur}
      onChange={(event) =&gt; field.handleChange(event.target.value)}
      error={field.state.meta.errors[0]}
    /&gt;
  )}
&lt;/form.Field&gt;
</code></pre>
<p>A validation message is not an apology. It is guidance.</p>
<p>Bad:</p>
<pre><code>Invalid value.
</code></pre>
<p>Better:</p>
<pre><code>Policy holder name is required.
</code></pre>
<p>Better still, when the rule is domain-specific:</p>
<pre><code>Coverage start date must be after the policy approval date.
</code></pre>
<p>The closer the error is to the user&#39;s mental model, the less support burden you create.</p>
<h2>Global Toasts Are Not an Error Strategy</h2>
<p>I have a strong bias here: global error toasts are overused.</p>
<p>They are fine for background actions:</p>
<pre><code>Report export failed. Try again later.
</code></pre>
<p>They are not fine as the only feedback for a failed form submit, failed route load, or failed section query.</p>
<p>Toasts disappear. They are hard to reread. They often don&#39;t say what changed. If the user needs the information to continue, put it in the interface where the failure happened.</p>
<p>Use toasts for secondary notifications. Use inline states for primary failures.</p>
<h2>Logging Without Leaking</h2>
<p>You still need error reporting.</p>
<p>But don&#39;t send everything everywhere. A useful report has context:</p>
<pre><code class="language-ts">// src/shared/lib/report-error.ts
interface ErrorContext {
  route?: string;
  policyId?: string;
  componentStack?: string;
  action?: string;
}

export function reportError(error: unknown, context: ErrorContext = {}) {
  if (import.meta.env.DEV) {
    console.error(error, context);
    return;
  }

  // Send to your reporting tool here.
  // Include context, but avoid sensitive policy/customer data.
}
</code></pre>
<p>For a CRM, this matters. Policy numbers, customer names, payment details, medical information, internal notes — these should not casually land in logs because a component threw.</p>
<p>Log identifiers when needed. Avoid payloads unless you have a reason and a retention policy.</p>
<h2>A Practical Error-Handling Checklist</h2>
<p>When I review a React screen, I ask these questions:</p>
<ul>
<li>What data is required for the route to be meaningful?</li>
<li>What data can fail locally without killing the page?</li>
<li>Does every failed query have a retry path?</li>
<li>Does every failed mutation preserve user input?</li>
<li>Does the user know whether their action was saved?</li>
<li>Are 404, 403, 409, and 500 treated differently where it matters?</li>
<li>Are validation errors close to the fields?</li>
<li>Are unexpected errors logged with useful context and without sensitive data?</li>
</ul>
<p>This is not bureaucracy. This is how you keep production software calm.</p>
<h2>What Comes Next</h2>
<p>The first four articles now cover the base layer of a serious React application:</p>
<ul>
<li><strong>Article #1:</strong> <a href="https://ma-x.im/blog/react-playbook-starting-new-project">the stack</a></li>
<li><strong>Article #2:</strong> <a href="https://ma-x.im/blog/react-playbook-typescript-patterns">TypeScript patterns</a></li>
<li><strong>Article #3:</strong> <a href="https://ma-x.im/blog/react-playbook-code-structure">code structure</a></li>
<li><strong>Article #4:</strong> this error-handling model</li>
</ul>
<p><strong>Article #5:</strong> <a href="https://ma-x.im/blog/react-playbook-data-fetching">data fetching with TanStack Query</a> — query keys, cache shape, prefetching, invalidation, stale time, pagination, and the boring details that decide whether a React app feels reliable or twitchy.</p>
<hr>
<p>After that staging demo, we changed the policy detail page. The policy itself stayed route-level. The timeline moved into a local query with its own fallback. Attachments got a retry button. Mutations kept user input visible after failure. The app did not become immune to backend problems. No frontend can do that.</p>
<p>But failures became smaller.</p>
<p>That is the whole point. Not hiding errors. Not pretending the system is fine. Making sure one broken piece does not take the rest of the work down with it.</p>
<p>If you&#39;re designing error handling for a React app and every failure currently becomes either a toast or a blank screen, reach out. There is usually a calmer architecture hiding one boundary lower.</p>
]]></content:encoded>
      <pubDate>Fri, 13 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Error Handling</category>
      <category>TanStack</category>
      <category>TypeScript</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Code Structure in React: The FSD Version I Actually Use</title>
      <link>https://ma-x.im/blog/react-playbook-code-structure</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-code-structure</guid>
      <description>Flat folders are fine until every feature starts touching routes, queries, forms, permissions, and UI state. Here&apos;s the React structure I use when a TanStack app grows past the simple stage without turning FSD into folder theater.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-code-structure/cover.png" alt="Code Structure in React: The FSD Version I Actually Use" /></p>
<p>At the insurance company, our React app started with a structure I still like for small projects.</p>
<pre><code>src/
  api/
  components/
  hooks/
  layouts/
  pages/
  types/
  router.ts
  main.tsx
</code></pre>
<p>For the first few months, it worked. A policy list page lived in <code>pages/</code>. Shared buttons lived in <code>components/</code>. Fetch functions lived in <code>api/</code>. Everyone understood where things went.</p>
<p>Then the policy builder arrived.</p>
<p>It was not a page anymore. It was a product inside the product: dynamic fields, conditional sections, autosave, permissions, validation, optimistic updates, draft recovery, and analytics. Suddenly one change touched <code>pages/</code>, <code>api/</code>, <code>types/</code>, <code>hooks/</code>, <code>components/</code>, and a couple of files nobody remembered creating.</p>
<p>I&#39;ve seen this pattern across multiple teams. A flat structure doesn&#39;t fail all at once. It fails quietly. First you add one more shared hook. Then one more domain type. Then a generic component that is not generic at all, just shared too early. Six months later the folders are still clean, but every feature is smeared across the project.</p>
<p>That is usually the moment people reach for Feature-Sliced Design.</p>
<p>Honestly, I think that instinct is right. FSD gives React projects a language for scale: layers, slices, public APIs, dependency direction. But if you apply it mechanically, it creates a different problem. The code becomes &quot;architecturally correct&quot; and painful to work with.</p>
<p>This article is the version I actually use in production React apps. Not pure FSD. Not flat folders forever. A practical structure for TanStack Query, TanStack Router, TanStack Form, and the kind of domain logic that appears in real applications.</p>
<blockquote>
<p>Architecture should make the next change easier to locate, not just easier to categorize.</p>
</blockquote>
<h2>Start Flat, Then Move When Pain Appears</h2>
<p>I don&#39;t start every React app with full FSD.</p>
<p>For a new Vite + TanStack project, this is still a good beginning:</p>
<pre><code>src/
  api/
    policies.ts
    query-keys.ts
  components/
    Button.tsx
    DataList.tsx
  layouts/
    RootLayout.tsx
  pages/
    PolicyListPage.tsx
    PolicyDetailPage.tsx
  types/
    policy.ts
  router.ts
  main.tsx
</code></pre>
<p>This structure is boring. Boring is good at the start.</p>
<p>The mistake is keeping it after the app stops being boring. You don&#39;t need FSD because the project has ten files. You need it when features become independent units of change.</p>
<p>The signal is not folder count. The signal is change pattern.</p>
<p>If every policy-related change touches five top-level folders, the structure is telling you something. If onboarding a developer requires explaining &quot;policy things are in several places, you&#39;ll get used to it,&quot; the structure is telling you something. If a file is in <code>shared/</code> because two components use it but both components belong to the same feature, the structure is telling you something.</p>
<p>At that point, I move to a sliced structure.</p>
<h2>The Structure</h2>
<p>The shape I use most often:</p>
<pre><code>src/
  app/
    router.ts
    providers.tsx
    query-client.ts
  pages/
    policy-list/
      PolicyListPage.tsx
      route.ts
    policy-detail/
      PolicyDetailPage.tsx
      route.ts
  widgets/
    policy-search-panel/
    policy-summary-card/
  features/
    create-policy/
    edit-policy-status/
    assign-policy-owner/
  entities/
    policy/
      api/
      model/
      ui/
      lib/
      index.ts
    customer/
      api/
      model/
      ui/
      index.ts
  shared/
    api/
    config/
    lib/
    ui/
</code></pre>
<p>The names come from FSD, but the goal is not to worship the names. The goal is to separate code by responsibility and change frequency.</p>
<p><strong><code>app/</code></strong> is application wiring. Providers, router creation, query client configuration, global error boundaries. It should know how the app starts. It should not know how policy validation works.</p>
<p><strong><code>pages/</code></strong> are route-level screens. They compose widgets, features, and entities. They should be thin. If a page file becomes a business logic container, something probably belongs lower.</p>
<p><strong><code>widgets/</code></strong> are larger UI blocks made from several domain pieces. A search panel, a dashboard section, a header with account controls.</p>
<p><strong><code>features/</code></strong> are user actions. Create policy. Update status. Assign owner. Submit payment method.</p>
<p><strong><code>entities/</code></strong> are domain concepts. Policy. Customer. Beneficiary. Claim. This is where domain types, query keys, API functions, selectors, and reusable entity UI usually live.</p>
<p><strong><code>shared/</code></strong> is for things that are truly domain-agnostic: buttons, date formatting, HTTP client, environment config, tiny utilities.</p>
<p>The import direction matters:</p>
<pre><code>app -&gt; pages -&gt; widgets -&gt; features -&gt; entities -&gt; shared
</code></pre>
<p>Lower layers don&#39;t import higher layers. <code>entities/policy</code> should not import from <code>features/create-policy</code>. <code>shared/ui/Button</code> should not know a policy exists.</p>
<p>That one rule prevents a lot of architectural rot.</p>
<h2>Where TanStack Query Goes</h2>
<p>The most common question in this stack: where do query hooks live?</p>
<p>My default: query keys and fetch functions live in the entity. Feature-specific mutations live in the feature if they represent a user action.</p>
<pre><code>src/
  entities/
    policy/
      api/
        policy.api.ts
        policy.queries.ts
      model/
        policy.types.ts
      index.ts
  features/
    edit-policy-status/
      api/
        edit-policy-status.mutation.ts
      ui/
        EditPolicyStatusButton.tsx
      index.ts
</code></pre>
<p>The entity owns the stable data contract:</p>
<pre><code class="language-ts">// src/entities/policy/api/policy.queries.ts
import { queryOptions } from &#39;@tanstack/react-query&#39;;
import { fetchPolicyById, fetchPolicies } from &#39;./policy.api&#39;;

export const policyKeys = {
  all: [&#39;policies&#39;] as const,
  lists: () =&gt; [...policyKeys.all, &#39;list&#39;] as const,
  list: (filters: PolicyListFilters) =&gt; [...policyKeys.lists(), filters] as const,
  details: () =&gt; [...policyKeys.all, &#39;detail&#39;] as const,
  detail: (policyId: string) =&gt; [...policyKeys.details(), policyId] as const,
};

export function policiesQueryOptions(filters: PolicyListFilters) {
  return queryOptions({
    queryKey: policyKeys.list(filters),
    queryFn: () =&gt; fetchPolicies(filters),
  });
}

export function policyDetailQueryOptions(policyId: string) {
  return queryOptions({
    queryKey: policyKeys.detail(policyId),
    queryFn: () =&gt; fetchPolicyById(policyId),
  });
}
</code></pre>
<p>The feature owns the action:</p>
<pre><code class="language-ts">// src/features/edit-policy-status/api/edit-policy-status.mutation.ts
import { useMutation, useQueryClient } from &#39;@tanstack/react-query&#39;;
import { policyKeys } from &#39;@/entities/policy&#39;;
import { updatePolicyStatus } from &#39;./edit-policy-status.api&#39;;

export function useEditPolicyStatus() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: updatePolicyStatus,
    onSuccess: (_, variables) =&gt; {
      queryClient.invalidateQueries({ queryKey: policyKeys.detail(variables.policyId) });
      queryClient.invalidateQueries({ queryKey: policyKeys.lists() });
    },
  });
}
</code></pre>
<p>This keeps the split honest. Reading policy data is part of the policy entity. Editing a status is a user action, so it belongs to a feature.</p>
<p>Could you put all mutations inside <code>entities/policy</code>? Sometimes, yes. For simple CRUD apps it works. But once mutations include permissions, analytics, form state, toasts, optimistic updates, or workflow-specific behavior, I prefer feature ownership. The code changes with the feature, so it should live with the feature.</p>
<h2>Where TanStack Router Goes</h2>
<p>TanStack Router adds an interesting pressure: route definitions are code, and code wants to be near the page it renders.</p>
<p>For small apps, one <code>src/router.ts</code> is fine. For larger apps, I split route pieces by page and compose them in <code>app/router.ts</code>.</p>
<pre><code>src/
  app/
    router.ts
  pages/
    policy-list/
      PolicyListPage.tsx
      policy-list.route.ts
    policy-detail/
      PolicyDetailPage.tsx
      policy-detail.route.ts
</code></pre>
<pre><code class="language-ts">// src/pages/policy-detail/policy-detail.route.ts
import { createRoute } from &#39;@tanstack/react-router&#39;;
import { rootRoute } from &#39;@/app/root-route&#39;;
import { policyDetailQueryOptions } from &#39;@/entities/policy&#39;;
import { PolicyDetailPage } from &#39;./PolicyDetailPage&#39;;

export const policyDetailRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies/$policyId&#39;,
  loader: ({ context, params }) =&gt;
    context.queryClient.ensureQueryData(policyDetailQueryOptions(params.policyId)),
  component: PolicyDetailPage,
});
</code></pre>
<p>The page route can import from entities because pages sit above entities. The entity must never import the route.</p>
<p>This also makes preloading natural. The route knows it needs a policy detail. The entity knows how to describe that query. TanStack Router and TanStack Query meet at the page boundary instead of leaking through the whole application.</p>
<h2>Where TanStack Form Goes</h2>
<p>Forms are where clean architecture usually goes to suffer.</p>
<p>A form is not just UI. It has schema, default values, validation, submission, dirty state, field components, and often domain-specific rules. If you split those across <code>shared/forms/</code>, <code>entities/</code>, <code>features/</code>, and <code>pages/</code> too early, every form change becomes a small hunt.</p>
<p>My rule:</p>
<blockquote>
<p>Put the form where the user action lives. Extract only the parts that are genuinely reused by another action.</p>
</blockquote>
<p>For creating a policy:</p>
<pre><code>src/
  features/
    create-policy/
      model/
        create-policy.schema.ts
        create-policy.types.ts
      api/
        create-policy.mutation.ts
      ui/
        CreatePolicyForm.tsx
        CoverageSection.tsx
        BeneficiariesSection.tsx
      index.ts
</code></pre>
<p>The feature owns the form because the form exists to perform the feature.</p>
<p>If <code>CoverageSection</code> later appears in three different policy workflows, then move it. Not before. Premature sharing is one of the fastest ways to make React code harder to change.</p>
<p>This is especially important with TanStack Form because field-level logic tends to be close to business rules:</p>
<pre><code class="language-tsx">// src/features/create-policy/ui/CreatePolicyForm.tsx
import { useForm } from &#39;@tanstack/react-form&#39;;
import { createPolicySchema } from &#39;../model/create-policy.schema&#39;;
import { useCreatePolicy } from &#39;../api/create-policy.mutation&#39;;

export function CreatePolicyForm() {
  const createPolicy = useCreatePolicy();

  const form = useForm({
    defaultValues: {
      holderName: &#39;&#39;,
      coverageType: &#39;standard&#39;,
      beneficiaries: [],
    },
    validators: {
      onSubmit: createPolicySchema,
    },
    onSubmit: ({ value }) =&gt; createPolicy.mutate(value),
  });

  return (
    &lt;form
      onSubmit={(event) =&gt; {
        event.preventDefault();
        form.handleSubmit();
      }}
    &gt;
      {/* fields */}
    &lt;/form&gt;
  );
}
</code></pre>
<p>The schema, mutation, and UI are close because they change together. That is not messy. That is locality.</p>
<h2>What Belongs in Shared</h2>
<p><code>shared/</code> is the most abused folder in React architecture.</p>
<p>If a file feels &quot;not specific enough,&quot; people put it in shared. Then shared becomes the new junk drawer: domain helpers, half-generic hooks, components with policy-specific props, validation rules from one workflow, and API helpers that know too much.</p>
<p>I use a stricter test.</p>
<p>A file belongs in <code>shared/</code> only if it can be moved to a different product in the same company and still make sense.</p>
<p>Good candidates:</p>
<pre><code>shared/
  api/
    http-client.ts
  config/
    env.ts
  lib/
    format-date.ts
    assert-never.ts
  ui/
    Button.tsx
    Dialog.tsx
    TextField.tsx
</code></pre>
<p>Bad candidates:</p>
<pre><code>shared/
  hooks/
    usePolicyValidation.ts
  lib/
    calculateCoveragePremium.ts
  ui/
    PolicyStatusBadge.tsx
</code></pre>
<p>Those are not shared. They are policy code. Put them in <code>entities/policy</code> or in the feature that owns the workflow.</p>
<p>This one discipline keeps FSD useful. When shared stays small, the rest of the architecture stays readable.</p>
<h2>Public APIs: The Boring Part That Matters</h2>
<p>Every slice should expose a public API through <code>index.ts</code>.</p>
<pre><code>src/entities/policy/index.ts
</code></pre>
<pre><code class="language-ts">export type { Policy, PolicyDetail, PolicyStatus } from &#39;./model/policy.types&#39;;
export { policyKeys, policyDetailQueryOptions, policiesQueryOptions } from &#39;./api/policy.queries&#39;;
export { PolicyStatusBadge } from &#39;./ui/PolicyStatusBadge&#39;;
</code></pre>
<p>Then other layers import from the slice root:</p>
<pre><code class="language-ts">import { PolicyStatusBadge, policyDetailQueryOptions } from &#39;@/entities/policy&#39;;
</code></pre>
<p>Not from internal paths:</p>
<pre><code class="language-ts">import { policyDetailQueryOptions } from &#39;@/entities/policy/api/policy.queries&#39;;
</code></pre>
<p>The public API gives you a boundary. You can reorganize internals without updating imports across the whole app. It also forces a small moment of intent: is this thing actually meant to be used outside the slice?</p>
<p>That question catches more design mistakes than people expect.</p>
<h2>The Rule I Break on Purpose</h2>
<p>Pure FSD can become too granular for real feature work. I break one rule deliberately: I colocate deeply related feature files even when they could be classified into separate layers.</p>
<p>If <code>CreatePolicyForm</code>, <code>createPolicySchema</code>, <code>createPolicyMutation</code>, and <code>CoverageSection</code> always change together, I keep them in <code>features/create-policy</code>.</p>
<p>I don&#39;t move <code>CoverageSection</code> to <code>widgets/</code> because it looks like a block.</p>
<p>I don&#39;t move <code>createPolicySchema</code> to <code>entities/policy</code> because it mentions policy fields.</p>
<p>I don&#39;t move a tiny helper to <code>shared/lib</code> because two files in the same feature use it.</p>
<p>This is not laziness. It is a design choice.</p>
<p>I&#39;ve been in the room where a team spent twenty minutes arguing whether a file was a feature, an entity, or a widget. Different company, different stack, same argument. At some point, the question stops being architectural and starts being clerical.</p>
<p>When that happens, I ask a simpler question: <strong>what changes with this file?</strong></p>
<p>If the answer is &quot;this feature,&quot; it stays in the feature.</p>
<h2>A Migration Path That Doesn&#39;t Hurt</h2>
<p>You don&#39;t need to restructure the entire app in one PR.</p>
<p>Start with one domain that already hurts. In the insurance CRM, that would have been policies.</p>
<p>Before:</p>
<pre><code>src/
  api/policies.ts
  components/PolicyStatusBadge.tsx
  hooks/usePolicyFilters.ts
  pages/PolicyListPage.tsx
  pages/PolicyDetailPage.tsx
  types/policy.ts
</code></pre>
<p>After:</p>
<pre><code>src/
  entities/
    policy/
      api/
        policy.api.ts
        policy.queries.ts
      model/
        policy.types.ts
      ui/
        PolicyStatusBadge.tsx
      index.ts
  pages/
    policy-list/
      PolicyListPage.tsx
    policy-detail/
      PolicyDetailPage.tsx
</code></pre>
<p>Then move the next workflow into <code>features/</code> when you touch it for real work.</p>
<p>Don&#39;t create empty folders for future architecture. Empty architecture is theater. Let structure follow actual code.</p>
<h2>How I Know the Structure Is Working</h2>
<p>Good structure has practical symptoms.</p>
<p>A new feature starts in one folder. A domain type is easy to find. Query invalidation is predictable. Shared stays boring. A page reads like composition instead of orchestration.</p>
<p>More importantly, changes have shape.</p>
<p>When a status-editing bug appears, you know to open <code>features/edit-policy-status</code>. When a policy response changes, you know to open <code>entities/policy</code>. When route preloading fails, you know to look at the page route.</p>
<p>That is the payoff. Not prettier folders. Faster orientation.</p>
<h2>What Comes Next</h2>
<p>The first three articles now give us the foundation:</p>
<ul>
<li><strong>Article #1:</strong> <a href="https://ma-x.im/blog/react-playbook-starting-new-project">the starting stack</a> — Vite, TanStack Query, Router, Form, and the minimal setup</li>
<li><strong>Article #2:</strong> <a href="https://ma-x.im/blog/react-playbook-typescript-patterns">TypeScript patterns</a> — typed API boundaries, query keys, route params, async state</li>
<li><strong>Article #3:</strong> this structure — how those pieces live together once the app grows</li>
</ul>
<p><strong>Article #4:</strong> <a href="https://ma-x.im/blog/react-playbook-error-handling">error handling</a> — Error Boundaries, TanStack Query error states, route-level failures, and what to show users when the happy path breaks.</p>
<hr>
<p>That policy builder eventually became one of the busiest parts of the insurance CRM. The structure was not perfect. No structure is. But after we moved policy code into a domain slice and kept workflow-specific logic inside features, the app became easier to reason about.</p>
<p>Not because FSD solved architecture for us. Because it gave us enough structure to stop guessing, and enough flexibility to keep related code close.</p>
<p>That&#39;s the version I trust: architecture as a working agreement, not a folder ceremony.</p>
<p>If you&#39;re trying to move a React codebase from flat folders into something more deliberate, feel free to reach out. The hard part is rarely naming the folders. The hard part is deciding what should change together.</p>
]]></content:encoded>
      <pubDate>Tue, 10 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Architecture</category>
      <category>FSD</category>
      <category>TanStack</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>TypeScript in React: Patterns That Actually Matter</title>
      <link>https://ma-x.im/blog/react-playbook-typescript-patterns</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-typescript-patterns</guid>
      <description>Not a TypeScript tutorial. A focused look at the patterns that eliminate real production bugs in React + TanStack apps — typed API layers, discriminated unions for async state, route param inference, and generic components done right.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-typescript-patterns/cover.png" alt="TypeScript in React: Patterns That Actually Matter" /></p>
<p>Six weeks into the insurance CRM project, we had a production incident. The backend team renamed a field — <code>policy_number</code> became <code>policyNumber</code> — as part of a snake_case-to-camelCase API migration. The migration happened on a Friday. By Monday morning, the policy list was blank for every user.</p>
<p>The bug took four minutes to find. The cause took longer to accept: we had typed the API response as <code>any</code>. Not carelessly — the original developer had written a comment: <em>&quot;TODO: add proper types.&quot;</em> The TODO was six months old.</p>
<p>Honest answer: I&#39;ve seen this exact scenario more times than I want to count. TypeScript installed, TypeScript ignored at the boundaries. <code>any</code> at the API layer, loose props in the middle, and full type safety only in the parts that didn&#39;t need it — utility functions, helpers, things that already worked.</p>
<p>This article is not a TypeScript tutorial. If you need to understand generics from first principles, there are better resources for that. What I want to cover are the specific patterns that prevent real production bugs in React + TanStack apps. The ones I reach for on every project now, not as best practices, but because I&#39;ve seen what happens without them.</p>
<h2>Pattern 1: The Typed API Layer</h2>
<p>The most important rule: <strong>type your API responses at the boundary, once.</strong></p>
<p>Not in the component. Not in the hook. At the fetch function that talks to the API. Everything downstream inherits the types automatically.</p>
<pre><code class="language-ts">// src/api/policies.ts
import type { Policy, PolicyDetail } from &#39;../types/policy&#39;;

export async function fetchPolicies(): Promise&lt;Policy[]&gt; {
  const res = await fetch(&#39;/api/policies&#39;);
  if (!res.ok) throw new Error(`GET /api/policies failed: ${res.status}`);
  return res.json() as Promise&lt;Policy[]&gt;;
}

export async function fetchPolicyById(policyId: string): Promise&lt;PolicyDetail&gt; {
  const res = await fetch(`/api/policies/${policyId}`);
  if (!res.ok) throw new Error(`GET /api/policies/${policyId} failed: ${res.status}`);
  return res.json() as Promise&lt;PolicyDetail&gt;;
}
</code></pre>
<pre><code class="language-ts">// src/types/policy.ts
export interface Policy {
  id: string;
  policyNumber: string;
  holderName: string;
  status: &#39;active&#39; | &#39;expired&#39; | &#39;pending&#39;;
  effectiveDate: string;
}

export interface PolicyDetail extends Policy {
  coverageType: string;
  premiumAmount: number;
  beneficiaries: Beneficiary[];
}

export interface Beneficiary {
  id: string;
  name: string;
  relationship: string;
  percentage: number;
}
</code></pre>
<p>Now <code>useQuery({ queryFn: fetchPolicies })</code> returns <code>Policy[]</code> — typed, autocompleted, checked at every usage. When the backend changes <code>policyNumber</code> to something else, TypeScript tells you <em>before</em> production.</p>
<p>The <code>as Promise&lt;Policy[]&gt;</code> cast on <code>res.json()</code> is intentional. <code>res.json()</code> returns <code>Promise&lt;unknown&gt;</code> in recent TypeScript. We&#39;re making an explicit contract: <em>this fetch function guarantees this shape.</em> If you want runtime validation as well, add Zod:</p>
<pre><code class="language-ts">import { z } from &#39;zod&#39;;

const PolicySchema = z.object({
  id: z.string(),
  policyNumber: z.string(),
  holderName: z.string(),
  status: z.enum([&#39;active&#39;, &#39;expired&#39;, &#39;pending&#39;]),
  effectiveDate: z.string(),
});

export async function fetchPolicies(): Promise&lt;Policy[]&gt; {
  const res = await fetch(&#39;/api/policies&#39;);
  if (!res.ok) throw new Error(`GET /api/policies failed: ${res.status}`);
  const raw = await res.json();
  return z.array(PolicySchema).parse(raw);
}
</code></pre>
<p>With Zod, a field rename or unexpected shape becomes a runtime error with a descriptive message, not a blank screen with no useful stack trace. Article #4 covers error handling strategies — for now, know this option exists.</p>
<h2>Pattern 2: Query Key Factories</h2>
<p>Query keys in TanStack Query are arrays. They look harmless until you have fifty queries and someone types <code>[&#39;policy&#39;, policyId]</code> in one file and <code>[&#39;policies&#39;, id]</code> in another. Invalidation stops working silently.</p>
<p>The fix is a query key factory — one object that owns all keys for a domain:</p>
<pre><code class="language-ts">// src/api/query-keys.ts
export const policyKeys = {
  all: [&#39;policies&#39;] as const,
  lists: () =&gt; [...policyKeys.all, &#39;list&#39;] as const,
  list: (filters: PolicyListFilters) =&gt; [...policyKeys.lists(), filters] as const,
  details: () =&gt; [...policyKeys.all, &#39;detail&#39;] as const,
  detail: (id: string) =&gt; [...policyKeys.details(), id] as const,
};
</code></pre>
<p>Usage:</p>
<pre><code class="language-ts">// In PolicyListPage
useQuery({
  queryKey: policyKeys.list({ status: &#39;active&#39; }),
  queryFn: () =&gt; fetchPolicies({ status: &#39;active&#39; }),
});

// In mutation after update
queryClient.invalidateQueries({ queryKey: policyKeys.lists() });

// In PolicyDetailPage
useQuery({
  queryKey: policyKeys.detail(policyId),
  queryFn: () =&gt; fetchPolicyById(policyId),
});
</code></pre>
<p>The <code>as const</code> assertions make the arrays readonly tuples instead of <code>(string | PolicyListFilters)[]</code>, which means TypeScript knows the exact structure. When you call <code>invalidateQueries({ queryKey: policyKeys.lists() })</code>, it correctly invalidates all list queries regardless of their filter arguments, because the key hierarchy matches.</p>
<p>This is a pattern I keep coming back to. Centralizing keys also makes it trivial to audit which queries exist in an application — you read one file.</p>
<h2>Pattern 3: Discriminated Unions for Async State</h2>
<p>The classic approach to async state in React:</p>
<pre><code class="language-tsx">const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
</code></pre>
<p>This is three independent booleans representing one state machine. The problem: you can have <code>loading: true</code> and <code>data: something</code> at the same time. TypeScript can&#39;t tell you that&#39;s impossible. Your component has to handle combinations that shouldn&#39;t exist.</p>
<p>TanStack Query solves this with its own state machine, but when you need custom async state — or when you want to model UI states explicitly — discriminated unions are the right tool:</p>
<pre><code class="language-ts">// src/types/async-state.ts
export type AsyncState&lt;T&gt; =
  | { status: &#39;idle&#39; }
  | { status: &#39;loading&#39; }
  | { status: &#39;error&#39;; error: Error }
  | { status: &#39;success&#39;; data: T };
</code></pre>
<p>Usage in a component:</p>
<pre><code class="language-tsx">function PolicyDetail({ policyId }: { policyId: string }) {
  const query = useQuery({
    queryKey: policyKeys.detail(policyId),
    queryFn: () =&gt; fetchPolicyById(policyId),
  });

  if (query.isPending) return &lt;PolicyDetailSkeleton /&gt;;
  if (query.isError) return &lt;ErrorMessage error={query.error} /&gt;;

  // TypeScript now knows query.data is PolicyDetail — not PolicyDetail | undefined
  const policy = query.data;

  return (
    &lt;div&gt;
      &lt;h1&gt;{policy.policyNumber}&lt;/h1&gt;
      &lt;p&gt;{policy.holderName}&lt;/p&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>After the <code>isError</code> guard, TypeScript narrows <code>query.data</code> to the non-undefined type. This is TanStack Query&#39;s built-in discriminated union working correctly. The principle applies when you build your own async state machines: model them as discriminated unions from the start, and TypeScript narrows automatically when you check <code>status</code>.</p>
<blockquote>
<p>TypeScript&#39;s type narrowing is only as good as your state model. If your state model allows impossible combinations, no amount of type annotations fixes that.</p>
</blockquote>
<h2>Pattern 4: Typed Route Params with TanStack Router</h2>
<p>This is where TanStack Router pays for itself. In article #1, we set up the router with a <code>Register</code> declaration. Here&#39;s what that gives you:</p>
<pre><code class="language-tsx">// src/pages/PolicyDetailPage.tsx
import { useParams } from &#39;@tanstack/react-router&#39;;

export function PolicyDetailPage() {
  // policyId is string — typed against the route definition
  // TypeScript error if this route doesn&#39;t have $policyId
  const { policyId } = useParams({ from: &#39;/policies/$policyId&#39; });

  // ...
}
</code></pre>
<p>Compare to React Router:</p>
<pre><code class="language-tsx">// React Router — manual cast, no safety
const { policyId } = useParams&lt;{ policyId: string }&gt;();
</code></pre>
<p>The React Router version compiles even if the route doesn&#39;t exist. The TanStack Router version fails to compile if the route doesn&#39;t have <code>$policyId</code>. That&#39;s the difference between type annotations and type inference.</p>
<p>Search params work the same way, but you need to define a validator on the route:</p>
<pre><code class="language-ts">// src/router.ts
import { z } from &#39;zod&#39;;

const policyListSearchSchema = z.object({
  status: z.enum([&#39;active&#39;, &#39;expired&#39;, &#39;pending&#39;]).optional(),
  page: z.number().int().positive().optional().default(1),
});

const policyListRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies&#39;,
  validateSearch: policyListSearchSchema,
  component: PolicyListPage,
});
</code></pre>
<pre><code class="language-tsx">// In PolicyListPage
const { status, page } = useSearch({ from: &#39;/policies&#39; });
// status: &#39;active&#39; | &#39;expired&#39; | &#39;pending&#39; | undefined
// page: number (default 1)
</code></pre>
<p>Search params validated with Zod, typed at the component level, consistent across navigation. No <code>URLSearchParams</code> parsing. No manual type assertions. Navigating to <code>/policies?status=invalid</code> either throws at the router level or falls back to the default — you control the behavior.</p>
<h2>Pattern 5: Generic Data Components</h2>
<p>React components that render lists of things are everywhere. The naive approach writes a new component for every list. The slightly better approach passes <code>items</code> as <code>any[]</code>. The right approach uses generics:</p>
<pre><code class="language-tsx">// src/components/DataList.tsx
interface DataListProps&lt;T&gt; {
  items: T[];
  getKey: (item: T) =&gt; string;
  renderItem: (item: T) =&gt; React.ReactNode;
  emptyMessage?: string;
}

export function DataList&lt;T&gt;({
  items,
  getKey,
  renderItem,
  emptyMessage = &#39;No items found&#39;,
}: DataListProps&lt;T&gt;) {
  if (items.length === 0) {
    return &lt;p className=&quot;text-muted-foreground&quot;&gt;{emptyMessage}&lt;/p&gt;;
  }

  return (
    &lt;ul className=&quot;space-y-2&quot;&gt;
      {items.map((item) =&gt; (
        &lt;li key={getKey(item)}&gt;{renderItem(item)}&lt;/li&gt;
      ))}
    &lt;/ul&gt;
  );
}
</code></pre>
<p>Usage:</p>
<pre><code class="language-tsx">&lt;DataList
  items={policies}
  getKey={(p) =&gt; p.id}
  renderItem={(p) =&gt; &lt;PolicyRow policy={p} /&gt;}
  emptyMessage=&quot;No policies match this filter&quot;
/&gt;
</code></pre>
<p>TypeScript infers <code>T</code> as <code>Policy</code> from the <code>items</code> prop. The <code>getKey</code> and <code>renderItem</code> functions are typed to <code>Policy</code> automatically. You can&#39;t pass a <code>Beneficiary</code> to <code>renderItem</code> by accident.</p>
<p>This pattern works for any container component: tables, grids, selects, autocompletes. Write it once, use it everywhere, get full type safety for free.</p>
<h2>Pattern 6: Typed Context</h2>
<p>React Context is commonly the place where types collapse to <code>any</code> or to <code>T | undefined</code> that requires a null check at every usage. A cleaner pattern:</p>
<pre><code class="language-ts">// src/context/auth-context.tsx
interface AuthContextValue {
  userId: string;
  role: &#39;admin&#39; | &#39;agent&#39; | &#39;viewer&#39;;
  logout: () =&gt; void;
}

const AuthContext = React.createContext&lt;AuthContextValue | null&gt;(null);

export function useAuth(): AuthContextValue {
  const ctx = React.useContext(AuthContext);
  if (!ctx) throw new Error(&#39;useAuth must be used within AuthProvider&#39;);
  return ctx;
}
</code></pre>
<p>The context itself accepts <code>null</code> (before the provider mounts). The <code>useAuth</code> hook throws if used outside the provider and returns the non-null type. Every component that calls <code>useAuth()</code> gets <code>AuthContextValue</code> without optional chaining. The invariant is checked once, at the hook boundary.</p>
<p>This is not a new pattern. It&#39;s in the React docs. But I keep seeing codebases where context values are <code>T | undefined</code> and components are littered with <code>auth?.userId ?? &#39;&#39;</code>. The throw-in-the-hook approach pushes the validation to the right place and cleans up every callsite.</p>
<h2>What to Carry Forward</h2>
<p>These six patterns cover the majority of TypeScript friction I encounter in React codebases:</p>
<ul>
<li><strong>Typed API layer</strong> — type once at the boundary, inherit everywhere</li>
<li><strong>Query key factories</strong> — centralize keys, prevent invalidation bugs</li>
<li><strong>Discriminated unions</strong> — model states correctly, let TypeScript narrow</li>
<li><strong>TanStack Router inference</strong> — route params typed against actual routes, not manual casts</li>
<li><strong>Generic components</strong> — one implementation, full type safety across all usages</li>
<li><strong>Typed context with throwing hooks</strong> — clean callsites, invariant checked once</li>
</ul>
<p>None of these require advanced TypeScript knowledge. They require the discipline to type at boundaries instead of in the middle, and to model state as what it actually is rather than what&#39;s convenient.</p>
<hr>
<p>Six months after that production incident, the insurance CRM had a typed API layer across every endpoint and Zod validation on the most critical ones. We had two more backend field renames during that time. Neither reached production. TypeScript caught both during development, before the PRs were even reviewed.</p>
<p>That&#39;s the whole point — not type coverage as a metric, but fewer incidents as an outcome.</p>
<p>If you&#39;re working through TypeScript patterns in a React codebase and hitting specific friction points — reach out. These problems are usually more mechanical than they appear.</p>
]]></content:encoded>
      <pubDate>Sat, 07 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TypeScript</category>
      <category>TanStack</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Designing Project Documentation for AI Coding Agents</title>
      <link>https://ma-x.im/blog/ai-agent-project-context</link>
      <guid isPermaLink="true">https://ma-x.im/blog/ai-agent-project-context</guid>
      <description>How structured documentation dramatically improves AI-generated code quality by giving agents the context they need.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/ai-agent-project-context/cover.png" alt="Designing Project Documentation for AI Coding Agents" /></p>
<p>I joined a project where everyone was writing code. Not just developers — project managers, analysts, even a designer who had never seen a terminal in his life. They would open VS Code, fire up Google&#39;s Gemini, and ask it to move a button, change a color, add border radius, tweak spacing. And the agent would do it. The code would work. And the codebase would get a little worse every time.</p>
<p>Because the agent had no context. It didn&#39;t know the project used a design system. It didn&#39;t know there were shared components for exactly these things. It didn&#39;t know that some files used CSS modules, others used inline styles, and a third group used something else entirely. So every AI-generated change was technically correct and architecturally wrong. The codebase turned into a patchwork of conflicting approaches.</p>
<p>I spent almost two weeks writing structured project documentation specifically for the agent. Architecture, component rules, naming conventions, styling approach, the whole picture. And then I ran my own test: I told the agent, &quot;Find 20 components that have something to fix.&quot; That&#39;s it. A dead simple, almost primitive prompt. The agent — now armed with full project context — went through the codebase, found real issues (wrong styling approach, missing design tokens, inconsistent patterns), and fixed them on the spot. It was the moment I realized: the model was never the problem. The missing context was.</p>
<h2>Context</h2>
<p>Most teams treat AI coding agents like extremely smart autocomplete. The agent sees the open files, maybe a README, and a prompt like &quot;implement this feature&quot;.</p>
<p>That&#39;s rarely enough.</p>
<p>In real projects, the missing context is huge:</p>
<ul>
<li>Why the product exists</li>
<li>Who the users are</li>
<li>Architectural constraints</li>
<li>Coding standards</li>
<li>Testing expectations</li>
<li>Future migration plans</li>
</ul>
<p>Humans fill these gaps from experience and project history. Agents don&#39;t.</p>
<p>While working on enterprise projects (including work done for large companies such as Procter &amp; Gamble and MedMe), I noticed the same pattern: agents generate better code when the project explicitly describes itself.</p>
<p>The solution that emerged was surprisingly simple.</p>
<p>Create a dedicated documentation layer designed for AI agents.</p>
<p>Not a README.
Not a wiki.</p>
<p>A structured project context directory.</p>
<h2>Core Concept</h2>
<p>The core idea is straightforward:</p>
<p>AI agents need explicit, structured context about the project.</p>
<p>Instead of relying on scattered documentation, the project exposes its knowledge in a predictable location.</p>
<p>Example:</p>
<pre><code>/docs
  product.md
  architecture.md
  coding-standards.md
  testing-strategy.md
  code-review.md
  design-system.md
  roadmap.md
  third-party-services.md
</code></pre>
<p>An index file explains what each document contains and when an agent should read it.</p>
<p>Example:</p>
<pre><code>docs/index.md

product.md
Short overview of the product: users, business goals, main workflows.

architecture.md
High-level architecture of the frontend and backend systems.

coding-standards.md
Project conventions and linting rules the agent must follow.

testing-strategy.md
What must be tested and how tests should be written.
</code></pre>
<p>Then the main instruction file references this folder.</p>
<p>Example:</p>
<pre><code>AGENT.md

When working on this project you MUST read the docs folder.

Start with:
docs/index.md

Based on the task you may need to read:
- architecture.md
- coding-standards.md
- testing-strategy.md
- design-system.md
</code></pre>
<p>This approach turns documentation into something closer to a context API for agents.</p>
<h2>Implementation Example</h2>
<p>A simplified instruction file for an agent might look like this:</p>
<pre><code class="language-md"># AGENT.md

This repository contains a React + Next.js application.

Before implementing any feature:
1. Read docs/index.md
2. Identify which documents are relevant for the task
3. Follow project conventions strictly

Key constraints:

- Use TypeScript strict mode
- Follow design tokens defined in docs/design-system.md
- All components must have tests (see docs/testing-strategy.md)
- Follow code review rules defined in docs/code-review.md
</code></pre>
<p>Example of coding standards exposed to the agent:</p>
<pre><code class="language-md"># coding-standards.md

Component rules:

- Components must be colocated with tests
- Use function components only
- Avoid default exports

Folder structure:

components/
  Button/
    Button.tsx
    Button.test.tsx
    Button.styles.ts
</code></pre>
<p>Example of testing expectations:</p>
<pre><code class="language-md"># testing-strategy.md

All UI components must include:

- unit tests (Vitest)
- interaction tests
- accessibility checks where applicable

Example:

describe(&quot;Button&quot;, () =&gt; {
  it(&quot;renders label&quot;, () =&gt; {})
  it(&quot;triggers click handler&quot;, () =&gt; {})
})
</code></pre>
<p>Once this context exists, agents consistently generate code that is much closer to what a real team would accept.</p>
<h2>Integrating Task Context via MCP</h2>
<p>Documentation alone is useful, but it becomes much more powerful when combined with external context.</p>
<p>Using MCP integrations, an agent can retrieve additional project history:</p>
<ul>
<li>Jira tasks</li>
<li>Git commits</li>
<li>bug reports</li>
<li>previous discussions</li>
</ul>
<p>Example workflow:</p>
<ol>
<li>Agent works on a component</li>
<li>It inspects commit history</li>
<li>Extracts issue IDs</li>
<li>Queries Jira via MCP</li>
<li>Reads bug descriptions</li>
</ol>
<p>This allows the agent to understand things like:</p>
<ul>
<li>why a component was implemented in a specific way</li>
<li>what bugs previously occurred</li>
<li>which constraints must not be broken</li>
</ul>
<p>Without this, agents tend to unknowingly reintroduce old bugs.</p>
<h2>Where It Breaks in Enterprise</h2>
<p>This approach works well, but it introduces new challenges.</p>
<h3>Documentation drift</h3>
<p>Docs become outdated quickly if they are not maintained.</p>
<p>Agents will follow outdated rules very confidently.</p>
<h3>Over-documentation</h3>
<p>Too many documents create the opposite problem: agents get lost in irrelevant context.</p>
<p>Keep documentation focused and scoped.</p>
<h3>Missing architectural intent</h3>
<p>If architecture documents only describe current implementation but not intent, agents will optimize locally and break long-term plans.</p>
<p>For example:</p>
<ul>
<li>migrating from Redux to TanStack Query</li>
<li>replacing a design system</li>
<li>upgrading React versions</li>
</ul>
<p>Agents need to know about these plans.</p>
<h2>Common Mistakes</h2>
<h3>Treating README as agent documentation</h3>
<p>README files are written for humans. They are narrative and incomplete.</p>
<p>Agents work better with:</p>
<ul>
<li>short</li>
<li>structured</li>
<li>explicit documentation.</li>
</ul>
<h3>Documenting only code structure</h3>
<p>Agents also need business context.</p>
<p>Example:</p>
<pre><code>Users: pharmacists
Main workflow: reviewing prescriptions
Critical requirement: audit traceability
</code></pre>
<p>Without this, agents optimize code but miss product constraints.</p>
<h3>Not documenting code review expectations</h3>
<p>Agents can generate correct code that still fails review.</p>
<p>For example:</p>
<ul>
<li>missing tests</li>
<li>wrong folder structure</li>
<li>inconsistent naming</li>
</ul>
<p>Explicit review rules prevent this.</p>
<h2>When NOT To Use This</h2>
<p>This approach is unnecessary for:</p>
<ul>
<li>very small repositories</li>
<li>short-lived prototypes</li>
<li>single-developer experiments</li>
</ul>
<p>The overhead only pays off when:</p>
<ul>
<li>multiple developers collaborate</li>
<li>AI agents are heavily used</li>
<li>the project has architectural complexity.</li>
</ul>
<h2>How I Apply This in Real Projects</h2>
<p>In projects where I use AI coding agents (primarily Claude-based tools, GPT assistants, and Gemini), I always start by creating a docs directory designed for the agent.</p>
<p>The process is simple:</p>
<ol>
<li>Write a short product overview</li>
<li>Describe architecture at a high level</li>
<li>Define coding and testing rules</li>
<li>Document code review expectations</li>
<li>Outline future architectural plans</li>
</ol>
<p>The two-week documentation effort I described at the beginning of this article wasn&#39;t typical — that was a rescue operation for a codebase that had already accumulated months of context-free AI changes. For a new project, the initial setup takes a day or two. After that, you maintain it incrementally.</p>
<p>The &quot;find 20 components to fix&quot; trick has become my standard smoke test for new documentation. If the agent can autonomously identify and fix real issues based on the context you&#39;ve written, the documentation is working. If it makes the same mistakes as before, the docs are missing something important.</p>
<h2>Practical Recommendations</h2>
<p>If you want to try this approach:</p>
<p>Start small.</p>
<p>Create just four documents:</p>
<pre><code>docs/product.md
docs/architecture.md
docs/coding-standards.md
docs/testing-strategy.md
</code></pre>
<p>Add an index file and reference it from your agent instructions.</p>
<p>Then gradually expand documentation as real problems appear.</p>
<p>Also remember:</p>
<ul>
<li>keep documents short</li>
<li>keep them structured</li>
<li>keep them updated.</li>
</ul>
<p>Think of them as system prompts for your codebase.</p>
<h2>Summary</h2>
<p>The project where non-developers were writing code through AI taught me something fundamental: models are already good enough. The bottleneck isn&#39;t intelligence — it&#39;s context. An agent without project documentation will confidently produce code that breaks every convention you have. An agent with structured context will find and fix problems you didn&#39;t even ask about.</p>
<p>A structured docs/ directory can describe:</p>
<ul>
<li>product context</li>
<li>architecture</li>
<li>coding standards</li>
<li>testing rules</li>
<li>code review expectations</li>
<li>future architectural plans</li>
</ul>
<p>Combined with integrations like Jira and Git history, this gives agents enough context to generate code that aligns with real production systems.</p>
<p>In practice, this simple change dramatically improves the quality of AI-generated code. Two weeks of documentation saved me months of cleanup.</p>
<hr>
<p>If you have feedback, alternative approaches, or just want to discuss this topic — feel free to reach out.</p>
]]></content:encoded>
      <pubDate>Fri, 06 Mar 2026 00:00:00 GMT</pubDate>
      <category>AI</category>
      <category>Documentation</category>
      <category>DX</category>
      <category>Architecture</category>
    </item>
    <item>
      <title>Starting a New React Project in 2026-2027: The Stack I Keep Coming Back To</title>
      <link>https://ma-x.im/blog/react-playbook-starting-new-project</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-playbook-starting-new-project</guid>
      <description>Every new React project starts with the same question: which tools? After years of watching teams assemble mismatched libraries that fight each other, I stopped choosing independently. This is the coherent stack I now use by default — and why.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-playbook-starting-new-project/cover.png" alt="Starting a New React Project in 2026-2027: The Stack I Keep Coming Back To" /></p>
<p>I was three weeks into a new client engagement — a mid-sized insurance company building a policy administration CRM from scratch. No legacy code, no inherited debt. Just a blank repo and a brief that said &quot;build it right this time.&quot;</p>
<p>The first real decision hit before I wrote a single component. Not React vs. something else — that was settled. The question was: <em>what handles routing? What handles server state? What handles forms? What handles client state?</em> Each one is a separate choice. Each one affects the others.</p>
<p>I&#39;ve been in that moment before. Different company, different domain, same paralysis. And I&#39;ve watched teams make the same mistake each time: they pick tools independently, without thinking about how they work together. React Router because someone knows it. Redux Toolkit because it was used at the last job. React Hook Form because of a tutorial someone bookmarked. Three months in, you have routing that&#39;s loosely typed, server state duplicated in Redux, and form validation that doesn&#39;t talk to your query layer. The tools work in isolation. The system doesn&#39;t.</p>
<p>That insurance CRM was where I fully committed to TanStack as an ecosystem. Not because it was new. Because it was the most coherent answer I&#39;d found to a question I&#39;d been asking for years: <em>how do these pieces actually fit together?</em></p>
<p>This is the first article in the <strong>React Playbook</strong> series — a hands-on guide to building production React apps with TanStack and a thoughtful architecture. We&#39;re starting here, at the beginning, because the choices you make in the first hour of a project shape everything that comes after.</p>
<h2>What TanStack Actually Is</h2>
<p>TanStack is a family of headless, framework-agnostic libraries maintained primarily by Tanner Linsley. The word <em>family</em> matters. These packages are designed to work independently — you can use TanStack Query with React Router, or TanStack Router with SWR — but they&#39;re built by the same team with consistent APIs, consistent TypeScript patterns, and a consistent philosophy about what a library should own vs. what your application should own.</p>
<p>The four packages that form the core of this series:</p>
<ul>
<li><strong>TanStack Query</strong> — async state. Fetching, caching, background refetches, mutations, optimistic updates. It doesn&#39;t care how you fetch; it cares about <em>what happens to the data after you fetch it</em>.</li>
<li><strong>TanStack Router</strong> — type-safe routing. Every route param, every search param, every navigate call is typed against your actual route tree. No <code>useParams&lt;{ id: string }&gt;()</code> manual type assertions.</li>
<li><strong>TanStack Form</strong> — form state management. Headless, composable, works with any validation library. Deep integration with Query mutations comes naturally.</li>
<li><strong>TanStack Start</strong> — full-stack framework built on top of TanStack Router. SSR, server functions, streaming. We&#39;ll cover this in article #15. For now, know it exists.</li>
</ul>
<p>What&#39;s not in TanStack: client state management — the kind of state that lives only in the browser and doesn&#39;t come from a server. For that, this series uses <strong>Reatom</strong>. I&#39;ll explain that choice properly in article #7. For now, note that <code>useState</code> and TanStack Query handle the vast majority of state in a typical app. Reatom enters the picture when you have genuinely complex reactive graphs.</p>
<blockquote>
<p>The best stack isn&#39;t the one with the individually best tools. It&#39;s the one where the tools share a language.</p>
</blockquote>
<h2>Two Ways to Start</h2>
<h3>Option 1: Vite (SPA)</h3>
<p>If you&#39;re building a client-rendered SPA — with a separate API, no SSR, no server functions — Vite is the starting point:</p>
<pre><code class="language-bash">npm create vite@latest my-crm-app -- --template react-ts
cd my-crm-app
npm install
</code></pre>
<p>Then add the TanStack core:</p>
<pre><code class="language-bash">npm install @tanstack/react-query @tanstack/react-router @tanstack/react-form
npm install -D @tanstack/router-devtools @tanstack/react-query-devtools
</code></pre>
<p>Add Zod for schema validation — it integrates directly with TanStack Form and pairs naturally with TypeScript:</p>
<pre><code class="language-bash">npm install zod
</code></pre>
<p>That&#39;s the complete starting dependency set. Not ten packages. Five.</p>
<h3>Option 2: TanStack Start (SSR / Full-stack)</h3>
<p>If you need SSR, server functions, or want to colocate data fetching with your routes:</p>
<pre><code class="language-bash">npx create-tsrouter-app@latest my-crm-app --template start-basic
cd my-crm-app
npm install
</code></pre>
<p>TanStack Start comes with React, TanStack Router, and Vinxi (the underlying build layer) preconfigured. You add TanStack Query and Form separately — same commands as above.</p>
<p>For the rest of this article — and the majority of this series — I&#39;ll use the Vite setup. We&#39;ll revisit TanStack Start in article #15 when we get to SSR and full-stack patterns.</p>
<h2>The Router Setup</h2>
<p>TanStack Router supports both file-based and code-based route definitions. I use code-based for new projects. It&#39;s more explicit, easier to understand when you&#39;re learning the ecosystem, and easier to refactor when your route tree changes.</p>
<p>Start by creating the route tree:</p>
<pre><code class="language-tsx">// src/router.ts
import { createRouter, createRoute, createRootRoute } from &#39;@tanstack/react-router&#39;;
import { RootLayout } from &#39;./layouts/RootLayout&#39;;
import { DashboardPage } from &#39;./pages/DashboardPage&#39;;
import { PolicyListPage } from &#39;./pages/PolicyListPage&#39;;
import { PolicyDetailPage } from &#39;./pages/PolicyDetailPage&#39;;

const rootRoute = createRootRoute({
  component: RootLayout,
});

const dashboardRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/&#39;,
  component: DashboardPage,
});

const policyListRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies&#39;,
  component: PolicyListPage,
});

const policyDetailRoute = createRoute({
  getParentRoute: () =&gt; rootRoute,
  path: &#39;/policies/$policyId&#39;,
  component: PolicyDetailPage,
});

const routeTree = rootRoute.addChildren([
  dashboardRoute,
  policyListRoute,
  policyDetailRoute,
]);

export const router = createRouter({ routeTree });

declare module &#39;@tanstack/react-router&#39; {
  interface Register {
    router: typeof router;
  }
}
</code></pre>
<p>The <code>Register</code> declaration at the bottom is what makes everything type-safe. From this point forward, every <code>useParams()</code>, every <code>&lt;Link to=&quot;&quot;&gt;</code>, every <code>navigate()</code> call is checked against your actual route tree. If you rename <code>$policyId</code> to <code>$id</code>, TypeScript will tell you every single broken reference in the codebase. That&#39;s not a nice-to-have at scale. That&#39;s essential.</p>
<h2>The App Entry Point</h2>
<pre><code class="language-tsx">// src/main.tsx
import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;
import { QueryClient, QueryClientProvider } from &#39;@tanstack/react-query&#39;;
import { RouterProvider } from &#39;@tanstack/react-router&#39;;
import { ReactQueryDevtools } from &#39;@tanstack/react-query-devtools&#39;;
import { router } from &#39;./router&#39;;

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 1000 * 60 * 5,
      retry: 1,
    },
  },
});

ReactDOM.createRoot(document.getElementById(&#39;root&#39;)!).render(
  &lt;React.StrictMode&gt;
    &lt;QueryClientProvider client={queryClient}&gt;
      &lt;RouterProvider router={router} /&gt;
      &lt;ReactQueryDevtools initialIsOpen={false} /&gt;
    &lt;/QueryClientProvider&gt;
  &lt;/React.StrictMode&gt;,
);
</code></pre>
<p>Two decisions embedded here that are worth naming explicitly.</p>
<p><strong><code>staleTime: 1000 * 60 * 5</code></strong> — five minutes. By default, TanStack Query considers data stale immediately and refetches on every component mount and every window focus event. For a CRM where users navigate between the same policy records repeatedly, that&#39;s a lot of unnecessary network traffic. Five minutes as a global baseline is a reasonable starting point. You&#39;ll override it per-query when freshness matters more.</p>
<p><strong><code>retry: 1</code></strong> — retry failed requests once, not three times (the default). The default behavior means a failed API call spends up to 30 seconds retrying before your user sees an error. In a CRM, users notice latency. Fail fast, show the error, let the user decide whether to retry.</p>
<h2>Your First Query</h2>
<p>A policy list page. Here&#39;s what the data layer looks like end to end:</p>
<pre><code class="language-ts">// src/api/policies.ts
import type { Policy } from &#39;../types/policy&#39;;

export async function fetchPolicies(): Promise&lt;Policy[]&gt; {
  const res = await fetch(&#39;/api/policies&#39;);
  if (!res.ok) throw new Error(`Failed to fetch policies: ${res.status}`);
  return res.json();
}
</code></pre>
<pre><code class="language-tsx">// src/pages/PolicyListPage.tsx
import { useQuery } from &#39;@tanstack/react-query&#39;;
import { Link } from &#39;@tanstack/react-router&#39;;
import { fetchPolicies } from &#39;../api/policies&#39;;

export function PolicyListPage() {
  const { data: policies, isLoading, isError } = useQuery({
    queryKey: [&#39;policies&#39;],
    queryFn: fetchPolicies,
  });

  if (isLoading) return &lt;div&gt;Loading policies...&lt;/div&gt;;
  if (isError) return &lt;div&gt;Failed to load policies&lt;/div&gt;;

  return (
    &lt;ul&gt;
      {policies.map((policy) =&gt; (
        &lt;li key={policy.id}&gt;
          &lt;Link to=&quot;/policies/$policyId&quot; params={{ policyId: policy.id }}&gt;
            {policy.policyNumber}
          &lt;/Link&gt;
        &lt;/li&gt;
      ))}
    &lt;/ul&gt;
  );
}
</code></pre>
<p>A few things worth making explicit.</p>
<p><strong><code>queryKey: [&#39;policies&#39;]</code></strong> — this is the identity of the data. TanStack Query caches, deduplicates, and invalidates data by key. When you update a policy, you&#39;ll call <code>queryClient.invalidateQueries({ queryKey: [&#39;policies&#39;] })</code> and this list will automatically refetch. The key design decision: keep keys predictable and structured. We&#39;ll go deep on key patterns in article #5.</p>
<p><strong><code>&lt;Link to=&quot;/policies/$policyId&quot; params={{ policyId: policy.id }}&gt;</code></strong> — fully type-checked. The <code>to</code> prop only accepts valid routes from your route tree. The <code>params</code> prop is typed to exactly what that route expects. You cannot pass a typo here and have it compile.</p>
<h2>What You Don&#39;t Need Yet</h2>
<p>I keep coming back to this piece of advice because I&#39;ve seen it ignored repeatedly on greenfield projects.</p>
<p>Do not install:</p>
<ul>
<li>An i18n library until you actually have a second language requirement</li>
<li>A component library until you understand your design system constraints</li>
<li>A global state manager beyond <code>useState</code> until Query&#39;s cache isn&#39;t enough</li>
<li>A charting library until you&#39;re building a chart</li>
<li>An analytics package until the product is real</li>
</ul>
<p>Every package you add before you need it is a decision made without enough information. The &quot;complete stack&quot; instinct — wanting to assemble everything before writing features — consistently results in wrong choices that are expensive to undo.</p>
<p>Honestly, the most common refactor I do on client codebases is removing things. Unused dependencies, abstractions that outlived their problems, config for features that were never built.</p>
<p>Start with the minimum. Add what you need when you need it.</p>
<h2>Project Structure</h2>
<p>Article #3 covers FSD (Feature-Sliced Design) properly, so I won&#39;t go deep here. But the starting skeleton that works for the first 20-30 features of any project:</p>
<pre><code>src/
  api/            ← fetch functions, typed response shapes
  pages/          ← page-level components wired to routes
  layouts/        ← RootLayout, AuthedLayout, etc.
  components/     ← shared, reusable UI
  hooks/          ← shared React hooks
  types/          ← TypeScript interfaces and domain types
  router.ts       ← route tree definition
  main.tsx        ← app entry point
</code></pre>
<p>This structure is deliberately flat. It scales to about 25-30 features before it starts feeling crowded. At that point, you&#39;ll naturally feel the pull toward domain-based organization — and that&#39;s when FSD becomes the right tool. We&#39;ll get there.</p>
<h2>Running It</h2>
<pre><code class="language-bash">npm run dev
</code></pre>
<p>You&#39;ll see two devtools panels appear: the TanStack Router devtools in the bottom corner and the React Query devtools floating panel. Both are worth learning. Router devtools shows your matched route, route params, and pending navigations. Query devtools shows every active query, its status, its data, its stale time, and lets you manually invalidate or refetch. Remove them for production by checking <code>import.meta.env.DEV</code>.</p>
<h2>What Comes Next</h2>
<p>This setup — Vite + TanStack Query + TanStack Router — is the foundation everything else in this series builds on. Over the next 29 articles:</p>
<ul>
<li><strong>Article #2:</strong> <a href="https://ma-x.im/blog/react-playbook-typescript-patterns">TypeScript patterns specific to this stack</a> — generics in query hooks, typed API layers, discriminated unions for UI states</li>
<li><strong>Article #3:</strong> <a href="https://ma-x.im/blog/react-playbook-code-structure">FSD architecture</a> — when the flat <code>pages/</code> structure starts hurting and what replaces it</li>
<li><strong>Article #4:</strong> Error handling — Error Boundaries, query error states, global error strategies</li>
<li><strong>Article #7:</strong> Reatom — when your client state grows beyond what <code>useState</code> handles cleanly</li>
</ul>
<p>The same QueryClient, the same router, the same project structure — growing incrementally instead of being replaced. That&#39;s the goal.</p>
<hr>
<p>That insurance CRM shipped twelve months later. Four developers, significant business logic, a forms-heavy UI with dozens of policy types. The TanStack ecosystem handled all of it without once making us fight our own tools. That&#39;s the standard I hold stacks to: not &quot;is it powerful,&quot; but &quot;does it get out of the way.&quot;</p>
<p>If you&#39;re starting a new React project and want to talk through stack choices before committing — feel free to reach out. I&#39;ve had this conversation more times than I can count and it rarely takes as long as people expect.</p>
]]></content:encoded>
      <pubDate>Tue, 03 Mar 2026 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>TanStack</category>
      <category>TypeScript</category>
      <category>React Playbook</category>
    </item>
    <item>
      <title>Next.js Static Export to GitHub Pages: A Production Setup Guide</title>
      <link>https://ma-x.im/blog/nextjs-ssg-github-pages</link>
      <guid isPermaLink="true">https://ma-x.im/blog/nextjs-ssg-github-pages</guid>
      <description>How to configure Next.js 15 static export and deploy to GitHub Pages with a custom domain. Covers App Router, basePath, react-markdown blog pipeline, GitHub Actions CI/CD, and common pitfalls.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/nextjs-ssg-github-pages/cover.png" alt="Next.js Static Export to GitHub Pages: A Production Setup Guide" /></p>
<p>Static export remains the simplest deployment model for content-focused sites. This article documents the setup behind this very site — ma-x.im — a Next.js 15 project deployed to GitHub Pages.</p>
<p>Honestly, this was my first static export project. Vercel exists and it&#39;s free too. But I wanted something dead simple — no platform dependencies, no vendor lock-in, just push and it&#39;s live. GitHub Pages felt like the most boring choice possible, and that was exactly the point. A few weeks in, I&#39;m genuinely happy with it. I don&#39;t think about deployments, I don&#39;t debug infrastructure, it just works. Sometimes the boring choice is the right one.</p>
<h2>Context</h2>
<p>The goal: a personal site with static content and a blog, deployed to GitHub Pages with a custom domain. Requirements are low ops overhead, no monthly costs, and good reliability. GitHub Pages handles all of this well for static content.</p>
<p>GitHub Pages constraints shape the architecture:</p>
<p><strong>Static hosting only.</strong> No server-side rendering, no API routes that execute at runtime, no middleware. Everything must be pre-rendered at build time.</p>
<p><strong>Project Page path prefix.</strong> If you deploy to <code>https://&lt;user&gt;.github.io/&lt;repo&gt;/</code>, all routes are prefixed with <code>/&lt;repo&gt;/</code>. This breaks absolute paths and requires explicit configuration. With a custom domain, this prefix disappears — the site serves at root.</p>
<p><strong>No redirects or rewrites.</strong> GitHub Pages serves files as-is. You cannot configure server-side redirects. Client-side routing works, but direct navigation to a non-existent file returns GitHub&#39;s 404 page unless you provide a <code>404.html</code>.</p>
<p><strong>404 handling.</strong> GitHub Pages serves <code>404.html</code> from the root when a file isn&#39;t found. Next.js static export generates this file from your <code>not-found.tsx</code>, but only if you have one.</p>
<h2>Core Concept</h2>
<h3>Next.js SSG vs SSR/ISR</h3>
<p>Next.js supports multiple rendering strategies. For GitHub Pages, only Static Site Generation (SSG) works:</p>
<ul>
<li><strong>SSR (Server-Side Rendering):</strong> Renders pages on each request. Requires a Node.js server. Not compatible with static hosting.</li>
<li><strong>ISR (Incremental Static Regeneration):</strong> Pre-renders at build, then revalidates on a schedule. Requires a server. Not compatible with static hosting.</li>
<li><strong>SSG (Static Site Generation):</strong> Pre-renders all pages at build time. Outputs plain HTML/CSS/JS files. Works on any static host.</li>
</ul>
<h3>Static Export in Next.js 15</h3>
<p>In Next.js 13+, static export is enabled via <code>output: &#39;export&#39;</code> in your config. The build process generates a folder (default: <code>out/</code>) containing all HTML files, assets, and client-side JavaScript bundles.</p>
<p>This mode disables features that require a server runtime:</p>
<ul>
<li>API routes (unless they&#39;re static JSON files)</li>
<li>Server Actions</li>
<li>Middleware</li>
<li>Dynamic rendering with <code>cookies()</code> or <code>headers()</code></li>
<li>Redirects and rewrites in <code>next.config.ts</code></li>
<li>Image Optimization API (requires a server)</li>
</ul>
<h3>basePath for Project Pages</h3>
<p>When deploying to <code>https://&lt;user&gt;.github.io/&lt;repo&gt;/</code>, the site lives at a subpath, not root. Without configuration, asset URLs and links break:</p>
<pre><code>Expected: /styles/main.css
Actual location: /&lt;repo&gt;/styles/main.css
→ 404
</code></pre>
<p>The <code>basePath</code> config prepends the prefix to all routes and asset imports:</p>
<pre><code class="language-typescript">// next.config.ts
const nextConfig: NextConfig = {
  basePath: &#39;/my-repo&#39;,
  output: &#39;export&#39;,
};
</code></pre>
<p>Next.js automatically handles:</p>
<ul>
<li><code>&lt;Link href=&quot;/about&quot;&gt;</code> becomes <code>/&lt;repo&gt;/about</code></li>
<li>Static imports and public folder assets</li>
<li>CSS/JS bundle paths</li>
</ul>
<p><strong>With a custom domain, basePath should not be set.</strong> The domain points directly to the repo&#39;s Pages site, so routes serve at root.</p>
<h3>trailingSlash</h3>
<p>Controls whether routes end with <code>/</code>:</p>
<ul>
<li><code>trailingSlash: false</code> → <code>/blog/my-post</code> (outputs <code>/blog/my-post.html</code>)</li>
<li><code>trailingSlash: true</code> → <code>/blog/my-post/</code> (outputs <code>/blog/my-post/index.html</code>)</li>
</ul>
<p>GitHub Pages handles both, but <code>trailingSlash: true</code> is more predictable for some edge cases with direct file navigation. The trade-off is URL aesthetics. This repo uses <code>false</code>.</p>
<h3>assetPrefix</h3>
<p>Separate from <code>basePath</code>, <code>assetPrefix</code> controls where JS/CSS bundles are loaded from. Useful when assets are served from a CDN. For most GitHub Pages deployments, it&#39;s not needed — <code>basePath</code> handles everything.</p>
<h3>next/image on Static Hosts</h3>
<p>The default <code>next/image</code> component uses Next.js Image Optimization API, which requires a server. On static export, you must either:</p>
<ol>
<li>Set <code>images: { unoptimized: true }</code> to disable optimization and use raw image URLs</li>
<li>Use a third-party loader (Cloudinary, Imgix, etc.)</li>
<li>Avoid <code>next/image</code> entirely and use <code>&lt;img&gt;</code> tags</li>
</ol>
<p>This repo uses native <code>&lt;img&gt;</code> tags with a custom fallback wrapper, avoiding the issue entirely.</p>
<h2>Implementation Example</h2>
<h3>1) Configure Next.js for static export (SSG)</h3>
<p>The current <code>next.config.ts</code> in this repo:</p>
<pre><code class="language-typescript">import type { NextConfig } from &#39;next&#39;;

const nextConfig: NextConfig = {
  output: &#39;export&#39;,
  images: {
    unoptimized: true,
  },
  trailingSlash: false,
};

export default nextConfig;
</code></pre>
<p>This configuration:</p>
<ul>
<li>Enables static export to <code>out/</code> directory</li>
<li>Disables image optimization (required for static hosting)</li>
<li>Uses clean URLs without trailing slashes</li>
</ul>
<p><strong>For a Project Page deployment</strong> (without custom domain), add <code>basePath</code>:</p>
<pre><code class="language-typescript">// Proposed change for Project Page deployment
const nextConfig: NextConfig = {
  output: &#39;export&#39;,
  basePath: &#39;/ma-x.im&#39;, // Repository name
  images: {
    unoptimized: true,
  },
  trailingSlash: false,
};
</code></pre>
<p><strong>Limitations to understand:</strong></p>
<p>With <code>output: &#39;export&#39;</code>, these features throw build errors or are ignored:</p>
<ul>
<li><code>redirects</code> and <code>rewrites</code> in config</li>
<li>Middleware (<code>middleware.ts</code>)</li>
<li>API routes with runtime logic</li>
<li>Server Actions</li>
<li><code>cookies()</code>, <code>headers()</code>, <code>searchParams</code> in Server Components (without <code>force-static</code>)</li>
</ul>
<h3>2) Make routing and links survive basePath (Project Page)</h3>
<p><strong>The failure mode:</strong> During development, the site runs at <code>localhost:3000/</code>. Links like <code>&lt;Link href=&quot;/blog&quot;&gt;</code> work. After deploying to <code>https://user.github.io/repo/</code>, the same link points to <code>https://user.github.io/blog</code> (missing the repo prefix), returning 404.</p>
<p><strong>How Next.js handles it:</strong> When <code>basePath</code> is configured, <code>next/link</code> and <code>next/router</code> automatically prepend it. You don&#39;t need to change your code — just avoid hardcoded absolute paths.</p>
<p>From this repo&#39;s <code>src/app/blog/page.tsx</code>:</p>
<pre><code class="language-tsx">import Link from &#39;next/link&#39;;

// This works correctly with or without basePath
&lt;Link href={\`/blog/\${post.id}\`}&gt;
  &lt;h2&gt;{post.title}&lt;/h2&gt;
&lt;/Link&gt;
</code></pre>
<p><strong>What breaks:</strong></p>
<ol>
<li><strong>Hardcoded absolute paths in JavaScript/CSS:</strong></li>
</ol>
<pre><code class="language-tsx">// Bad: Ignores basePath
const logoUrl = &#39;/images/logo.png&#39;;

// Good: Use relative paths or public folder imports
import logo from &#39;@/public/images/logo.png&#39;;
</code></pre>
<ol start="2">
<li><strong>Metadata URLs (OpenGraph, Twitter, canonical):</strong></li>
</ol>
<p>From this repo&#39;s <code>src/app/blog/[slug]/page.tsx</code>:</p>
<pre><code class="language-tsx">export async function generateMetadata({ params }: BlogPostPageProps): Promise&lt;Metadata&gt; {
  const { slug } = await params;
  const post = blogPosts.find((p) =&gt; p.id === slug);

  return {
    openGraph: {
      url: \`https://ma-x.im/blog/\${post.id}\`, // Hardcoded full URL
    },
  };
}
</code></pre>
<p>This works because the site uses a custom domain at root. For a Project Page, you&#39;d need:</p>
<pre><code class="language-tsx">// Proposed change for Project Page
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || &#39;https://user.github.io/repo&#39;;

url: \`\${BASE_URL}/blog/\${post.id}\`,
</code></pre>
<ol start="3">
<li><strong>Sitemap and RSS links:</strong></li>
</ol>
<p>This repo generates a sitemap via <code>app/sitemap.ts</code>. All URLs must use the full absolute base. Note the <code>export const dynamic = &#39;force-static&#39;</code> — it is required for <code>output: &#39;export&#39;</code>, otherwise the build fails:</p>
<pre><code class="language-typescript">// app/sitemap.ts
import type { MetadataRoute } from &#39;next&#39;;
import { blogPosts } from &#39;@/data/posts&#39;;

export const dynamic = &#39;force-static&#39;;

const BASE_URL = &#39;https://ma-x.im&#39;;

export default function sitemap(): MetadataRoute.Sitemap {
  const posts = blogPosts.map((post) =&gt; ({
    url: \`\${BASE_URL}/blog/\${post.id}\`,
    lastModified: new Date(post.date),
    changeFrequency: &#39;monthly&#39; as const,
    priority: 0.7,
  }));

  return [
    { url: BASE_URL, lastModified: new Date(), changeFrequency: &#39;monthly&#39;, priority: 1 },
    { url: \`\${BASE_URL}/blog\`, lastModified: new Date(), changeFrequency: &#39;weekly&#39;, priority: 0.8 },
    { url: \`\${BASE_URL}/contact\`, lastModified: new Date(), changeFrequency: &#39;yearly&#39;, priority: 0.5 },
    ...posts,
  ];
}
</code></pre>
<p>New blog posts are picked up automatically — the sitemap is built from the same <code>blogPosts</code> array used by the blog pages.</p>
<p>Pair the sitemap with <code>app/robots.ts</code> so search engines can discover it. Same rule applies — <code>export const dynamic = &#39;force-static&#39;</code> is required:</p>
<pre><code class="language-typescript">// app/robots.ts
import type { MetadataRoute } from &#39;next&#39;;

export const dynamic = &#39;force-static&#39;;

export default function robots(): MetadataRoute.Robots {
  return {
    rules: {
      userAgent: &#39;*&#39;,
      allow: &#39;/&#39;,
    },
    sitemap: &#39;https://ma-x.im/sitemap.xml&#39;,
  };
}
</code></pre>
<h3>3) Blog pipeline: Markdown + react-markdown (repo-specific)</h3>
<p>This repo stores blog posts as TypeScript files with embedded markdown content, not as <code>.md</code> files. This approach has trade-offs:</p>
<p><strong>Advantages:</strong></p>
<ul>
<li>Type safety on post metadata</li>
<li>No build-time file system reading</li>
<li>Posts are bundled with the application</li>
</ul>
<p><strong>Disadvantages:</strong></p>
<ul>
<li>Content editing requires code changes</li>
<li>Larger JavaScript bundles if content grows</li>
<li>No separation between content and code</li>
</ul>
<p><strong>Post structure</strong> in <code>src/data/posts/types.ts</code>:</p>
<pre><code class="language-typescript">export interface BlogPost {
  id: string;
  title: string;
  excerpt: string;
  tags: string[];
  date: string;
  readTime: string;
  content: string; // markdown content
}
</code></pre>
<p><strong>Static route generation</strong> in <code>src/app/blog/[slug]/page.tsx</code>:</p>
<pre><code class="language-tsx">import { blogPosts } from &#39;@/data/posts/index&#39;;

// Generate static pages for all posts at build time
export async function generateStaticParams() {
  return blogPosts.map((post) =&gt; ({
    slug: post.id,
  }));
}

// Return 404 for slugs not in the list (required for output: &#39;export&#39;)
export const dynamicParams = false;
</code></pre>
<p>The <code>dynamicParams = false</code> is critical. Without it, Next.js assumes dynamic routes might be handled at runtime — incompatible with static export.</p>
<p><strong>Rendering markdown</strong> via <code>src/components/MarkdownContent.tsx</code>:</p>
<pre><code class="language-tsx">&#39;use client&#39;;

import ReactMarkdown from &#39;react-markdown&#39;;
import remarkGfm from &#39;remark-gfm&#39;;
import { Prism as SyntaxHighlighter } from &#39;react-syntax-highlighter&#39;;
import { oneDark } from &#39;react-syntax-highlighter/dist/esm/styles/prism&#39;;

export function MarkdownContent({ content }: { content: string }) {
  return (
    &lt;ReactMarkdown
      remarkPlugins={[remarkGfm]}
      components={{
        code: ({ className, children, ...props }: any) =&gt; {
          const match = /language-(\w+)/.exec(className || &#39;&#39;);
          const codeString = String(children).replace(/\n$/, &#39;&#39;);

          if (match) {
            return (
              &lt;SyntaxHighlighter style={oneDark} language={match[1]} PreTag=&quot;div&quot;&gt;
                {codeString}
              &lt;/SyntaxHighlighter&gt;
            );
          }

          return &lt;code className=&quot;px-2 py-0.5 bg-gray-100 rounded text-sm&quot; {...props}&gt;{children}&lt;/code&gt;;
        },
      }}
    &gt;
      {content}
    &lt;/ReactMarkdown&gt;
  );
}
</code></pre>
<p><strong>Common pitfalls:</strong></p>
<ol>
<li><p><strong>Missing <code>generateStaticParams</code>:</strong> Dynamic routes without static params fail on export with a build error.</p>
</li>
<li><p><strong>Forgetting <code>dynamicParams = false</code>:</strong> The build succeeds, but routes not in <code>generateStaticParams</code> cause errors.</p>
</li>
<li><p><strong>Runtime file system access:</strong> If you read <code>.md</code> files with <code>fs</code>, ensure it happens only in <code>generateStaticParams</code> or <code>generateMetadata</code>, not in component render functions. In App Router, this works correctly since those functions run at build time.</p>
</li>
<li><p><strong>Syntax highlighter SSR issues:</strong> Libraries like <code>react-syntax-highlighter</code> can have hydration mismatches. Marking the component with <code>&#39;use client&#39;</code> and ensuring consistent rendering helps.</p>
</li>
</ol>
<h3>4) GitHub Actions deployment to GitHub Pages</h3>
<p>The existing workflow at <code>.github/workflows/deploy.yml</code>:</p>
<pre><code class="language-yaml">name: Deploy to GitHub Pages

on:
  push:
    branches: [master, new-version-v2]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: &quot;pages&quot;
  cancel-in-progress: false

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: &#39;20&#39;
          cache: &#39;npm&#39;

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Type check
        run: npx tsc --noEmit

      - name: Build
        run: npm run build

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./out

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
</code></pre>
<p><strong>Key elements:</strong></p>
<ol>
<li><strong>Permissions:</strong> <code>pages: write</code> and <code>id-token: write</code> are required for the official deploy action. <code>contents: read</code> allows checkout.</li>
<li><strong>Lint and type check before build:</strong> Catches issues before producing artifacts. Lint validates code style, <code>tsc --noEmit</code> verifies types without emitting files.</li>
<li><strong>Concurrency:</strong> Prevents overlapping deployments with <code>cancel-in-progress: false</code> (lets the current deployment finish rather than canceling mid-deploy).</li>
<li><strong>Artifact upload:</strong> The <code>upload-pages-artifact</code> action packages <code>./out</code> (Next.js static export directory) for deployment.</li>
<li><strong>Two-job structure:</strong> Build and deploy are separate jobs. The deploy job waits for build with <code>needs: build</code>.</li>
<li><strong>Environment:</strong> The <code>github-pages</code> environment enables GitHub&#39;s deployment tracking and status checks.</li>
</ol>
<p><strong>Repo settings checklist:</strong></p>
<ol>
<li>Navigate to repository Settings → Pages</li>
<li>Under &quot;Build and deployment&quot;, set Source to &quot;GitHub Actions&quot;</li>
<li>Do not select a branch — the workflow handles deployment</li>
<li>If using a custom domain, enter it under &quot;Custom domain&quot;</li>
<li>Enforce HTTPS (checkbox)</li>
</ol>
<h3>5) Custom domain on GitHub Pages</h3>
<p>This repo uses the custom domain <code>ma-x.im</code>. Setup involves three parts:</p>
<p><strong>1. CNAME file in the repository:</strong></p>
<p>The file <code>public/CNAME</code> contains:</p>
<pre><code>ma-x.im
</code></pre>
<p>This file must exist in the deployed output. Since it&#39;s in <code>public/</code>, Next.js copies it to <code>out/</code> during build. The GitHub Pages deploy action then includes it in the deployment artifact.</p>
<p><strong>2. Repository settings:</strong></p>
<p>In Settings → Pages → Custom domain, enter <code>ma-x.im</code>. GitHub validates DNS and provisions an SSL certificate.</p>
<p><strong>3. DNS configuration:</strong></p>
<p>For an apex domain like <code>ma-x.im</code>, configure DNS at your registrar:</p>
<p><strong>Option A: A records (IPv4)</strong></p>
<pre><code>A     @     185.199.108.153
A     @     185.199.109.153
A     @     185.199.110.153
A     @     185.199.111.153
</code></pre>
<p><strong>Option B: ALIAS/ANAME record (if supported)</strong></p>
<pre><code>ALIAS    @    &lt;user&gt;.github.io
</code></pre>
<p>For a subdomain like <code>www.ma-x.im</code>:</p>
<pre><code>CNAME    www    &lt;user&gt;.github.io
</code></pre>
<p><strong>HTTPS certificate provisioning:</strong></p>
<p>After DNS propagates, GitHub automatically requests a certificate from Let&#39;s Encrypt. This can take minutes to hours depending on DNS propagation. During this time, the site may show a certificate warning.</p>
<p><strong>Keeping CNAME stable:</strong></p>
<p>The current setup (CNAME in <code>public/</code>) works correctly. An alternative for repos where you can&#39;t commit to <code>public/</code>:</p>
<pre><code class="language-yaml">      - name: Build
        run: npm run build

      - name: Add CNAME
        run: echo &quot;ma-x.im&quot; &gt; ./out/CNAME

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./out
</code></pre>
<h3>6) Local verification and &quot;it works on my machine&quot; traps</h3>
<p>After building, verify the static output locally:</p>
<pre><code class="language-bash"># Build the site
npm run build

# Serve the output directory
npx serve out
</code></pre>
<p>This serves at <code>http://localhost:3000</code> by default. All routes should work, including direct navigation to <code>/blog/building-design-systems-that-scale</code>.</p>
<p><strong>Testing basePath locally:</strong></p>
<p>If deploying as a Project Page with <code>basePath: &#39;/my-repo&#39;</code>, the local test needs to simulate the path prefix:</p>
<pre><code class="language-bash">npx serve out -l 3000
# Then access at http://localhost:3000/my-repo/
</code></pre>
<p><strong>&quot;Works locally, broken in production&quot; scenarios:</strong></p>
<ol>
<li><strong>Missing CNAME:</strong> Site deploys but custom domain doesn&#39;t work.</li>
<li><strong>Absolute paths in code:</strong> Work at root locally, break with basePath or different domain.</li>
<li><strong>Environment variables:</strong> <code>NEXT_PUBLIC_*</code> variables are baked in at build time. If you test with <code>.env.local</code> but CI doesn&#39;t have the same values, behavior differs.</li>
<li><strong>Browser caching:</strong> Hard refresh or incognito mode when debugging deployment issues.</li>
</ol>
<h2>Where It Breaks in Enterprise</h2>
<p>Static export works for content sites but hits walls in enterprise contexts:</p>
<p><strong>No server features.</strong> Auth, personalization, A/B testing, and anything requiring runtime logic must move to external services or client-side JavaScript.</p>
<p><strong>Preview and drafts.</strong> Editorial workflows typically need preview deployments of unpublished content. Static export requires rebuilding for every preview — doable but slow.</p>
<p><strong>Search.</strong> Client-side search works for small sites (load a JSON index, search in JavaScript). For larger content, you need an external search service.</p>
<p><strong>Comments.</strong> Disqus, Giscus, or similar third-party embeds. No server means no self-hosted comment storage.</p>
<p><strong>Build time scaling.</strong> As content grows, build times grow. A 1000-post blog might take 10+ minutes to build. This is manageable but affects development velocity.</p>
<p><strong>Image optimization.</strong> Without the Next.js optimization API, you handle image resizing externally or serve unoptimized assets. On fast connections, this is fine. On mobile, it hurts.</p>
<p><strong>Multi-locale with basePath.</strong> Supporting multiple languages with static export works but gets complex with custom domains per locale or locale prefixes combined with basePath.</p>
<p><strong>Analytics and privacy.</strong> Client-side analytics (Google Analytics, Plausible) work. Server-side analytics (for privacy) require a proxy or separate service.</p>
<h2>Common Mistakes</h2>
<p><strong>Forgetting basePath for Project Pages.</strong> Routes and assets return 404. The site looks broken on GitHub but works locally.</p>
<p><strong>Using next/image without <code>unoptimized: true</code>.</strong> Build fails with an error about Image Optimization requiring a server.</p>
<p><strong>Expecting redirects/rewrites to work.</strong> They&#39;re silently ignored on static export. Use client-side navigation or restructure URLs.</p>
<p><strong>Missing <code>generateStaticParams</code> for dynamic routes.</strong> Build error: &quot;Page with <code>output: export</code> has dynamic route but no <code>generateStaticParams</code>.&quot;</p>
<p><strong>Using <code>dynamicParams = true</code> with static export.</strong> Build might succeed but unknown slugs cause issues. Always set <code>dynamicParams = false</code>.</p>
<p><strong>Runtime environment variables.</strong> <code>process.env.MY_VAR</code> in client components doesn&#39;t work — it&#39;s replaced at build time. Use <code>NEXT_PUBLIC_</code> prefix for client-visible values.</p>
<p><strong>Deploying the wrong folder.</strong> Next.js outputs to <code>out/</code> by default, but if you forget to check the workflow, you might deploy <code>.next/</code> or the root folder.</p>
<p><strong>Broken canonical/OG URLs.</strong> Hardcoded URLs that don&#39;t account for basePath or custom domain result in wrong social previews and SEO issues.</p>
<h2>When NOT To Use This</h2>
<p>Static export to GitHub Pages is wrong when:</p>
<ul>
<li>You need authentication or user sessions</li>
<li>Content is personalized per user</li>
<li>You need middleware (geo-redirects, A/B tests at the edge)</li>
<li>You need API routes with runtime logic</li>
<li>Build times exceed acceptable CI limits (hundreds of pages or more)</li>
<li>You need ISR for frequently changing content</li>
</ul>
<p>For these cases, consider Vercel, Netlify, or Cloudflare Pages — platforms with server/edge support that handle these features natively with Next.js.</p>
<h2>How I Apply This in Real Projects</h2>
<p>For personal sites and blogs, I keep the stack minimal: static export, GitHub Pages, and a custom domain. No external services beyond what&#39;s required.</p>
<p>I automate the deployment pipeline once and don&#39;t touch it. The workflow in this repo hasn&#39;t changed significantly since initial setup.</p>
<p>I intentionally don&#39;t build: comments, search (for small sites), analytics (unless required), or complex CMS integrations. Each adds maintenance burden disproportionate to value for personal projects.</p>
<p>When a project genuinely needs server features, I move to a platform that supports them rather than working around static export limitations.</p>
<h2>Practical Recommendations</h2>
<p><strong>Config checklist:</strong></p>
<ul>
<li><code>output: &#39;export&#39;</code> in <code>next.config.ts</code></li>
<li><code>basePath</code> set if deploying to <code>/&lt;repo&gt;/</code> (omit for custom domain)</li>
<li><code>images: { unoptimized: true }</code> if using <code>next/image</code></li>
<li><code>trailingSlash</code> set based on URL preference</li>
<li><code>dynamicParams = false</code> on all dynamic routes</li>
</ul>
<p><strong>Content pipeline:</strong></p>
<ul>
<li><code>generateStaticParams</code> returns all valid slugs</li>
<li>Metadata URLs use full absolute URLs for social sharing</li>
<li>CNAME file in <code>public/</code> for custom domains</li>
</ul>
<p><strong>CI/CD checks:</strong></p>
<ul>
<li>Lint runs before build (<code>npm run lint</code>)</li>
<li>Type check runs before build (<code>npx tsc --noEmit</code>)</li>
<li>Build artifact is correct directory (<code>out/</code>)</li>
<li>Workflow triggers on correct branches</li>
</ul>
<p><strong>Regression tests:</strong></p>
<ul>
<li>Manually verify a few routes after deployment</li>
<li>Check 404 page appears for invalid routes</li>
<li>Verify assets load (images, CSS, JS)</li>
<li>Test OG images in social share debuggers</li>
</ul>
<h2>Summary</h2>
<p>Next.js static export to GitHub Pages is a production-ready approach for content sites. It&#39;s what powers this site, and after running it for a while I can say the setup is genuinely low-maintenance.</p>
<p>The key decisions — <code>output: &#39;export&#39;</code>, <code>images: { unoptimized: true }</code>, <code>generateStaticParams</code> with <code>dynamicParams = false</code> — address the common failure modes. Understanding the constraints (no server runtime, basePath for Project Pages, explicit static generation for dynamic routes) saves you from most surprises.</p>
<p>The approach scales well for small to medium sites. For larger content needs or server-dependent features, migration to a full hosting platform is the right move rather than fighting static export limitations.</p>
<p>If you have feedback, alternative approaches, or just want to discuss this topic — feel free to reach out.
  </p>
]]></content:encoded>
      <pubDate>Wed, 25 Feb 2026 00:00:00 GMT</pubDate>
      <category>Next.js</category>
      <category>GitHub Pages</category>
      <category>SSG</category>
      <category>CI/CD</category>
      <category>DevOps</category>
    </item>
    <item>
      <title>Building Design Systems That Scale</title>
      <link>https://ma-x.im/blog/building-design-systems-that-scale</link>
      <guid isPermaLink="true">https://ma-x.im/blog/building-design-systems-that-scale</guid>
      <description>How to build component libraries that teams actually want to use.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/building-design-systems-that-scale/cover.png" alt="Building Design Systems That Scale" /></p>
<p>At Motorola Solutions I walked into a codebase with hundreds of internal projects. Every team followed the brand book — in theory. In practice, it was chaos. Buttons looked different everywhere. Navigation behaved differently in every app. Everyone thought they were on-brand. Nobody actually was.</p>
<p>They asked me to help build a unified design system. Nine months of work, tight collaboration between designers and developers. The result: a component library that snapped together like Lego. You didn&#39;t think about padding, font sizes, spacing — everything just worked out of the box. We shipped variants for React and Angular simultaneously. The designers had specced the components so precisely that they composed into each other without friction.</p>
<p>Then came adoption. Migration was painful. Versioning became its own nightmare — but I&#39;ll get to that.</p>
<p>Building a design system is the easy part. Getting teams to actually use it is where most fail.</p>
<h2>The Problem</h2>
<p>I&#39;ve watched multiple design system efforts die. Not because the components were poorly designed, but because they didn&#39;t solve the right problems at the right time. Teams would build beautiful libraries, document everything meticulously, and then watch as nobody used them.</p>
<h2>Start with Real Problems</h2>
<p>The best design systems I&#39;ve built started with actual pain points:</p>
<ul>
<li><strong>Inconsistent UI patterns</strong> across features</li>
<li><strong>Slow feature development</strong> due to rebuilding common components</li>
<li><strong>Accessibility gaps</strong> that were hard to track</li>
<li><strong>Design-dev handoff friction</strong> that slowed everything down</li>
</ul>
<h2>Key Principles</h2>
<h3>1. Component API Design Matters</h3>
<p>Your components should be <strong>easy to use correctly</strong> and <strong>hard to use incorrectly</strong>.</p>
<pre><code class="language-tsx">// Bad: Too many props, unclear usage
&lt;Button 
  variant=&quot;primary&quot; 
  size=&quot;md&quot; 
  loading={false} 
  disabled={false}
  onClick={handleClick}
/&gt;

// Good: Clear defaults, composable
&lt;Button onClick={handleClick}&gt;
  Click me
&lt;/Button&gt;

&lt;Button variant=&quot;secondary&quot; loading&gt;
  Loading...
&lt;/Button&gt;
</code></pre>
<h3>2. Documentation is Your Product</h3>
<p>Nobody reads long documentation. Make it:</p>
<ul>
<li><strong>Visual</strong> - Show examples first</li>
<li><strong>Interactive</strong> - Let people play with props</li>
<li><strong>Searchable</strong> - Find what you need fast</li>
<li><strong>Practical</strong> - Real use cases, not toy examples</li>
</ul>
<p>I use Storybook for this. Every component gets:</p>
<ul>
<li>Multiple usage examples</li>
<li>Props documentation</li>
<li>Accessibility notes</li>
<li>Do&#39;s and don&#39;ts</li>
</ul>
<h3>3. Build for Adoption</h3>
<p>The best design system is the one people use. Focus on:</p>
<p><strong>Developer Experience:</strong></p>
<ul>
<li>TypeScript support out of the box</li>
<li>Clear error messages</li>
<li>Intuitive prop names</li>
<li>Good defaults</li>
</ul>
<p><strong>Performance:</strong></p>
<ul>
<li>Tree-shakeable exports</li>
<li>Lazy loading for heavy components</li>
<li>Optimized bundle size</li>
</ul>
<p><strong>Flexibility:</strong></p>
<ul>
<li>Composable components</li>
<li>Escape hatches when needed</li>
<li>Theme customization</li>
</ul>
<h2>The Tech Stack</h2>
<p>Here&#39;s what I typically use:</p>
<ul>
<li><strong>React + TypeScript</strong> - Type safety is non-negotiable</li>
<li><strong>Tailwind CSS</strong> - Utility-first scaling, easy theming</li>
<li><strong>Radix UI</strong> - Accessible primitives</li>
<li><strong>Storybook</strong> - Component documentation</li>
<li><strong>Turborepo</strong> - Monorepo management</li>
<li><strong>Changesets</strong> - Version management</li>
</ul>
<h2>Common Pitfalls</h2>
<h3>Over-engineering Early</h3>
<p>Don&#39;t build 50 components on day one. Start with:</p>
<ol>
<li>Buttons</li>
<li>Inputs</li>
<li>Layout primitives</li>
<li>Typography system</li>
</ol>
<p>Add more as real needs emerge.</p>
<h3>Ignoring Migration Path</h3>
<p>At Motorola Solutions, migration was the hardest part. Hundreds of projects, each with their own component variations. We couldn&#39;t flip a switch. Some teams adopted early, others dragged their feet for months. Eventually every project had the same interface, the same navigation, the same look — and honestly, that felt great. But then versioning caught up with us: some teams locked to an early version and never updated. When they finally did — months later — things broke because the API had evolved. We spent weeks helping teams catch up. If I could redo one thing, it would be investing more in automated migration tooling from day one.</p>
<p>If you&#39;re replacing an old system, make migration gradual:</p>
<ul>
<li>Support both old and new side-by-side</li>
<li>Provide codemods where possible</li>
<li>Document migration guides</li>
<li>Track adoption metrics</li>
</ul>
<h3>Skipping Accessibility</h3>
<p>Build it in from day one. Every component should:</p>
<ul>
<li>Work with keyboard navigation</li>
<li>Have proper ARIA labels</li>
<li>Support screen readers</li>
<li>Handle focus management</li>
</ul>
<h2>Measuring Success</h2>
<p>Track these metrics:</p>
<ol>
<li><strong>Adoption rate</strong> - % of features using the system</li>
<li><strong>Component coverage</strong> - % of UI built with system components</li>
<li><strong>Development velocity</strong> - Time to build new features</li>
<li><strong>Consistency score</strong> - How many UI patterns exist</li>
</ol>
<h2>Conclusion</h2>
<p>After the Motorola Solutions project, I started seeing design systems differently. The system itself isn&#39;t the product — adoption is the product. The best component library in the world is worthless if teams work around it instead of using it. Ours was far from perfect, but it worked because designers and developers built it together, and because the components were genuinely easier to use than rolling your own.</p>
<p>Start small. Iterate based on real feedback from real teams. And always prioritize the developer experience — because if it&#39;s easier to build from scratch than to use your system, that&#39;s exactly what people will do.</p>
<hr>
<p><em>Have questions about building design systems? <a href="https://ma-x.im/contact">Reach out</a> — I&#39;ve been through this and happy to share more.</em>
  </p>
]]></content:encoded>
      <pubDate>Thu, 15 Jan 2026 00:00:00 GMT</pubDate>
      <category>Design Systems</category>
      <category>React</category>
      <category>Architecture</category>
    </item>
    <item>
      <title>Type-Safe React Architecture</title>
      <link>https://ma-x.im/blog/type-safe-react-architecture</link>
      <guid isPermaLink="true">https://ma-x.im/blog/type-safe-react-architecture</guid>
      <description>Leveraging TypeScript for better developer experience and fewer runtime errors.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/type-safe-react-architecture/cover.png" alt="Type-Safe React Architecture" /></p>
<p>I remember the exact moment I realized I couldn&#39;t go back. I had been writing TypeScript for a while, got used to it, and then joined a project that was pure JavaScript. No types. No autocompletion for props. No compile-time errors. Just... hope.</p>
<p>It was unbearable. How do you know what this function returns? You don&#39;t — you read the implementation. What props does this component accept? You check the source. Did someone rename a field in the API response? You&#39;ll find out in production.</p>
<p>The thing about TypeScript is that you don&#39;t see the bugs it prevents. You introduce strict types and your code just... works more reliably. You never get the dramatic &quot;TypeScript saved us from a production outage&quot; moment because the bug never made it past your editor. It stays invisible. But go back to a project without it, and you feel the absence immediately.</p>
<p>TypeScript isn&#39;t just about catching bugs. It&#39;s about building systems that are <strong>impossible to misuse</strong>.</p>
<h2>Why Type Safety Matters</h2>
<p>After 20+ years of engineering, I&#39;ve learned: the best code is code that guides you toward correct usage.</p>
<h3>The Cost of Runtime Errors</h3>
<p>Runtime errors in production are expensive:</p>
<ul>
<li><strong>User frustration</strong> - Broken features hurt trust</li>
<li><strong>Debug time</strong> - Finding issues in production is slow</li>
<li><strong>Lost revenue</strong> - Crashes during checkout cost money</li>
<li><strong>Team velocity</strong> - Fear of breaking things slows development</li>
</ul>
<p>TypeScript moves these errors to <strong>compile time</strong>.</p>
<h2>Architecture Patterns</h2>
<h3>1. Strict Type Configuration</h3>
<p>Start with the strictest possible <code>tsconfig.json</code>:</p>
<pre><code class="language-json">{
  &quot;compilerOptions&quot;: {
    &quot;strict&quot;: true,
    &quot;noUncheckedIndexedAccess&quot;: true,
    &quot;noImplicitReturns&quot;: true,
    &quot;noFallthroughCasesInSwitch&quot;: true,
    &quot;exactOptionalPropertyTypes&quot;: true
  }
}
</code></pre>
<p>Yes, it&#39;s painful at first. But it catches <strong>so many bugs</strong>.</p>
<h3>2. Component Props as Contracts</h3>
<p>Your component props are API contracts:</p>
<pre><code class="language-tsx">// Weak typing
interface ButtonProps {
  variant?: string;
  size?: string;
  onClick?: Function;
}

// Strong typing
type ButtonVariant = &quot;primary&quot; | &quot;secondary&quot; | &quot;ghost&quot;;
type ButtonSize = &quot;sm&quot; | &quot;md&quot; | &quot;lg&quot;;

interface ButtonProps {
  variant?: ButtonVariant;
  size?: ButtonSize;
  onClick?: (event: React.MouseEvent&lt;HTMLButtonElement&gt;) =&gt; void;
  children: React.ReactNode;
}
</code></pre>
<p>Now your IDE will autocomplete variants and catch typos.</p>
<h3>3. Discriminated Unions for State</h3>
<p>Handle complex state with discriminated unions:</p>
<pre><code class="language-tsx">type FetchState&lt;T&gt; =
  | { status: &quot;idle&quot; }
  | { status: &quot;loading&quot; }
  | { status: &quot;success&quot;; data: T }
  | { status: &quot;error&quot;; error: Error };

function UserProfile() {
  const [state, setState] = useState&lt;FetchState&lt;User&gt;&gt;({
    status: &quot;idle&quot;,
  });

  // TypeScript knows what properties exist in each branch
  if (state.status === &quot;success&quot;) {
    return &lt;div&gt;{state.data.name}&lt;/div&gt;; // ✅ data exists
  }

  if (state.status === &quot;error&quot;) {
    return &lt;div&gt;{state.error.message}&lt;/div&gt;; // ✅ error exists
  }

  return &lt;Loading /&gt;;
}
</code></pre>
<p>This pattern <strong>eliminates entire classes of bugs</strong>.</p>
<h3>4. Generic Components Done Right</h3>
<p>Make your components reusable without losing type safety:</p>
<pre><code class="language-tsx">interface SelectOption&lt;T&gt; {
  value: T;
  label: string;
}

interface SelectProps&lt;T&gt; {
  options: SelectOption&lt;T&gt;[];
  value: T;
  onChange: (value: T) =&gt; void;
}

function Select&lt;T&gt;({ options, value, onChange }: SelectProps&lt;T&gt;) {
  return (
    &lt;select
      value={String(value)}
      onChange={(e) =&gt; {
        const option = options.find(
          (opt) =&gt; String(opt.value) === e.target.value
        );
        if (option) onChange(option.value);
      }}
    &gt;
      {options.map((opt) =&gt; (
        &lt;option key={String(opt.value)} value={String(opt.value)}&gt;
          {opt.label}
        &lt;/option&gt;
      ))}
    &lt;/select&gt;
  );
}

// Usage - fully type-safe!
&lt;Select&lt;UserRole&gt;
  options={[
    { value: &quot;admin&quot;, label: &quot;Admin&quot; },
    { value: &quot;user&quot;, label: &quot;User&quot; },
  ]}
  value={role}
  onChange={setRole} // TypeScript knows this is UserRole
/&gt;
</code></pre>
<h3>5. Form Validation with Zod</h3>
<p>Combine React Hook Form with Zod for bulletproof forms:</p>
<pre><code class="language-tsx">import { z } from &quot;zod&quot;;
import { useForm } from &quot;react-hook-form&quot;;
import { zodResolver } from &quot;@hookform/resolvers/zod&quot;;

const userSchema = z.object({
  email: z.string().email(&quot;Invalid email&quot;),
  age: z.number().min(18, &quot;Must be 18+&quot;),
  role: z.enum([&quot;admin&quot;, &quot;user&quot;]),
});

type UserFormData = z.infer&lt;typeof userSchema&gt;;

function UserForm() {
  const { register, handleSubmit, formState: { errors } } = 
    useForm&lt;UserFormData&gt;({
      resolver: zodResolver(userSchema),
    });

  const onSubmit = (data: UserFormData) =&gt; {
    // data is fully typed and validated!
    console.log(data);
  };

  return (
    &lt;form onSubmit={handleSubmit(onSubmit)}&gt;
      &lt;input {...register(&quot;email&quot;)} /&gt;
      {errors.email &amp;&amp; &lt;span&gt;{errors.email.message}&lt;/span&gt;}
    &lt;/form&gt;
  );
}
</code></pre>
<p><strong>One schema</strong>, both runtime validation and TypeScript types.</p>
<h2>API Integration</h2>
<p>Type your API responses:</p>
<pre><code class="language-tsx">// Define your API types
interface User {
  id: string;
  name: string;
  email: string;
}

interface ApiResponse&lt;T&gt; {
  data: T;
  error?: string;
}

// Type-safe API client
async function fetchUser(id: string): Promise&lt;User&gt; {
  const response = await fetch(`/api/users/${id}`);
  const json: ApiResponse&lt;User&gt; = await response.json();
  
  if (json.error) {
    throw new Error(json.error);
  }
  
  return json.data;
}

// Use with TanStack Query
const { data: user } = useQuery({
  queryKey: [&quot;user&quot;, id],
  queryFn: () =&gt; fetchUser(id),
});

// user is typed as User | undefined
</code></pre>
<h2>Testing Benefits</h2>
<p>TypeScript makes testing easier:</p>
<pre><code class="language-tsx">import { render, screen } from &quot;@testing-library/react&quot;;

// TypeScript catches missing props
render(&lt;Button&gt;Click me&lt;/Button&gt;); // ✅

// @ts-expect-error - variant must be valid
render(&lt;Button variant=&quot;invalid&quot;&gt;Click me&lt;/Button&gt;); // ❌
</code></pre>
<h2>Developer Experience Wins</h2>
<p>With proper TypeScript setup:</p>
<ol>
<li><strong>Refactoring is safe</strong> - Rename a prop, find all usages instantly</li>
<li><strong>Autocomplete everywhere</strong> - Your IDE knows what&#39;s valid</li>
<li><strong>Self-documenting code</strong> - Types explain usage</li>
<li><strong>Faster onboarding</strong> - New devs understand APIs from types</li>
<li><strong>Fewer PR comments</strong> - Type checker catches issues</li>
</ol>
<h2>Common Mistakes</h2>
<h3>Using <code>any</code></h3>
<p>Never use <code>any</code>. Use <code>unknown</code> if you must:</p>
<pre><code class="language-tsx">// Bad
function processData(data: any) {
  return data.value; // No safety
}

// Good
function processData(data: unknown) {
  if (typeof data === &quot;object&quot; &amp;&amp; data !== null &amp;&amp; &quot;value&quot; in data) {
    return (data as { value: string }).value;
  }
  throw new Error(&quot;Invalid data&quot;);
}
</code></pre>
<h3>Ignoring Errors</h3>
<p>Don&#39;t <code>@ts-ignore</code> your way out of problems. Fix the types.</p>
<h3>Over-complicating Types</h3>
<p>Keep it simple. Complex types are hard to maintain.</p>
<h2>Conclusion</h2>
<p>Every time I join a project that doesn&#39;t have TypeScript, my first instinct is to add it. Not because I enjoy configuring <code>tsconfig.json</code> — but because I&#39;ve experienced the difference too many times. The bugs you don&#39;t see. The refactors that just work. The onboarding where new developers understand APIs from reading types instead of hoping.</p>
<p>Type safety isn&#39;t optional for serious React applications. It&#39;s the foundation of reliable software, great DX, and confident refactoring. Invest in your type system. Your future self will thank you.</p>
<hr>
<p><em>Want to discuss TypeScript architecture? <a href="https://ma-x.im/contact">Let&#39;s connect</a>.</em>
  </p>
]]></content:encoded>
      <pubDate>Sun, 28 Dec 2025 00:00:00 GMT</pubDate>
      <category>TypeScript</category>
      <category>React</category>
      <category>DX</category>
    </item>
    <item>
      <title>Complex Forms Done Right</title>
      <link>https://ma-x.im/blog/complex-forms-done-right</link>
      <guid isPermaLink="true">https://ma-x.im/blog/complex-forms-done-right</guid>
      <description>Patterns for managing complex form state, validation, and user experience.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/complex-forms-done-right/cover.png" alt="Complex Forms Done Right" /></p>
<p>At Motorola Solutions I built a multi-step contract wizard where users selected products, services, additional options, and pricing tiers — dozens of fields that depended on each other. It wasn&#39;t just a form. It was a live calculator that recalculated totals, toggled conditional sections, and validated business rules at every step.</p>
<p>Later, at an insurance company, I worked on a policy builder that asked everything from property type and vehicle model to how many pets you own and how long you&#39;ve had them. Every answer changed what came next. Every combination produced a different quote.</p>
<p>Forms like these are where most implementations fall apart. A simple login form? Easy. A multi-step wizard with conditional fields, async validation, dynamic pricing, and file uploads? That&#39;s a different engineering problem entirely.</p>
<h2>The Challenge</h2>
<p>Complex forms have multiple concerns:</p>
<ul>
<li><strong>State management</strong> - Field values, touched, dirty states</li>
<li><strong>Validation</strong> - Sync and async, field-level and form-level</li>
<li><strong>UX</strong> - When to show errors, loading states, success feedback</li>
<li><strong>Performance</strong> - Re-renders, large forms, dynamic fields</li>
<li><strong>Accessibility</strong> - Screen readers, keyboard navigation, error announcements</li>
</ul>
<h2>The Right Tools</h2>
<p>After building dozens of complex forms, here&#39;s my stack:</p>
<ul>
<li><strong>React Hook Form</strong> - Uncontrolled forms, great performance</li>
<li><strong>Zod</strong> - Schema validation, type inference</li>
<li><strong>TanStack Query</strong> - Async validation, mutations</li>
<li><strong>Radix UI</strong> - Accessible form primitives</li>
</ul>
<h2>Architecture Patterns</h2>
<h3>1. Schema-First Design</h3>
<p>Define your form schema first:</p>
<pre><code class="language-tsx">import { z } from &quot;zod&quot;;

const addressSchema = z.object({
  street: z.string().min(1, &quot;Street is required&quot;),
  city: z.string().min(1, &quot;City is required&quot;),
  country: z.string().min(1, &quot;Country is required&quot;),
  postalCode: z.string().regex(/^d{5}$/, &quot;Invalid postal code&quot;),
});

const userSchema = z.object({
  email: z.string().email(&quot;Invalid email&quot;),
  password: z
    .string()
    .min(8, &quot;At least 8 characters&quot;)
    .regex(/[A-Z]/, &quot;Need uppercase letter&quot;)
    .regex(/[0-9]/, &quot;Need a number&quot;),
  confirmPassword: z.string(),
  address: addressSchema,
  terms: z.literal(true, {
    errorMap: () =&gt; ({ message: &quot;You must accept terms&quot; }),
  }),
}).refine((data) =&gt; data.password === data.confirmPassword, {
  message: &quot;Passwords don&#39;t match&quot;,
  path: [&quot;confirmPassword&quot;],
});

type UserFormData = z.infer&lt;typeof userSchema&gt;;
</code></pre>
<p><strong>One schema</strong> defines:</p>
<ul>
<li>Field types</li>
<li>Validation rules</li>
<li>Error messages</li>
<li>TypeScript types</li>
</ul>
<h3>2. Async Validation</h3>
<p>Check email availability without blocking the UI:</p>
<pre><code class="language-tsx">const emailSchema = z.string().email().refine(
  async (email) =&gt; {
    const response = await fetch(`/api/check-email?email=${email}`);
    const { available } = await response.json();
    return available;
  },
  { message: &quot;Email already taken&quot; }
);
</code></pre>
<p>With React Hook Form:</p>
<pre><code class="language-tsx">&lt;input
  {...register(&quot;email&quot;, {
    validate: async (value) =&gt; {
      const result = await emailSchema.safeParseAsync(value);
      return result.success || result.error.errors[0].message;
    },
  })}
/&gt;
</code></pre>
<h3>3. Multi-Step Forms</h3>
<p>Break complex forms into steps:</p>
<pre><code class="language-tsx">function MultiStepForm() {
  const [step, setStep] = useState(1);
  const methods = useForm&lt;UserFormData&gt;();

  const onSubmit = async (data: UserFormData) =&gt; {
    if (step &lt; 3) {
      setStep(step + 1);
      return;
    }

    // Final submission
    await createUser(data);
  };

  return (
    &lt;FormProvider {...methods}&gt;
      &lt;form onSubmit={methods.handleSubmit(onSubmit)}&gt;
        {step === 1 &amp;&amp; &lt;PersonalInfoStep /&gt;}
        {step === 2 &amp;&amp; &lt;AddressStep /&gt;}
        {step === 3 &amp;&amp; &lt;ReviewStep /&gt;}

        &lt;div&gt;
          {step &gt; 1 &amp;&amp; (
            &lt;button type=&quot;button&quot; onClick={() =&gt; setStep(step - 1)}&gt;
              Back
            &lt;/button&gt;
          )}
          &lt;button type=&quot;submit&quot;&gt;
            {step &lt; 3 ? &quot;Next&quot; : &quot;Submit&quot;}
          &lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/FormProvider&gt;
  );
}
</code></pre>
<h3>4. Dynamic Field Arrays</h3>
<p>Add/remove fields dynamically:</p>
<pre><code class="language-tsx">import { useFieldArray } from &quot;react-hook-form&quot;;

function SkillsForm() {
  const { control, register } = useForm();
  const { fields, append, remove } = useFieldArray({
    control,
    name: &quot;skills&quot;,
  });

  return (
    &lt;div&gt;
      {fields.map((field, index) =&gt; (
        &lt;div key={field.id}&gt;
          &lt;input
            {...register(`skills.${index}.name`)}
            placeholder=&quot;Skill name&quot;
          /&gt;
          &lt;input
            type=&quot;number&quot;
            {...register(`skills.${index}.years`)}
            placeholder=&quot;Years&quot;
          /&gt;
          &lt;button type=&quot;button&quot; onClick={() =&gt; remove(index)}&gt;
            Remove
          &lt;/button&gt;
        &lt;/div&gt;
      ))}
      
      &lt;button
        type=&quot;button&quot;
        onClick={() =&gt; append({ name: &quot;&quot;, years: 0 })}
      &gt;
        Add Skill
      &lt;/button&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<h3>5. Conditional Fields</h3>
<p>Show/hide fields based on other values:</p>
<pre><code class="language-tsx">function PaymentForm() {
  const { register, watch } = useForm();
  const paymentMethod = watch(&quot;paymentMethod&quot;);

  return (
    &lt;div&gt;
      &lt;select {...register(&quot;paymentMethod&quot;)}&gt;
        &lt;option value=&quot;card&quot;&gt;Credit Card&lt;/option&gt;
        &lt;option value=&quot;bank&quot;&gt;Bank Transfer&lt;/option&gt;
      &lt;/select&gt;

      {paymentMethod === &quot;card&quot; &amp;&amp; (
        &lt;&gt;
          &lt;input {...register(&quot;cardNumber&quot;)} placeholder=&quot;Card Number&quot; /&gt;
          &lt;input {...register(&quot;cvv&quot;)} placeholder=&quot;CVV&quot; /&gt;
        &lt;/&gt;
      )}

      {paymentMethod === &quot;bank&quot; &amp;&amp; (
        &lt;&gt;
          &lt;input {...register(&quot;accountNumber&quot;)} placeholder=&quot;Account&quot; /&gt;
          &lt;input {...register(&quot;routingNumber&quot;)} placeholder=&quot;Routing&quot; /&gt;
        &lt;/&gt;
      )}
    &lt;/div&gt;
  );
}
</code></pre>
<h2>Error Handling UX</h2>
<p>Show errors at the right time:</p>
<pre><code class="language-tsx">function SmartInput({ name, label, ...props }) {
  const {
    register,
    formState: { errors, touchedFields, isSubmitted },
  } = useFormContext();

  const error = errors[name];
  const showError = error &amp;&amp; (touchedFields[name] || isSubmitted);

  return (
    &lt;div&gt;
      &lt;label htmlFor={name}&gt;{label}&lt;/label&gt;
      &lt;input
        id={name}
        aria-invalid={showError ? &quot;true&quot; : &quot;false&quot;}
        aria-describedby={showError ? `${name}-error` : undefined}
        {...register(name)}
        {...props}
      /&gt;
      {showError &amp;&amp; (
        &lt;span id={`${name}-error`} role=&quot;alert&quot;&gt;
          {error.message}
        &lt;/span&gt;
      )}
    &lt;/div&gt;
  );
}
</code></pre>
<p>Only show errors after:</p>
<ol>
<li>User has touched the field, OR</li>
<li>Form has been submitted</li>
</ol>
<p>This prevents angry red errors while typing.</p>
<h2>Performance Optimization</h2>
<p>For large forms (50+ fields):</p>
<pre><code class="language-tsx">const { register } = useForm({
  mode: &quot;onBlur&quot;, // Validate on blur, not on change
  shouldUnregister: true, // Unregister unmounted fields
});
</code></pre>
<p>Use <code>Controller</code> only for complex inputs:</p>
<pre><code class="language-tsx">import { Controller } from &quot;react-hook-form&quot;;

&lt;Controller
  name=&quot;birthDate&quot;
  control={control}
  render={({ field }) =&gt; (
    &lt;DatePicker
      value={field.value}
      onChange={field.onChange}
    /&gt;
  )}
/&gt;
</code></pre>
<h2>File Uploads</h2>
<p>Handle files properly:</p>
<pre><code class="language-tsx">function FileUpload() {
  const { register, watch } = useForm();
  const file = watch(&quot;avatar&quot;);

  return (
    &lt;div&gt;
      &lt;input
        type=&quot;file&quot;
        accept=&quot;image/*&quot;
        {...register(&quot;avatar&quot;)}
      /&gt;
      {file?.[0] &amp;&amp; (
        &lt;img
          src={URL.createObjectURL(file[0])}
          alt=&quot;Preview&quot;
        /&gt;
      )}
    &lt;/div&gt;
  );
}
</code></pre>
<h2>Submission with TanStack Query</h2>
<p>Clean mutation handling:</p>
<pre><code class="language-tsx">function UserForm() {
  const methods = useForm&lt;UserFormData&gt;();
  const mutation = useMutation({
    mutationFn: createUser,
    onSuccess: () =&gt; {
      toast.success(&quot;User created!&quot;);
      methods.reset();
    },
    onError: (error) =&gt; {
      toast.error(error.message);
    },
  });

  const onSubmit = (data: UserFormData) =&gt; {
    mutation.mutate(data);
  };

  return (
    &lt;form onSubmit={methods.handleSubmit(onSubmit)}&gt;
      {/* fields */}
      &lt;button type=&quot;submit&quot; disabled={mutation.isPending}&gt;
        {mutation.isPending ? &quot;Saving...&quot; : &quot;Save&quot;}
      &lt;/button&gt;
    &lt;/form&gt;
  );
}
</code></pre>
<h2>Accessibility Checklist</h2>
<p>✅ Labels for every input
✅ Error messages announced to screen readers
✅ Keyboard navigation works
✅ Focus management (auto-focus first error)
✅ Required fields marked
✅ Clear validation feedback</p>
<h2>Testing</h2>
<p>Test forms thoroughly:</p>
<pre><code class="language-tsx">import { render, screen, waitFor } from &quot;@testing-library/react&quot;;
import userEvent from &quot;@testing-library/user-event&quot;;

test(&quot;shows validation errors&quot;, async () =&gt; {
  render(&lt;UserForm /&gt;);
  
  const user = userEvent.setup();
  const submitButton = screen.getByRole(&quot;button&quot;, { name: /submit/i });
  
  await user.click(submitButton);
  
  await waitFor(() =&gt; {
    expect(screen.getByText(/email is required/i)).toBeInTheDocument();
  });
});
</code></pre>
<h2>Conclusion</h2>
<p>The Motorola contract wizard and the insurance policy builder taught me the same lesson: complex forms aren&#39;t a UI problem — they&#39;re a data modeling problem. Get the schema right first, handle validation at the schema level, and the rest follows.</p>
<p>Schema-first validation. Smart error timing. Performance-aware rendering. Accessibility from day one. Thorough testing. Get these right, and even the most complex forms become manageable.</p>
<hr>
<p><em>Need help with complex forms? <a href="https://ma-x.im/contact">Get in touch</a>.</em>
  </p>
]]></content:encoded>
      <pubDate>Wed, 10 Dec 2025 00:00:00 GMT</pubDate>
      <category>Forms</category>
      <category>React Hook Form</category>
      <category>Architecture</category>
    </item>
    <item>
      <title>React Performance Optimization</title>
      <link>https://ma-x.im/blog/react-performance-optimization</link>
      <guid isPermaLink="true">https://ma-x.im/blog/react-performance-optimization</guid>
      <description>Practical techniques for building fast React applications at scale.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/react-performance-optimization/cover.png" alt="React Performance Optimization" /></p>
<p>I joined a large Canadian automotive company as one of three senior frontend engineers. The site was public-facing — a product catalog where users browsed vehicle models, configurations, and pricing. Built on Sitecore with React components on top.</p>
<p>The first thing I noticed: the catalog page re-rendered on every interaction. Not just the part you touched — everything. The page pulled data from separate endpoints for vehicle types, models, specifications, and pricing. Every state change triggered a cascade of re-renders across all of them. It was the first time in my career I saw genuine, visible lag on a production public website — not some internal admin panel, but a site real customers used every day.</p>
<p>That became our first priority. We spent weeks profiling, restructuring data flow, and in some cases rewriting components from scratch because the original code was unsalvageable. The performance boost was dramatic and became our biggest early win with the team.</p>
<p>That experience taught me something I keep coming back to: performance isn&#39;t about premature optimization. It&#39;s about building systems that stay fast as they grow.</p>
<h2>When to Optimize</h2>
<p>Don&#39;t optimize everything. Optimize when:</p>
<ul>
<li>Users report slowness</li>
<li>Metrics show issues (Core Web Vitals)</li>
<li>Profiling reveals bottlenecks</li>
<li>You&#39;re building data-heavy features</li>
</ul>
<p><strong>Measure first, optimize second.</strong></p>
<h2>Profiling Tools</h2>
<p>Use the right tools:</p>
<ol>
<li><strong>React DevTools Profiler</strong> - See component render times</li>
<li><strong>Chrome Performance</strong> - Analyze full page performance</li>
<li><strong>Web Vitals</strong> - Track real user metrics</li>
<li><strong>Lighthouse</strong> - Automated audits</li>
</ol>
<h2>Common Performance Issues</h2>
<h3>1. Unnecessary Re-renders</h3>
<p><strong>Problem:</strong> Component re-renders when props haven&#39;t changed.</p>
<pre><code class="language-tsx">// Bad: Creates new object every render
function Parent() {
  return &lt;Child config={{ theme: &quot;dark&quot; }} /&gt;;
}

// Good: Stable reference
const CONFIG = { theme: &quot;dark&quot; };

function Parent() {
  return &lt;Child config={CONFIG} /&gt;;
}

// Or use useMemo for dynamic values
function Parent() {
  const config = useMemo(() =&gt; ({ theme: &quot;dark&quot; }), []);
  return &lt;Child config={config} /&gt;;
}
</code></pre>
<p><strong>Use <code>React.memo</code> wisely:</strong></p>
<pre><code class="language-tsx">const ExpensiveComponent = React.memo(({ data }) =&gt; {
  // Heavy rendering logic
  return &lt;div&gt;{/* ... */}&lt;/div&gt;;
});
</code></pre>
<h3>2. Large Lists Without Virtualization</h3>
<p><strong>Problem:</strong> Rendering 10,000 rows kills performance.</p>
<p><strong>Solution:</strong> Use virtualization.</p>
<pre><code class="language-tsx">import { useVirtualizer } from &quot;@tanstack/react-virtual&quot;;

function VirtualList({ items }) {
  const parentRef = useRef&lt;HTMLDivElement&gt;(null);

  const virtualizer = useVirtualizer({
    count: items.length,
    getScrollElement: () =&gt; parentRef.current,
    estimateSize: () =&gt; 50,
  });

  return (
    &lt;div ref={parentRef} style={{ height: &quot;500px&quot;, overflow: &quot;auto&quot; }}&gt;
      &lt;div
        style={{
          height: `${virtualizer.getTotalSize()}px`,
          position: &quot;relative&quot;,
        }}
      &gt;
        {virtualizer.getVirtualItems().map((virtualRow) =&gt; (
          &lt;div
            key={virtualRow.index}
            style={{
              position: &quot;absolute&quot;,
              top: 0,
              left: 0,
              width: &quot;100%&quot;,
              height: `${virtualRow.size}px`,
              transform: `translateY(${virtualRow.start}px)`,
            }}
          &gt;
            {items[virtualRow.index].name}
          &lt;/div&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<p>Only renders visible items. Huge performance win.</p>
<h3>3. Heavy Computations in Render</h3>
<p><strong>Problem:</strong> Expensive calculations on every render.</p>
<pre><code class="language-tsx">// Bad: Recalculates every render
function DataTable({ data }) {
  const sorted = data.sort((a, b) =&gt; a.value - b.value);
  const filtered = sorted.filter(item =&gt; item.active);
  
  return &lt;Table data={filtered} /&gt;;
}

// Good: Memoized
function DataTable({ data }) {
  const processed = useMemo(() =&gt; {
    const sorted = [...data].sort((a, b) =&gt; a.value - b.value);
    return sorted.filter(item =&gt; item.active);
  }, [data]);
  
  return &lt;Table data={processed} /&gt;;
}
</code></pre>
<h3>4. Inline Functions as Props</h3>
<p><strong>Problem:</strong> New function every render = child re-renders.</p>
<pre><code class="language-tsx">// Bad
function Parent() {
  return (
    &lt;Child onClick={() =&gt; console.log(&quot;clicked&quot;)} /&gt;
  );
}

// Good
function Parent() {
  const handleClick = useCallback(() =&gt; {
    console.log(&quot;clicked&quot;);
  }, []);
  
  return &lt;Child onClick={handleClick} /&gt;;
}
</code></pre>
<h3>5. Context Provider Re-renders</h3>
<p><strong>Problem:</strong> Context value changes = all consumers re-render.</p>
<pre><code class="language-tsx">// Bad: New object every render
function ThemeProvider({ children }) {
  const [theme, setTheme] = useState(&quot;light&quot;);
  
  return (
    &lt;ThemeContext.Provider value={{ theme, setTheme }}&gt;
      {children}
    &lt;/ThemeContext.Provider&gt;
  );
}

// Good: Memoized value
function ThemeProvider({ children }) {
  const [theme, setTheme] = useState(&quot;light&quot;);
  
  const value = useMemo(() =&gt; ({ theme, setTheme }), [theme]);
  
  return (
    &lt;ThemeContext.Provider value={value}&gt;
      {children}
    &lt;/ThemeContext.Provider&gt;
  );
}
</code></pre>
<h2>Code Splitting</h2>
<p>Load code only when needed:</p>
<pre><code class="language-tsx">import { lazy, Suspense } from &quot;react&quot;;

const Dashboard = lazy(() =&gt; import(&quot;./Dashboard&quot;));
const Settings = lazy(() =&gt; import(&quot;./Settings&quot;));

function App() {
  return (
    &lt;Suspense fallback={&lt;Loading /&gt;}&gt;
      &lt;Routes&gt;
        &lt;Route path=&quot;/dashboard&quot; element={&lt;Dashboard /&gt;} /&gt;
        &lt;Route path=&quot;/settings&quot; element={&lt;Settings /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/Suspense&gt;
  );
}
</code></pre>
<h2>Image Optimization</h2>
<p>Images are usually the biggest assets:</p>
<pre><code class="language-tsx">// Use modern formats
&lt;img
  src=&quot;hero.webp&quot;
  srcSet=&quot;hero-sm.webp 400w, hero-md.webp 800w, hero-lg.webp 1200w&quot;
  sizes=&quot;(max-width: 640px) 400px, (max-width: 1024px) 800px, 1200px&quot;
  alt=&quot;Hero&quot;
  loading=&quot;lazy&quot;
/&gt;
</code></pre>
<p>Use <code>loading=&quot;lazy&quot;</code> for below-the-fold images.</p>
<h2>State Management Performance</h2>
<h3>Use Zustand for Better Performance</h3>
<pre><code class="language-tsx">import { create } from &quot;zustand&quot;;

const useStore = create((set) =&gt; ({
  todos: [],
  addTodo: (todo) =&gt; set((state) =&gt; ({
    todos: [...state.todos, todo]
  })),
}));

// Only re-renders when todos change
function TodoList() {
  const todos = useStore((state) =&gt; state.todos);
  return &lt;div&gt;{/* ... */}&lt;/div&gt;;
}

// Never re-renders
function AddTodo() {
  const addTodo = useStore((state) =&gt; state.addTodo);
  return &lt;button onClick={() =&gt; addTodo({ text: &quot;New&quot; })}&gt;Add&lt;/button&gt;;
}
</code></pre>
<h3>TanStack Query for Server State</h3>
<pre><code class="language-tsx">const { data } = useQuery({
  queryKey: [&quot;users&quot;],
  queryFn: fetchUsers,
  staleTime: 5 * 60 * 1000, // 5 minutes
});
</code></pre>
<p>Automatic caching, deduplication, background refetching.</p>
<h2>Bundle Size Optimization</h2>
<h3>Tree Shaking</h3>
<pre><code class="language-tsx">// Bad: Imports entire library
import _ from &quot;lodash&quot;;

// Good: Import only what you need
import debounce from &quot;lodash/debounce&quot;;
</code></pre>
<h3>Analyze Bundle</h3>
<pre><code class="language-bash">npm install --save-dev vite-plugin-bundle-analyzer
</code></pre>
<p>Find and remove large dependencies.</p>
<h2>React Compiler (Coming Soon)</h2>
<p>React 19+ will have automatic memoization. But until then, manual optimization is needed.</p>
<h2>Performance Checklist</h2>
<p>✅ Profile before optimizing
✅ Use React.memo for expensive components
✅ Virtualize large lists
✅ Code split routes
✅ Lazy load images
✅ Optimize images (WebP, sizing)
✅ Memoize expensive computations
✅ Use proper state management
✅ Analyze bundle size
✅ Monitor Core Web Vitals</p>
<h2>Conclusion</h2>
<p>After the automotive catalog project, I never look at performance the same way. When it&#39;s bad, users feel it immediately — and they leave. When you fix it, the numbers speak for themselves. Every project where I&#39;ve invested in performance early paid it back many times over.</p>
<p>Measure with real tools. Profile before you guess. Fix the actual bottlenecks, not the ones you imagine. And keep monitoring in production — performance degrades silently if nobody&#39;s watching.</p>
<hr>
<p><em>Need help with React performance? <a href="https://ma-x.im/contact">Let&#39;s talk</a>.</em>
  </p>
]]></content:encoded>
      <pubDate>Sat, 22 Nov 2025 00:00:00 GMT</pubDate>
      <category>React</category>
      <category>Performance</category>
      <category>Optimization</category>
    </item>
    <item>
      <title>Monorepo Best Practices</title>
      <link>https://ma-x.im/blog/monorepo-best-practices</link>
      <guid isPermaLink="true">https://ma-x.im/blog/monorepo-best-practices</guid>
      <description>How to structure and manage large-scale monorepo projects effectively.</description>
      <content:encoded><![CDATA[<p><img src="https://ma-x.im/blog/monorepo-best-practices/cover.png" alt="Monorepo Best Practices" /></p>
<p>I learned to love monorepos the hard way — by suffering through the alternative.</p>
<p>At a large Canadian automotive company, we had a repository for basic UI components, a separate repository for complex components that depended on them, and then six separate repositories — one for each product line: cars, motorcycles, engines, marine, power equipment, and more. Plus utility repos for shared configs, linting rules, and deployment scripts.</p>
<p>When you released a patch to the base components, the chain reaction began. Update base components, then update complex components, then update all six product repos. Something always broke somewhere in that chain. A CI job would hang. A version mismatch would surface. A team using an older version would suddenly discover their build was broken. We outsourced CI/CD to external contractors, which added another layer of friction — every issue turned into a cross-team investigation.</p>
<p>We even started preparing a plan to consolidate everything into a single monorepo. I left the company before they finished.</p>
<p>That experience made me a firm believer: if your projects share code, put them together.</p>
<h2>Why Monorepo?</h2>
<p><strong>Benefits:</strong></p>
<ul>
<li><strong>Shared code</strong> — one source of truth for components, utils</li>
<li><strong>Atomic changes</strong> — update library and consumers in one PR</li>
<li><strong>Better refactoring</strong> — see all usages across projects</li>
<li><strong>Unified tooling</strong> — one config for linting, testing, building</li>
<li><strong>Easier onboarding</strong> — everything in one repo</li>
</ul>
<p><strong>Drawbacks:</strong></p>
<ul>
<li><strong>Slower CI</strong> if not optimized</li>
<li><strong>More complex tooling</strong> needed</li>
<li><strong>Harder access control</strong> (everything visible)</li>
</ul>
<p>For most teams, benefits outweigh drawbacks. And for teams that have lived through multi-repo dependency hell — it&#39;s not even a question.</p>
<h2>Tools</h2>
<p>I use <strong>Turborepo</strong>. It&#39;s fast, simple, and works great with npm/pnpm.</p>
<p>Alternatives:</p>
<ul>
<li><strong>Nx</strong> - More features, steeper learning curve</li>
<li><strong>Lerna</strong> - Older, less maintained</li>
<li><strong>Rush</strong> - Microsoft&#39;s tool, great for large teams</li>
</ul>
<h2>Structure</h2>
<p>Clean structure is critical:</p>
<pre><code>my-monorepo/
├── apps/
│   ├── web/              # Main web app
│   ├── admin/            # Admin dashboard
│   └── mobile/           # React Native app
├── packages/
│   ├── ui/               # Shared component library
│   ├── utils/            # Shared utilities
│   ├── config/           # Shared configs (eslint, ts)
│   └── types/            # Shared TypeScript types
├── tooling/
│   ├── eslint-config/
│   └── typescript-config/
├── package.json
├── turbo.json
└── pnpm-workspace.yaml
</code></pre>
<p><strong>Apps</strong> = Deployable applications
<strong>Packages</strong> = Shared libraries
<strong>Tooling</strong> = Dev tools and configs</p>
<h2>Package Setup</h2>
<p>Each package has its own <code>package.json</code>:</p>
<pre><code class="language-json">{
  &quot;name&quot;: &quot;@repo/ui&quot;,
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;private&quot;: true,
  &quot;main&quot;: &quot;./src/index.ts&quot;,
  &quot;types&quot;: &quot;./src/index.ts&quot;,
  &quot;exports&quot;: {
    &quot;.&quot;: &quot;./src/index.ts&quot;,
    &quot;./button&quot;: &quot;./src/button.tsx&quot;
  }
}
</code></pre>
<p>Apps import packages:</p>
<pre><code class="language-tsx">import { Button } from &quot;@repo/ui&quot;;
import { formatDate } from &quot;@repo/utils&quot;;
</code></pre>
<p>TypeScript resolves these via workspace protocol.</p>
<h2>Turborepo Configuration</h2>
<p><code>turbo.json</code>:</p>
<pre><code class="language-json">{
  &quot;tasks&quot;: {
    &quot;build&quot;: {
      &quot;dependsOn&quot;: [&quot;^build&quot;],
      &quot;outputs&quot;: [&quot;dist/**&quot;, &quot;.next/**&quot;]
    },
    &quot;test&quot;: {
      &quot;dependsOn&quot;: [&quot;^build&quot;]
    },
    &quot;lint&quot;: {},
    &quot;dev&quot;: {
      &quot;cache&quot;: false,
      &quot;persistent&quot;: true
    }
  }
}
</code></pre>
<p><strong>Key features:</strong></p>
<ul>
<li><code>dependsOn</code> - Build dependencies first</li>
<li><code>outputs</code> - Cache these directories</li>
<li><code>cache: false</code> - Don&#39;t cache dev server</li>
</ul>
<h2>Workspace Protocol</h2>
<p>Use <code>pnpm</code> for best monorepo support:</p>
<p><code>pnpm-workspace.yaml</code>:</p>
<pre><code class="language-yaml">packages:
  - &quot;apps/*&quot;
  - &quot;packages/*&quot;
  - &quot;tooling/*&quot;
</code></pre>
<p>Install workspace dependencies:</p>
<pre><code class="language-json">{
  &quot;dependencies&quot;: {
    &quot;@repo/ui&quot;: &quot;workspace:*&quot;,
    &quot;@repo/utils&quot;: &quot;workspace:*&quot;
  }
}
</code></pre>
<h2>Shared Configuration</h2>
<p>Share ESLint config:</p>
<pre><code class="language-js">// tooling/eslint-config/index.js
module.exports = {
  extends: [
    &quot;eslint:recommended&quot;,
    &quot;plugin:@typescript-eslint/recommended&quot;,
    &quot;plugin:react/recommended&quot;,
  ],
  // ... rules
};
</code></pre>
<p>Use in packages:</p>
<pre><code class="language-json">{
  &quot;eslintConfig&quot;: {
    &quot;extends&quot;: [&quot;@repo/eslint-config&quot;]
  }
}
</code></pre>
<p>Same pattern for TypeScript, Prettier, Jest configs.</p>
<h2>CI/CD Optimization</h2>
<p><strong>Problem:</strong> CI runs all tests on every commit, even if code didn&#39;t change.</p>
<p><strong>Solution:</strong> Turborepo&#39;s remote caching.</p>
<pre><code class="language-bash"># Only rebuild what changed
turbo run build --filter=[HEAD^1]

# Use remote cache
turbo run build --cache-dir=.turbo/cache
</code></pre>
<p>GitHub Actions example:</p>
<pre><code class="language-yaml">name: CI

on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - uses: pnpm/action-setup@v2
        with:
          version: 8
      
      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: &#39;pnpm&#39;
      
      - run: pnpm install
      
      - name: Build
        run: pnpm turbo run build --filter=[HEAD^1]
      
      - name: Test
        run: pnpm turbo run test --filter=[HEAD^1]
</code></pre>
<p><strong>Huge time savings</strong> on large PRs.</p>
<h2>Versioning Strategy</h2>
<p>For internal packages (not published to npm):</p>
<pre><code class="language-json">{
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;private&quot;: true
}
</code></pre>
<p>For published packages, use <strong>Changesets</strong>:</p>
<pre><code class="language-bash">pnpm add -DW @changesets/cli
pnpm changeset init
</code></pre>
<p>Create changeset:</p>
<pre><code class="language-bash">pnpm changeset
</code></pre>
<p>It asks:</p>
<ul>
<li>Which packages changed?</li>
<li>Semver bump (major/minor/patch)?</li>
<li>Changelog entry?</li>
</ul>
<p>Then in CI:</p>
<pre><code class="language-bash">pnpm changeset version
pnpm changeset publish
</code></pre>
<p>Automatic versioning and publishing.</p>
<h2>Common Pitfalls</h2>
<h3>1. Not Using Task Dependencies</h3>
<pre><code class="language-json">{
  &quot;tasks&quot;: {
    &quot;build&quot;: {
      &quot;dependsOn&quot;: [&quot;^build&quot;] // Build deps first!
    }
  }
}
</code></pre>
<p>Without this, apps might build before their dependencies.</p>
<h3>2. Circular Dependencies</h3>
<p><strong>Never</strong> create circular dependencies:</p>
<pre><code>❌ @repo/ui -&gt; @repo/utils -&gt; @repo/ui
</code></pre>
<p>Keep dependency graph acyclic.</p>
<h3>3. Too Many Small Packages</h3>
<p>Don&#39;t create a package for every util function. Group related code:</p>
<pre><code>✅ @repo/utils (contains formatDate, parseUrl, etc.)
❌ @repo/format-date, @repo/parse-url, ...
</code></pre>
<h3>4. Ignoring Cache Configuration</h3>
<p>Configure <code>outputs</code> properly:</p>
<pre><code class="language-json">{
  &quot;tasks&quot;: {
    &quot;build&quot;: {
      &quot;outputs&quot;: [&quot;dist/**&quot;, &quot;build/**&quot;, &quot;.next/**&quot;]
    }
  }
}
</code></pre>
<p>Missing this = no caching benefits.</p>
<h2>Testing in Monorepo</h2>
<p>Shared Jest config:</p>
<pre><code class="language-js">// packages/jest-config/jest.config.js
module.exports = {
  preset: &quot;ts-jest&quot;,
  testEnvironment: &quot;jsdom&quot;,
  moduleNameMapper: {
    &quot;^@repo/(.*)$&quot;: &quot;&lt;rootDir&gt;/../$1/src&quot;,
  },
};
</code></pre>
<p>Run all tests:</p>
<pre><code class="language-bash">turbo run test
</code></pre>
<p>Run tests for specific package:</p>
<pre><code class="language-bash">turbo run test --filter=@repo/ui
</code></pre>
<h2>Development Workflow</h2>
<p>Start all apps in dev mode:</p>
<pre><code class="language-bash">turbo run dev --parallel
</code></pre>
<p>Work on specific app:</p>
<pre><code class="language-bash">turbo run dev --filter=web
</code></pre>
<p>Watch mode for package:</p>
<pre><code class="language-bash">turbo run dev --filter=@repo/ui
</code></pre>
<h2>Migration Strategy</h2>
<p>Moving to monorepo? Do it gradually:</p>
<ol>
<li><strong>Setup monorepo structure</strong></li>
<li><strong>Move shared code</strong> to packages</li>
<li><strong>Migrate one app</strong> at a time</li>
<li><strong>Update CI/CD</strong> incrementally</li>
<li><strong>Train team</strong> on new workflows</li>
</ol>
<p>Don&#39;t do big-bang migrations. Too risky.</p>
<h2>Conclusion</h2>
<p>Every time I set up a monorepo from scratch, I think about those six separate repos and the hours we spent chasing version mismatches across them. The initial monorepo setup takes effort — no question. But the alternative is worse: scattered code, broken dependency chains, and a CI pipeline that nobody fully understands.</p>
<p>Get the structure right. Invest in proper tooling. Cache aggressively. And keep your dependency graph clean. Future you will be grateful.</p>
<hr>
<p><em>Questions about monorepos? <a href="https://ma-x.im/contact">Reach out</a>.</em>
  </p>
]]></content:encoded>
      <pubDate>Wed, 05 Nov 2025 00:00:00 GMT</pubDate>
      <category>Monorepo</category>
      <category>Architecture</category>
      <category>DevOps</category>
    </item>
  </channel>
</rss>