<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ashutosh Jha on Medium]]></title>
        <description><![CDATA[Stories by Ashutosh Jha on Medium]]></description>
        <link>https://medium.com/@thealonemusk?source=rss-dde67f5733a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W8zCd8-zCt1LE4fwZ0_J-w.png</url>
            <title>Stories by Ashutosh Jha on Medium</title>
            <link>https://medium.com/@thealonemusk?source=rss-dde67f5733a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:06:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thealonemusk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Machine Unlearning: Why You Can’t DELETE FROM weights WHERE fact = ‘X’]]></title>
            <link>https://medium.com/@thealonemusk/machine-unlearning-why-you-cant-delete-from-weights-where-fact-x-6773f9f7040d?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6773f9f7040d</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-unlearning]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Tue, 10 Nov 2026 13:34:44 GMT</pubDate>
            <atom:updated>2026-11-10T13:34:55.055Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Not another “neural networks are black boxes” post. Machine unlearning promises to make a model forget on demand, and whether that can work hinges on one question: is knowledge separable in a network’s weights? We follow the evidence through ROME, superposition, relearning attacks, pruning and task vectors, and land on an answer that is “sort of, and not the way you want.”</em></p><p>By : Ashutosh Jha</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QI7qo34myzVdLZWqcPLkyw.png" /></figure><h3>Your lawyer sends you one Slack message</h3><p>Picture this. Your team ships a 7B-parameter model. Three months later, legal pings you: a user has invoked their right to erasure, and the model has memorized something about them. <em>Remove it. By Friday.</em></p><p>If this were a database, you’d write one line:</p><pre>DELETE FROM facts WHERE subject = &#39;user_4821&#39;;</pre><p>Row gone. Every other row untouched. The query planner doesn’t even blink.</p><p>Now try writing that line for a neural network. Which table? Which column? Which of the 7 billion floats <em>is</em> user_4821?</p><p>That question, <strong>is knowledge separable in weights?</strong>, sounds philosophical. It isn’t. It’s the engineering bottleneck underneath model editing, machine unlearning, AI safety, compliance, model merging and interpretability. If knowledge is separable, we can edit, audit and remove. If it isn’t, every one of those is a probabilistic patch on a system we don’t fully control.</p><p>This is the problem <strong>machine unlearning</strong> exists to solve: make a trained model behave as if it had never seen some data, concept or capability, without paying for a full retrain. It shows up as a compliance question (erasure requests), a safety question (removing hazardous knowledge) and a cost question (shrinking a general model into a domain specialist). Every approach quietly bets on one answer to the separability question, so that’s where we start.</p><p>Let’s go through what the evidence actually says.</p><h3>1. First, Define “Separable” — Because Everyone Means Something Different</h3><p>Half the confusion in this field comes from papers using the same word for three different claims:</p><ul><li><strong>Parametric separability</strong> — there is a small set of weights S such that editing or zeroing S removes a fact without hurting anything else. This is DELETE by <em>location</em>. It’s the strongest claim.</li><li><strong>Representational separability</strong> — the fact corresponds to a <em>direction</em> (or subspace) in activation space, not to individual neurons. You intervene on activations, not on weights.</li><li><strong>Delta separability</strong> — whatever the model learned during a training phase is captured as a vector in weight space (fine-tuned weights minus pretrained weights) that can be added or subtracted.</li></ul><p>In semi-formal terms:</p><pre>Parametric:       ∃ sparse mask m ∈ {0,1}^P such that<br>                  model(θ ⊙ (1 − m)) forgets fact F<br>                  AND keeps performance on everything else<br><br>Representational: ∃ direction v in activation space such that<br>                  projecting v out of the residual stream removes F<br><br>Delta:            θ_final = θ_pre + Σ λᵢ · τᵢ<br>                  where each τᵢ ≈ an independent skill or fact<br></pre><p>Spoiler: the answers differ across all three. That’s why the literature looks contradictory when it isn’t.</p><blockquote>“Ask ‘is knowledge separable?’ without saying which kind, and you’ll find a paper that says yes and a paper that says no. Both will be right.”</blockquote><h3>2. The Optimistic Case: Facts Seem to Live in Specific Places</h3><p>There’s real evidence that knowledge is more localized than you’d expect from a pile of 7 billion floats.</p><p><strong>Transformer MLPs behave like key-value memories.</strong> Geva et al. (2021) showed that a feed-forward block, W_out · σ(W_in · x), can be read as a memory. Rows of W_in act as <em>keys</em> that fire on input patterns; columns of W_out act as <em>values</em> that push the output distribution toward particular tokens. Feed the model “The Eiffel Tower is in…”, and some keys light up for the subject and write “Paris-ness” into the residual stream.</p><p><strong>Causal tracing points at mid-layer MLPs.</strong> Meng et al. (2022) ran a neat experiment: corrupt the subject tokens in a factual prompt, then restore hidden states one layer and token at a time and see which restoration brings the correct answer back. For GPT-style models, the strongest effect concentrated in mid-layer MLPs at the <em>last subject token</em>. That’s a location.</p><p><strong>ROME turns that into an edit.</strong> If an MLP’s output projection acts like a linear associative memory, W k ≈ v, then you can overwrite one association with a closed-form rank-one update:</p><pre>Ŵ = W + Λ · (C⁻¹ k*)ᵀ<br>Λ = (v* − W k*) / ((C⁻¹ k*)ᵀ k*)</pre><p>Here k* is the key for the subject, v* is the value you want the layer to produce, and C = K Kᵀ is the covariance of keys the layer sees in normal operation. The C⁻¹ term steers the update so it avoids disturbing other keys. In code, it’s almost embarrassingly short:</p><pre>import torch<br>def rank_one_edit(W, C_inv, k_star, v_star):<br>    # W:      (d_out, d_in)  MLP down-projection<br>    # C_inv:  (d_in, d_in)   inverse covariance of keys from normal text<br>    # k_star: (d_in,)        key for the subject, e.g. &quot;Space Needle&quot;<br>    # v_star: (d_out,)       value we want the layer to emit, e.g. &quot;Paris&quot;<br>    u = C_inv @ k_star                      # direction that avoids other keys<br>    residual = v_star - W @ k_star          # what the layer currently gets wrong<br>    delta = torch.outer(residual, u) / (u @ k_star)<br>    return W + delta                        # now W_new @ k_star == v_starp</pre><p>The paper’s headline demo: change “the Space Needle is in Seattle” to “the Space Needle is in Paris,” and the edit generalizes across paraphrases (“To see the Space Needle, you should fly to…”).</p><p><strong>Circuits tell a similar story for behaviors.</strong> Wang et al. (2022) traced the indirect-object-identification behavior in GPT-2 small to a circuit of roughly two dozen attention heads. Olsson et al. (2022) found induction heads, the small circuit behind in-context copying. A complex behavior reduced to a handful of components is about as “separable” as neural networks get.</p><p>So far, so good. Now the bad news.</p><h3>3. The Pessimistic Case: Superposition</h3><p>Here’s the problem with the neat story: <strong>a model has far more things to represent than it has dimensions to represent them in.</strong></p><p>Elhage et al. (2022) formalized this in <em>Toy Models of Superposition</em>. If the world has n features but your hidden layer has only d &lt; n dimensions, and features are <em>sparse</em> (rarely active at the same time), the network can pack features as almost-orthogonal, non-orthogonal directions and tolerate a little interference.</p><p>The math behind why this works is quietly beautiful. Two random unit vectors in d dimensions have a dot product with standard deviation about 1/√d. At d = 4096, that’s roughly 0.016. Per the Johnson–Lindenstrauss lemma, you can fit a number of vectors that grows <em>exponentially</em> in d while keeping every pairwise cosine small. So a 4096-dim residual stream can hold orders of magnitude more “features” than 4096.</p><p>You can see this happen in about 20 lines:</p><pre>import torch, torch.nn as nn<br>n, d = 5, 2          # 5 features squeezed into 2 dimensions<br>sparsity = 0.9       # each feature is &quot;off&quot; 90% of the time<br>W = nn.Parameter(torch.randn(d, n) * 0.1)<br>b = nn.Parameter(torch.zeros(n))<br>opt = torch.optim.Adam([W, b], lr=1e-2)<br>for step in range(5000):<br>    mask = (torch.rand(1024, n) &gt; sparsity).float()<br>    x = torch.rand(1024, n) * mask           # sparse features in [0, 1]<br>    h = x @ W.T                              # compress: (1024, 2)<br>    x_hat = torch.relu(h @ W + b)            # reconstruct: (1024, 5)<br>    loss = ((x - x_hat) ** 2).mean()<br>    opt.zero_grad(); loss.backward(); opt.step()<br>G = W.T @ W   # (5, 5) Gram matrix; off-diagonals = interference between features</pre><p>At high sparsity, the five feature directions typically settle into something like a regular pentagon in 2D: five features, two dimensions, a bit of interference that the ReLU and bias clean up. Crank the sparsity down (features active more often) and the model drops features instead, because interference gets too expensive.</p><p>The consequence for our question is brutal:</p><ul><li><strong>Neurons are polysemantic.</strong> A single neuron participates in many unrelated features, because the “natural” basis for features isn’t the neuron basis.</li><li><strong>Every weight serves many facts.</strong> There is no region you can zero out that holds exactly one fact.</li><li><strong>Capacity is tight.</strong> In controlled synthetic experiments, Allen-Zhu and Li (2024) measured that transformers store roughly 2 bits of knowledge per parameter when trained enough. There’s no slack to give each fact a private region.</li></ul><p>The interpretability response is <strong>sparse autoencoders (SAEs)</strong>: train an overcomplete dictionary on activations so that each activation decomposes into a few active, hopefully monosemantic features. Anthropic’s <em>Towards Monosemanticity</em> (2023) did this on a one-layer model, and <em>Scaling Monosemanticity</em> (2024) scaled it to a production-size model, finding interpretable features like the famous Golden Gate Bridge feature. Clamp that feature high and the model starts to steer every answer toward the bridge.</p><p>That’s representational separability working. But SAEs have their own caveats: features split and absorb each other as you change dictionary size, reconstruction is never perfect, and there’s no guarantee the dictionary you found is <em>the</em> canonical one.</p><blockquote>“Knowledge isn’t stored in weights like rows in a table. It’s stored like a melody in a piano: spread across the strings, and recoverable only if you hit the right chord.”</blockquote><h3>4. Localization Is Not the Same as Editability</h3><p>Here’s a result that should make you suspicious of tidy stories. Hase et al. (2023) asked a simple question: if causal tracing says a fact lives in layer L, is layer L the best place to edit it? <strong>Mostly, no.</strong> Localization from causal tracing barely predicted which layer produced the best edit. Where the model <em>reads</em> a fact and where you can most effectively <em>rewrite</em> it are different questions.</p><p>Edits also have blast radius that a database UPDATE never would:</p><ul><li><strong>Ripple effects.</strong> Change “Messi plays for Inter Miami,” then ask where Messi’s team is based. Multi-hop consequences often don’t follow. The model absorbed a <em>string association</em>, not a fact in a knowledge graph.</li><li><strong>The reversal curse.</strong> Berglund et al. (2023) showed models trained on “A is B” often fail to answer “B is A.” Knowledge is stored in a direction-and-form-specific way that no clean “fact object” explains.</li><li><strong>Edit accumulation.</strong> ROME-style edits work for one fact. MEMIT-style methods extend this to thousands in a batch, but editing sequentially at scale has been shown to gradually degrade the model.</li></ul><p>If facts were cleanly separable objects, none of these would happen. They happen because a “fact” is not a unit the network ever had to respect.</p><h3>5. The Unlearning Toolbox, and the Test It Keeps Failing</h3><p>Machine unlearning is separability with a deadline attached. The gold standard definition: the unlearned model should be indistinguishable from one <strong>retrained from scratch without the forget set.</strong> Retraining a frontier model every time someone files a request is obviously not an option, so we approximate.</p><p>The common toolbox:</p><ul><li><strong>Gradient ascent</strong> on the forget set (maximize loss on what you want gone).</li><li><strong>Representation-misdirection methods</strong> like RMU, introduced alongside the WMDP benchmark (Li et al., 2024), which perturb intermediate activations on hazardous-knowledge data while preserving them elsewhere.</li><li><strong>Preference-style methods</strong> that train the model to refuse or deflect on the forget set.</li></ul><p>Then comes the failure mode that matters most: <strong>relearning attacks.</strong> A growing line of work shows that “unlearned” knowledge often comes back after a small amount of fine-tuning, sometimes on data that doesn’t even contain the forgotten facts, and that linear probes can still decode it from the activations. The knowledge wasn’t <em>removed</em>. The pathway to it was <em>suppressed</em>.</p><blockquote>“If a few hundred gradient steps can bring a fact back, you didn’t delete it. You hid it.”</blockquote><p>This is the clearest evidence against strong parametric separability. If a fact lived in a removable region, removing the region would stick.</p><h3>6. The Surprising Win: Separability in Weight Differences</h3><p>Now the twist. Even if knowledge isn’t separable <em>in</em> the weights, it appears surprisingly separable <em>between</em> them.</p><p>Ilharco et al. (2023) defined a <strong>task vector</strong> as the difference between fine-tuned and pretrained weights, τ = θ_ft − θ_pre. Add it to the base model and you add the skill. Subtract it and you (partially) remove it. Add several and you get multi-skill models, with no extra training.</p><pre>def task_vector(finetuned, pretrained):<br>    return {k: finetuned[k] - pretrained[k] for k in pretrained}<br><br>def apply(pretrained, vectors, coeffs):<br>    out = {k: v.clone() for k, v in pretrained.items()}<br>    for tau, lam in zip(vectors, coeffs):<br>        for k in out:<br>            out[k] += lam * tau[k]<br>    return out<br># Add a skill:     coeffs = [+0.5]<br># Forget a skill:  coeffs = [-0.5]   (task negation)</pre><p>Why would this work at all? Because fine-tuning from a shared pretrained initialization tends to stay inside one flat basin of the loss landscape, where the function is approximately linear in the weight change. Ortiz-Jimenez et al. (2023) called the useful version of this <strong>weight disentanglement</strong>: different tasks’ updates affect the function on different input regions, so they don’t step on each other. LoRA’s success points the same way. If a fine-tune can be captured by a low-rank delta, then what the model <em>learned</em> is a much lower-dimensional object than the weights it lives in.</p><p>The caveats are real: interference between task vectors grows as you stack more of them (TIES-merging and DARE exist precisely to patch this), negation causes collateral damage on related abilities, and it only works well when the tasks are genuinely disjoint.</p><p>But notice what this means: knowledge is <strong>separable in the training trajectory</strong> even when it’s tangled in the final weights.</p><h3>7. An Honest Comparison of Every Approach</h3><p>No single technique answers the question. Here’s how each one scores on what it actually delivers.</p><p><strong>Pruning / weight ablation</strong> — Zero out weights or neurons correlated with a behavior. <em>Wins on:</em> simplicity, and it genuinely works for coarse behaviors. <em>Loses on:</em> superposition. Any neuron you cut carries unrelated features, so collateral damage is nearly guaranteed.</p><p><strong>Locate-then-edit (ROME / MEMIT)</strong> — Closed-form rank-one updates to mid-layer MLPs. <em>Wins on:</em> speed (no training) and precision for single, simple facts. <em>Loses on:</em> ripple effects, the reversal curse, and degradation under many sequential edits.</p><p><strong>SAE feature steering / ablation</strong> — Intervene on learned sparse features in activation space. <em>Wins on:</em> interpretability. You can name what you’re turning down. <em>Loses on:</em> feature splitting, imperfect reconstruction, and the fact that it’s an inference-time intervention, not a weight change.</p><p><strong>Task vectors / model merging</strong> — Add or subtract weight deltas. <em>Wins on:</em> compositionality and zero extra training. <em>Loses on:</em> interference at scale and a hard dependency on a shared pretrained starting point.</p><p><strong>Unlearning (gradient ascent, RMU, preference methods)</strong> — Train the forgetting in. <em>Wins on:</em> generality. It doesn’t need to know <em>where</em> the knowledge is. <em>Loses on:</em> robustness. Relearning attacks and probes show the knowledge often survives.</p><p><strong>Retrain without the data (exact unlearning, e.g. SISA-style sharding)</strong> — Train separate shards so you only retrain the affected one. <em>Wins on:</em> provable deletion. <em>Loses on:</em> cost, and slightly weaker models because no shard sees all the data.</p><blockquote>“Every method either gives up on knowing where the knowledge is, or pays for knowing it with collateral damage.”</blockquote><h3>8. If You Can’t Find Separability, Build It</h3><p>The most interesting recent idea flips the question: stop <em>searching</em> for modularity after training and <strong>force it during training.</strong></p><p><strong>Gradient routing</strong> (Cloud et al., 2024) masks gradients so that data from a chosen domain can only update a designated region of the network. You decide up front where a capability lives, so ablating that region later is a real deletion rather than a hopeful one.</p><p><strong>Mixture of Experts</strong> looks like it should give this for free, but doesn’t. Mixtral’s analysis found that expert routing tracks syntax and token patterns far more than topics. Experts specialize, but not along the lines a human would draw, so you can’t say “delete the biology expert.”</p><p><strong>Adapters and LoRA modules</strong> give you separable <em>deltas</em> by construction. Keep a module per tenant, per domain, or per sensitive dataset, and deleting it is a file deletion.</p><p>The pattern is consistent: <strong>separability is cheaper to train in than to discover afterward.</strong></p><h3>9. Unlearning by Subtraction: Can You Prune a Model Into a Domain Expert?</h3><p>There’s a quieter cousin of unlearning that attacks the same problem from the opposite direction. Instead of asking a model to forget one fact, ask it to forget <em>almost everything except one domain</em>. Take a giant general model, keep what programming needs, and discard the history, geography and literature. If knowledge were cleanly separable, this would be easy. It isn’t, and the reasons are the ones we’ve been tracing.</p><p><strong>First, untangle three goals that get conflated.</strong></p><ul><li><strong>Parameter reduction:</strong> fewer weights stored. It saves memory and latency, and says nothing about what the model forgot.</li><li><strong>Capability reduction:</strong> the model gets worse at irrelevant tasks. Fine-tuning on code can do this just by teaching the model what to prioritize, without removing a single stored fact about ancient Greece.</li><li><strong>Knowledge erasure:</strong> specific information becomes inaccessible or unrecoverable. This is the only one that counts as unlearning in the strict sense, and the hardest.</li></ul><p>Removing 40% of the parameters does not remove 40% of the knowledge. Thanks to superposition, parameter count and knowledge capacity aren’t linearly related.</p><p><strong>The shared-parameters problem.</strong> A tempting picture splits a model into “coding parameters,” “history parameters” and everything else. Treat that as a conceptual cartoon, not a measured partition. Coding leans on math, English, logic and algorithmic knowledge, and some of that also serves physics and economics. Prune the “physics” parameters and you may damage the mathematical reasoning your coding model needs. The hard bucket is always the shared one.</p><p><strong>What the literature actually does.</strong> Three recent papers extract a domain model by removing parts of a general one:</p><ul><li><strong>D-Pruner</strong> (<a href="https://aclanthology.org/2024.findings-naacl.91/">Zhang et al., Findings of NAACL 2024</a>) is a dual-pruning method. It scores weights by how much removing them hurts general capability, using open-domain calibration data, then folds those scores into the training loss on domain data so the pruned model keeps both generality and domain knowledge. It was evaluated on healthcare and legal tasks.</li><li><strong>Cus-Prun</strong> (<a href="https://aclanthology.org/2025.findings-acl.1201/">Zhao et al., Findings of ACL 2025</a>) prunes neurons that are irrelevant along three axes (language, domain and task) and builds the expert model without any post-training.</li><li><strong>TrimLLM</strong> (<a href="https://aclanthology.org/2025.acl-long.33/">Hu et al., ACL 2025</a>) removes whole layers instead of individual weights, based on an observed layer-wise specialization, which gives speedups without special hardware or kernels. It was evaluated on medical, legal and financial domains.</li></ul><p>Judging by their abstracts, the success metric in all three is domain performance at a smaller size, not proof that the discarded knowledge is gone. That gap is exactly where unlearning begins.</p><p><strong>How the importance scoring works.</strong> The basic move is to score every component (neuron, head, layer) on how much a domain needs it, then triage. A first-order version looks like this:</p><pre>def importance(acts, grads=None):<br>    # acts:  (N, n_neurons) activations over a calibration set<br>    # grads: (N, n_neurons) gradient of the loss w.r.t. those activations<br>    if grads is None:<br>        return acts.abs().mean(0)               # average activation magnitude<br>    return (acts * grads).abs().mean(0)         # ~ loss change if the neuron is zeroed<br><br>def triage(I_domain, I_general, hi_d, hi_g):<br>    keep     = I_domain &gt; hi_d                   # the domain needs it<br>    evaluate = (~keep) &amp; (I_general &gt; hi_g)      # general-only: test before cutting<br>    prune    = ~(keep | evaluate)                # low on both: candidates to remove<br>    return keep, evaluate, prune</pre><p>The caveat is that average activation can mislead. A neuron that rarely fires but is essential for one rare operation looks unimportant on average. Gradient-aware scores and direct ablation tests (zero it, measure the loss change) are safer. The “evaluate” bucket is the interesting one: a component might be essential for natural language but not for code generation, and yet coding instructions, documentation and error messages all depend on language. That makes this a constrained problem, not a free-for-all:</p><pre>maximize   domain_performance(θ&#39;)<br>subject to size(θ&#39;) ≤ B<br>           general_language_ability(θ&#39;) ≥ ε</pre><p><strong>A hybrid pipeline.</strong> Put together, a defensible extraction recipe looks like this:</p><ol><li>Measure importance on domain data and on general data.</li><li>Triage components into domain-critical, general-only, and low-on-both.</li><li>Prune the low-on-both group first; test the general-only group carefully.</li><li>Run recovery training on domain data.</li><li>Evaluate domain performance, general retention, and, the step that turns pruning into unlearning, whether the discarded knowledge is really gone.</li></ol><p><strong>Distillation is the other route.</strong> Train a smaller student on the domain-relevant outputs of the large teacher. The student never sees most of the teacher’s other knowledge, so it’s the model-compression cousin of “retrain without the data.” You pay in training cost and fidelity, and you gain a model with no leftover weights for the forgotten material to hide in.</p><p><strong>Why this matters for the unlearning story.</strong> Every method in section 5 leaves the weights in place and changes how they’re read, so a little fine-tuning can reopen the path. Pruning physically removes capacity. A relearning attack on a pruned model would have to rebuild what was cut, not re-expose it. Whether that makes pruning more robust is, as far as I know, an open empirical question, and a cheap one to test: prune, then run the same relearning and probing attacks on the pruned model and the original.</p><p>A clean research question falls out of this: what is the <em>smallest</em> subnetwork that keeps 95% of the original model’s domain performance?</p><pre>minimize   |θ&#39;|<br>subject to P_domain(θ&#39;) / P_domain(θ) ≥ 0.95</pre><p>Plot size against domain score and the knee of that curve tells you how much of the model the domain actually needs. One caveat for mixture-of-experts models: total parameters and active parameters differ, so dropping rarely used experts and cutting per-token compute are related but different problems.</p><blockquote>“Pruning tells you what a domain needs. It doesn’t, by itself, tell you what the model forgot.”</blockquote><h3>10. What We Still Don’t Know</h3><p>Honest claims about unlearning come with open questions attached. These are the ones that decide whether any of this gets better or worse as models scale:</p><ul><li><strong>What counts as “forgotten”?</strong> Exact unlearning has a clean definition: indistinguishable from a model retrained without the data. Approximate methods don’t. Output behavior, activation probes and relearning speed can each disagree about whether a fact is gone.</li><li><strong>Is there a guarantee?</strong> Provable deletion exists for sharded training, but not for editing, gradient-based unlearning or pruning. Without a formal notion of “this model could have been trained without that data,” compliance teams are left with empirical evidence.</li><li><strong>Does pruning resist relearning?</strong> Section 9 argues it might, because the capacity is physically gone. It’s a testable claim, and I’m not aware of it being settled.</li><li><strong>Does superposition get better or worse with scale?</strong> Bigger models have more dimensions per layer and vastly more to know. Nobody has a clean answer on whether the polysemantic share shrinks, stays flat or grows.</li><li><strong>Are SAE features canonical?</strong> Features split as the dictionary grows, and different training runs can produce different dictionaries. “Delete this feature” is only as solid as the dictionary it came from.</li><li><strong>Where does knowledge end and capability begin?</strong> “Chemistry facts” and “the ability to reason about chemistry” aren’t the same object, and unlearning benchmarks struggle to say which one they removed.</li><li><strong>Do the results transfer?</strong> Most causal-tracing and editing work is on GPT-2- and GPT-J-class models. Newer architectures haven’t had the same scrutiny.</li></ul><h3>11. So… Is Knowledge Separable?</h3><p>Here’s the answer I’d defend in a design review:</p><ul><li><strong>Parametric separability (a region you can zero out):</strong> mostly <strong>no.</strong> Superposition and relearning attacks say weights are shared across facts and knowledge survives local removal.</li><li><strong>Representational separability (a direction you can intervene on):</strong> <strong>partially yes.</strong> SAEs and causal tracing find real, steerable structure, with caveats on canonicity and completeness.</li><li><strong>Delta separability (a vector you can add or subtract):</strong> <strong>often yes</strong>, under shared initialization and disjoint tasks.</li><li><strong>Behavioral circuits:</strong> <strong>yes</strong> for narrow, well-studied behaviors, with no guarantee of generalizing to everything.</li></ul><p>There’s also a version of this you’ll hear in interviews:</p><blockquote>“Can we delete a fact from an LLM?”</blockquote><p>The strong answer isn’t yes or no. It’s: <strong>we can cheaply suppress it, expensively relocate it, and reliably delete it only by retraining or by having designed the model for it in advance.</strong> Then name the trade-off for each. That one answer signals you understand superposition, editing, and unlearning at once.</p><h3>Closing Thought</h3><p>The database analogy was always the wrong mental model. Databases store facts in addressable cells because we designed them to. Neural networks store knowledge in whatever geometry minimizes loss, and that geometry reuses every parameter for everything it can.</p><p>So the right question was never “where is the fact?” It’s “<strong>what is the cheapest intervention that changes the function the way I need, and how will I verify it stayed changed?</strong>” Sometimes that’s a rank-one edit. Sometimes it’s a subtracted task vector. Sometimes it’s a retrain. And increasingly, it’s a decision you make before training even starts.</p><p>Knowledge isn’t separable by default. It’s separable by <em>design</em>, and the field is only beginning to learn how to design for it.</p><p><em>Next in this series: a deep dive into sparse autoencoders: how they’re trained, why feature splitting happens, and what “monosemantic” really buys you.</em></p><h3>Further Reading</h3><ul><li><a href="https://aclanthology.org/2021.emnlp-main.446.pdf">Geva et al., <em>Transformer Feed-Forward Layers Are Key-Value Memories</em> (2021)</a></li><li><a href="https://aclanthology.org/2022.acl-long.581/">Dai et al., <em>Knowledge Neurons in Pretrained Transformers</em> (2022)</a></li><li><a href="https://arxiv.org/abs/2202.05262">Meng et al., <em>Locating and Editing Factual Associations in GPT</em> (ROME, 2022) and <em>Mass-Editing Memory in a Transformer</em> (MEMIT, 2023)</a></li><li><a href="https://arxiv.org/abs/2209.10652">Elhage et al., <em>Toy Models of Superposition</em> (2022)</a></li><li>Wang et al., <em>Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 Small</em> (2022)</li><li>Bricken et al., <em>Towards Monosemanticity</em> (2023); Templeton et al., <em>Scaling Monosemanticity</em> (2024)</li><li>Hase et al., <em>Does Localization Inform Editing?</em> (2023)</li><li>Berglund et al., <em>The Reversal Curse</em> (2023)</li><li>Ilharco et al., <em>Editing Models with Task Arithmetic</em> (2023)</li><li>Ortiz-Jimenez et al., <em>Task Arithmetic in the Tangent Space</em> (2023)</li><li>Allen-Zhu and Li, <em>Physics of Language Models: Part 3.3, Knowledge Capacity Scaling Laws</em> (2024)</li><li>Li et al., <em>The WMDP Benchmark</em> (2024)</li><li>Cloud et al., <em>Gradient Routing</em> (2024)</li><li>Zhang et al., <a href="https://aclanthology.org/2024.findings-naacl.91/"><em>Pruning as a Domain-specific LLM Extractor</em></a> (Findings of NAACL 2024)</li><li>Zhao et al., <a href="https://aclanthology.org/2025.findings-acl.1201/"><em>Pruning General Large Language Models into Customized Expert Models</em></a> (Findings of ACL 2025)</li><li>Hu et al., <a href="https://aclanthology.org/2025.acl-long.33/"><em>TrimLLM: Progressive Layer Dropping for Domain-Specific LLMs</em></a> (ACL 2025)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6773f9f7040d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I Read the AI Agent Code. The Model Wasn’t the Interesting Part.]]></title>
            <link>https://medium.com/@thealonemusk/i-read-the-ai-agent-code-the-model-wasnt-the-interesting-part-f38805e7ae48?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f38805e7ae48</guid>
            <category><![CDATA[frontier-models]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai-harness]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Mon, 26 Oct 2026 12:20:10 GMT</pubDate>
            <atom:updated>2026-10-26T12:20:10.468Z</atom:updated>
            <content:encoded><![CDATA[<p><em>how I stopped blaming the LLM and started reading the 400 lines of Python wrapped around it.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TaMFtDipGB8Bbc6OtrNX9w.png" /></figure><h3>The Intern With Amnesia</h3><p>Picture the smartest intern you’ve ever hired. Reads any doc in seconds, writes clean code, never gets tired. There’s just one catch: every 30 seconds they forget everything except the sticky notes you hand them. Also, they sometimes say wrong things with total confidence. And once in a while, they email your customer twice.</p><p>Would you give that intern the production database and walk away?</p><p>Of course not. You’d build a system around them: checklists, deadlines, an audit trail, and a rule that says “if you’ve tried the same thing three times, stop and ask for help.”</p><p>That system is the <strong>harness</strong>. After reading the source of several agent frameworks, I’m convinced it’s where most of the real engineering lives.</p><p>The model is a function: tokens in, tokens out. Non-deterministic, slow, expensive, occasionally wrong. An RPC dependency with a personality. Everything that decides whether your agent survives production is code <em>you</em> write:</p><ul><li>What goes into the context, and what gets thrown out</li><li>How tool calls are validated, executed, timed out and retried</li><li>Where state lives, and what a crash at step 14 of 20 costs you</li><li>How the loop decides it’s done, stuck, or going in circles</li></ul><p>If you’ve built distributed systems, this will feel familiar. We know how to wrap flaky dependencies. We just forget to, because this one talks in English.</p><p>Let’s read some code.</p><h3>The 25 Lines Every Tutorial Ships</h3><p>This is the canonical agent. You’ll find some version of it on the first page of most quickstarts.</p><pre># naive_agent.py<br>from anthropic import Anthropic<br>client = Anthropic()<br>def run_agent(task, tools, tool_schemas, max_steps=20):<br>    messages = [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: task}]<br>    for _ in range(max_steps):<br>        resp = client.messages.create(<br>            model=&quot;claude-sonnet-4-6&quot;,<br>            max_tokens=4096,<br>            tools=tool_schemas,<br>            messages=messages,<br>        )<br>        messages.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: resp.content})<br>        if resp.stop_reason != &quot;tool_use&quot;:<br>            return resp.content[0].text<br>        results = []<br>        for block in resp.content:<br>            if block.type == &quot;tool_use&quot;:<br>                out = tools[block.name](**block.input)      # (1)<br>                results.append({<br>                    &quot;type&quot;: &quot;tool_result&quot;,<br>                    &quot;tool_use_id&quot;: block.id,<br>                    &quot;content&quot;: str(out),                    # (2)<br>                })<br>        messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: results})<br>    return &quot;max steps reached&quot;                              # (3)</pre><p>Works great in a demo. Now read it like an SRE at 3 a.m.:</p><ol><li><strong>Line 1: raw execution.</strong> The model’s arguments go straight into your function. No validation, no try/except, no timeout. A tool that raises kills the run. A tool that hangs kills the process.</li><li><strong>Line 2: </strong><strong>str(out) is unbounded.</strong> One cat of a 4 MB log and your next request is a million tokens, or a hard error.</li><li><strong>Line 3: failure is a string.</strong> The caller can’t tell “done” from “gave up” from “stuck in a loop.”</li></ol><p>Bonus round: the full history is resent every step (quadratic token cost), nothing is persisted, and nothing is idempotent. Every one of those becomes a row in the benchmark table later.</p><h3>Four Things the Model Won’t Do for You</h3><h3>1. Tool Execution: The Bouncer at the Door</h3><p>Here’s what a good harness <em>thinks</em> every time the model asks for a tool call:</p><blockquote><strong><em>Step 1, Validation:</em></strong><em> “Let’s see… the model wants </em><em>read_file with </em><em>path=42. That&#39;s an int. Schema says string. Reject it, and tell the model exactly why.&quot;</em></blockquote><blockquote><strong><em>Step 2, Idempotency:</em></strong><em> “This tool sends email. Have I seen this exact call before? Yes? Replay the old result. Do NOT send it again.”</em></blockquote><blockquote><strong><em>Step 3, Deadline:</em></strong><em> “You get 30 seconds. After that, I’m cutting you off.”</em></blockquote><blockquote><strong><em>Step 4, Output budget:</em></strong><em> “40K characters? You get 8K, a marker saying how much I cut, and instructions for asking for the rest.”</em></blockquote><p>And in code:</p><pre># harness/tools.py<br>import asyncio, time, hashlib, json<br>from dataclasses import dataclass<br>from pydantic import BaseModel, ValidationError<br><br>MAX_OUT = 8_000<br>@dataclass<br>class ToolResult:<br>    content: str<br>    is_error: bool = False<br>    truncated: bool = False<br>    latency_ms: float = 0.0<br>class Tool:<br>    def __init__(self, name, fn, args_model, timeout_s=30.0, side_effecting=False):<br>        self.name, self.fn = name, fn<br>        self.args_model = args_model<br>        self.timeout_s = timeout_s<br>        self.side_effecting = side_effecting<br>    async def run(self, raw_args, idem_cache):<br>        t0 = time.perf_counter()<br>        try:<br>            args = self.args_model(**raw_args)<br>        except ValidationError as e:<br>            # Feed the error back. Models self-correct surprisingly well.<br>            return ToolResult(f&quot;Invalid arguments: {e.errors()}&quot;, is_error=True)<br>        key = None<br>        if self.side_effecting:<br>            key = hashlib.sha256(<br>                f&quot;{self.name}:{json.dumps(raw_args, sort_keys=True)}&quot;.encode()<br>            ).hexdigest()<br>            if key in idem_cache:<br>                return idem_cache[key]          # replay, don&#39;t re-execute<br>        try:<br>            out = await asyncio.wait_for(self.fn(args), self.timeout_s)<br>            text, trunc = str(out), False<br>            if len(text) &gt; MAX_OUT:<br>                cut = len(text) - MAX_OUT<br>                text = (text[:MAX_OUT] + f&quot; ...[truncated {cut} chars. &quot;<br>                        f&quot;Narrow the query or use offset/limit.]&quot;)<br>                trunc = True<br>            res = ToolResult(text, truncated=trunc)<br>        except asyncio.TimeoutError:<br>            res = ToolResult(f&quot;Tool timed out after {self.timeout_s}s&quot;, is_error=True)<br>        except Exception as e:<br>            res = ToolResult(f&quot;{type(e).__name__}: {e}&quot;, is_error=True)<br>        res.latency_ms = (time.perf_counter() - t0) * 1000<br>        if key and not res.is_error:<br>            idem_cache[key] = res<br>        return res</pre><p>Look at that truncation message. It’s not politeness. If you tell the model <em>how to recover</em>, it stops re-issuing the same call and hoping.</p><h3>2. Memory: Three Layers, Not One</h3><p>“Memory” is three different things people lump together:</p><ul><li><strong>Working context:</strong> the live message list. Lives for one run. Managed by windowing and compaction.</li><li><strong>Scratchpad:</strong> the plan, decisions made, facts found. Survives crashes. A structured store, not a chat log.</li><li><strong>Long-term memory:</strong> facts and outcomes across tasks, pulled in by retrieval.</li></ul><p>The naive loop has only the first one, and it grows forever. Cost is quadratic: step <em>k</em> pays for all the previous turns. At 30 steps and roughly 2K tokens per step, that’s around 930K input tokens for a single task, before caching.</p><p>Two fixes did most of the work.</p><p><strong>Fix #1: Observation compaction.</strong> Old tool results are the bulk of your context and the least valuable thing in it once the model has used them. Replace stale payloads with a stub, but keep the call and the assistant’s reasoning.</p><pre># harness/context.py<br>KEEP_RECENT = 4   # most recent tool results stay verbatim<br><br>def compact(messages):<br>    idxs = [<br>        i for i, m in enumerate(messages)<br>        if m[&quot;role&quot;] == &quot;user&quot; and isinstance(m[&quot;content&quot;], list)<br>        and any(b.get(&quot;type&quot;) == &quot;tool_result&quot; for b in m[&quot;content&quot;])<br>    ]<br>    stale = set(idxs[:-KEEP_RECENT])<br>    out = []<br>    for i, m in enumerate(messages):<br>        if i not in stale:<br>            out.append(m)<br>            continue<br>        blocks = []<br>        for b in m[&quot;content&quot;]:<br>            if b.get(&quot;type&quot;) == &quot;tool_result&quot;:<br>                n = len(str(b[&quot;content&quot;]))<br>                b = {**b, &quot;content&quot;: f&quot;[result elided: {n} chars. Re-run the tool if needed.]&quot;}<br>            blocks.append(b)<br>        out.append({**m, &quot;content&quot;: blocks})<br>    return out</pre><p><strong>Fix #2: A structured scratchpad.</strong> Don’t trust the model to remember the plan through 40 turns of noise. Keep a small state object, checkpoint it, and re-inject it every step.</p><pre># harness/state.py<br>from dataclasses import dataclass, field, asdict<br>import json, sqlite3<br>@dataclass<br>class RunState:<br>    run_id: str<br>    goal: str<br>    plan: list = field(default_factory=list)<br>    done_steps: list = field(default_factory=list)<br>    facts: dict = field(default_factory=dict)<br>    step: int = 0<br>    tokens_in: int = 0<br>    tokens_out: int = 0<br>class StateStore:<br>    def __init__(self, path=&quot;agent_state.db&quot;):<br>        self.db = sqlite3.connect(path)<br>        self.db.execute(&quot;CREATE TABLE IF NOT EXISTS runs(&quot;<br>                        &quot;run_id TEXT PRIMARY KEY, state TEXT, messages TEXT)&quot;)<br>    def checkpoint(self, st, messages):<br>        self.db.execute(&quot;REPLACE INTO runs VALUES(?,?,?)&quot;,<br>                        (st.run_id, json.dumps(asdict(st)),<br>                         json.dumps(messages, default=str)))<br>        self.db.commit()<br>    def resume(self, run_id):<br>        row = self.db.execute(&quot;SELECT state, messages FROM runs WHERE run_id=?&quot;,<br>                              (run_id,)).fetchone()<br>        return (RunState(**json.loads(row[0])), json.loads(row[1])) if row else None</pre><p>Checkpoint after every step and a crash, a deploy, or a rate-limit stall costs you one step instead of the whole run. For long-running agents, this is the single biggest operational win, and it has nothing to do with the model.</p><h3>3. State and Loop Control: Give Failure a Name</h3><p>The loop needs real terminal states, not a string. It also needs to catch pathological behavior the model will never catch in itself:</p><ul><li><strong>Repeated identical calls:</strong> same tool, same args. Warn once, then abort.</li><li><strong>Consecutive tool errors:</strong> the agent is flailing.</li><li><strong>Budgets</strong> on steps, tokens and wall-clock, enforced by the harness and not by politely asking the model.</li></ul><pre># harness/loop.py<br>from enum import Enum<br>from collections import Counter<br>import json<br>class Outcome(Enum):<br>    SUCCESS = &quot;success&quot;<br>    BUDGET_EXHAUSTED = &quot;budget_exhausted&quot;<br>    LOOP_DETECTED = &quot;loop_detected&quot;<br>    TOOL_FAILURE = &quot;tool_failure&quot;<br>    MODEL_ERROR = &quot;model_error&quot;<br>async def run(tools, schemas, store, state, messages, cfg):<br>    seen = Counter()<br>    consecutive_errors = 0<br>    while state.step &lt; cfg.max_steps:<br>        if state.tokens_in + state.tokens_out &gt; cfg.max_tokens:<br>            return Outcome.BUDGET_EXHAUSTED, state<br>        ctx = inject_scratchpad(compact(messages), state)   # re-state the plan<br>        resp = await call_model_with_retry(ctx, schemas, cfg)<br>        if resp is None:<br>            return Outcome.MODEL_ERROR, state<br>        state.tokens_in += resp.usage.input_tokens<br>        state.tokens_out += resp.usage.output_tokens<br>        messages.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: resp.content})<br>        if resp.stop_reason != &quot;tool_use&quot;:<br>            store.checkpoint(state, messages)<br>            return Outcome.SUCCESS, state<br>        results = []<br>        for b in (b for b in resp.content if b.type == &quot;tool_use&quot;):<br>            sig = (b.name, json.dumps(b.input, sort_keys=True))<br>            seen[sig] += 1<br>            if seen[sig] &gt;= 3:<br>                return Outcome.LOOP_DETECTED, state<br>            note = &quot; [harness] You already ran this exact call. Try something else.&quot; \<br>                   if seen[sig] == 2 else &quot;&quot;<br>            r = await tools[b.name].run(b.input, cfg.idem_cache)<br>            consecutive_errors = consecutive_errors + 1 if r.is_error else 0<br>            results.append({&quot;type&quot;: &quot;tool_result&quot;, &quot;tool_use_id&quot;: b.id,<br>                            &quot;content&quot;: r.content + note, &quot;is_error&quot;: r.is_error})<br>        messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: results})<br>        if consecutive_errors &gt;= cfg.max_consecutive_errors:<br>            return Outcome.TOOL_FAILURE, state<br>        state.step += 1<br>        store.checkpoint(state, messages)<br>    return Outcome.BUDGET_EXHAUSTED, state</pre><p>That Outcome enum is the most underrated thing in the file. Once failures have names, you can count them, alert on them, and discover that LOOP_DETECTED is most of your failures. Which tells you to fix your tool descriptions, not your prompt.</p><h3>4. Retries: Two Layers, Never Mix Them</h3><p>There are two kinds of retry, and confusing them is how you get runaway cost and duplicate side effects:</p><ol><li><strong>Transport retries</strong> (429, 5xx, timeouts) on the model call. Exponential backoff with jitter. Safe, because a model call is a pure function of its input.</li><li><strong>Semantic retries:</strong> the model sees a tool error and tries again. That’s the loop’s job, bounded by budgets, and made safe by idempotency keys.</li></ol><pre># harness/model.py<br>import asyncio, random<br>from anthropic import APIStatusError, APIConnectionError<br><br>RETRYABLE = {408, 409, 429, 500, 502, 503, 529}<br>async def call_model_with_retry(ctx, schemas, cfg, attempts=5):<br>    for i in range(attempts):<br>        try:<br>            return await cfg.client.messages.create(<br>                model=cfg.model, max_tokens=cfg.max_out,<br>                tools=schemas, messages=ctx, timeout=cfg.model_timeout_s)<br>        except APIStatusError as e:<br>            if e.status_code not in RETRYABLE or i == attempts - 1:<br>                return None<br>        except APIConnectionError:<br>            if i == attempts - 1:<br>                return None<br>        await asyncio.sleep(min(30, (2 ** i) + random.random()))<br>    return None</pre><h3>Seven Ways Agents Break (None of Them Are “The Model Is Too Dumb”)</h3><p>These are failure patterns that show up again and again once you read agent code with an SRE’s eyes. Odds are, a few are hiding in yours.</p><p><strong>1. The Truncation Death Spiral.</strong> A tool returns 40K characters. You truncate at 8K with a bare .... The model concludes the file ends there and makes decisions on half the data. Fix: say it out loud. [truncated N chars, use offset/limit].</p><p><strong>2. The Compaction That Ate the Evidence.</strong> Version one of compaction deleted old tool results entirely, call included. The model, with no trail of what it had already tried, re-ran the same exploration commands from scratch. Compact the <em>payload</em>. Keep the <em>trail</em>.</p><p><strong>3. The Retry That Sent Two Emails.</strong> A transport timeout hits a tool that actually succeeded server-side. The retry fires it again. Congratulations, you’ve reinvented the classic distributed-systems duplicate write, now wearing an AI costume. Idempotency keys. Always.</p><p><strong>4. Error Messages Are Prompts.</strong> A bare KeyError: &#39;id&#39; produces a confused model. Invalid arguments: field &#39;id&#39; is required (string) produces a correct retry almost every time. Your error strings are part of your prompt, so write them like it.</p><p><strong>5. Parallel Calls, Shared Mutable State.</strong> The model emits two tool calls in one turn, both writing the same file. You run them concurrently. Order mattered. Rule of thumb: read-only tools may run in parallel, side-effecting tools run sequentially.</p><p><strong>6. The Web Page That Gave Orders.</strong> A fetch tool returns a page containing instructions. Tool output is <em>untrusted input</em>. Delimit it, never grant it authority, and gate destructive tools behind policy checks that live outside the model.</p><p><strong>7. Twin Tools.</strong> Two tools with near-identical descriptions (search_docs and find_docs) and the model ping-pongs between them. Merge them and a whole class of loops disappears.</p><h3>Benchmarks: Show Me the Numbers (Yours)</h3><p>I could paste in a table of impressive-looking percentages here. I won’t, because I haven’t run your tasks on your model, and made-up benchmarks are how engineers stop trusting each other’s blog posts. Below is the protocol and the exact harness that produces every number, so you can run it and fill in the table with results you can defend.</p><p><strong>Ground rules</strong></p><ul><li>50 to 100 tasks with programmatic success checks (tests pass, DB state matches, file diff matches). No LLM-as-judge for the headline metric.</li><li>5 runs per task per configuration. Agents are stochastic, so one run tells you nothing. Report the mean with a 95% bootstrap confidence interval.</li><li>Config <strong>A</strong> is the naive loop. Config <strong>B</strong> is the hardened harness. Same model, same prompt, same tool backends.</li><li>Inject realistic faults, seeded so A and B face the <em>identical</em> sequence: 5% transient 5xx on model calls, 10% tool timeouts, a few oversized outputs, one flaky side-effecting tool.</li></ul><p><strong>What to measure</strong></p><ul><li><strong>Task success:</strong> programmatic check passes.</li><li><strong>Latency:</strong> p50 and p95 wall-clock to a terminal state.</li><li><strong>Token cost:</strong> tokens per task, and more importantly <em>cost per successful task</em> (total spend divided by successes). A cheap agent that fails half the time isn’t cheap.</li><li><strong>Retries:</strong> transport and semantic, counted separately.</li><li><strong>Duplicate side effects:</strong> side-effecting calls executed more than once.</li><li><strong>Crash-lost steps:</strong> kill the process at a random step and count what you lose.</li></ul><p><strong>Results (fill in from your run)</strong></p><ul><li>Task success (mean, 95% CI): A [__ ± __] vs B [__ ± __]</li><li>p50 latency (s): A [__] vs B [__]</li><li>p95 latency (s): A [__] vs B [__]</li><li>Avg tokens per task: A [__] vs B [__]</li><li>Cost per successful task ($): A [__] vs B [__]</li><li>Transport retries per task: A [__] vs B [__]</li><li>Semantic retries per task: A [__] vs B [__]</li><li>Duplicate side effects: A [__] vs B [__]</li><li>Steps lost on a mid-run crash: A [__] vs B [__]</li></ul><p>My prediction, to be tested and not trusted: success improves mostly thanks to validation, errors-as-data and loop detection. Compaction shrinks tokens. Idempotency drives duplicate side effects to zero. And p50 latency barely moves, while <strong>p95 drops hard</strong>, because timeouts and loop aborts chop the long tail. The tail is where a harness earns its keep.</p><p>Here’s the harness:</p><pre># bench.py<br>import asyncio, time, random, statistics<br>from dataclasses import dataclass<br>@dataclass<br>class RunResult:<br>    success: bool<br>    latency_s: float<br>    tokens: int<br>    cost_usd: float<br>    transport_retries: int<br>    semantic_retries: int<br>    dup_side_effects: int<br>PRICE_IN, PRICE_OUT = 3.0 / 1e6, 15.0 / 1e6   # set to your model&#39;s real pricing<br>class FaultyBackend:<br>    &quot;&quot;&quot;Wraps real tools with seeded fault injection.&quot;&quot;&quot;<br>    def __init__(self, seed, p_timeout=0.10, p_oversize=0.05):<br>        self.rng = random.Random(seed)<br>        self.p_timeout, self.p_oversize = p_timeout, p_oversize<br>        self.side_effect_log = []<br>    def maybe_fault(self):<br>        r = self.rng.random()<br>        if r &lt; self.p_timeout:<br>            raise asyncio.TimeoutError()<br>        return r &lt; self.p_timeout + self.p_oversize   # True =&gt; oversize output<br>async def run_one(agent_fn, task, seed):<br>    backend = FaultyBackend(seed)<br>    t0 = time.perf_counter()<br>    outcome, state, meta = await agent_fn(task, backend)<br>    dt = time.perf_counter() - t0<br>    dup = len(backend.side_effect_log) - len(set(backend.side_effect_log))<br>    return RunResult(<br>        success=task.check(backend), latency_s=dt,<br>        tokens=state.tokens_in + state.tokens_out,<br>        cost_usd=state.tokens_in * PRICE_IN + state.tokens_out * PRICE_OUT,<br>        transport_retries=meta[&quot;transport_retries&quot;],<br>        semantic_retries=meta[&quot;semantic_retries&quot;],<br>        dup_side_effects=dup)<br>def bootstrap_ci(xs, n=2000, alpha=0.05):<br>    rng = random.Random(0)<br>    means = sorted(statistics.fmean(rng.choices(xs, k=len(xs))) for _ in range(n))<br>    return means[int(n * alpha / 2)], means[int(n * (1 - alpha / 2))]<br>async def benchmark(agent_fn, tasks, runs=5):<br>    results = []<br>    for task in tasks:<br>        for k in range(runs):<br>            results.append(await run_one(agent_fn, task, seed=hash((task.id, k))))<br>    succ = [float(r.success) for r in results]<br>    lat = sorted(r.latency_s for r in results)<br>    ok = [r for r in results if r.success]<br>    return {<br>        &quot;success&quot;: (statistics.fmean(succ), *bootstrap_ci(succ)),<br>        &quot;p50_s&quot;: lat[len(lat) // 2],<br>        &quot;p95_s&quot;: lat[int(len(lat) * 0.95)],<br>        &quot;tokens_avg&quot;: statistics.fmean(r.tokens for r in results),<br>        &quot;cost_per_success&quot;: sum(r.cost_usd for r in results) / max(1, len(ok)),<br>        &quot;transport_retries&quot;: statistics.fmean(r.transport_retries for r in results),<br>        &quot;semantic_retries&quot;: statistics.fmean(r.semantic_retries for r in results),<br>        &quot;dup_side_effects&quot;: sum(r.dup_side_effects for r in results),<br>    }</pre><p>Seed the faults per task and run index, so both configs face identical bad luck. Otherwise you’re benchmarking the dice.</p><h3>What I’d Tell Someone Starting Today</h3><ol><li><strong>Treat the model as an unreliable dependency.</strong> Timeouts, retries, validation, circuit breakers. You already know this playbook.</li><li><strong>Write error messages like prompts</strong>, because they are.</li><li><strong>Bound everything:</strong> steps, tokens, wall-clock, tool output size. The harness enforces limits. The model won’t.</li><li><strong>Checkpoint every step.</strong> Resumability is cheap on day one and brutal on day ninety.</li><li><strong>Compact payloads, keep the trail.</strong></li><li><strong>Give your failures names</strong> so you can fix the biggest one first.</li><li><strong>Measure cost per successful task</strong>, with confidence intervals, under injected faults. Demos are fault-free. Production isn’t.</li><li><strong>Treat tool output as untrusted input.</strong></li></ol><p>Swapping to a better model might buy you a few points of success rate. Fixing the harness is the difference between an agent you demoed and an agent you’d let run while you sleep.</p><p>The model gets the headline. The harness gets the pager.</p><p><em>If you’ve got your own agent-failure horror story, drop it in the comments. I’m collecting them for the next post: profiling the harness itself, and where the milliseconds go once you take the model call out of the picture.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f38805e7ae48" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[As a Developer, Please Slap Yourself If You Can’t Explain Even 10 of These Terms]]></title>
            <link>https://medium.com/@thealonemusk/as-a-developer-please-slap-yourself-if-you-cant-explain-even-10-of-these-terms-a2d941e0455d?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2d941e0455d</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Fri, 09 Oct 2026 15:06:03 GMT</pubDate>
            <atom:updated>2026-10-09T15:06:03.391Z</atom:updated>
            <content:encoded><![CDATA[<p><em>(Yes, I’m slapping myself too. Read on.)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OjQiXqi15QHT6j5SalncgQ.png" /></figure><p>A while ago, I sat in a design review where someone said, “We’ll just add read replicas, and eventual consistency will take care of the rest.”</p><p>Nobody asked what “eventual” meant. Nobody asked <em>how</em> eventual. Two weeks later, users were seeing their own payments disappear and reappear.</p><p>Most of us learn databases the way we learn city streets: enough to get to work, never enough to survive a detour. We know CREATE INDEX exists. We’ve heard “CAP theorem” in an interview. We nod when someone says “quorum.”</p><p>This article is a checklist and a mirror. Below are <strong>70 terms</strong> that sit at the core of every serious data system. If you can’t explain at least 10 of them <em>without Googling</em>, close your IDE, take a breath, and read on. If you can explain all of them, skip to the end and tell me what I got wrong.</p><p>I’ve grouped them by the problem they solve, because trivia doesn’t stick but mental models do.</p><h3>1. How Data Is Found: Indexing &amp; Storage Engines</h3><p><strong>Indexing.</strong> A separate data structure that trades write cost and space for faster reads. Without an index, a query is a full scan: O(n). With one, it’s typically O(log n) or O(1).</p><p><strong>B-tree.</strong> The default index structure in most relational databases (Postgres, MySQL/InnoDB). A balanced, wide tree where each node maps to a disk page (typically 4–16 KB). Fan-out is high, so even a billion rows fit in a tree about 4 levels deep. Updates are in-place, which means random I/O on writes.</p><p><strong>LSM tree (Log-Structured Merge tree).</strong> The opposite philosophy, used by RocksDB, Cassandra, and LevelDB. Writes go to an in-memory <strong>memtable</strong> and are flushed as immutable sorted files (<strong>SSTables</strong>). Writes are sequential and fast, but reads may check multiple files.</p><p><strong>Compaction.</strong> The background process in LSM trees that merges SSTables, drops overwritten values, and purges tombstones. It’s the hidden tax of LSM: <em>write amplification</em>. A single logical write may be rewritten 10–30 times over its life.</p><p><strong>Bloom filter.</strong> A probabilistic bitset that answers “is this key <em>definitely not</em> here, or <em>maybe</em> here?” with no false negatives. LSM trees use it to skip SSTables that can’t contain a key, saving disk reads.</p><p><strong>Composite index.</strong> An index on multiple columns, like (user_id, created_at). Column order matters: the index serves queries on the <strong>leftmost prefix</strong>. It helps WHERE user_id = ? but not WHERE created_at = ? alone.</p><p><strong>Covering index.</strong> An index that contains <em>all</em> columns a query needs, so the engine never touches the base table (an “index-only scan”). Postgres does this with INCLUDE.</p><p><strong>Secondary indexes.</strong> Indexes on non-primary-key columns. In a distributed system, they’re painful: either <strong>local</strong> (each partition indexes its own data, so queries scatter-gather) or <strong>global</strong> (one index spanning partitions, so writes become distributed transactions or lag behind).</p><p><strong>Columnar vs. row-based storage.</strong> Row stores keep all fields of a row together, which is ideal for OLTP (fetch one user). Columnar stores keep each column together, which is ideal for OLAP (average of one column over a billion rows). Columns compress well because similar values sit adjacent, and queries read only the columns they need.</p><blockquote><strong><em>Low-latency note:</em></strong><em> In C++-land, this is the same argument as AoS vs. SoA: array-of-structs vs. struct-of-arrays. It’s all about cache lines and what you drag into memory.</em></blockquote><h3>2. How the Database Decides: Query Execution</h3><p><strong>Query planner / Cost-based optimizer (CBO).</strong> SQL says <em>what</em>; the planner decides <em>how</em>. A CBO enumerates plans (index scan vs. seq scan, nested loop vs. hash join vs. merge join), estimates each plan’s cost from table <strong>statistics</strong> (row counts, value histograms), and picks the cheapest.</p><p>Stale statistics mean bad estimates, which means a bad plan. Many “the query suddenly got slow at 3 AM” incidents are a planner flipping from an index scan to a sequential scan after a data distribution shift. Run EXPLAIN ANALYZE before you blame the hardware.</p><p><strong>Normalization.</strong> Structuring data to eliminate redundancy (1NF → 2NF → 3NF → BCNF). Each fact lives in one place, so updates can’t produce inconsistencies.</p><p><strong>Denormalization.</strong> Deliberately duplicating data to avoid joins. You pay with write complexity and the risk of drift; you gain read speed. It’s a <em>performance optimization</em>, not a modeling sin, but only after you’ve measured.</p><p><strong>Materialized views.</strong> A query result stored physically and refreshed periodically or incrementally. It’s denormalization managed by the database, with the cost shifted from read time to refresh time.</p><h3>3. Transactions &amp; Isolation: Where Bugs Hide</h3><p><strong>ACID.</strong> <em>Atomicity</em> (all or nothing), <em>Consistency</em> (invariants preserved, partly the application’s job), <em>Isolation</em> (concurrent transactions don’t interfere), <em>Durability</em> (committed means it survives a crash).</p><p><strong>BASE.</strong> <em>Basically Available, Soft state, Eventually consistent.</em> It’s the philosophical counterpoint to ACID, accepting temporary inconsistency in exchange for availability and scale.</p><p><strong>Write-ahead logging (WAL).</strong> Before modifying any data page, the database appends the change to a sequential log and fsyncs it. If it crashes, it replays the log. This is how durability coexists with fast in-memory page modifications, and the same log powers replication.</p><p><strong>Checkpointing.</strong> Periodically flushing dirty pages to disk and recording a log position, so recovery only replays WAL <em>after</em> that point. Without checkpoints, recovery time grows unbounded.</p><p><strong>MVCC (Multi-Version Concurrency Control).</strong> Instead of overwriting rows, the database keeps multiple versions, each tagged with transaction IDs. Readers see a consistent version and never block writers. The cost is garbage: old versions need cleanup (Postgres VACUUM).</p><p><strong>Snapshot isolation.</strong> Each transaction sees a snapshot of the database as of its start time, usually implemented via MVCC. It prevents dirty reads, non-repeatable reads, and phantoms, but <em>still allows write skew</em>. (More on that below.)</p><h4>The Anomalies, Explained</h4><p><strong>Dirty read.</strong> Reading another transaction’s <em>uncommitted</em> data. If that transaction rolls back, you acted on data that never existed.</p><p><strong>Phantom read.</strong> You run SELECT ... WHERE age &gt; 30 twice in a transaction. Between runs, someone inserts a matching row, and a new “phantom” row appears.</p><p><strong>Read skew.</strong> You read two related values at different points in time and see an inconsistent pair. Example: you check Account A (before a transfer) and Account B (after it), and the total looks wrong.</p><p><strong>Write skew.</strong> Two transactions read overlapping data, make decisions based on it, and write to <em>different</em> rows, violating an invariant. Classic example: two on-call doctors each check “is at least one other doctor on call?”, both see yes, and both go off call. Snapshot isolation allows this. Only <strong>serializable</strong> isolation (SSI or true locking) prevents it.</p><h4>Locking</h4><p><strong>Pessimistic locking.</strong> Lock first, then work (SELECT ... FOR UPDATE). It’s safe under high contention, but it reduces concurrency.</p><p><strong>Optimistic locking.</strong> Work first, then check for conflicts at commit (usually a version column: UPDATE ... WHERE id = ? AND version = ?). It’s great under low contention, but under high contention you drown in retries.</p><p><strong>Deadlock.</strong> Transaction A holds lock 1 and wants lock 2; B holds lock 2 and wants lock 1. Databases detect cycles in the wait-for graph and kill a victim. Defense: acquire locks in a consistent global order.</p><p><strong>Lock escalation.</strong> The database promotes many fine-grained locks (rows) into one coarse lock (table) to save memory. It’s well known in SQL Server. A batch job touching 5,000+ rows can suddenly block the entire table.</p><h3>4. Replication: Copies, Lag, and Lies</h3><p><strong>Read replicas.</strong> Asynchronous copies serving read traffic. They scale reads, not writes. The catch is <strong>replication lag</strong>: a user writes, then reads from a replica that hasn’t caught up and sees stale data. The fix is “read-your-writes” routing (pin that user to the leader for a short window).</p><p><strong>Leader–Follower replication.</strong> One node accepts writes and streams changes to followers. It’s simple and avoids write conflicts. Synchronous followers give durability but hurt latency; asynchronous ones risk losing acknowledged writes on leader failure.</p><p><strong>Multi-leader replication.</strong> Multiple nodes accept writes, typically one per datacenter. It gives better write latency and tolerance of DC outages, but you inherit <strong>write conflicts</strong>. You’ll need resolution strategies: last-write-wins (lossy), CRDTs, or application-level merging.</p><p><strong>Logical vs. physical replication.</strong> <em>Physical</em> ships the raw WAL bytes, giving an exact byte-level copy that is fast but version- and architecture-coupled. <em>Logical</em> ships row-level changes (“insert this row into table X”), which are decoupled from storage format and allow selective table replication and cross-version upgrades.</p><p><strong>CDC (Change Data Capture).</strong> Streaming a database’s changes (usually by tailing the WAL, as Debezium does with Postgres) into Kafka or other consumers. It’s how you keep caches, search indexes, and warehouses in sync <em>without</em> dual writes, which are a bug factory.</p><p><strong>Geo-replication.</strong> Replicating across regions for disaster recovery and locality. The speed of light is not negotiable: cross-region synchronous writes cost ~50–200 ms per round trip.</p><h3>5. Distributed Agreement: Quorum, Consensus, and CAP</h3><p><strong>Quorum.</strong> With <em>N</em> replicas, a write needs <em>W</em> acknowledgements and a read queries <em>R</em> replicas. If <strong>R + W &gt; N</strong>, the read set and write set overlap, so at least one node has the latest value. Typical: N=3, W=2, R=2. (Caveat: sloppy quorums and clock issues can still produce stale reads.)</p><p><strong>Consensus.</strong> Getting a group of nodes to agree on a single value despite failures. <strong>Paxos</strong> and <strong>Raft</strong> are the canonical algorithms. They underpin etcd, ZooKeeper (Zab), and Consul. In Raft, a leader is elected by majority vote, and entries are committed once replicated to a majority.</p><p><strong>CAP theorem.</strong> In the presence of a <strong>P</strong>artition, you must choose between <strong>C</strong>onsistency (linearizability) and <strong>A</strong>vailability. The most common misreading is “pick 2 of 3.” Partitions aren’t optional; networks fail. The real choice is <em>during a partition</em>, and PACELC extends it: <em>else</em> (no partition), you still trade <strong>L</strong>atency vs. <strong>C</strong>onsistency.</p><p><strong>Strong consistency.</strong> Every read sees the most recent completed write, as if there were one copy of the data (linearizability). It’s easy to reason about and expensive in latency.</p><p><strong>Eventual consistency.</strong> If writes stop, all replicas <em>eventually</em> converge. It promises nothing about <em>when</em> or what you read in between. It’s cheap, scalable, and dangerous if you assume it’s “almost strong.”</p><p><strong>Split-brain.</strong> A partition causes two nodes to both believe they are the leader, and both accept writes. It leads to divergent data and is often unrecoverable without data loss. Prevent it with quorum-based election and <strong>fencing tokens</strong>.</p><p><strong>Failover.</strong> Promoting a follower when the leader dies. Hard parts: detecting failure reliably (timeouts vs. slow nodes), avoiding split-brain, and handling lost un-replicated writes.</p><p><strong>High availability (HA).</strong> Designing so the system survives component failure with minimal downtime. Measured in nines: 99.99% means about 52 minutes of downtime per year. Every nine costs exponentially more.</p><h4>Distributed Transactions</h4><p><strong>Two-phase commit (2PC).</strong> A coordinator asks all participants to <em>prepare</em> (vote), then to <em>commit</em> if all say yes. Its fatal flaw is that it’s <strong>blocking</strong>: if the coordinator crashes after participants vote yes, they’re stuck holding locks, unable to decide alone.</p><p><strong>Three-phase commit (3PC).</strong> Adds a <em>pre-commit</em> phase to avoid blocking, assuming bounded network delays and no partitions. In real, partition-prone networks it doesn’t hold up, which is why practical systems prefer consensus-based commit (Spanner uses Paxos groups with 2PC across them).</p><h3>6. Scaling &amp; Partitioning</h3><p><strong>Vertical scaling.</strong> A bigger machine. It’s simple and has no distributed complexity, but there is a hard ceiling and a single point of failure.</p><p><strong>Horizontal scaling.</strong> More machines. It’s near-unlimited, but you now own all of distributed systems.</p><p><strong>Hash partitioning.</strong> partition = hash(key) % N. It gives uniform distribution but destroys key ordering, so range queries must hit every partition.</p><p><strong>Range partitioning.</strong> Contiguous key ranges per partition. It’s great for range scans, but sequential keys (timestamps, auto-increment IDs) create hotspots.</p><p><strong>Time-series partitioning.</strong> Partitioning by time window (daily or monthly). Old partitions can be dropped in O(1) instead of a slow DELETE, and recent data stays hot and cached.</p><p><strong>Consistent hashing.</strong> Nodes and keys are placed on a ring; each key belongs to the next node clockwise. Adding or removing a node moves only ~1/N of the keys instead of nearly all of them (as % N would). <strong>Virtual nodes</strong> smooth the distribution.</p><p><strong>Rebalancing.</strong> Moving partitions between nodes when the cluster changes. Done carelessly, it saturates the network and degrades live traffic.</p><p><strong>Resharding.</strong> Changing the partitioning scheme itself (say, 16 shards to 64). It’s the operation that makes teams age visibly.</p><p><strong>Hot partition.</strong> One partition receiving disproportionate traffic (a celebrity user, a viral key). Mitigations: key salting, splitting, caching, or request coalescing.</p><p><strong>Data skew.</strong> Uneven data distribution across partitions, which gives uneven storage and load. Often caused by a poor partition key.</p><p><strong>Data locality.</strong> Keeping computation close to its data, such as same node, same rack, same region. Moving code is cheaper than moving terabytes.</p><p><strong>Federation.</strong> Splitting databases by function (users DB, orders DB, products DB) rather than by key. It reduces per-DB load but makes cross-DB joins an application concern.</p><p><strong>Load balancing.</strong> Distributing requests across servers: round-robin, least-connections, consistent hashing, or latency-aware. L4 (TCP) is faster; L7 (HTTP) is smarter.</p><p><strong>Connection pooling.</strong> Reusing a fixed set of DB connections instead of opening one per request. Postgres connections are heavyweight (one process each), so tools like PgBouncer matter. The classic bug: a pool sized larger than the DB can handle, which turns a traffic spike into an outage.</p><p><strong>Caching.</strong> Keeping hot data in a faster layer. The hard problems are <em>invalidation</em>, <em>stampedes</em> (thundering herd on expiry), and <em>consistency</em> with the source of truth.</p><h3>7. Survival Patterns: Protecting Systems Under Stress</h3><p><strong>Backpressure.</strong> When a consumer is slower than a producer, the system must <em>signal upstream to slow down</em> rather than buffering unboundedly. Unbounded queues don’t solve overload; they convert it into memory exhaustion plus latency.</p><p><strong>Throttling.</strong> Slowing or deferring requests to protect resources, usually applied by the <em>server</em> reacting to load.</p><p><strong>Rate limiting.</strong> Enforcing a policy limit per client or key (100 req/sec). Common algorithms: token bucket, leaky bucket, sliding window. In distributed setups, the counter itself becomes a consistency problem.</p><p><strong>Circuit breaker.</strong> After repeated failures calling a dependency, “open” the circuit and fail fast instead of piling up timeouts. Periodically allow a probe request (<em>half-open</em>) to test recovery. It prevents cascading failure.</p><p><strong>Idempotency.</strong> An operation that can be applied multiple times with the same result as once. Because networks make retries inevitable, every payment, order, or message handler needs idempotency keys. “Exactly-once” delivery is a myth; <strong>at-least-once plus idempotency</strong> is the reality.</p><h3>8. Data Evolution &amp; Analytics</h3><p><strong>Schema evolution.</strong> Changing data structures without breaking producers or consumers. Rules of thumb: additive changes are safe, removing or renaming fields is not. Use the expand/contract pattern.</p><p><strong>Schema registry.</strong> A central service (Confluent’s, for example) storing versioned schemas (Avro/Protobuf) and enforcing compatibility rules (backward, forward, full) before a producer can publish.</p><p><strong>Data migration.</strong> Moving data between systems or schemas with zero downtime. The standard playbook is dual write, backfill, verify, cut over reads, then retire the old path, with the ability to roll back at every stage.</p><p><strong>Data warehouse.</strong> Structured, modeled, query-optimized storage for analytics (Snowflake, BigQuery, Redshift). Schema-on-write.</p><p><strong>Data lake.</strong> Raw data in cheap object storage (S3) in open formats like Parquet. Schema-on-read. Flexible, but it can become a swamp without governance. The <strong>lakehouse</strong> (Delta, Iceberg, Hudi) tries to get the best of both.</p><h3>The Slap Test</h3><p>Count honestly:</p><ul><li><strong>0–10 explained:</strong> You’re operating on vibes. Pick three sections above and go deep.</li><li><strong>10–30:</strong> Solid mid-level. Your gap is probably in isolation levels and distributed agreement.</li><li><strong>30–55:</strong> You’ve been paged at 3 AM and learned from it.</li><li><strong>55–70:</strong> Write your own article. I’ll read it.</li></ul><p>What matters isn’t memorizing definitions. It’s being able to answer the <em>follow-up</em>. Not “what is eventual consistency?” but “what does the user see in the 200 ms after they click Pay, and what happens if the replica is 4 seconds behind?”</p><p>The best engineers I’ve worked with don’t know more buzzwords. They know <strong>what breaks, when, and why</strong>.</p><p>So go ahead: pick the term you flinched at while reading, and spend this weekend on it. Read the paper, build a toy version, break it on purpose.</p><p><em>If this made you slap yourself, a clap and a follow go a long way. And tell me in the comments which term got you.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2d941e0455d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Jev Doesn’t Chat. That Might Be the Point.]]></title>
            <link>https://medium.com/@thealonemusk/jev-doesnt-chat-that-might-be-the-point-2f12663c661f?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f12663c661f</guid>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[jevs]]></category>
            <category><![CDATA[inference]]></category>
            <category><![CDATA[agentic-ai-architecture]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Fri, 18 Sep 2026 18:43:33 GMT</pubDate>
            <atom:updated>2026-09-18T18:47:05.467Z</atom:updated>
            <content:encoded><![CDATA[<p>Not another AI launch recap. This one pulls apart TypeSafe’s “System One Model” claim-by-claim — the architecture, the economics, and the HN thread that poked holes in the marketing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0-D3Ied-psYthZzWJZsOag.png" /><figcaption>AI generated Image</figcaption></figure><h3>Every AI launch this year promises the same thing: more reasoning, longer context, better code.</h3><p>On September 15, 2026, Diogo Almeida a name attached to the InstructGPT paper, the research that taught ChatGPT to behave like an assistant — shipped something aimed at a completely different bottleneck. His new company, TypeSafe AI, came out of two years of stealth with a model that cannot write a sentence.</p><p>No chat box. No completions. You send it a blob of state — a support ticket, a game frame, a JSON object describing an account’s login history — plus a set of typed questions you define in advance. It sends back typed answers: a probability, a choice from your enum, a score on your rubric. Each with a confidence value attached. That’s the entire interface.</p><blockquote>“Think of Jev as a frontier-intelligence function call: unstructured state in, typed probabilistic decisions out.”</blockquote><p>Let’s actually pull apart whether that claim holds up.</p><h3>1. The Naming Is Doing More Work Than It Looks Like</h3><p>TypeSafe calls this new category a <strong>System One Model</strong>, and Jev is the first one. The name is a Kahneman reference — fast, intuitive System 1 thinking versus slow, deliberate System 2 reasoning. It’s a quiet dig at the rest of the industry: you don’t need a $10/MTok reasoning model doing full chain-of-thought to decide whether a support ticket is urgent. You need a classifier that’s honest about its confidence.</p><p>“Jev” itself is named after William Stanley Jevons, the economist behind the <strong>Jevons paradox</strong> — the idea that making a resource radically cheaper doesn’t reduce total consumption, it explodes it, by unlocking use cases that weren’t viable at the old price. TypeSafe’s bet: “smart if-statement” calls — classify, route, score, extract, escalate — are currently too slow and expensive to sprinkle liberally through production code. Drop the cost two orders of magnitude, and you don’t just save money on the calls you’re already making — you change what people are willing to build.</p><h3>2. What’s Actually New Under the Hood</h3><p>Three architectural claims are doing the real work here.</p><p><strong>Parallel, not autoregressive, sampling.</strong> Regular LLMs generate token-by-token, each one conditioned on the last — which is why a single call can take anywhere from a few seconds to several minutes on a reasoning model. Jev evaluates every question against the state in a single parallel pass. TypeSafe’s own numbers: <strong>70–500ms end-to-end</strong>, versus 3 to 329 seconds for comparable frontier-model calls. That’s where the “20–200x faster” figure comes from.</p><p><strong>RLCD instead of RLHF/RLVR.</strong> Standard post-training optimizes for what human raters prefer (RLHF) or what can be programmatically verified (RLVR). TypeSafe’s <strong>Reinforcement Learning for Calibrated Decisions</strong> optimizes for something narrower: probabilities that are actually calibrated, meaning a 90%-confidence answer is right roughly 90% of the time.</p><blockquote>“A model can do a task 95% of the time — but if it doesn’t say when it’s in the 5%, it can’t actually be automated.”</blockquote><p><strong>A closed output space, by construction.</strong> Because Jev never generates free text, it structurally cannot produce a malformed answer. TypeSafe’s “zero type errors” number isn’t measured — it’s guaranteed by the architecture, the same way a switch statement can&#39;t return a value outside its cases.</p><p>Pricing follows the same logic:</p><pre>Input:  $0.042 / million tokens  <br>Output: free (nothing gets generated)</pre><pre>1M calls × 1,000 input tokens avg ≈ $42</pre><p>Cheap enough that a moderation layer, a fraud check, or an agent-output verifier stops being something you ration and starts being something you run on every request by default.</p><h3>3. The Evidence — Read the Fine Print</h3><p>TypeSafe deserves some credit here: their launch post publishes its own weaknesses next to its wins, which is rarer than it should be. Their workflow evals compare Jev against the <em>average</em> of GPT-6 Astra and Fable 5.1 acting as reference labels, and Jev reportedly sits on the Pareto frontier for speed and cost across close to two orders of magnitude, while landing close on accuracy for narrow, structured tasks.</p><p>“Close on accuracy for narrow, structured tasks” is carrying a lot of weight in that sentence — and the days since launch have been mostly people pulling on that thread.</p><p><strong>The “frontier model” framing is contested.</strong> The most repeated independent pushback: calling Jev a frontier model borrows credibility it hasn’t independently earned — it can’t code, chat, or write a sentence — so putting it on a chart next to GPT-6 implicitly accepts frontier LLMs as the accuracy baseline while quietly changing what’s being measured. The fairer claim, made across several outlets covering the launch, is that TypeSafe genuinely pushed out the speed-and-cost frontier for structured decisions. That’s real. It’s a different thing from rivaling a frontier LLM’s intelligence.</p><p><strong>The benchmarks are vendor-graded homework.</strong> Every number in the launch post — latency, efficiency gains, workflow-eval charts — comes from TypeSafe’s own harness, on their own machines, against reference labels generated by other labs’ models rather than ground truth. No independent benchmark existed in the days after launch, and access is still waitlist-gated.</p><p><strong>“Can’t hallucinate” is narrower than the headline.</strong> TypeSafe is actually careful about this in their own FAQ. Jev can’t hallucinate <em>text</em> because it emits no text — the output is drawn from a closed schema, so a malformed response is structurally impossible. That says nothing about whether the <em>chosen</em> answer is correct. Almeida has reportedly conceded this directly: confident-and-wrong remains possible for any model, present or future.</p><p><strong>The Doom demo is real, but narrower than the clip suggests.</strong> The widely-shared real-time Doom-playing demo runs on structured text state — enemy coordinates, distances, angles fed in as JSON — not pixels. That’s a legitimate demonstration of low-latency decision-making under time pressure. It’s not evidence of general game-playing intelligence, since the model effectively has information (exact coordinates, no occlusion) a human fighting through pixels wouldn’t have.</p><p><strong>Adversarial input is the open question that actually matters.</strong> One early integration project — a moderation bot swapping OpenAI’s moderation endpoint for Jev — flagged something worth sitting with:</p><blockquote>“Jev reads message text literally and doesn’t treat it as hostile, so a message can try to talk the model out of a flag.”</blockquote><p>For a chat assistant, prompt-injection resistance is a nice-to-have safety layer. For a decision layer sitting in front of routing, fraud scoring, or moderation — exactly the workloads TypeSafe is pitching Jev for — an unadversarial reading of untrusted input is closer to a core requirement than an edge case.</p><h3>4. Where This Actually Fits</h3><p>Strip away the “frontier” framing and the comparison charts, and the underlying shape is genuinely interesting: Jev isn’t competing with GPT-6 or Claude. It’s competing with the <em>pattern</em> of using a full LLM call as a classifier — which is what a huge amount of “AI-powered” production code already does, at 3-to-300-second latency and RLHF-shaped confidence nobody should actually trust.</p><p>Worth testing against your own workload if you’re building:</p><ul><li>a support router that classifies tickets and pulls structured fields before a human or another model touches them</li><li>a fraud or account-risk scorer that needs a confidence value to decide auto-approve vs. escalate-to-human</li><li>an agent-output verifier checking whether a completed run actually satisfied the task, at agent scale</li></ul><p>Wrong shelf entirely if you’re looking for something to replace a chatbot, an agent, or anything requiring open-ended reasoning or explanation. It was never built to be there, and TypeSafe hasn’t claimed otherwise.</p><h3>Closing Thought</h3><p>Jev is a legitimate architectural bet, dressed in launch-post marketing that oversells it. The speed and cost numbers appear to hold up to the limited, waitlist-gated scrutiny available so far. The “frontier intelligence” framing does not hold up nearly as well — several outlets that ran with the comparison charts uncritically have already had to walk it back.</p><p>The more interesting question isn’t whether Jev is a frontier model. It isn’t, and it doesn’t need to be. It’s whether <strong>“a really smart switch statement,”</strong> as one widely-shared explainer thread put it, is a big enough idea to justify an entirely new model class. Given how much production AI spend right now goes toward using a $10/MTok reasoning model to answer “is this urgent: yes or no” — the answer might genuinely be yes.</p><p><em>Next up: I’ll try wiring Jev into an actual workload — a ticket router — and see if the 500ms and $42/million-calls numbers survive contact with something real.</em></p><p>AI TypeSafe Jev Machine Learning Software Architecture</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f12663c661f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SOLID, KISS, DRY, YAGNI: The Design Principles Behind Every Pattern]]></title>
            <link>https://medium.com/@thealonemusk/solid-kiss-dry-yagni-the-design-principles-behind-every-pattern-36cbcfd5ffdb?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/36cbcfd5ffdb</guid>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[programming-interviews]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Sat, 29 Aug 2026 13:51:50 GMT</pubDate>
            <atom:updated>2026-10-09T17:46:48.986Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design patterns get all the attention, but principles are what actually decide whether your code ages well. Here’s the deep dive, interview-style.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qAzZSHTqEUxvMsUVHXJSEA.png" /></figure><p>Design patterns are solutions to recurring problems. <strong>Principles</strong> are the reasoning that tells you <em>whether a pattern is the right solution at all</em> — and they come up in interviews just as often, usually as the harder, more open-ended half of a design conversation (“walk me through what’s wrong with this class”).</p><p>This article covers the principles that actually get asked about in interviews: the <strong>SOLID</strong> five, plus <strong>DRY</strong>, <strong>KISS</strong>, <strong>YAGNI</strong>, and <strong>Law of Demeter</strong>. For each, the same structure as before:</p><ul><li><strong>What it actually means</strong> (not the acronym-expansion version)</li><li><strong>A concrete before/after in C++</strong></li><li><strong>The interview question that gets asked</strong>, with a strong answer</li><li><strong>The follow-up trap</strong> that tests whether you understand the principle’s <em>limits</em>, not just its definition</li></ul><h3>Part 1: SOLID</h3><h3>1. Single Responsibility Principle (SRP)</h3><p><strong>What it actually means:</strong> Often misquoted as “a class should do one thing.” The more precise formulation (Robert Martin’s later phrasing) is: <strong>a class should have only one reason to change</strong> — one axis of responsibility, tied to one actor or stakeholder who could request a change to it.</p><pre>// Violates SRP — two reasons to change: business rules AND persistence format<br>class Invoice {<br>public:<br>    double calculateTotal() { /* business logic */ return 0.0; }<br>    void saveToDatabase() { /* SQL, connection handling */ }<br>    std::string renderAsPdf() { /* PDF formatting */ }<br>};<br><br>// Fixed - each class has exactly one reason to change<br>class Invoice {<br>public:<br>    double calculateTotal() { /* business logic only */ return 0.0; }<br>};<br>class InvoiceRepository {<br>public:<br>    void save(const Invoice&amp; inv) { /* persistence only */ }<br>};<br>class InvoicePdfRenderer {<br>public:<br>    std::string render(const Invoice&amp; inv) { /* formatting only */ return &quot;&quot;; }<br>};</pre><p><strong>Interview question:</strong> <em>“Isn’t ‘one reason to change’ subjective? How do you actually apply this when reviewing a PR?”</em></p><p><strong>Strong answer:</strong> It is somewhat subjective at the margins, which is why the “one reason to change” framing is more useful than “does one thing” — ask <em>who</em> would request a change to this class. If the answer spans multiple stakeholders (a DBA wants to change the storage schema, a finance person wants to change tax calculation, a designer wants to change PDF layout), that’s your signal the class is doing too much, regardless of line count. In review, a practical heuristic: if you can’t summarize a class’s purpose in one sentence without using “and,” it’s a candidate for splitting.</p><p><strong>Follow-up trap:</strong> <em>“Can you over-apply SRP? What does that look like?”</em></p><p>Good answer: Yes — taken too far, SRP produces a swarm of tiny classes each wrapping a single method, which increases indirection without increasing clarity, and makes the actual flow of a use case hard to trace across a dozen files. SRP is about cohesion, not about minimizing class size. A class with five tightly related methods that all serve one responsibility and always change together is <em>more</em> SRP-compliant than five classes that must always be modified in lockstep.</p><h3>2. Open/Closed Principle (OCP)</h3><p><strong>What it actually means:</strong> Software entities should be <strong>open for extension, but closed for modification</strong> — you should be able to add new behavior without editing existing, tested code.</p><pre>// Violates OCP — adding a new shape means editing this function<br>double area(Shape shape) {<br>    if (shape.type == ShapeType::Circle) return 3.14159 * shape.radius * shape.radius;<br>    if (shape.type == ShapeType::Square) return shape.side * shape.side;<br>    // every new shape = another branch here, risking regressions in existing branches<br>}<br><br>// Fixed - new shapes extend, never modify, existing code<br>class Shape {<br>public:<br>    virtual double area() const = 0;<br>    virtual ~Shape() = default;<br>};<br>class Circle : public Shape {<br>public:<br>    double area() const override { return 3.14159 * r_ * r_; }<br>private:<br>    double r_;<br>};<br>// Adding Triangle later: new class, zero changes to Shape or existing shapes.</pre><p><strong>Interview question:</strong> <em>“OCP sounds great in theory. What’s the actual cost of designing for it upfront, and when is that cost not worth paying?”</em></p><p><strong>Strong answer:</strong> Designing for extension upfront means introducing abstraction — interfaces, virtual dispatch, indirection — before you know the actual shape of future extension. If that guess is wrong (you build for extension along axis A, but the real future need is axis B), you’ve paid abstraction cost for nothing and still have to modify code anyway. This is why OCP is best applied <em>retroactively</em>, the first time you actually need a second variant — not speculatively for a single, currently-only implementation. Uncle Bob himself has walked back the “design for every possible future change” reading in favor of “let the pattern of actual changes guide where you add extension points.”</p><p><strong>Follow-up trap:</strong> <em>“Give a concrete example where following OCP made a codebase worse, not better.”</em></p><p>Good answer: A team builds a PaymentProcessor with a strategy interface anticipating &quot;we might support many payment providers&quot; — but the product only ever ships with one (Stripe) for its entire life. The interface, factory, and DI wiring add three files and a layer of indirection for a &quot;family&quot; of exactly one implementation, making the code harder to navigate for zero realized benefit. This is a textbook case of premature OCP — the fix would have been a concrete StripePaymentProcessor class, refactored into an interface <em>only</em> when a second provider was actually being added.</p><h3>3. Liskov Substitution Principle (LSP)</h3><p><strong>What it actually means:</strong> Subtypes must be substitutable for their base types <strong>without altering the correctness of the program</strong> — a subclass shouldn’t just have the same method signatures, it must honor the same <em>behavioral contract</em> (preconditions, postconditions, invariants).</p><pre>// The classic violation: Square &quot;is-a&quot; Rectangle mathematically,<br>// but not behaviorally, once you allow independent width/height mutation.<br>class Rectangle {<br>public:<br>    virtual void setWidth(int w) { width_ = w; }<br>    virtual void setHeight(int h) { height_ = h; }<br>    int area() const { return width_ * height_; }<br>protected:<br>    int width_ = 0, height_ = 0;<br>};<br><br>class Square : public Rectangle {<br>public:<br>    void setWidth(int w) override { width_ = height_ = w; }   // breaks caller expectations<br>    void setHeight(int h) override { width_ = height_ = h; }<br>};<br>void resize(Rectangle&amp; r) {<br>    r.setWidth(5);<br>    r.setHeight(10);<br>    assert(r.area() == 50);  // FAILS for Square - LSP violated<br>}</pre><p><strong>Interview question:</strong> <em>“Square-is-a-Rectangle is the textbook example. Give me a realistic LSP violation you’d actually catch in a code review — not the geometry one.”</em></p><p><strong>Strong answer:</strong> A very common real-world case: ReadOnlyCollection inheriting from a mutable Collection base and throwing UnsupportedOperationException (or its equivalent) from add()/remove(). Any code that&#39;s written against the base Collection type and calls .add() — perfectly valid per the base contract — now crashes at runtime for a subtype that&#39;s supposed to be substitutable. This is exactly LSP violated: the subtype narrows the postconditions the base type promised (in this case, &quot;adding an element succeeds&quot;). The fix is usually to invert the hierarchy — have a narrow ReadableCollection interface that Collection extends, rather than making read-only a &quot;restricted&quot; subtype of mutable.</p><p><strong>Follow-up trap:</strong> <em>“How does LSP relate to preconditions and postconditions specifically — what’s the rule for each?”</em></p><p>Good answer: A subclass may <strong>weaken preconditions</strong> (accept a broader range of valid inputs than the base class required — that’s still compatible, since anything valid for the base is still valid) but must <strong>not strengthen preconditions</strong> (rejecting inputs the base class accepted breaks callers). Conversely, a subclass may <strong>strengthen postconditions</strong> (guarantee more than the base promised) but must <strong>not weaken postconditions</strong> (promise less than callers were told to expect, like Square::setWidth silently also changing height, which is an <em>undocumented side effect</em> the base contract never mentioned).</p><h3>4. Interface Segregation Principle (ISP)</h3><p><strong>What it actually means:</strong> Clients shouldn’t be forced to depend on methods they don’t use — many small, focused interfaces beat one large, general-purpose one.</p><pre>// Violates ISP — a Robot doesn&#39;t eat, but is forced to implement eat()<br>class Worker {<br>public:<br>    virtual void work() = 0;<br>    virtual void eat() = 0;<br>    virtual ~Worker() = default;<br>};<br><br>class RobotWorker : public Worker {<br>public:<br>    void work() override { /* ... */ }<br>    void eat() override { throw std::logic_error(&quot;Robots don&#39;t eat&quot;); }  // red flag<br>};<br>// Fixed - segregated interfaces, implement only what applies<br>class Workable { public: virtual void work() = 0; virtual ~Workable() = default; };<br>class Eatable  { public: virtual void eat() = 0;  virtual ~Eatable() = default; };<br>class HumanWorker : public Workable, public Eatable { /* implements both, correctly */ };<br>class RobotWorker  : public Workable                { /* implements only work() */ };</pre><p><strong>Interview question:</strong> <em>“Where’s the line between good interface segregation and interface explosion — 15 tiny single-method interfaces that are a pain to wire up?”</em></p><p><strong>Strong answer:</strong> Segregate along <strong>client usage patterns</strong>, not along “one method per interface” as a rule of thumb. If two methods are <em>always</em> needed together by every client that needs either one, keeping them on the same interface is fine and doesn’t violate ISP — the principle is about not forcing <em>unrelated</em> capabilities on a client, not about atomizing every interface to a single method. In practice: look at your actual call sites. If every implementer either needs the full interface or none of it, ISP isn’t being violated even if the interface has 5 methods. If implementers regularly need 2 of 8 methods and throw/stub the rest, that’s the smell ISP is meant to catch.</p><p><strong>Follow-up trap:</strong> <em>“You’ve got a </em><em>RobotWorker that stubs </em><em>eat() by throwing. Besides splitting the interface, what&#39;s a way to detect this smell automatically, in code review or static analysis?&quot;</em></p><p>Good answer: A grep-able smell: search the codebase for method bodies that just throw, assert(false), return null, or have a // TODO: not applicable comment — these are almost always ISP violations in disguise (or LSP violations, since a throwing override also breaks substitutability, as discussed above). Some teams add a lint rule flagging any override whose entire body is a single throw statement, specifically to surface this pattern for review.</p><h3>5. Dependency Inversion Principle (DIP)</h3><p><strong>What it actually means:</strong> High-level modules shouldn’t depend on low-level modules — <strong>both should depend on abstractions</strong>. And abstractions shouldn’t depend on details; details should depend on abstractions. This is the principle that makes dependency injection possible, not the same thing as DI itself.</p><pre>// Violates DIP — high-level PaymentService directly depends on the concrete, low-level MySQLDatabase<br>class MySQLDatabase {<br>public:<br>    void save(const std::string&amp; record) { /* mysql-specific code */ }<br>};<br><br>class PaymentService {<br>public:<br>    void processPayment() {<br>        db_.save(&quot;payment record&quot;);  // tightly coupled to MySQL specifically<br>    }<br>private:<br>    MySQLDatabase db_;<br>};<br>// Fixed - both depend on an abstraction<br>class Database {<br>public:<br>    virtual void save(const std::string&amp; record) = 0;<br>    virtual ~Database() = default;<br>};<br>class MySQLDatabase : public Database {<br>public:<br>    void save(const std::string&amp; record) override { /* mysql-specific code */ }<br>};<br>class PaymentService {<br>public:<br>    explicit PaymentService(Database&amp; db) : db_(db) {}  // depends on the interface<br>    void processPayment() { db_.save(&quot;payment record&quot;); }<br>private:<br>    Database&amp; db_;<br>};</pre><p><strong>Interview question:</strong> <em>“Is DIP the same thing as Dependency Injection? People use the terms interchangeably.”</em></p><p><strong>Strong answer:</strong> No, and this distinction is a good signal of depth in an interview. DIP is the <strong>design principle</strong>: high-level and low-level modules should both depend on abstractions, inverting the “naive” dependency direction where high-level code depends directly on low-level details. Dependency Injection is one <strong>technique</strong> for satisfying DIP — passing dependencies in via constructor/setter rather than having a class construct its own dependencies. You can follow DIP without a DI framework (manual constructor injection, as shown above, is enough), and you can use DI mechanically without actually achieving DIP (e.g., injecting a concrete MySQLDatabase instead of an abstraction — that&#39;s DI without the inversion).</p><p><strong>Follow-up trap:</strong> <em>“Where does the abstraction — the </em><em>Database interface — actually live? Who owns it, PaymentService&#39;s module or MySQLDatabase&#39;s module?&quot;</em></p><p>Good answer: This is the crux of DIP that’s often missed. The abstraction should be owned by the <strong>high-level module</strong> (or a neutral layer above both), not by the low-level module. Database should live alongside PaymentService, defined in terms of what the high-level policy needs — not alongside MySQLDatabase, defined in terms of what MySQL happens to offer. This is what actually &quot;inverts&quot; the dependency: in a naive design, low-level details dictate the interface; under true DIP, high-level policy dictates the interface, and low-level details must conform to it. Get this backwards and you&#39;ve built DI plumbing without DIP&#39;s actual benefit — the interface still leaks low-level assumptions.</p><h3>Part 2: The Other Classics</h3><h3>6. DRY — Don’t Repeat Yourself</h3><p><strong>What it actually means:</strong> <em>“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”</em> Critically — this is about <strong>knowledge duplication</strong>, not <strong>code that merely looks similar</strong>. This distinction is the single most common way DRY is misapplied.</p><p><strong>Interview question:</strong> <em>“Two functions have identical-looking code but represent different business rules that happen to coincide right now. Do you DRY them up into one shared function?”</em></p><p><strong>Strong answer:</strong> No — and this is the sharpest test of whether someone actually understands DRY versus pattern-matching on “duplicate code = bad.” If calculateShippingDiscount() and calculateLoyaltyDiscount() both currently compute price * 0.1, that&#39;s <strong>coincidental duplication</strong>, not knowledge duplication — they represent two independent business rules that happen to have the same value today. Merging them into a shared calculateDiscount() creates a false coupling: when Product asks to change the loyalty discount to 15%, you either break shipping discount too, or you&#39;re back to splitting the &quot;shared&quot; function apart under time pressure. DRY targets duplicated <em>concepts</em>, not duplicated <em>text</em>. A useful test: &quot;if I had to change this business rule, would I want the other one to change too?&quot; If no, don&#39;t merge them, no matter how identical the code looks today.</p><p><strong>Follow-up trap:</strong> <em>“What’s the practical failure mode of over-applying DRY, and what do people call the resulting anti-pattern?”</em></p><p>Good answer: Sometimes called “premature abstraction” or, more pointedly, <strong>DRY-driven coupling</strong> — collapsing superficially similar code into one shared function/class that now has multiple unrelated callers, each needing slightly different behavior. Over time this function accumulates conditional flags (isShipping ? ... : ...) to serve all callers, becoming harder to understand than the original &quot;duplicated&quot; version ever was. Sandi Metz&#39;s framing is useful here: prefer the &quot;wrong abstraction&quot; pain of some duplication over the &quot;wrong abstraction&quot; pain of a shared function that&#39;s grown a maze of special cases — duplication is far cheaper to fix later than an incorrect abstraction that many callers now depend on.</p><h3>7. KISS — Keep It Simple, Stupid</h3><p><strong>What it actually means:</strong> Systems work best when kept simple; unnecessary complexity should be avoided — but “simple” needs a precise definition, or the principle becomes an excuse to under-engineer.</p><p><strong>Interview question:</strong> <em>“‘Simple’ is doing a lot of work in KISS. How do you define simple in a way that’s actually actionable, not just a vibe?”</em></p><p><strong>Strong answer:</strong> Rich Hickey’s distinction between <strong>“simple” and “easy”</strong> is the most rigorous framing here: simple means <em>not compound</em> — few interleaved concerns, low essential complexity, easy to reason about in isolation. Easy means <em>familiar or close at hand</em> — using an ORM because you already know it, even if it hides significant complexity underneath. These aren’t the same, and conflating them is exactly how KISS gets misapplied: people choose the “easy” (familiar) tool and call it “simple,” when it may have quietly imported a large surface area of hidden behavior. A genuinely simple design has few moving parts that interact in few ways — regardless of whether it’s the path of least resistance to write today.</p><p><strong>Follow-up trap:</strong> <em>“When does pursuing KISS conflict with SOLID — for instance, OCP or DIP — and how do you resolve that tension?”</em></p><p>Good answer: Directly and often. Adding an interface + DI for a single, currently-only implementation is <em>more</em> moving parts (an abstraction, a factory or injection point, an implementation) than just calling the concrete class directly — SOLID’s OCP/DIP push toward more indirection <em>in anticipation of</em> future change, while KISS pushes toward the minimum structure that solves <em>today’s</em> problem. The resolution most experienced engineers land on: apply SOLID’s abstractions <strong>when a second concrete case actually shows up</strong>, not speculatively — this keeps you KISS-compliant right up until the moment OCP/DIP’s benefit becomes real rather than hypothetical. This is the same “extract on second occurrence” instinct that resolves the DRY-vs-premature-abstraction tension above — it’s a recurring theme, not a coincidence.</p><h3>8. YAGNI — You Aren’t Gonna Need It</h3><p><strong>What it actually means:</strong> Don’t implement functionality on the speculation that you might need it later — build for the requirements you actually have.</p><p><strong>Interview question:</strong> <em>“How is YAGNI different from just being lazy or under-engineering? Where’s the line?”</em></p><p><strong>Strong answer:</strong> YAGNI isn’t “don’t think ahead” — it’s “don’t <em>build</em> ahead.” The distinction is between <strong>design decisions that keep future options open cheaply</strong> (using an interface where a concrete class would do, if it costs almost nothing — arguably not even a YAGNI violation) versus <strong>building actual unused functionality</strong> (a full plugin system, configurable strategies for three algorithms when you use one, a generic EventBus for events that don&#39;t exist yet). The former is inexpensive foresight; the latter is speculative work that (a) may guess wrong about the actual future shape of the requirement, and (b) adds real maintenance and cognitive cost <em>today</em>, for a &quot;later&quot; that may never arrive. The test: &quot;am I writing code to solve a problem I have, or a problem I&#39;m imagining I might have?&quot;</p><p><strong>Follow-up trap:</strong> <em>“Product tells you ‘we’ll definitely need multi-tenancy in 6 months, but not for this release.’ Does YAGNI mean you ignore that entirely?”</em></p><p>Good answer: No — this is the nuance that separates a good answer from a rote “just don’t build it” answer. YAGNI applies to <em>implementing</em> speculative functionality now, not to <em>ignoring known future requirements when they affect present decisions that are expensive to reverse</em>. If a known-but-not-yet-needed requirement (multi-tenancy) would be catastrophically expensive to retrofit later — e.g., it changes your primary key/data model shape — it’s reasonable to make <em>cheap, low-cost accommodations now</em> (like including a tenant_id column you don&#39;t populate meaningfully yet) without building the actual multi-tenant <em>behavior</em>. YAGNI says don&#39;t build the feature; it doesn&#39;t say be reckless about decisions with high reversal cost, when you have credible, concrete information about what&#39;s coming.</p><h3>9. Law of Demeter (Principle of Least Knowledge)</h3><p><strong>What it actually means:</strong> A method should only talk to its <strong>immediate friends</strong> — its own fields, its parameters, objects it creates, and its direct component objects — not to objects obtained <em>through</em> those objects. Informally: “don’t talk to strangers,” or “one dot” (with caveats for fluent APIs).</p><pre>// Violates Law of Demeter — reaching through Customer -&gt; Wallet -&gt; Money<br>void chargeCustomer(Customer&amp; customer, double amount) {<br>    if (customer.getWallet().getMoney().getBalance() &gt;= amount) {  // train wreck<br>        customer.getWallet().getMoney().subtract(amount);<br>    }<br>}<br><br>// Fixed - Customer exposes behavior, not internal structure<br>class Wallet {<br>public:<br>    bool canAfford(double amount) const { return balance_ &gt;= amount; }<br>    void charge(double amount) { balance_ -= amount; }<br>private:<br>    double balance_;<br>};<br>class Customer {<br>public:<br>    bool canAfford(double amount) const { return wallet_.canAfford(amount); }<br>    void charge(double amount) { wallet_.charge(amount); }<br>private:<br>    Wallet wallet_;<br>};<br>void chargeCustomer(Customer&amp; customer, double amount) {<br>    if (customer.canAfford(amount)) customer.charge(amount);  // one dot, talks to a friend only<br>}</pre><p><strong>Interview question:</strong> <em>“Doesn’t strict Law of Demeter just mean you write a ton of pass-through wrapper methods (</em><em>Customer::canAfford just calling </em><em>Wallet::canAfford)? Isn&#39;t that its own kind of bloat?&quot;</em></p><p><strong>Strong answer:</strong> Yes, taken rigidly, this is exactly the criticism the Law of Demeter draws, and it’s a fair one. The pattern above only pays off if Customer::canAfford represents genuine <strong>behavior delegation</strong> — Customer is exposing a capability, not just mechanically forwarding a getter chain. If every single method on an inner object needs a matching wrapper on the outer object &quot;just in case,&quot; you get a bloated, low-value facade. The principle is best applied selectively, at genuine module/layer boundaries (don&#39;t let calling code reach through your domain object into a third-party library&#39;s internals, for example) rather than mechanically at every object composition in the codebase. Martin Fowler himself has noted the &quot;one dot&quot; heuristic is a simplification and shouldn&#39;t be applied as a hard style-checker rule.</p><p><strong>Follow-up trap:</strong> <em>“Does calling </em><em>.getWallet().getBalance() really matter if </em><em>Wallet is a stable, simple value-object with no real internal logic? Is LoD worth applying there?&quot;</em></p><p>Good answer: This is the key nuance — Law of Demeter is really about <strong>coupling to structure that might change</strong>, not dots-on-a-line as a syntactic rule. If Wallet is a stable, near-immutable value type (like reaching into a std::pair or a simple DTO) unlikely to ever change its internal shape, chaining through it costs little and doesn&#39;t meaningfully increase fragility. LoD earns its value protecting against reaching through <strong>objects whose internal structure is likely to change</strong> — domain objects with evolving relationships — where a chain of getters means every caller breaks the moment that internal structure is refactored. Applying it uniformly to every object graph, including stable value types, is over-application of the same kind KISS and DRY warn against above.</p><h3>Closing thoughts</h3><p>Notice the thread running through nearly every “follow-up trap” question above: <strong>every principle has a counter-force that keeps it honest.</strong> OCP’s cost is DIP’s abstraction tax. DRY’s cost is coupling unrelated concepts. KISS pulls against SOLID’s future-proofing. YAGNI pulls against principled foresight. Even Law of Demeter, applied without judgment, produces its own bloat.</p><p>This is the actual signal senior interviewers are listening for — not whether you can define SOLID, but whether you instinctively reach for the <em>opposing</em> principle when one is being over-applied. Junior answers state the principle. Senior answers state the principle, then immediately name its limit.</p><p><em>Part of an ongoing series on software design fundamentals — see also the companion piece on the [Gang of Four design patterns and their interview questions].</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36cbcfd5ffdb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gang of Four patterns: Design Patterns Every Engineer Should Actually Understand (Not Just…]]></title>
            <link>https://medium.com/@thealonemusk/gang-of-four-patterns-design-patterns-every-engineer-should-actually-understand-not-just-5ed373f14dab?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ed373f14dab</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[programming-interviews]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Sat, 29 Aug 2026 13:51:11 GMT</pubDate>
            <atom:updated>2026-10-09T17:57:58.143Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Gang of Four patterns: Design Patterns Every Engineer Should Actually Understand (Not Just Memorize)</strong></h3><h3>A deep dive into the classic Gang of Four patterns with the interview questions that separate people who’ve used them from people who’ve only read about them</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3EoWma1thT-SaDPxWxIakw.png" /></figure><p>Every engineer has, at some point, crammed a list of 23 design patterns the night before a system design interview. Most forget them a week later — not because the patterns are hard, but because they were learned as trivia instead of as <em>solutions to specific pain</em>.</p><p>This article takes the opposite approach. For each pattern, we’ll cover:</p><ul><li><strong>The actual problem it solves</strong> (not the textbook definition)</li><li><strong>A minimal, correct implementation</strong> (C++, since that’s where these patterns show their teeth — RAII, ownership, and object lifetime all matter)</li><li><strong>The interview question that actually gets asked</strong>, with a strong answer</li><li><strong>The follow-up trap question</strong> interviewers use to check if you understand trade-offs, not just syntax</li></ul><p>We’ll group patterns the way the Gang of Four did: <strong>Creational</strong>, <strong>Structural</strong>, <strong>Behavioral</strong> — but we’ll skip the museum-piece patterns nobody uses (Flyweight in its pure form, I’m looking at you) in favor of depth on the ones that come up constantly in real codebases and real interviews.</p><h3>Part 1: Creational Patterns</h3><p>These patterns are about <strong>decoupling object creation from object use</strong>. The question they all answer, in different ways, is: <em>“How do I construct this object without hard-coding exactly what gets constructed and how?”</em></p><h3>1. Singleton</h3><p><strong>The problem:</strong> You need exactly one instance of something — a config manager, a connection pool, a logger — and global access to it, without scattering global variables across your codebase.</p><pre>class Logger {<br>public:<br>    static Logger&amp; instance() {<br>        static Logger instance;  // Meyer&#39;s Singleton — thread-safe since C++11<br>        return instance;<br>    }<br><br>    void log(const std::string&amp; msg) {<br>            std::lock_guard&lt;std::mutex&gt; lock(mtx_);<br>            std::cout &lt;&lt; &quot;[LOG] &quot; &lt;&lt; msg &lt;&lt; &quot;\n&quot;;<br>        }<br>        Logger(const Logger&amp;) = delete;<br>        Logger&amp; operator=(const Logger&amp;) = delete;<br>private:<br>    Logger() = default;<br>    std::mutex mtx_;<br>};</pre><p><strong>Interview question:</strong> <em>“Implement a thread-safe Singleton in C++. Why is the static local variable approach preferred over double-checked locking?”</em></p><p><strong>Strong answer:</strong> Since C++11, the standard guarantees that initialization of a function-local static variable is thread-safe — the compiler inserts the necessary synchronization for you. Double-checked locking (checking a pointer, locking, checking again, then allocating) was a common pre-C++11 workaround, but it&#39;s notoriously easy to get wrong because it relies on memory ordering guarantees that weren&#39;t formally part of the language until the C++11 memory model existed. Meyer&#39;s Singleton is shorter, correct by construction, and lets the compiler do the hard part.</p><p><strong>Follow-up trap:</strong> <em>“What’s wrong with Singletons as a design choice, independent of the implementation?”</em></p><p>Good answer: Singletons introduce hidden global state, which makes unit testing hard (you can’t easily swap in a mock), creates implicit coupling between unrelated parts of the system, and can cause subtle bugs with destruction order at program exit (the “static initialization order fiasco,” and its mirror image on teardown). Most senior engineers treat Singleton as a pattern to justify carefully, not reach for by default — dependency injection is usually the better answer.</p><h3>2. Factory Method</h3><p><strong>The problem:</strong> A class needs to create objects, but it shouldn’t know the concrete type it’s creating — that decision should be deferred to subclasses or configuration.</p><pre>class Shape {<br>public:<br>    virtual void draw() const = 0;<br>    virtual ~Shape() = default;<br>};<br><br>class Circle : public Shape {<br>public:<br>    void draw() const override { std::cout &lt;&lt; &quot;Drawing Circle\n&quot;; }<br>};<br><br>class Square : public Shape {<br>public:<br>    void draw() const override { std::cout &lt;&lt; &quot;Drawing Square\n&quot;; }<br>};<br><br>class ShapeFactory {<br>public:<br>    static std::unique_ptr&lt;Shape&gt; create(const std::string&amp; type) {<br>        if (type == &quot;circle&quot;) return std::make_unique&lt;Circle&gt;();<br>        if (type == &quot;square&quot;) return std::make_unique&lt;Square&gt;();<br>        throw std::invalid_argument(&quot;Unknown shape type: &quot; + type);<br>    }<br>};</pre><p><strong>Interview question:</strong> <em>“What’s the difference between Factory Method and the Simple Factory shown above? Isn’t </em><em>ShapeFactory::create just a switch statement in disguise?&quot;</em></p><p><strong>Strong answer:</strong> Yes — technically, this is a “Simple Factory,” a common shortcut, not the textbook Factory Method pattern. True Factory Method puts the creation logic inside a virtual method on a base class, so <em>subclasses</em> decide what gets created:</p><pre>class Dialog {<br>public:<br>    virtual std::unique_ptr&lt;Button&gt; createButton() const = 0;<br>    void render() {<br>        auto btn = createButton();<br>        btn-&gt;draw();<br>    }<br>    virtual ~Dialog() = default;<br>};<br><br>class WindowsDialog : public Dialog {<br>public:<br>    std::unique_ptr&lt;Button&gt; createButton() const override {<br>        return std::make_unique&lt;WindowsButton&gt;();<br>    }<br>};</pre><p>The distinction matters: Simple Factory centralizes creation logic in one place with conditionals (violates Open/Closed if you keep adding branches); true Factory Method distributes creation across a class hierarchy, so adding a new type means adding a new subclass, not touching existing code.</p><p><strong>Follow-up trap:</strong> <em>“When would you prefer Simple Factory over true Factory Method, given that it ‘violates’ Open/Closed?”</em></p><p>Good answer: When the set of types is small, stable, and unlikely to grow through inheritance — e.g., parsing a config string into one of five known enum-backed types. Factory Method’s benefit is extensibility through subclassing, but that flexibility has a cost: more classes, more indirection. If you don’t need third parties or future contributors to add new types without touching your code, Simple Factory is often the pragmatic, more readable choice.</p><h3>3. Abstract Factory</h3><p><strong>The problem:</strong> You need to create <em>families</em> of related objects that must be used together — e.g., UI components that must all match one theme (Windows buttons with Windows checkboxes, not mixed with Mac ones).</p><pre>class Button { <br>public: <br>    virtual void paint() = 0; <br>    virtual ~Button() = default; <br>};<br><br>class Checkbox { <br>public: <br>    virtual void paint() = 0; <br>    virtual ~Checkbox() = default; <br>};<br><br>class GUIFactory {<br>public:<br>    virtual std::unique_ptr&lt;Button&gt; createButton() = 0;<br>    virtual std::unique_ptr&lt;Checkbox&gt; createCheckbox() = 0;<br>    virtual ~GUIFactory() = default;<br>};<br><br>class MacFactory : public GUIFactory {<br>public:<br>    std::unique_ptr&lt;Button&gt; createButton() override { return std::make_unique&lt;MacButton&gt;(); }<br>    std::unique_ptr&lt;Checkbox&gt; createCheckbox() override { return std::make_unique&lt;MacCheckbox&gt;(); }<br>};</pre><p><strong>Interview question:</strong> <em>“How is Abstract Factory different from Factory Method, precisely?”</em></p><p><strong>Strong answer:</strong> Factory Method creates <em>one</em> product via a single overridable method — it’s about deferring the decision of “which subtype.” Abstract Factory creates <em>families</em> of related products via multiple methods on one factory interface, and internally it’s usually implemented <em>using</em> several Factory Methods. The real signal for “you need Abstract Factory” is when products must be <strong>consistent with each other</strong> — swapping the factory should swap the entire family atomically, so you never accidentally end up with a MacButton next to a WindowsCheckbox.</p><p><strong>Follow-up trap:</strong> <em>“Your Abstract Factory has 8 product types now, and you need a 9th product family (Linux). What’s the actual cost of that change?”</em></p><p>Good answer: Adding a new <em>family</em> (Linux) is cheap and Open/Closed-compliant — one new subclass implementing all 8 factory methods. Adding a new <em>product type</em> (say, Slider) across every existing family is expensive: you must touch the abstract interface <em>and</em> every concrete factory. This is the core trade-off of Abstract Factory — it optimizes for &quot;many families, stable product set,&quot; not the reverse.</p><h3>4. Builder</h3><p><strong>The problem:</strong> An object has many optional constructor parameters, and telescoping constructors (Pizza(size, cheese, pepperoni, mushroom, thinCrust, ...)) become unreadable and error-prone.</p><pre>class HttpRequest {<br>public:<br>    std::string url, method = &quot;GET&quot;, body;<br>    std::map&lt;std::string, std::string&gt; headers;<br>};<br><br>class HttpRequestBuilder {<br>public:<br>    HttpRequestBuilder&amp; setUrl(std::string url) { req_.url = std::move(url); return *this; }<br>    HttpRequestBuilder&amp; setMethod(std::string m) { req_.method = std::move(m); return *this; }<br>    HttpRequestBuilder&amp; addHeader(std::string k, std::string v) { req_.headers[k] = v; return *this; }<br>    HttpRequestBuilder&amp; setBody(std::string b) { req_.body = std::move(b); return *this; }<br>    HttpRequest build() { return req_; }<br>private:<br>    HttpRequest req_;<br>};<br>// Usage:<br>auto req = HttpRequestBuilder()<br>    .setUrl(&quot;https://api.example.com&quot;)<br>    .setMethod(&quot;POST&quot;)<br>    .addHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>    .setBody(&quot;{}&quot;)<br>    .build();</pre><p><strong>Interview question:</strong> <em>“Why not just use a struct with default member initializers, or designated initializers (C++20)? Why do you need a Builder class at all?”</em></p><p><strong>Strong answer:</strong> For simple aggregates with independent fields, designated initializers are genuinely better — less code, same clarity. Builder earns its keep when construction has <strong>invariants that must hold across steps</strong>, needs <strong>validation before the object exists</strong> (throwing in build() rather than leaving a half-valid object around), or when construction is <strong>multi-step and order-dependent</strong> (e.g., a SQL query builder where .where() must come after .select()). It also shines when you want to return an <em>immutable</em> final object — the Builder handles all the mutable intermediate state so the &quot;real&quot; object never has setters.</p><p><strong>Follow-up trap:</strong> <em>“Your </em><em>HttpRequestBuilder::build() returns a copy. What&#39;s the modern C++ fix, and what does it cost you?&quot;</em></p><p>Good answer: Add ref-qualified overloads — HttpRequest build() &amp;&amp; — so that when build() is called on an rvalue (the common chained-call case), the compiler can move instead of copy. The cost is API surface complexity and it forecloses reusing the same builder instance to stamp out multiple similar objects, which some designs actually want.</p><h3>5. Prototype</h3><p><strong>The problem:</strong> Creating an object is expensive (deep object graph, DB round-trip, expensive computation to reach some state), but you often need a near-identical copy with small tweaks.</p><pre>class Shape {<br>public:<br>    virtual std::unique_ptr&lt;Shape&gt; clone() const = 0;<br>    virtual ~Shape() = default;<br>};<br><br>class Polygon : public Shape {<br>public:<br>    Polygon(std::vector&lt;Point&gt; pts) : points_(std::move(pts)) {}<br>    std::unique_ptr&lt;Shape&gt; clone() const override {<br>        return std::make_unique&lt;Polygon&gt;(*this);  // copy constructor does the work<br>    }<br>private:<br>    std::vector&lt;Point&gt; points_;<br>};</pre><p><strong>Interview question:</strong> <em>“What’s the difference between Prototype and just using a copy constructor?”</em></p><p><strong>Strong answer:</strong> Prototype is the copy constructor <em>exposed through a virtual interface</em> so that code holding only a Shape* (not knowing the concrete type) can still clone correctly — this is what solves the &quot;slicing&quot; problem you&#39;d get from Shape copy = *basePtr;. The pattern&#39;s real value shows up in polymorphic collections: you can clone an entire heterogeneous vector&lt;unique_ptr&lt;Shape&gt;&gt; without a giant switch statement checking typeid.</p><p><strong>Follow-up trap:</strong> <em>“Shallow copy or deep copy — which does Prototype imply?”</em></p><p>Good answer: Neither, inherently — it’s the implementer’s choice per class, and it’s a common interview gotcha to assume deep copy is required. If Polygon held a shared_ptr&lt;Texture&gt;, a shallow clone (sharing the texture) might be exactly correct and far cheaper. The pattern only commits you to <em>an</em> explicit, class-controlled copy strategy — not to any particular depth.</p><h3>Part 2: Structural Patterns</h3><p>These patterns are about composing classes and objects into larger structures while keeping them decoupled.</p><h3>6. Adapter</h3><p><strong>The problem:</strong> You have an existing class with an incompatible interface, and you can’t (or shouldn’t) modify it — often because it’s a third-party library.</p><pre>// Third-party, can&#39;t modify:<br>class LegacyPrinter {<br>public:<br>    void printOldFormat(const char* text) { std::cout &lt;&lt; &quot;[Legacy] &quot; &lt;&lt; text &lt;&lt; &quot;\n&quot;; }<br>};<br><br>// Your interface:<br>class IPrinter {<br>public:<br>    virtual void print(const std::string&amp; text) = 0;<br>    virtual ~IPrinter() = default;<br>};<br>class LegacyPrinterAdapter : public IPrinter {<br>public:<br>    explicit LegacyPrinterAdapter(LegacyPrinter&amp; legacy) : legacy_(legacy) {}<br>    void print(const std::string&amp; text) override {<br>        legacy_.printOldFormat(text.c_str());<br>    }<br>private:<br>    LegacyPrinter&amp; legacy_;<br>};</pre><pre><strong>Interview question:</strong> <em>“Adapter vs Facade — both wrap another interface. What’s the actual distinction?”</em></pre><p><strong>Strong answer:</strong> Adapter’s job is <strong>interface translation for one existing object</strong> so it satisfies an interface your code already expects — the relationship is usually one-to-one, and the adapter doesn’t add new behavior, just conforms the shape. Facade’s job is <strong>simplification across many objects</strong> — it introduces a brand-new, simpler interface in front of a whole subsystem, and callers were never expecting the subsystem’s original interface at all. Adapter is about compatibility; Facade is about convenience.</p><p><strong>Follow-up trap:</strong> <em>“Object Adapter (composition, shown above) vs Class Adapter (multiple inheritance) — which do you prefer in C++, and why?”</em></p><p>Good answer: Object Adapter, almost always. Class Adapter uses multiple inheritance from both the target interface and the adaptee, which lets you override the adaptee’s virtual methods directly — occasionally useful — but it locks you to compile-time adaptation of a single concrete adaptee class, and multiple inheritance in C++ brings diamond-problem risk. Object Adapter composes at runtime via a reference or pointer, can adapt any subclass of the adaptee polymorphically, and is far easier to reason about.</p><h3>7. Decorator</h3><p><strong>The problem:</strong> You want to add responsibilities to an object dynamically, without subclassing every combination (imagine CoffeeWithMilk, CoffeeWithMilkAndSugar, CoffeeWithSugarAndWhip... — combinatorial explosion).</p><pre>class Coffee {<br>public:<br>    virtual double cost() const = 0;<br>    virtual std::string description() const = 0;<br>    virtual ~Coffee() = default;<br>};<br><br>class SimpleCoffee : public Coffee {<br>public:<br>    double cost() const override { return 2.0; }<br>    std::string description() const override { return &quot;Coffee&quot;; }<br>};<br>class MilkDecorator : public Coffee {<br>public:<br>    explicit MilkDecorator(std::unique_ptr&lt;Coffee&gt; c) : wrapped_(std::move(c)) {}<br>    double cost() const override { return wrapped_-&gt;cost() + 0.5; }<br>    std::string description() const override { return wrapped_-&gt;description() + &quot; + Milk&quot;; }<br>private:<br>    std::unique_ptr&lt;Coffee&gt; wrapped_;<br>};<br>// Usage: stack decorators at runtime<br>auto order = std::make_unique&lt;MilkDecorator&gt;(std::make_unique&lt;SimpleCoffee&gt;());</pre><p><strong>Interview question:</strong> <em>“How is Decorator different from just using inheritance to add each feature?”</em></p><p><strong>Strong answer:</strong> Inheritance is static — decided at compile time, and it multiplies classes for every combination of features (N independent features → up to 2^N subclasses if you want every combo). Decorator composes behavior at <strong>runtime</strong> by wrapping objects, so you build exactly the combination you need on the fly — MilkDecorator(SugarDecorator(SimpleCoffee)) — without a class existing for every permutation. It&#39;s essentially inheritance traded for composition, in line with &quot;favor composition over inheritance.&quot;</p><p><strong>Follow-up trap:</strong> <em>“Decorator vs Chain of Responsibility — both wrap/chain objects that forward calls. What’s the real difference?”</em></p><p>Good answer: Decorator’s chain always processes the <em>full</em> request through every layer and augments the result — every decorator in the stack runs. Chain of Responsibility’s chain is about <strong>choosing one handler</strong> — each link decides whether to handle the request or pass it along, and typically only one link actually does the work, with the rest declining. Structurally similar; semantically opposite intents (augmentation-of-all vs selection-of-one).</p><h3>8. Facade</h3><p><strong>The problem:</strong> A subsystem has many interacting classes with a complex combined interface. Most callers just want to do the common thing without learning all of it.</p><pre>class CPU { public: void freeze() {} void jump(long) {} void execute() {} };<br>class Memory { public: void load(long, const std::string&amp;) {} };<br>class HardDrive { public: std::string read(long, int) { return &quot;&quot;; } };<br><br>class ComputerFacade {<br>public:<br>    void start() {<br>        cpu_.freeze();<br>        memory_.load(0, hdd_.read(0, 1024));<br>        cpu_.jump(0);<br>        cpu_.execute();<br>    }<br>private:<br>    CPU cpu_;<br>    Memory memory_;<br>    HardDrive hdd_;<br>};<br>// Usage: caller doesn&#39;t need to know boot sequence details<br>ComputerFacade().start();</pre><p><strong>Interview question:</strong> <em>“Does introducing a Facade violate encapsulation or the Single Responsibility Principle, since it now knows about many subsystems?”</em></p><p><strong>Strong answer:</strong> It’s a deliberate, justified exception. The Facade’s single responsibility <em>is</em> “orchestrate the subsystem for the common case” — that’s cohesive, not a violation. What it doesn’t do is <em>hide</em> the subsystem entirely: well-designed facades don’t prevent advanced callers from reaching the underlying classes directly when they need finer control. A Facade that becomes the <em>only</em> way to access the subsystem risks becoming a God Object; a good one is an optional convenience layer, not a wall.</p><p><strong>Follow-up trap:</strong> <em>“Your Facade is now called from 40 places and has grown 15 methods covering unrelated workflows. Is that still a Facade?”</em></p><p>Good answer: Probably not anymore — that’s a sign the “simplification” mission has drifted into becoming a second subsystem in its own right, often called a “god facade.” The fix is usually splitting it into several smaller, workflow-specific facades rather than one that has become a dumping ground.</p><h3>9. Proxy</h3><p><strong>The problem:</strong> You need to control access to an object — lazily create it, check permissions before forwarding calls, cache results, or add logging — without the caller knowing anything changed.</p><pre>class Image {<br>public:<br>    virtual void display() = 0;<br>    virtual ~Image() = default;<br>};<br><br>class RealImage : public Image {<br>public:<br>    explicit RealImage(std::string path) : path_(std::move(path)) { loadFromDisk(); }<br>    void display() override { std::cout &lt;&lt; &quot;Displaying &quot; &lt;&lt; path_ &lt;&lt; &quot;\n&quot;; }<br>private:<br>    void loadFromDisk() { std::cout &lt;&lt; &quot;Loading &quot; &lt;&lt; path_ &lt;&lt; &quot; (expensive)\n&quot;; }<br>    std::string path_;<br>};<br>class ProxyImage : public Image {  // Virtual Proxy: lazy loading<br>public:<br>    explicit ProxyImage(std::string path) : path_(std::move(path)) {}<br>    void display() override {<br>        if (!real_) real_ = std::make_unique&lt;RealImage&gt;(path_);<br>        real_-&gt;display();<br>    }<br>private:<br>    std::string path_;<br>    std::unique_ptr&lt;RealImage&gt; real_;<br>};</pre><p><strong>Interview question:</strong> <em>“Proxy vs Decorator — the code structure looks almost identical (wrap-and-forward). How do you tell them apart in a design review?”</em></p><p><strong>Strong answer:</strong> Look at <em>intent and control</em>, not structure. Decorator is about the <em>client choosing</em> to add behavior, usually stacking several — the client knows it’s decorating. Proxy is about <em>controlling access</em> to an object the client thinks it’s using directly — the client often doesn’t know a proxy is even there (that’s the point: ProxyImage and RealImage are interchangeable from the caller&#39;s view). Also, Decorator implementations typically always delegate; some Proxy variants (Protection Proxy) may <em>refuse</em> to delegate at all if access is denied.</p><p><strong>Follow-up trap:</strong> <em>“Name the four common Proxy variants and one real use case for each.”</em></p><p>Good answer:</p><ul><li><strong>Virtual Proxy</strong> — lazy initialization (shown above; e.g., loading large images only when scrolled into view)</li><li><strong>Protection Proxy</strong> — access control (checking permissions before forwarding a call to a sensitive object)</li><li><strong>Remote Proxy</strong> — represents an object in a different address space (this is literally what gRPC/RPC stubs are)</li><li><strong>Caching Proxy</strong> — memoizes expensive calls behind the same interface (e.g., an HTTP proxy caching responses)</li></ul><h3>Part 3: Behavioral Patterns</h3><p>These patterns are about how objects communicate and distribute responsibility.</p><h3>10. Strategy</h3><p><strong>The problem:</strong> You have a family of interchangeable algorithms (sorting comparators, payment methods, compression schemes) and want to swap them at runtime without conditionals scattered everywhere.</p><pre>class CompressionStrategy {<br>public:<br>    virtual std::vector&lt;uint8_t&gt; compress(const std::vector&lt;uint8_t&gt;&amp; data) = 0;<br>    virtual ~CompressionStrategy() = default;<br>};<br><br>class ZipStrategy : public CompressionStrategy {<br>public:<br>    std::vector&lt;uint8_t&gt; compress(const std::vector&lt;uint8_t&gt;&amp; data) override { /* ... */ return data; }<br>};<br><br>class Compressor {<br>public:<br>    explicit Compressor(std::unique_ptr&lt;CompressionStrategy&gt; s) : strategy_(std::move(s)) {}<br>    void setStrategy(std::unique_ptr&lt;CompressionStrategy&gt; s) { strategy_ = std::move(s); }<br>    std::vector&lt;uint8_t&gt; run(const std::vector&lt;uint8_t&gt;&amp; data) { return strategy_-&gt;compress(data); }<br>private:<br>    std::unique_ptr&lt;CompressionStrategy&gt; strategy_;<br>};</pre><p><strong>Interview question:</strong> <em>“In modern C++, would you even bother with a virtual </em><em>CompressionStrategy interface, or use </em><em>std::function?&quot;</em></p><p><strong>Strong answer:</strong> It depends on the strategy’s complexity and lifetime. std::function&lt;std::vector&lt;uint8_t&gt;(const std::vector&lt;uint8_t&gt;&amp;)&gt; is lighter-weight and avoids the vtable + heap allocation dance for simple, stateless strategies — great when the &quot;algorithm&quot; really is just a function. The virtual-interface version is better when a strategy needs to carry meaningful internal state, has multiple related methods (not just one), or when you want the type system to enforce a specific contract more strongly than a callable&#39;s signature can. In performance-critical code, it&#39;s also worth remembering std::function has type-erasure overhead comparable to virtual dispatch — templating on the strategy type (static polymorphism, CRTP-style) gives you zero-overhead strategy swapping if the compiler can inline it, at the cost of set-in-stone-at-compile-time selection.</p><p><strong>Follow-up trap:</strong> <em>“Strategy vs State — both swap out behavior via composition and a common interface. What actually distinguishes them?”</em></p><p>Good answer: It’s about <em>who decides</em> and <em>whether transitions happen</em>. In Strategy, the <strong>client</strong> chooses the algorithm, and strategies are unaware of each other — there’s no notion of moving from one strategy to another. In State, the <strong>state objects themselves</strong> often trigger transitions to other states as part of handling a request, and the context’s behavior changes as a <em>side effect</em> of its internal state changing over time. Structurally near-identical UML; behaviorally, Strategy is static per-call, State is dynamic per-lifetime.</p><h3>11. Observer</h3><p><strong>The problem:</strong> When one object’s state changes, an open-ended set of other objects need to be notified — without the subject knowing who or how many observers exist.</p><pre>class Observer {<br>public:<br>    virtual void onUpdate(int newValue) = 0;<br>    virtual ~Observer() = default;<br>};<br><br>class Subject {<br>public:<br>    void subscribe(Observer* o) { observers_.push_back(o); }<br>    void unsubscribe(Observer* o) {<br>        observers_.erase(std::remove(observers_.begin(), observers_.end(), o), observers_.end());<br>    }<br>    void setValue(int v) {<br>        value_ = v;<br>        for (auto* o : observers_) o-&gt;onUpdate(value_);<br>    }<br>private:<br>    int value_ = 0;<br>    std::vector&lt;Observer*&gt; observers_;<br>};</pre><p><strong>Interview question:</strong> <em>“This implementation stores raw </em><em>Observer* pointers. What breaks in production, and how do you fix it?&quot;</em></p><p><strong>Strong answer:</strong> The classic bug: an observer is destroyed without calling unsubscribe(), and the subject later calls onUpdate on a dangling pointer — undefined behavior, often a crash under load that&#39;s hard to reproduce. Fixes, in increasing robustness: (1) enforce unsubscription in the observer&#39;s destructor via a back-reference to the subject (fragile if the subject is already destroyed); (2) store std::weak_ptr&lt;Observer&gt; instead of raw pointers, and .lock() each one before calling — dead observers simply return nullptr and get skipped, no dangling access. Weak pointers are the standard modern-C++ answer to this exact failure mode.</p><p><strong>Follow-up trap:</strong> <em>“Your </em><em>setValue notifies observers synchronously, in a loop, on the calling thread. What are the concurrency implications, and what&#39;s the alternative?&quot;</em></p><p>Good answer: Synchronous notification means the subject’s thread blocks for however long the slowest observer takes, and if an observer’s onUpdate re-enters the subject (e.g., calls setValue again or unsubscribes itself mid-iteration), you can get iterator invalidation or reentrancy bugs — the unsubscribe mid-loop case above is a real landmine. The alternative is an async/event-queue based Observer: push notifications onto a queue and let each observer process on its own thread/executor, decoupling notification from processing time, at the cost of losing strict ordering guarantees and adding queue-management complexity.</p><h3>12. Command</h3><p><strong>The problem:</strong> You want to represent a request (with its parameters) as a first-class object, so it can be queued, logged, undone, or executed later — decoupling the invoker from the receiver.</p><pre>class Command {<br>public:<br>    virtual void execute() = 0;<br>    virtual void undo() = 0;<br>    virtual ~Command() = default;<br>};<br><br>class Light {<br>public:<br>    void on() { std::cout &lt;&lt; &quot;Light ON\n&quot;; }<br>    void off() { std::cout &lt;&lt; &quot;Light OFF\n&quot;; }<br>};<br>class LightOnCommand : public Command {<br>public:<br>    explicit LightOnCommand(Light&amp; l) : light_(l) {}<br>    void execute() override { light_.on(); }<br>    void undo() override { light_.off(); }<br>private:<br>    Light&amp; light_;<br>};<br>class RemoteControl {<br>public:<br>    void pressButton(std::unique_ptr&lt;Command&gt; cmd) {<br>        cmd-&gt;execute();<br>        history_.push_back(std::move(cmd));<br>    }<br>    void pressUndo() {<br>        if (!history_.empty()) { history_.back()-&gt;undo(); history_.pop_back(); }<br>    }<br>private:<br>    std::vector&lt;std::unique_ptr&lt;Command&gt;&gt; history_;<br>};</pre><p><strong>Interview question:</strong> <em>“How would you implement undo/redo for a text editor using Command — and what’s the memory trade-off?”</em></p><p><strong>Strong answer:</strong> Each edit operation (insert, delete, format) becomes a Command object storing enough state to reverse itself — for a delete, that means storing the deleted text and its position. You maintain two stacks: undoStack and redoStack. Executing a new command clears redoStack (branching history is discarded, standard editor behavior). The trade-off is memory: storing full before/after state per command is simple but can balloon for large documents; the alternative is storing <em>deltas</em> (diffs) rather than full snapshots, which is more memory-efficient but more complex to implement and compose correctly, especially with overlapping edits.</p><p><strong>Follow-up trap:</strong> <em>“Command vs Strategy — both wrap a single method behind an interface, both are handed to another object. What’s different?”</em></p><p>Good answer: Strategy is about <em>how</em> to do something — it’s interchangeable algorithms for the same task, usually stateless with respect to “when.” Command is about <em>what</em> to do and <em>when</em>, packaged as a request — it typically carries its own parameters/state, supports being queued or logged or undone, and represents an action with a lifecycle (pending → executed → possibly undone). You can queue Commands; you generally don’t queue Strategies.</p><h3>13. State</h3><p><strong>The problem:</strong> An object’s behavior must change based on its internal state, and you want to avoid a giant switch(state) block scattered across every method.</p><pre>class VendingMachineState {<br>public:<br>    virtual void insertCoin(class VendingMachine&amp; m) = 0;<br>    virtual void selectItem(class VendingMachine&amp; m) = 0;<br>    virtual ~VendingMachineState() = default;<br>};<br><br>class VendingMachine {<br>public:<br>    void setState(std::unique_ptr&lt;VendingMachineState&gt; s) { state_ = std::move(s); }<br>    void insertCoin() { state_-&gt;insertCoin(*this); }<br>    void selectItem() { state_-&gt;selectItem(*this); }<br>private:<br>    std::unique_ptr&lt;VendingMachineState&gt; state_;<br>};<br>class NoCoinState : public VendingMachineState {<br>public:<br>    void insertCoin(VendingMachine&amp; m) override;  // transitions to HasCoinState<br>    void selectItem(VendingMachine&amp; m) override { std::cout &lt;&lt; &quot;Insert coin first\n&quot;; }<br>};</pre><p><strong>Interview question:</strong> <em>“Where does the transition logic live — in the State classes, or in the Context (</em><em>VendingMachine)? Defend your choice.&quot;</em></p><p><strong>Strong answer:</strong> Both are valid, with a real trade-off. Putting transitions <strong>inside each State</strong> (as sketched above — NoCoinState::insertCoin constructs and sets a HasCoinState) keeps the Context thin and each state&#39;s rules self-contained, but scatters the overall state machine&#39;s shape across many files, making the full picture harder to see at a glance. Putting transitions <strong>in the Context</strong> via a lookup table ({State, Event} -&gt; NextState) centralizes the state machine&#39;s shape in one place — much easier to audit, diagram, or generate — at the cost of the Context needing to know about all states and events. For anything safety-critical or with more than ~5 states, I&#39;d lean toward the centralized table for auditability; for small, simple machines, in-state transitions are less code.</p><p><strong>Follow-up trap:</strong> <em>“How is State fundamentally different from just using an enum and a switch statement?”</em></p><p>Good answer: Functionally, for small state machines, an enum+switch can be perfectly fine and is arguably more readable. State pattern earns its cost when: each state has substantial, distinct <em>behavior</em> (not just a value) across multiple methods; new states are added over the program’s life (enum+switch requires touching every switch — Open/Closed violation; new State subclasses don’t); or per-state data needs to persist between calls without polluting the Context’s fields. If you’re just branching on a value with no real per-state logic, the pattern is overkill — this is a very common interview trap answer where candidates apply State reflexively.</p><h3>14. Template Method</h3><p><strong>The problem:</strong> Several algorithms share the same overall structure/steps but differ in the details of individual steps — you want to define the skeleton once and let subclasses fill in the blanks.</p><pre>class DataProcessor {<br>public:<br>    void process() {  // Template method — the fixed algorithm skeleton<br>        readData();<br>        transform();<br>        writeData();<br>    }<br>    virtual ~DataProcessor() = default;<br><br>protected:<br>    virtual void readData() = 0;<br>    virtual void transform() = 0;<br>    virtual void writeData() { std::cout &lt;&lt; &quot;Writing to default output\n&quot;; }  // has a default<br>};<br>class CsvProcessor : public DataProcessor {<br>protected:<br>    void readData() override { std::cout &lt;&lt; &quot;Reading CSV\n&quot;; }<br>    void transform() override { std::cout &lt;&lt; &quot;Parsing CSV rows\n&quot;; }<br>};</pre><p><strong>Interview question:</strong> <em>“Template Method vs Strategy — both let you customize part of an algorithm. When do you pick one over the other?”</em></p><p><strong>Strong answer:</strong> Template Method uses <strong>inheritance</strong>: subclasses override specific steps, but the overall algorithm’s control flow lives in the base class and can’t be rearranged by subclasses — only the steps’ contents change. Strategy uses <strong>composition</strong>: the entire algorithm is swapped out as one unit, injected at runtime. Rule of thumb: if the <em>sequence of steps</em> is fixed and only individual step implementations vary, Template Method is a natural, low-ceremony fit. If you need to swap the <em>whole</em> algorithm at runtime, or want to avoid the tight coupling that inheritance creates, Strategy is more flexible — and more testable, since you can inject a mock strategy without subclassing.</p><p><strong>Follow-up trap:</strong> <em>“</em><em>writeData() has a default implementation in the base class, but </em><em>readData() and </em><em>transform() are pure virtual. What&#39;s this variation called, and why mix them?&quot;</em></p><p>Good answer: This is using <strong>hook methods</strong> — steps with sensible defaults that subclasses <em>may</em> override but aren’t forced to, versus <strong>abstract primitive operations</strong> that subclasses <em>must</em> implement. Mixing them lets you make the minimum contract explicit: force subclasses to define what’s genuinely variable per algorithm (how to read, how to transform) while offering a reasonable default for what’s often the same (writing to standard output/storage), reducing boilerplate in the common case without losing extensibility for the uncommon one.</p><h3>15. Chain of Responsibility</h3><p><strong>The problem:</strong> A request should be handled by one of several possible handlers, but you don’t want the sender to know or care which one — and the set of handlers should be easy to reconfigure.</p><pre>class Handler {<br>public:<br>    void setNext(std::shared_ptr&lt;Handler&gt; next) { next_ = std::move(next); }<br>    virtual void handle(int requestLevel) {<br>        if (next_) next_-&gt;handle(requestLevel);<br>    }<br>    virtual ~Handler() = default;<br>protected:<br>    std::shared_ptr&lt;Handler&gt; next_;<br>};<br><br>class ManagerHandler : public Handler {<br>public:<br>    void handle(int requestLevel) override {<br>        if (requestLevel &lt;= 100) std::cout &lt;&lt; &quot;Manager approves\n&quot;;<br>        else Handler::handle(requestLevel);  // pass up the chain<br>    }<br>};<br>class DirectorHandler : public Handler {<br>public:<br>    void handle(int requestLevel) override {<br>        if (requestLevel &lt;= 1000) std::cout &lt;&lt; &quot;Director approves\n&quot;;<br>        else Handler::handle(requestLevel);<br>    }<br>};</pre><p><strong>Interview question:</strong> <em>“This chain is built manually with </em><em>setNext. In a real system with 15 middleware-style handlers (auth, rate-limiting, logging, validation...), how would you structure this differently — and does this remind you of anything you&#39;ve used?&quot;</em></p><p><strong>Strong answer:</strong> This is exactly the shape of HTTP middleware in frameworks like Express.js, ASP.NET, or Go’s net/http middleware chains — Chain of Responsibility is the pattern underneath &quot;middleware.&quot; At scale, you&#39;d typically build the chain from an ordered list/config rather than hand-wiring setNext calls (for (auto&amp; h : handlers) prev-&gt;setNext(h);), which makes reordering or conditionally including handlers (e.g., skip rate-limiting in dev) a config change rather than a code change.</p><p><strong>Follow-up trap:</strong> <em>“What happens if no handler in the chain handles the request? How do you guard against that, and what’s the failure mode if you don’t?”</em></p><p>Good answer: If every handler in the chain declines and passes along, and the chain ends in nullptr with no terminal handler, the request silently disappears — no error, no log, just nothing happens, which is a genuinely nasty class of bug to debug in production. The fix is a <strong>terminal/default handler</strong> at the end of every chain that either throws, logs an &quot;unhandled request&quot; error, or applies a safe default — never leave the end of the chain implicit.</p><h3>Closing thoughts</h3><p>If there’s one meta-lesson across all 15 patterns above, it’s this: <strong>every pattern is a trade-off, not a best practice to apply universally.</strong> The interview questions that actually separate strong candidates aren’t “can you recite the UML diagram” — they’re “do you know <em>when this pattern costs more than it’s worth</em>.” Singleton costs testability. Abstract Factory costs flexibility when the product set (not the family set) grows. Decorator costs debuggability when stacks get deep. State costs code volume for simple machines.</p><p>The engineers who ace these interviews aren’t the ones who memorized 23 diagrams — they’re the ones who’ve been burned by applying a pattern in the wrong place, and can tell you exactly what that felt like.</p><p><em>If you found this useful, I write about low-latency systems, C++, and backend design on Medium regularly — follow for more deep dives like this one.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ed373f14dab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything Is a Plugin: Inside DeepSeek Harness and the Math That Makes It Work]]></title>
            <link>https://medium.com/@thealonemusk/everything-is-a-plugin-inside-deepseek-harness-and-the-math-that-makes-it-work-c046f3ed1b04?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c046f3ed1b04</guid>
            <category><![CDATA[ai-engineering]]></category>
            <category><![CDATA[harness-engineering]]></category>
            <category><![CDATA[deepseek-v4]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[plugins]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Sun, 16 Aug 2026 14:21:57 GMT</pubDate>
            <atom:updated>2026-08-16T14:23:46.069Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A deep dive into Cordis, the effect/coeffect calculus quietly underpinning DeepSeek’s new agent harness — and why it might matter more than the model release it shipped next to.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oE3kV9mDWiloDy6OmWvH_Q.png" /><figcaption>Deepseek Harness</figcaption></figure><p>On August 13–14, 2026, DeepSeek shipped two things at once: <strong>DeepSeek-V4-Pro</strong>, an updated flagship model tuned for agentic workloads, and <strong>DeepSeek Harness</strong> (dsh) — an open-source, MIT-licensed agent harness positioned as an alternative to Claude Code. The model got the headline. The harness got the GitHub stars. But sitting underneath both, largely unread, is a 90-page formal paper — <em>A Programming Paradigm for Spatiotemporal Composability</em>, out of Peking University and DeepSeek-AI — that does something almost nobody does in an industry launch cycle: it proves, with real theorems, why the architecture works instead of just asserting that it does.</p><p>This piece is for the people who want more than “everything is a plugin” as a tagline. We’re going to open the hood: what dsh actually is, what problem it&#39;s solving, and then — the part everyone else is skipping — the actual calculus (revertible effects, reactive coeffects, fibers, and a proof of confluence) that makes the plugin metaphor more than marketing.</p><h3>Part 1: What Shipped</h3><h3>The model layer</h3><p>DeepSeek-V4-Pro rolled out across DeepSeek’s web app, mobile app, and API simultaneously, with native support for OpenAI’s Responses API and Codex integration. Developers hitting V4 through the API will pay noticeably more than they did for prior DeepSeek models — a signal in itself, since DeepSeek built its reputation on undercutting Western labs on price. The subtext from multiple outlets covering the launch was consistent: DeepSeek is no longer trying to win purely on token price or raw benchmark score. It’s moving up a layer, into the same territory Anthropic staked out with Claude Code — the layer that governs <em>how</em> a model uses tools, manipulates files, holds session state, and executes long-running work.</p><h3>The harness layer</h3><p>dsh ships as a <strong>developer preview</strong> — DeepSeek is explicit that breaking changes are coming, not hypothetical. Structurally, it departs from the CLI-only convention: it serves a full local web UI by default (http://127.0.0.1:3080), giving builders live visibility into stats that most harnesses hide entirely — tokens per second, cache hit rate, turn count, running time. In one reported real-world test (building a live ISS tracker), a run burned through roughly 20 million tokens across two turns and about 35 minutes, finishing with a 100% cache hit rate — the kind of number a black-box harness would simply never surface to you.</p><p>The more structurally interesting claim is that dsh can call <strong>other coding agents as sub-agents</strong> — Claude Code, Codex — from inside its own orchestration. That reframes what &quot;competitor&quot; even means here. It&#39;s less &quot;pick DeepSeek Harness <em>instead of</em> Claude Code&quot; and more &quot;DeepSeek Harness as a chassis that other agents can be mounted inside of.&quot;</p><p>All of it — models, tools, skills, sessions, sandboxes, filesystems, the execution loop, the orchestration layer, even the UI — is described by DeepSeek as implemented as a plugin, built on a meta-framework called <strong>Cordis</strong>. That word, Cordis, is the actual subject of this article.</p><h3>Part 2: The Problem Cordis Is Actually Solving</h3><p>Before touching a single equation, it’s worth being precise about what “dynamic composition” means and why it’s hard, because the paper spends its entire introduction building this case empirically before it writes a single formula.</p><h3>Composition, historically, is static</h3><p>Function calls, module imports, class inheritance — resolved at compile time, fixed for the life of the process. That’s been fine for most of software history. It is not fine for a system that’s supposed to load, unload, and reconfigure components <em>while running</em>, which is exactly what plugin architectures and self-modifying agent harnesses both demand.</p><p>The paper identifies <strong>two orthogonal dimensions</strong> this dynamism breaks along:</p><ul><li><strong>Temporal composability</strong> — when you remove a component, can you completely and safely undo everything it did to the shared environment? Every resource it allocated, every event listener it registered, every piece of state it mutated — reclaimed, in order, on removal.</li><li><strong>Spatial composability</strong> — can components discover, declare, and resolve dependencies on each other in a way the system can verify, as those dependencies appear, disappear, and get replaced at runtime?</li></ul><p>In a statically compiled world, temporal composability is just lexical scoping — RAII in C++, try/finally bracket patterns. Spatial composability is just module import resolution. Easy, solved, boring. The moment components arrive and depart <em>at runtime</em>, both problems get dramatically harder, because there&#39;s no compile-time scope left to lean on.</p><h3>The VS Code audit</h3><p>The authors don’t just assert this is hard — they measured it. They pulled the top 100 VS Code extensions by install count and checked two things:</p><ul><li><strong>87 of 100</strong> contain executable code and therefore cannot be safely unloaded without restarting VS Code’s entire extension host. VS Code gives you a deactivate() hook, but it&#39;s a shutdown callback fired during host termination — not a live-removal mechanism — and worse, it separates cleanup code from the activation code that created the mess in the first place, so verifying a deactivate() actually undoes everything an activate() did is left entirely to the extension author&#39;s memory.</li><li>Only <strong>7 of 100</strong> declare structured extensionDependencies on other (non-built-in) extensions. And even when they do, VS Code&#39;s mechanism for cross-extension access — vscode.extensions.getExtension(...).exports — returns an untyped any, so there&#39;s no checked contract even when a dependency <em>is</em> declared.</li></ul><p>That’s the empirical grounding for the whole paper: the industry’s most widely used extensible editor has essentially no real temporal or spatial composability, and it isn’t unique — the same two failure modes recur across plugin ecosystems generally, differing only in degree.</p><h3>Why agent harnesses make this urgent, not academic</h3><p>Here’s the paragraph that explains why this paper exists <em>now</em>, specifically. A future agent harness — DeepSeek is explicit about this being the motivating case — may generate and deploy modifications to its own components continuously, while still serving live requests, with little or no human oversight.</p><p>Without temporal composability: every self-modification forces a full restart, discarding all accumulated process state. At the frequency a self-evolving system would operate, that unavailability compounds fast, in-flight tasks get repeatedly disrupted, and — the sharpest version of the risk — a faulty self-modification can disable the very process that would be needed to recover from it.</p><p>Without spatial composability: every module has to detect and adapt to changes in the modules it depends on through ad hoc means, and a naive code-replacement strategy can silently break dependents or introduce circular dependencies that only surface at reload time — in production, mid-run.</p><p>The industry’s actual fallback for both problems has been coarse-grained: operating systems give you temporal composability at the granularity of a <em>process</em> (just restart it), and container orchestrators like Kubernetes give you spatial composability at the granularity of a <em>service</em> (let the orchestrator manage the dependency graph between containers). That’s a real, workable substitute — right up until you need composition at a finer grain than “restart the whole process” or “route through the network.” An agent harness swapping out one tool implementation mid-session is exactly that finer grain, and neither substitute reaches it.</p><h3>Part 3: The Actual Math — Effects and Coeffects, Made Runtime-Native</h3><p>This is where the paper stops being a systems paper and becomes a genuine piece of programming-language theory. Two classical ideas get lifted from compile-time type theory into something a runtime can operate on directly.</p><h3>Effects: what a computation does to its environment</h3><p>In an effect system, a typing judgment gets annotated with the side effects a computation may produce — this goes back to Lucassen and Gifford in the 1980s, formalized categorically by Moggi via monads, and popularized in Haskell by Wadler. Plotkin and Power’s <em>algebraic effects</em> later decoupled effect interfaces from their implementations entirely, giving rise to effect handlers — the mechanism now showing up in languages like Koka, Eff, and OCaml 5.</p><p>All of this, historically, is <strong>static</strong>: effects are tracked within a lexically fixed scope and discharged by a compile-time handler. That’s the exact assumption Cordis breaks.</p><h3>Coeffects: what a computation needs from its environment</h3><p>Dual to effects, a coeffect system annotates the <em>context</em> rather than the type — what resources, permissions, or services a computation requires from its environment, rather than what it does to it. This traces to Uustalu and Vene’s work on comonads, unified into a general theory of context-dependence by Petricek, Orchard, and Mycroft, and later merged with graded effect systems by Gaboardi et al. Same limitation as effects: static, compile-time, fixed scope.</p><h3>The lift: reifying both as first-class runtime types</h3><p>Here’s the actual conceptual leap. Instead of extending a static type system with more annotations, Cordis <strong>reifies</strong> the conceptual structure of effects and coeffects as runtime-operable <em>context types</em> — data the system can inspect, compose, and act on while the program is executing, rather than facts the compiler checks once and discards.</p><p>Concretely, an effect on a context Γ is modeled not just as a transformation Γ → Γ, but as a function</p><pre>Γ → Γ × (Γ → Γ)</pre><p>— applied to the current context, it returns both the new context <em>and</em> an explicit inverse function. Supplying that inverse at the point of application is what makes the effect <strong>revertible</strong>. Pairs of these transformations form what the paper calls a <em>twisted composition monoid</em> — sequential composition of two effects composes their forward transformations normally, but composes their inverses in the <em>opposite order</em>, which is exactly the algebraic structure that makes last-in-first-out (LIFO) teardown fall out for free rather than needing to be hand-coded.</p><p>The runtime then tracks an <strong>effect context</strong> ∂Γ = Γ × (Γ → Γ) — a pair of (current state, accumulator), where the accumulator is the running composite of every inverse applied so far. Two primitive operations do all the work:</p><ul><li>track(f, g) — apply forward transformation f, and compose inverse g onto the accumulator</li><li>recover — apply the accumulated inverse to the current state, and reset the accumulator to identity</li></ul><p>The paper proves track is a genuine <strong>monoid homomorphism</strong> (composing two tracked effects equals tracking their composite), and proves the core soundness theorem: applying any sequence of tracked effects and then calling recover returns you <em>exactly</em> to the state you started at. That&#39;s not an informal guarantee — it&#39;s a proved algebraic identity. Cleanup isn&#39;t something a plugin author writes and hopes is complete; it&#39;s something the composition structure derives automatically from the inverses supplied at the point of action.</p><h3>Independence: why order doesn’t have to matter</h3><p>There’s a subtlety the paper doesn’t skip past: what happens when you need to revert an effect <em>out of order</em> — say, unloading component A while component B, loaded afterward, is still active? A single accumulator reverts everything in strict LIFO order and nothing else. Real systems need more flexibility than that.</p><p>The answer is a formal notion of <strong>independence</strong> between effect functions: two effects are independent when every transformation one can perform commutes with every transformation the other can perform, <em>and</em> neither one’s transformations disturb the inverse the other yields. Where that holds — proved via a “transformation monoid” generated by each effect’s forward maps and yielded inverses — the paper shows you can revert a family of effects in <strong>any</strong> order, not just LIFO, and still land back at the correct starting state (Corollary 21 in the paper, if you want to go find it). That’s the theorem that lets multiple components’ effects interleave freely at runtime without the system having to serialize everyone through one global teardown queue.</p><h3>Reactive coeffects: dependency resolution as a live system, not a boot-time step</h3><p>On the coeffect side, the runtime models dependencies as a partial function from keys to typed values — get(k) reads a bound dependency, set(k, v) installs one (and, cleanly, set itself turns out to have exactly the type of a revertible effect function — provisioning a dependency and tracking effects are <em>literally the same primitive</em> under this model, which is a genuinely elegant unification).</p><p>The reactive part is a classification function, notify_d(σ, σ′), that fires on <em>every</em> state transition and answers one question for a component with dependency specification d: did this change flip the component from unsatisfied to satisfied (<strong>activating</strong>), satisfied to unsatisfied (<strong>deactivating</strong>), or neither (<strong>neutral</strong>)? Because coeffect state only ever changes through tracked effect functions — and those are decidably checkable — this classification is well-defined at <em>every</em> transition, not just at designated checkpoints. A component doesn&#39;t poll to see if its dependencies are ready. It gets told, structurally, the moment the answer changes.</p><p>Two refinements extend this further, and they map directly onto real multi-tenant and access-control problems:</p><ul><li><strong>Coeffect isolation</strong> — the same dependency key can resolve to <em>different</em> bound values in different contexts, via a realm indirection table. This is how you’d give two plugin instances different database connections under one logical key name — genuinely useful for multi-tenant systems and sandboxed testing.</li><li><strong>Coeffect interception</strong> — cross-cutting metadata gets attached to a dependency access without touching the dependency’s value at all, merged from both the context and the component’s own declaration, with the context taking priority. This is how an orchestrator could grant a community-authored plugin read-only database access while a trusted core plugin keeps full access — without either plugin’s code changing.</li></ul><h3>Part 4: Components, Fibers, and the Actual State Machine</h3><p>Zooming out from individual effects and coeffects, the paper assembles a full operational semantics for a running system of components. This is the part that reads almost like a distributed-systems paper wearing a type-theory paper’s clothes.</p><p>A <strong>component</strong> is formalized as a triple (d, p, e): a coeffect specification d (what it needs), a provision p (what keys it may supply to others), and a witnessed effect function e (what it does, with its inverse). A <strong>fiber</strong> is a live instantiation of a component — carrying its own lifecycle state, its own portion of the shared dependency table, a retirement flag, and a pointer to its parent fiber (since components can register other components, forming a real tree — think of a top-level plugin that itself loads sub-plugins).</p><p>The base lifecycle is a two-state machine — Inactive ⇄ Active — but the paper immediately admits this is an idealization and expands it into the real four-state machine any production runtime needs:</p><pre>INACTIVE → RELOADING → ACTIVE → UNLOADING → INACTIVE</pre><p><strong>RELOADING</strong> exists because an activation might run multiple asynchronous steps (an effect <em>iterator</em>, not just a single atomic function) — and the environment can shift mid-activation. At each iteration boundary, the runtime checks whether the fiber’s target dependency resolution has changed; if it has, the transition diverts into UNLOADING instead of completing, so you never finish activating a component against dependencies that are no longer the ones it started against.</p><p><strong>UNLOADING</strong> is the most consequential addition, because it’s what makes the ordering guarantee real instead of aspirational. When a fiber’s target changes (its dependencies vanish, or it’s explicitly retired), it doesn’t immediately run its inverse. It first marks itself UNLOADING — which pulls its provided keys <em>out</em> of what dependents can see — and then it <em>waits</em> for every dependent that had resolved a key against it to finish tearing down first. Only once nobody still depends on it does the accumulated inverse actually run. This is the formal answer to a very concrete failure mode: a component whose teardown code needs the very resource being withdrawn (think: closing a connection pool means handing connections back to whatever provided the pool). The two-stage split — stop providing, <em>then</em> wait, <em>then</em> recover — is precisely what makes &quot;provider outlives every consumer that depends on it&quot; a provable property (<strong>Theorem 63, &quot;Ordering,&quot;</strong> in the paper) rather than a race condition you hope doesn&#39;t happen.</p><p>Error handling gets the same rigor: a failing effect iteration routes through UNLOADING exactly like a normal deactivation — recovering everything installed up to the failure point — before landing in a terminal INACTIVE(error) state that the runtime won&#39;t automatically retry against an unchanged environment. A component whose activation fails doesn&#39;t take its siblings down with it; the failure is scoped to that one fiber, which is precisely the behavior you&#39;d want from a plugin host.</p><h3>The theorem that makes all of it trustworthy: Confluence</h3><p>All of this machinery is nondeterministic by design — multiple fibers can be mid-transition simultaneously, and the calculus commits to no particular scheduling order. So the paper has to answer the obvious worry: if load order and interleaving are arbitrary, can the same set of operations produce <em>different</em> final states depending on scheduling luck?</p><p>The answer, proved as the paper’s capstone result, is <strong>no</strong>. Under the independence conditions established earlier, and given that every component is “total” on what it provides (it installs every key it declares), the system is <strong>confluent</strong>: any two valid execution sequences that perform the same insertions and retirements converge — after accounting for internal fiber renaming — to the <em>same</em> quiescent state, up to a defined equivalence on states. Concretely, the paper shows that any interleaved, dynamic sequence of loads/unloads/reconfigurations reduces to a canonical form equivalent to loading every surviving component exactly once, in dependency order, from scratch.</p><p>That’s the theorem that lets you reason about a live, constantly-reconfigured Cordis application <em>as if</em> it had been statically assembled. An orchestrator that adds a plugin, removes it, swaps a provider, and reverts the swap is guaranteed — provably, not empirically — to land exactly where writing the final configuration down once, up front, would have landed. Combined with a separate progress theorem (the system never deadlocks and always reaches a quiescent state, under a straightforward acyclicity condition on the dependency graph), this is the pair of results that turns “everything is a plugin” from a slogan into something you can actually build a production system on top of.</p><h3>Part 5: From Theorems to Working Code</h3><p>The implementation, <strong>Cordis</strong>, layers this in three tiers: a core library realizing the effect/coeffect primitives directly (ctx.effect(), ctx.set(), ctx.get(), ctx.isolate(), ctx.intercept()), a declarative component loader on top that adds configuration reconciliation, and application frameworks — like Koishi — built on both.</p><p>Two implementation details are worth calling out because they show the theory earning its keep in practice, not just on paper:</p><ul><li><strong>Incremental reconciliation.</strong> When an orchestrator’s declared configuration changes, the loader doesn’t blindly tear down and rebuild. It dispatches per changed field — a config change gets diffed and only reloads on material change; an isolate change triggers a targeted realm reassignment (using a clever &quot;delimiter tag&quot; trick to determine, without ambiguity, whether a moved dependency binding belongs to the entry being relocated or to something else sharing its old realm); only an actual url or id change forces a full rebuild. The confluence theorem is exactly what licenses this laziness — because the final state is provably independent of <em>how</em> you got there, the loader is free to take the cheapest path available.</li><li><strong>Hot Module Replacement with zero developer-annotated boundaries.</strong> Unlike Webpack or Vite HMR, which require you to explicitly opt modules into hot-reload acceptance, Cordis’s HMR needs none of that. Because every fiber already fully bounds its component’s tracked effects, replacing a module reduces to disposing the old fiber (which mechanically recovers everything it installed) and instantiating a new one from the reloaded source. The engine even does transactional multi-file reloads with rollback: it classifies changed modules through a dependency-graph fixed point (accepted vs. declined vs. requiring a full restart), detects which component entries are stale, and if <em>any</em> module in the batch fails to import, it restores every cache and rebuilds every stale entry from backup — so a syntax error during a hot-reload never leaves the system in a half-upgraded state.</li></ul><h3>Part 6: The Production Evidence — Koishi</h3><p>The theory doesn’t stay theoretical. Cordis has been running in production for four years inside <strong>Koishi</strong>, an open-source chatbot framework, which has accumulated more than <strong>4,000 community-contributed plugins</strong> — IM adapters, database drivers, admin consoles, end-user features — authored independently by people who mostly never coordinated with each other beyond agreeing on a shared dependency key name.</p><p>That’s the case study’s real value, and the paper is candid about scoping it correctly: it’s an <em>existence-and-adoption</em> result, not a controlled benchmark. There’s no measured comparison against a monolithic architecture, no productivity study. What it <em>does</em> demonstrate is that (1) the same primitives carried a complete production chatbot server <em>and</em>, in a completely different runtime, Koishi’s browser-based web console — establishing genuine generality, not domain-specificity — and (2) an orchestrator can disable a plugin from a web console and watch its effects unwind cleanly in place, with the HMR engine re-applying edited plugins on save while every other live connection in the system stays untouched.</p><h3>Part 7: Where the Paper Admits It Isn’t Finished</h3><p>Refreshingly, the paper spends real space on its own open problems instead of hand-waving past them:</p><ul><li><strong>Dependency typing is nominal, not structural.</strong> Two independently developed plugins can collide on the same key name with completely unrelated interfaces, and the runtime has no way to catch it — key identity alone establishes the link. The paper discusses three partial mitigations (namespacing keys by package, semver-style peer dependencies as Cordis currently does via npm, or a fully structural compatibility check analogous to structural subtyping) and explicitly leaves picking a winner as future work.</li><li><strong>Decomposing away circular dependencies can blow up component count.</strong> Two components that need each other bidirectionally have to be split into four to eliminate the cycle (two “cores” plus two directional integration components) — and in the general case, avoiding an n-way tangle this way scales <em>quadratically</em> in the number of interacting components. The paper is honest that this trades correctness for authoring overhead and defers the fix to tooling (bundling, convention-based wiring, scaffolding) rather than the model itself.</li><li><strong>Single language, single ecosystem.</strong> Every claim is validated in TypeScript, inside Koishi. Whether the paradigm holds up cleanly in Rust, Python, or a systems language with a different execution model is discussed at length (Section 6.4 walks through what a language needs — closures, a way to introduce/retract code at runtime, typed dependency declarations, interposable access) but isn’t empirically tested outside the one implementation.</li><li><strong>Reversibility has a boundary, and stuff crosses it.</strong> Not every effect can be perfectly undone — writing bytes to a socket, once sent, can’t be un-sent. The paper’s honest answer is a formal split between <em>acquisition</em> (installing a trackable record — like a file descriptor — which stays inside the reversible boundary) and <em>emission</em> (data actually leaving the system, which doesn’t), with output-commit deferral or explicit compensating actions as the only real options once something crosses that line. This is the same output-commit problem rollback-recovery research has wrestled with for decades, and the paper doesn’t claim to have solved it — just to have named the boundary precisely.</li></ul><h3>Part 8: How This Stacks Up Against What Already Exists</h3><p>The paper’s related-work section is unusually thorough, and worth summarizing because it clarifies exactly what’s new here versus what’s a remix of known ideas:</p><ul><li><strong>Against monadic effect libraries</strong> (ZIO, Effect-TS, fp-ts): those buy tracking by making you write your entire program inside a monadic type. Cordis tracks effects as an overlay on ordinary host code — no embedding required — and, more importantly, a monadic system’s “requirement” is satisfied once by an interpreter and then forgotten, whereas Cordis re-resolves requirements continuously as providers come and go.</li><li><strong>Against algebraic-effects-as-capabilities</strong> (Brachthäuser et al.’s Effekt language): Effekt makes effects visible so a compiler can attach different <em>interpretations</em> to the same operation. Cordis makes them visible so a <em>runtime</em> can track and reverse them. Effekt disciplines effects statically at the type level with lexically scoped capabilities; Cordis disciplines them dynamically, aiming at complete resource recovery on component removal.</li><li><strong>Against reversible-computing theory</strong> (Heunen et al.’s dagger arrows, reversible process calculi like RCCS): the closest formal cousin. The difference is scope — categorical reversible-computing frameworks typically require the <em>entire</em> computation be invertible by construction, with two-sided inverses derived from the category’s structure. Cordis asks far less: only that each atomic effect supply a one-sided inverse, at the point of use, from which composite inverses follow by composition alone.</li><li><strong>Against OSGi and iPOJO</strong>, the closest systems-level precedent: both already do availability-reactive activation of services. What they lack is exactly the temporal half — recovery happens through a hand-written deactivation callback, synchronous, unenforced, and easy to get wrong (which loops back to the VS Code audit that opened the paper). Cordis’s contribution there is making the inverse structural rather than a duty the developer might forget.</li><li><strong>Against React’s </strong><strong>useEffect</strong>, interestingly the closest <em>structural</em> precedent in mainstream tooling — a cleanup function returned alongside the effect, run automatically before re-execution or unmount. Its limitation is composability: hooks can&#39;t be called conditionally, in loops, or assembled from other hooks, so there&#39;s no way to build a composite inverse out of existing ones. Cordis effects are ordinary functions that compose freely and can run asynchronously — the composability React&#39;s rules explicitly forbid.</li></ul><h3>Why This Matters More Than the Model Release</h3><p>Benchmark leaderboards get reset every few weeks. A formally verified calculus for how software components compose, activate, and tear down at runtime — with actual preservation, progress, and confluence proofs, not just design-doc prose — is the kind of artifact that, if it’s right, outlives several generations of the models sitting on top of it.</p><p>The paper’s own closing argument is the sharpest framing of why this specific moment matters. Koishi’s 4,000 plugins were reviewed, however loosely, by a human community over four years. The regime this architecture is actually built for hasn’t been production-tested yet: a <strong>self-evolving agent</strong> generating, deploying, and retiring its own components continuously, with no human reviewing the diffs, expected to run for months without a restart. That’s exactly the setting where “does the teardown <em>provably</em> undo everything” and “does the dependency graph <em>provably</em> resolve correctly under constant churn” stop being nice theoretical properties and become the only thing separating a working autonomous system from a slow-motion, undebuggable memory leak.</p><p>Effect systems took decades to go from Moggi’s monads to async/await becoming boring, mainstream syntax. Coeffects are much earlier on that same curve. Somebody had to be first to drag them out of the type checker and into a system that&#39;s actually running, unattended, in production. This week, it happened to be DeepSeek — and whether or not dsh itself becomes anyone&#39;s daily driver, Cordis is the kind of idea that tends to get quietly reinvented by everyone else within a few years if nobody adopts it directly first.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c046f3ed1b04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Kernel, A Thousand Lies: How Docker Fools Every App Into Thinking It’s Alone]]></title>
            <link>https://medium.com/@thealonemusk/one-kernel-a-thousand-lies-how-docker-fools-every-app-into-thinking-its-alone-7b53dc07ac5e?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b53dc07ac5e</guid>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-image]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Mon, 10 Aug 2026 16:47:27 GMT</pubDate>
            <atom:updated>2026-08-11T18:00:03.756Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Not another “Docker in 5 minutes” post. This one goes under the hood — kernel primitives, real code, and why Docker beat every alternative that came before and after it.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7wPR3I2838a1iGISw92txg.png" /><figcaption>AI Generated Image</figcaption></figure><h3>Every developer has said it at least once, usually right before a deadline:</h3><blockquote><em>“It works on my machine.”</em></blockquote><p>Four words that have caused more Slack arguments than any bug ever written. Docker exists to make that sentence obsolete. Let’s actually understand the machinery behind <em>how</em>.</p><h3>1. The Kernel Primitives Docker Is Built On</h3><p>This is the part most explainers skip, and it’s the part that actually matters. Docker isn’t magic — it’s a very good API wrapped around three Linux kernel features that already existed:</p><p><strong>Namespaces</strong> — control what a process can <em>see</em>. Linux has several: pid (its own process tree), net (its own network stack, IP, ports), mnt (its own filesystem view), uts (its own hostname), ipc, and user. A containerized process looks up and sees PID 1 as itself — even though on the host it might be PID 48213.</p><p><strong>Control groups (cgroups)</strong> — control what a process can <em>use</em>. CPU shares, memory limits, disk I/O, network bandwidth. This is why you can run docker run --memory=512m and know the container physically cannot exceed that, rather than just hoping it behaves.</p><p><strong>Union filesystems (OverlayFS)</strong> — control how image layers <em>stack</em>. Each Dockerfile instruction creates a read-only layer; at runtime, Docker adds one thin writable layer on top. Multiple containers can share the same read-only base layers on disk while keeping independent write layers.</p><blockquote><em>“Docker didn’t invent isolation. It took primitives the Linux kernel already had — and gave them an API a human could actually use.”</em></blockquote><p>This matters because it explains <em>why</em> containers are fast: there’s no second kernel to boot, no hypervisor translating hardware calls. It’s one kernel, wearing different name tags for different processes.</p><h3>2. Image Architecture: What’s Actually Inside a Docker Image</h3><p>An image isn’t a single file — it’s a manifest plus a stack of layers, each identified by a content hash (SHA256). This is defined by the <strong>OCI Image Spec</strong>, an open standard, not something Docker keeps proprietary.</p><pre>myapp:latest<br>├── manifest.json          (points to layers + config)<br>├── config.json             (env vars, entrypoint, exposed ports)<br>└── layers/<br>    ├── sha256:a1b2... (base OS layer — e.g. Alpine)<br>    ├── sha256:c3d4... (installed dependencies)<br>    ├── sha256:e5f6... (application code)<br>    └── sha256:g7h8... (final config layer)</pre><p>This layered, hash-addressed design is why docker pull only downloads layers you don&#39;t already have, and why two images sharing a base (say, python:3.11-slim) don&#39;t duplicate that base on disk. It&#39;s essentially <strong>git for filesystems</strong>.</p><h3>3. Multi-Stage Builds: The Concept That Separates Beginners From Production Engineers</h3><p>Naive Dockerfiles bloat images by shipping build tools (compilers, package managers, source code) into production. Multi-stage builds fix this by using multiple FROM statements in one file — only the final stage ships.</p><p>dockerfile</p><pre># ---- Build stage ----<br>FROM golang:1.22 AS builder<br>WORKDIR /src<br>COPY go.mod go.sum ./<br>RUN go mod download<br>COPY . .<br>RUN CGO_ENABLED=0 go build -o /app/server .<br><br># ---- Runtime stage ----<br>FROM gcr.io/distroless/static-debian12<br>COPY --from=builder /app/server /server<br>USER nonroot:nonroot<br>ENTRYPOINT [&quot;/server&quot;]</pre><p>The result: a final image with no compiler, no shell, no package manager — just a statically linked binary on a minimal base. Images shrink from hundreds of MBs to single-digit MBs. Attack surface shrinks with it.</p><blockquote><em>“The best production image is the one with the least stuff in it for an attacker to work with.”</em></blockquote><p>This is also where distroless images come in — base images with no shell, no apt, no curl. If someone does compromise the app, they don&#39;t even get a shell to pivot from.</p><h3>4. Networking: The Part Nobody Reads the Docs For</h3><p>Docker containers don’t talk to the world directly by default — they go through virtual networking:</p><ul><li><strong>bridge</strong> (default) — containers get a private internal IP, NAT’d through the host. Good for local dev.</li><li><strong>host</strong> — container shares the host’s network stack directly. Faster, but no isolation between container and host ports.</li><li><strong>overlay</strong> — spans multiple Docker hosts, used in Swarm/Kubernetes-style multi-node setups.</li><li><strong>none</strong> — no networking at all, for maximum isolation.</li></ul><p>bash</p><pre>docker network create --driver bridge my-net<br>docker run --network=my-net --name api myapi:latest<br>docker run --network=my-net --name db postgres:16<br># `api` can now resolve `db` by container name via Docker&#39;s embedded DNS</pre><p>That last line is easy to skim past but it’s genuinely important: Docker gives you <strong>service discovery by container name</strong> for free, on a user-defined bridge network. No manual IP wiring required.</p><h3>5. Volumes and Persistence: Containers Are Meant to Die</h3><p>A core design principle: containers are <strong>ephemeral</strong>. Kill one, and its writable layer disappears. That’s a feature, not a bug — but it means anything that needs to survive (databases, uploaded files) must live outside the container’s filesystem.</p><pre>docker volume create pgdata<br>docker run -d \<br>  --name db \<br>  -v pgdata:/var/lib/postgresql/data \<br>  postgres:16</pre><p>pgdata is managed by Docker, lives outside any single container&#39;s lifecycle, and can be reattached to a brand-new container instantly. Compare this to <strong>bind mounts</strong> (-v /host/path:/container/path), which map a specific host directory in directly — great for local dev, riskier for production since it couples the container to host filesystem specifics.</p><blockquote><em>“A container should be able to die at any moment without anyone noticing. If that thought makes you nervous, your state isn’t in the right place yet.”</em></blockquote><h3>6. Docker Swarm: Orchestration Docker Built In-House</h3><p>Running one container is easy. Running the <em>same</em> service reliably across ten machines — with automatic restarts, load balancing, and zero-downtime updates — is a different problem entirely. That’s orchestration, and Docker shipped its own answer: <strong>Docker Swarm</strong>.</p><p>Swarm turns a group of machines into a single logical cluster. You join machines as either <strong>managers</strong> (which make scheduling decisions and hold cluster state) or <strong>workers</strong> (which just run containers). The genuinely nice part is that it uses the Docker CLI you already know — there’s no new tool to learn.</p><pre># On the first machine — becomes a manager<br>docker swarm init --advertise-addr &lt;MANAGER-IP&gt;<br># On other machines - join as workers, using the token Swarm gives you<br>docker swarm join --token &lt;WORKER-TOKEN&gt; &lt;MANAGER-IP&gt;:2377<br># Deploy a replicated service across the cluster<br>docker service create \<br>  --name web \<br>  --replicas 5 \<br>  --publish 80:80 \<br>  myapp:latest</pre><p>That one docker service create command schedules five replicas of myapp across whichever nodes have capacity, load-balances traffic between them via Swarm&#39;s built-in routing mesh, and — if a node dies — automatically reschedules the lost replicas elsewhere. No manual intervention.</p><blockquote><em>“A single </em><em>docker run asks a machine to do something. </em><em>docker service create asks a </em>cluster<em> to make sure something keeps happening — even after machines fail.&quot;</em></blockquote><p>Swarm also extends <strong>Compose</strong> files into <strong>stack</strong> files, so multi-service applications deploy the same way locally and in a cluster:</p><pre>docker stack deploy -c docker-compose.yml myapp-stack</pre><h3>Swarm vs. Kubernetes — Why One Won and One Didn’t</h3><p>Both solve the same core problem: scheduling containers across many machines and keeping them healthy. But the industry overwhelmingly settled on Kubernetes, and it’s worth understanding why, rather than just accepting it.</p><p><strong>Swarm’s pitch was simplicity.</strong> It’s built into the Docker CLI, has a gentle learning curve, and can go from zero to a running cluster in minutes. For small teams or simpler deployments, that’s genuinely valuable — Swarm is not a bad tool, it’s an <em>underused</em> one.</p><p><strong>Kubernetes’s pitch was depth.</strong> It has a far richer scheduling model (affinity rules, taints and tolerations, custom resource definitions), a massive ecosystem of extensions (Helm, operators, service meshes), and it’s what every major cloud provider builds managed offerings around (EKS, GKE, AKS).</p><blockquote><em>“Swarm optimized for the day you deploy. Kubernetes optimized for the two years after — when your infrastructure gets weird, and you need an escape hatch for every weird case.”</em></blockquote><p>That’s really the deciding factor: most production systems don’t stay simple for long, and when they don’t, Kubernetes’ complexity — which feels like overkill on day one — starts paying for itself. Swarm still sees real use in smaller shops that value operational simplicity over infinite extensibility, but it lost the “industry standard” race for exactly the reason you’d guess: Kubernetes bet on flexibility, and flexibility is what large, messy, real-world infrastructure eventually demands.</p><h3>7. Docker vs. Its Alternatives — An Honest Comparison</h3><p>Docker gets called “the” container tool, but it’s really one implementation of ideas that have several competitors, each with real trade-offs.</p><p><strong>Docker</strong> — A full platform: daemon, CLI, build system, and registry tooling in one. <em>Wins on:</em> the biggest ecosystem, the best documentation, and Compose for spinning up multi-service local dev setups. <em>Loses on:</em> its daemon runs as root by default (a rootless mode exists but isn’t the default path most people follow), and historically that daemon has been a single point of failure.</p><p><strong>Podman</strong> — A daemonless, rootless-by-default container engine that’s largely CLI-compatible with Docker. <em>Wins on:</em> a stronger default security posture since there’s no root daemon to compromise; it’s the standard in Red Hat and other enterprise Linux shops. <em>Loses on:</em> a smaller ecosystem, and its Compose equivalent (podman-compose) is less mature than the real thing.</p><p><strong>LXC/LXD</strong> — System containers that run closer to lightweight VMs, complete with full init systems. <em>Wins on:</em> being a better fit for long-lived environments that need to feel like a real, persistent machine. <em>Loses on:</em> not matching the immutable, single-process application model that most modern software is built around.</p><p><strong>containerd / CRI-O</strong> — Low-level container runtimes never meant to be touched directly by humans. <em>Wins on:</em> being what Kubernetes actually runs under the hood — minimal, fast, and purpose-built. <em>Loses on:</em> offering no friendly CLI or build tooling, so you’d never reach for these standalone in day-to-day development.</p><p><strong>Virtual Machines (VMware, VirtualBox, KVM)</strong> — Full hardware virtualization, each with its own separate guest kernel. <em>Wins on:</em> the strongest isolation boundary available — a genuinely separate kernel that’s far harder to escape. <em>Loses on:</em> being heavy — minutes to boot, gigabytes of overhead, and none of Docker’s layer caching.</p><p><strong>Firecracker (microVMs)</strong> — Lightweight VMs purpose-built for serverless, and what actually powers AWS Lambda under the hood. <em>Wins on:</em> VM-grade isolation with boot speeds that come close to containers. <em>Loses on:</em> a much narrower tooling ecosystem, since it’s built for platform operators rather than everyday developers.</p><p><strong>gVisor</strong> — A sandboxed runtime that intercepts syscalls in userspace, layered on top of containers. <em>Wins on:</em> adding an extra isolation boundary beyond what a normal container gives you — it’s what backs Google Cloud Run. <em>Loses on:</em> real performance overhead for syscall-heavy workloads.</p><blockquote><em>“Choosing a container tool isn’t about which one is ‘best.’ It’s about where you want the isolation boundary to sit — and how much overhead you’re willing to trade for it.</em></blockquote><p>The honest takeaway: <strong>Docker won developer mindshare, not because it’s the most secure or the most minimal option, but because it optimized for the develop→build→ship loop better than anything else at the time it mattered.</strong> Podman is arguably more secure by default today. Firecracker is what actually runs your serverless functions behind the scenes. Docker is what you, the human, interact with daily.</p><h3>8. Why the Industry Actually Trusts It</h3><p>Trust in Docker isn’t just habit — it’s backed by a few concrete, verifiable things:</p><p><strong>Open standards, not lock-in.</strong> Docker donated its image and runtime specifications to the <strong>Open Container Initiative (OCI)</strong> in 2015. Any OCI-compliant tool — Podman, containerd, CRI-O — can run a Docker-built image. You’re not betting on one company’s proprietary format.</p><p><strong>It became the substrate under Kubernetes.</strong> Even as the industry moved to containerd as Kubernetes’ default runtime, the <em>image format</em> Docker popularized remained the standard unit everyone builds against. Billions of container pulls happen through registries built on this format every month.</p><p><strong>Reproducibility is auditable, not promised.</strong> A Dockerfile is version-controlled, diffable, and reviewable in a pull request — same as application code. Compare that to a wiki page titled “server setup instructions” that’s three years out of date.</p><p><strong>The registry ecosystem matured into critical infrastructure.</strong> Docker Hub, GitHub Container Registry, AWS ECR, Google Artifact Registry — image distribution became boring, reliable plumbing, which is exactly what you want from infrastructure.</p><p><strong>Security tooling caught up.</strong> Image scanning (Trivy, Grype, Docker Scout), signed images (Sigstore/cosign), and rootless execution modes mean the “containers are less secure than VMs” argument from a decade ago no longer holds up unexamined.</p><blockquote><em>“Industries don’t trust tools because they’re perfect. They trust tools because the failure modes are known, documented, and survivable.”</em></blockquote><p>That’s really the crux of it. Docker isn’t trusted because it’s flawless — it’s trusted because a decade of production incidents, postmortems, and security research have mapped out exactly where its edges are, and the tooling around those edges (Podman for rootless needs, gVisor for extra sandboxing, Firecracker for multi-tenant isolation) has matured alongside it.</p><h3>Closing Thought</h3><p>Docker’s real contribution wasn’t inventing container isolation — Linux already had the primitives, and LXC had already wrapped them once. Docker’s contribution was making the <strong>build → ship → run</strong> loop so frictionless that an entire industry standardized around its image format, even when they didn’t standardize around Docker’s own daemon.</p><p>The next time you write FROM python:3.11-slim, you&#39;re not just picking a base image — you&#39;re opting into namespaces, cgroups, and a layered union filesystem that Linux has quietly supported for over a decade, now wrapped in a form simple enough to type in one line.</p><p>That’s the real engineering story here: not a new idea, but an old idea finally packaged well enough for the entire industry to build on top of it.</p><p><em>Next in this series: a deep dive into Kubernetes itself — pods, deployments, services, and the controller-based model that made it the industry’s default answer to “how do I run this at scale.”</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b53dc07ac5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Trusting Code. Start Sandboxing It.]]></title>
            <link>https://medium.com/@thealonemusk/stop-trusting-code-start-sandboxing-it-25a68cfd850c?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/25a68cfd850c</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[sandbox]]></category>
            <category><![CDATA[containerization]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Mon, 10 Aug 2026 16:25:26 GMT</pubDate>
            <atom:updated>2026-08-10T16:26:19.896Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How sandboxes, containers, and Docker quietly became the safety net of modern software.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-cwF5Qo1cVH0JF_wb9Big.png" /><figcaption>AI Generated Image</figcaption></figure><p>Picture this: you clone a random GitHub repo at 2 AM because it promises to “10x your productivity.” You run npm install. Somewhere in that dependency tree, a post-install script decides to poke around your ~/.ssh folder.</p><p>Did that just happen? Maybe. Probably not. But you don’t <em>know</em> — and that uncertainty is the whole problem.</p><blockquote><em>“The safest code is code that can’t hurt you even when it’s trying to.”</em></blockquote><p>That single idea is the reason sandboxes and containers exist. Not because developers are careless, but because trust doesn’t scale. You can’t manually audit every package, script, or AI-generated snippet you run. So instead of trusting the code, engineers learned to distrust it <em>by design</em> — and build walls around it instead.</p><p>Let’s break down how.</p><h3>1. What Even Is a “Sandbox”?</h3><p>Forget the buzzword for a second. A sandbox is exactly what it sounds like from childhood: a contained space where you can build, break, and make a mess without wrecking the living room.</p><p>In computing terms, a sandbox is an <strong>isolated execution environment</strong>. Code runs inside it, but:</p><ul><li>It can’t freely touch your file system</li><li>It can’t casually reach the network</li><li>It can’t see or modify processes outside its walls</li><li>If it crashes or misbehaves, the damage stays inside the box</li></ul><p>Your browser is already doing this right now. Every tab you open runs JavaScript in a sandbox — which is <em>exactly</em> why a malicious website can’t reach into your Downloads folder and start deleting files.</p><blockquote><em>“A sandbox doesn’t ask code to behave. It makes misbehavior physically impossible.”</em></blockquote><p>That distinction — <em>prevention</em> over <em>good intentions</em> — is the entire philosophy of secure systems design.</p><h3>2. Enter Containers: Sandboxes for Real Applications</h3><p>Browser sandboxes are great for JavaScript, but what about full applications? A Python service with fifteen dependencies. A database. A build pipeline that only works on “some guy’s machine.”</p><p>This is where <strong>containers</strong> come in and where Docker rewired how the industry ships software.</p><p>A container packages:</p><ul><li>Your application code</li><li>Its exact dependencies and versions</li><li>A minimal slice of an operating system environment</li></ul><p>…into one portable unit that runs identically everywhere — your laptop, a teammate’s machine, or a data center on the other side of the planet.</p><blockquote><em>“It works on my machine” stopped being a debugging strategy the day containers became standard.</em></blockquote><p>The magic isn’t virtualization in the traditional sense — a container isn’t a full virtual machine with its own kernel. Instead, containers use <strong>OS-level isolation</strong> (namespaces and cgroups on Linux) to <em>feel</em> like a full isolated system while sharing the host kernel underneath. That’s why containers start in milliseconds, not minutes.</p><h3>3. Docker: The Company That Made Containers Cool</h3><p>Containers technically existed before Docker (LXC, chroot jails, Solaris Zones — the concept is old). But Docker, launched in 2013, did something crucial: it made containers <strong>usable</strong>.</p><p>A single file — the Dockerfile — could now describe an entire environment:</p><pre>FROM python:3.11-slim<br>WORKDIR /app<br>COPY requirements.txt .<br>RUN pip install -r requirements.txt<br>COPY . .<br>CMD [&quot;python&quot;, &quot;app.py&quot;]</pre><p>Run docker build, then docker run, and you&#39;ve spun up an isolated, reproducible environment in seconds. No more three-page onboarding docs titled &quot;How to Set Up Your Dev Environment (Good Luck).&quot;</p><blockquote><em>“Docker didn’t invent isolation. It made isolation </em>convenient<em> — and convenience is what actually changes behavior at scale.”</em></blockquote><p>That’s the underrated lesson here: security and reproducibility tools only work if people <em>actually use them</em>. Docker won not because it was the most technically novel idea, but because it removed friction.</p><h3>4. Why This Matters More Than Ever in the AI Era</h3><p>Here’s the twist most people miss: sandboxes used to be mostly about <em>isolating untrusted humans’ code</em>. Now they’re about isolating <strong>AI-generated code</strong>.</p><p>When an AI assistant writes and executes code on your behalf — installing packages, running scripts, touching files — you’re extending trust to something that has no skin in the game and no real understanding of consequences. It might be right 99% of the time. But what about the 1%?</p><p>Sandboxes solve this beautifully:</p><ul><li>AI-generated code runs in an ephemeral container</li><li>If it does something wrong, the <em>container</em> dies — not your system</li><li>Nothing persists unless it’s explicitly written to a safe output location</li></ul><blockquote><em>“You don’t need to trust the code. You just need to make sure it can’t do anything you’d regret.”</em></blockquote><p>This is precisely why coding agents, notebooks, and cloud IDEs (think GitHub Codespaces, Replit, or Claude’s own code execution environments) now run everything inside sandboxed containers by default. It’s not paranoia — it’s just good engineering hygiene.</p><h3>5. The Core Building Blocks, Simplified</h3><p>If you strip away the jargon, sandboxing and containerization rest on a few core ideas:</p><p><strong>Isolation</strong> — Code runs in its own bubble, separate from the host system.</p><p><strong>Reproducibility</strong> — The same container behaves identically everywhere, killing “works on my machine” forever.</p><p><strong>Ephemerality</strong> — Spin it up, use it, destroy it. Nothing lingers unless you choose to keep it.</p><p><strong>Least privilege</strong> — Give the environment only the access it strictly needs — nothing more.</p><p>Every modern DevOps practice — CI/CD pipelines, Kubernetes clusters, serverless functions — is really just these four ideas, scaled up and dressed in different clothing.</p><h3>6. A Quick Mental Model</h3><p>Think of it like hotel rooms versus owning a house.</p><ul><li><strong>Your local machine</strong> = your house. Everything you own is there. One bad guest could trash it.</li><li><strong>A sandbox/container</strong> = a hotel room. Guests can rearrange the furniture, spill things, even trash the minibar — and when they check out, the room resets. Your house stays untouched.</li></ul><blockquote><em>“You don’t let strangers into your house to test if they’re trustworthy. You give them a hotel room first.”</em></blockquote><p>That’s containers, in one sentence.</p><h3>Closing Thought</h3><p>Sandboxes and Docker aren’t just DevOps trivia — they represent a quiet but fundamental shift in how software is built: from <em>“trust the code”</em> to <em>“design so trust doesn’t matter.”</em></p><p>The next time you docker run something without a second thought, remember — you&#39;re not just launching a process. You&#39;re standing inside a small, disposable universe, built specifically so that if something goes wrong in there, it <em>stays</em> in there.</p><p>And in a world where code increasingly writes itself, that boundary might be the most important line of defense we have.</p><p><em>If this piece resonated, you’ll probably enjoy digging into Kubernetes next — because once you understand a single container, orchestrating thousands of them is where things get genuinely fascinating.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25a68cfd850c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What I Actually Learned From Senior Engineers (In 1.5 Years of Watching Them Code)]]></title>
            <link>https://medium.com/@thealonemusk/what-i-actually-learned-from-senior-engineers-in-1-5-years-of-watching-them-code-99d5c4ef1efb?source=rss-dde67f5733a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/99d5c4ef1efb</guid>
            <category><![CDATA[low-level-programming]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Ashutosh Jha]]></dc:creator>
            <pubDate>Sun, 28 Jun 2026 17:30:38 GMT</pubDate>
            <atom:updated>2026-06-28T17:30:38.814Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z0dARCuYJa8WTF5DqxJa_g.png" /></figure><p>I didn’t plan to steal patterns from senior engineers.</p><p>It just happened. I’d watch someone review my code, and they’d fix something in a way that seemed overly complicated. “Why add all this error handling? Just catch the exception.” “Why split this into stages? Just do it all at once.” “Why log this? We can see it in the debugger.”</p><p>Then six months later, I’d be debugging a production incident at 2am, and I’d think: “Oh. OH. That’s why they did that.”</p><p>Over the past 1.5 years, I’ve learned more from watching how senior engineers code than from any tutorial or blog post. Not the theory. The actual <em>habits</em> that separate someone who writes code that works from someone who writes code that survives production.</p><p>Here’s what I actually stole.</p><h3>Lesson 1: Config Changes Kill Systems (And They Don’t Tell You Why)</h3><p>My first week, I wrote a config reload function. Seemed straightforward:</p><pre>void reloadConfig(const std::string&amp; filepath) {<br>    currentConfig_ = loadConfigFile(filepath);<br>    applyToSystem();<br>}</pre><p>Simple. Clean. 15 lines.</p><p>One of the seniors (let’s call him Gurpreet) was reviewing and said: “This will destroy us.”</p><p>Me: “It’s like 15 lines…”</p><p>Gurpreet: “Yeah, that’s the problem.”</p><p>He spent 20 minutes showing me his version. It was 200 lines. Validation stages. Rollback logic. Extensive logging. Guard clauses. I thought he was paranoid.</p><p>Six months later, we changed the database pool size via config. Forgot to validate it. The service crashed. All 500K concurrent users hit errors. I got paged at 2am.</p><p>While debugging, I saw Mark’s 200-line version in git history (commented out, because “it was overkill”).</p><p>I pulled it into production that night. Problem solved.</p><p>Now I understand: <strong>Config changes are some of the most dangerous operations in software. One bad value and your entire system collapses.</strong></p><p>Here’s what I learned Mark was actually doing:</p><pre>// ❌ What I wrote (destroyed our service)<br>void reloadConfig(const std::string&amp; filepath) {<br>    currentConfig_ = loadConfigFile(filepath);<br>    applyToSystem();<br>}<br><br>// ✓ What Gurpreet does (survived production)<br>bool reloadConfig(const std::string&amp; filepath) noexcept {<br>    // Step 1: Validate BEFORE touching anything<br>    auto result = validateConfigFile(filepath);<br>    if (!result) {<br>        logger_-&gt;error(&quot;Config validation failed: {}&quot;, result.error());<br>        return false;  // Old config untouched<br>    }<br>    <br>    auto newConfig = std::move(result.value());<br>    <br>    // Step 2: Test connections with new config<br>    if (!testDatabaseConnections(newConfig)) {<br>        logger_-&gt;error(&quot;Database test failed&quot;);<br>        return false;  // Still using old config<br>    }<br>    <br>    // Step 3: Atomic swap (all-or-nothing)<br>    {<br>        std::lock_guard&lt;std::mutex&gt; lock(configMutex_);<br>        logger_-&gt;info(&quot;Applying config: poolSize {} -&gt; {}&quot;,<br>                     currentConfig_.database.poolSize,<br>                     newConfig.database.poolSize);<br>        currentConfig_ = std::move(newConfig);<br>    }<br>    <br>    logger_-&gt;info(&quot;Config reload successful&quot;);<br>    return true;<br>}</pre><p>What I didn’t know: <strong>Senior engineers have been woken up at 3am because a config reload broke production. They code accordingly.</strong></p><h3>Lesson 2: Logging Isn’t Just “Nice to Have” (It’s How You Save Your Life)</h3><p>Early on, I thought logging was for debugging during development. “In production, we just have monitoring.”</p><p>Then we had a production bug: “Sometimes customer orders process as $0 instead of $50.”</p><p>I looked at the code. Nothing wrong. But I couldn’t figure out what was happening.</p><p>A senior named Priya looked at my code and asked: “What does it log when the order processing fails?”</p><p>Me: “It… doesn’t? It just skips?”</p><p>Priya: “Yeah. That’s why you can’t debug it.”</p><p>She showed me her code from another service:</p><pre>// ❌ What I was doing<br>try {<br>    processPayment(order);<br>} catch (...) {<br>    // Silent failure<br>}<br><br><br>// ✓ What Priya does<br>try {<br>    processPayment(order);<br>} catch (const std::exception&amp; e) {<br>    logger_-&gt;error(&quot;Payment processing failed&quot;,<br>        spdlog::details::make_extra_value_dict({<br>            {&quot;order_id&quot;, order.id},<br>            {&quot;customer_id&quot;, order.customerId},<br>            {&quot;amount&quot;, order.amount},<br>            {&quot;error&quot;, std::string(e.what())},<br>            {&quot;timestamp&quot;, getCurrentTimestamp()}<br>        }));<br>    <br>    // Optional: re-throw or handle<br>    if (shouldAlert(e)) {<br>        alertOncall(&quot;Payment processing error: {}&quot;, e.what());<br>    }<br>    <br>    throw;<br>}</pre><p>Two weeks later, same bug happened. This time, Priya’s logging caught it instantly:</p><pre>2024-03-15 14:23:45 Payment processing failed<br>  order_id: 12345<br>  customer_id: 789<br>  amount: 50.00<br>  error: &quot;Could not convert &#39;50.00 USD&#39; to float - expected numeric value&quot;</pre><p>Found the bug in 5 minutes. The currency symbol was breaking our parser.</p><p><strong>I learned: Logging isn’t optional. It’s how you debug production systems when you can’t attach a debugger.</strong></p><p>Now I log:</p><ul><li>State transitions (what changed?)</li><li>Error context (what data caused this?)</li><li>Timing (how long did this take?)</li><li>Preconditions (what were we expecting?)</li></ul><p>Priya once told me: “If you can’t see it in the logs, it didn’t happen.”</p><h3>Lesson 3: RAII Isn’t Some Abstract C++ Concept (It’s How You Stop Leaking Memory)</h3><p>For months, I manually managed resources:</p><pre>// ❌ What I did<br>void reconnectDatabase() {<br>    for (auto&amp; conn : connections_) {<br>        conn-&gt;close();<br>        delete conn;  // Manual cleanup<br>    }<br>    connections_.clear();<br>    <br>    for (int i = 0; i &lt; poolSize_; ++i) {<br>        auto conn = new DatabaseConnection(config_);<br>        connections_.push_back(conn);<br>    }<br>}</pre><p>Seemed fine. Until we had an incident: database connection would fail partway through, some connections would leak, and the pool would silently have fewer connections than expected.</p><p>Our senior Amit showed me his pool:</p><pre>// ✓ What Amit does<br>class DatabasePool {<br>private:<br>    using ConnPtr = std::unique_ptr&lt;DatabaseConnection&gt;;<br>    std::vector&lt;ConnPtr&gt; connections_;<br>public:<br>    bool reconfigure(const DatabaseConfig&amp; config) noexcept {<br>        try {<br>            // Create in temp vector<br>            std::vector&lt;ConnPtr&gt; newConnections;<br>            <br>            for (int i = 0; i &lt; config.poolSize; ++i) {<br>                auto conn = std::make_unique&lt;DatabaseConnection&gt;(config);<br>                <br>                if (!conn-&gt;ping()) {<br>                    return false;  // All newConnections auto-destroyed<br>                }<br>                <br>                newConnections.push_back(std::move(conn));<br>            }<br>            <br>            // Atomic swap - old connections auto-destroyed<br>            connections_ = std::move(newConnections);<br>            return true;<br>        } catch (...) {<br>            // Old connections still alive, nothing changed<br>            return false;<br>        }<br>    }<br>    <br>    // No destructor needed - unique_ptr handles cleanup<br>};</pre><p>Why this mattered: If anything threw during reconnection, the old pool stayed intact. No leaks. No partial states. The exception automatically cleaned up what it created.</p><p>I asked Amit why he did this. He said: “I’ve debugged memory leaks at 3am before. Now I don’t.”</p><p><strong>I learned: RAII isn’t some theoretical pattern. It’s how you prevent entire categories of bugs.</strong></p><h3>Lesson 4: Validation Before Applying Changes (Not After)</h3><p>This one confused me for a while.</p><p>I’d write code like:</p><pre>// ❌ My way<br>auto config = loadConfigFile(path);<br>applyConfig(config);  // What if config is invalid?<br>validateConfig(config);  // Oops, too late</pre><p>Our architect Vinesh looked at this and sighed: “If validation fails, what do you do?”</p><p>Me: “Um… rollback?”</p><p>Vinesh : “From what state?”</p><p>He showed me the proper pattern:</p><pre>// ✓ Aditya&#39;s way<br>auto result = loadAndValidateConfig(path);<br>if (!result) {<br>    logger_-&gt;error(&quot;Validation failed: {}&quot;, result.error());<br>    return false;  // Old config untouched<br>}<br><br><br>auto config = std::move(result.value());<br>// Only now, after successful validation<br>applyConfig(config);  // We know this is safe</pre><p>Why I resisted: Seemed verbose. More code. Slower.</p><p>Why it matters: One day, someone changed our config format. Didn’t update the validation. The old code would load the new config (partially) and crash. With proper validation? It would reject the config and log exactly what was wrong.</p><p><strong>I learned: Validate completely BEFORE you change anything. Otherwise you end up in impossible-to-debug states.</strong></p><h3>Lesson 5: Failing Gracefully With Timeouts (Not Waiting Forever)</h3><p>I had a service that called external APIs. Worked great for months. Then one day an external API got slow. Not broken — just slow. 30 seconds per request instead of 500ms.</p><p>My code didn’t have timeouts. So it just… waited.</p><p>Thousands of users calling my service. Thousands of threads piling up waiting for the external API. Server ran out of connection slots. My service crashed.</p><p>The external API was down, so my entire service went down. <strong>Cascading failure.</strong></p><p>A senior named Neha showed me what she does:</p><pre>// ❌ What I wrote (no timeout = cascade)<br>auto response = httpClient_.get(&quot;https://external-api.com/data&quot;);<br><br>// ✓ What Neha does<br>try {<br>    httpClient_.setConnectTimeout(std::chrono::seconds(2));<br>    httpClient_.setReadTimeout(std::chrono::seconds(3));<br>    <br>    auto response = httpClient_.get(&quot;https://external-api.com/data&quot;);<br>    <br>    if (!response) {<br>        logger_-&gt;warn(&quot;Request failed&quot;);<br>        // Try cache, or return default<br>        return getFromCache(id).value_or(defaultValue);<br>    }<br>    <br>    return response;<br>} catch (const TimeoutException&amp; e) {<br>    logger_-&gt;warn(&quot;Request timeout after 3s: {}&quot;, e.what());<br>    return getFromCache(id).value_or(defaultValue);  // Graceful degradation<br>} catch (const std::exception&amp; e) {<br>    logger_-&gt;error(&quot;Request failed: {}&quot;, e.what());<br>    return getFromCache(id).value_or(defaultValue);<br>}</pre><p>The pattern: Always set timeouts. Never wait forever. Gracefully degrade when external services fail.</p><p><strong>I learned: Your system should be resilient to other people’s failures.</strong></p><h3>Lesson 6: const Correctness (Compiler Prevents Your Bugs)</h3><p>I didn’t understand const correctness at first. Seemed like busywork.</p><p>Then I made a bug that took 3 days to find:</p><pre>void updateUser(const UserId&amp; id) {<br>    auto user = getUser(id);<br>    user.email = &quot;wrong@email.com&quot;;  // Oops, modified the wrong user<br>    // Didn&#39;t realize I was modifying user cache<br>}</pre><p>It modified the cached user object. Other threads reading the cache got corrupted data.</p><p>A senior named Anil showed me his version:</p><pre>void updateUser(const UserId&amp; id) {<br>    std::shared_lock&lt;std::shared_mutex&gt; lock(cacheMutex_);<br>    <br>    // Compiler prevents modification<br>    std::shared_ptr&lt;const User&gt; user = getUser(id);<br>    <br>    // This won&#39;t compile:<br>    // user-&gt;email = &quot;wrong@email.com&quot;;  // ❌ Compiler error<br>    <br>    // You have to do:<br>    auto modifiedUser = *user;  // Make a copy<br>    modifiedUser.email = &quot;correct@email.com&quot;;<br>    <br>    // Consciously update back<br>    lock.unlock();<br>    updateUserInDatabase(modifiedUser);<br>    updateUserInCache(id, modifiedUser);<br>}</pre><p>Anil said: “Let the compiler catch bugs you would find at 3am.”</p><p><strong>I learned: const correctness isn’t style. It’s how you prevent concurrent modification bugs.</strong></p><h3>Lesson 7: One Pattern I Didn’t Expect (Deleting Code)</h3><p>Here’s something no one tells you: senior engineers delete code constantly.</p><p>I once asked a senior why she removed 500 lines I had written.</p><p>Her: “Nobody used it.”</p><p>Me: “But we might need it later…”</p><p>Her: “Then you’ll write it when you need it. Right now, it’s just cognitive load.”</p><p>She showed me git history. That function hadn’t been called in 18 months. But:</p><ul><li>Every refactor had to consider it</li><li>New developers waste time understanding it</li><li>Bug fixes might break it</li><li>Potential security issues hiding in unused code</li></ul><p><strong>I learned: The best code is code that doesn’t exist.</strong></p><h3>Lesson 8: Code Reviews Are Free Debugging (Not Personal Attacks)</h3><p>Early in my career, I took code reviews personally.</p><p>Someone pointed out a bug and I felt attacked. “Why couldn’t I just write it right the first time?”</p><p>A mentor named Divya pulled me aside and said: “Code reviews aren’t about judging you. They’re about catching bugs before they become 3am incidents.”</p><p>Then she showed me. We reviewed code together. She wasn’t criticizing me she was teaching me to see problems I couldn’t see alone.</p><p>“Here’s what I’ve learned the hard way,” she’d say, then explain the pattern.</p><p><strong>I learned: Senior engineers aren’t being mean in code reviews. They’re sharing scars from production incidents.</strong></p><h3>The Meta-Pattern I Didn’t Expect</h3><p>After 1.5 years, I noticed something:</p><p>Every senior engineer writes code like they’ve already seen it break.</p><ul><li>They validate before applying changes (because config reload broke production)</li><li>They log aggressively (because they debugged silent failures)</li><li>They use RAII (because they’ve cleaned up memory leaks)</li><li>They set timeouts (because external services failed)</li><li>They use const (because they debugged concurrent modification)</li><li>They delete unused code (because they’ve maintained legacy systems)</li></ul><p><strong>They’re not paranoid. They’re experienced.</strong></p><p>The patterns aren’t theoretical. They’re practical responses to real failures.</p><h3>How to Actually Steal These Patterns</h3><p>Here’s what I actually did to learn:</p><ul><li><strong>Watched code reviews</strong> — I’d sit with seniors during reviews. Asked “why” constantly.</li><li><strong>Read git history</strong> — Found commits that fixed bugs. Asked “what went wrong?” Then I saw the pattern they added to prevent it.</li><li><strong>Attended incident post-mortems</strong> — This was eye-opening. “Oh, this incident could have been caught by logging?” Then I saw why they log so much.</li><li><strong>Did code reviews with them</strong> — When they reviewed my code, I didn’t argue. I asked why. They’d tell me stories.</li><li><strong>Made mistakes myself</strong> — Wrote bad code. Got paged at 2am. Fixed it. Learned the lesson forever.</li><li><strong>Asked “Why?”</strong> — Constantly. “Why add all this error handling?” “Why validate before applying?” Every answer was a story about production breaking.</li></ul><h3>The Honest Truth</h3><p>I didn’t steal these patterns from books or tutorials.</p><p>I stole them by watching people who’d already paid the price of learning them the hard way.</p><p>Every “paranoid” pattern came from someone debugging production at 3am.</p><p>Every “overly verbose” validation came from someone getting paged because a config broke everything.</p><p>Every “unnecessary” log message came from someone unable to debug a silent failure.</p><p><strong>Senior engineers aren’t smarter. They’ve just already broken production. Multiple times.</strong></p><p>Now go find a senior engineer in your codebase. Watch how they code. Ask why. Listen to the stories.</p><p>In 1.5 years, you’ll have stolen more valuable patterns than any course could teach you.</p><p>And maybe, you’ll avoid some of the 3am incidents that taught them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99d5c4ef1efb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>