<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>polycratia — build log</title>
  <link>https://polycratia.com/blog/</link>
  <description>Build log — first-person notes from production: payments, crypto rails, marketplaces, LLM pipelines.</description>
  <language>en</language>
  <atom:link href="https://polycratia.com/feed.xml" rel="self" type="application/rss+xml"/>
  <item>
    <title>Why a scheduled JWKS refresh skips the throttle and still spends it</title>
    <link>https://polycratia.com/blog/why-a-scheduled-jwks-refresh-skips-the-throttle-and-still-spends-it/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/why-a-scheduled-jwks-refresh-skips-the-throttle-and-still-spends-it/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>A resource server that verifies JWTs against a remote key set has two reasons to go fetch that key set: a token arrived naming a key id the server does not hold, or the server&#x27;s own ticker came…</description>
    <content:encoded><![CDATA[<p>A resource server that verifies JWTs against a remote key set has two reasons to go fetch that key set: a token arrived naming a key id the server does not hold, or the server's own ticker came round. Those two triggers want opposite throttling rules, and they share one piece of state: the time the issuer was last asked. Conflate them and you get one of two bad outcomes, either a key set that never catches a rotation, or an issuer that anyone can drive into the ground by minting tokens with invented key ids.</p>
<p>I hit this writing the JWKS cache in <a href="https://github.com/polycratia/mcpaudience">mcpaudience</a>, the resource-server half of MCP authorization. The resolution fits in one function signature. The interesting part sits elsewhere: in what the two triggers are forced to share.</p>
<h2 id="the-gate-belongs-to-the-arriving-token">The gate belongs to the arriving token<a class="hlink" href="#the-gate-belongs-to-the-arriving-token" title="Link to this section">#</a></h2>
<p>A token shows up with <code>kid: rsa-9</code>. The server does not hold <code>rsa-9</code>. There are two explanations and the server cannot tell them apart without asking: either the issuer rotated and the copy in memory is behind, or nobody ever published that key id at all. That asymmetry is the whole problem. Minting a token with a random <code>kid</code> costs a caller nothing, and it does not even have to be signed by anything real, because the signature gets checked after the key is found. The fetch it provokes costs an HTTPS round trip to the authorization server. Without a floor, an unauthenticated caller turns one cheap loop into sustained load at the issuer, and the first thing that breaks is token verification for every legitimate request.</p>
<p>So the request path gets a floor: <code>MinRefresh</code>, 30 seconds by default. Not a cache TTL. A minimum gap between two attempts to reach the issuer.</p>
<h2 id="the-tickers-period-is-already-its-bound">The ticker's period is already its bound<a class="hlink" href="#the-tickers-period-is-already-its-bound" title="Link to this section">#</a></h2>
<p>The scheduled fetch has none of that exposure. Nobody outside the process can make it happen more often, because the only thing that triggers it is a ticker the server configured. Its period <em>is</em> the rate limit, and the way to ask the issuer less often is to lengthen the period. Put a second gate underneath it and a configured period shorter than <code>MinRefresh</code> silently becomes <code>MinRefresh</code>: two knobs, one of them lying.</p>
<p>Which leaves one decision. The fetch has to know who asked. In <code>jwks.go</code> that is a named boolean, not a flag threaded through four call sites:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// trigger is what asked for a fetch, which is what decides whether MinRefresh</span>
<span class="c1">// has anything to say about it.</span>
<span class="kd">type</span><span class="w"> </span><span class="nx">trigger</span><span class="w"> </span><span class="kt">bool</span>

<span class="kd">const</span><span class="w"> </span><span class="p">(</span>
<span class="w">    </span><span class="nx">arrivingToken</span><span class="w"> </span><span class="nx">trigger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="nx">ownSchedule</span><span class="w">   </span><span class="nx">trigger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span>
<span class="p">)</span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">j</span><span class="w"> </span><span class="o">*</span><span class="nx">JWKS</span><span class="p">)</span><span class="w"> </span><span class="nx">refresh</span><span class="p">(</span><span class="nx">ctx</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">Context</span><span class="p">,</span><span class="w"> </span><span class="nx">kid</span><span class="w"> </span><span class="kt">string</span><span class="p">,</span><span class="w"> </span><span class="nx">asked</span><span class="w"> </span><span class="nx">trigger</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="nx">KeySetRefresh</span><span class="p">,</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">now</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">now</span><span class="p">()</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">since</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">now</span><span class="p">.</span><span class="nx">Sub</span><span class="p">(</span><span class="nx">j</span><span class="p">.</span><span class="nx">attempted</span><span class="p">);</span><span class="w"> </span><span class="nx">asked</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">arrivingToken</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="o">!</span><span class="nx">j</span><span class="p">.</span><span class="nx">attempted</span><span class="p">.</span><span class="nx">IsZero</span><span class="p">()</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">since</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">minRefresh</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">tooSoon</span><span class="p">{</span><span class="nx">since</span><span class="p">:</span><span class="w"> </span><span class="nx">since</span><span class="p">,</span><span class="w"> </span><span class="nx">interval</span><span class="p">:</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">minRefresh</span><span class="p">()}</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="c1">// Recorded before the fetch rather than after it, so an attempt that fails</span>
<span class="w">    </span><span class="c1">// or is abandoned spends the interval too.</span>
<span class="w">    </span><span class="nx">j</span><span class="p">.</span><span class="nx">attempted</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">now</span>
<span class="w">    </span><span class="nx">fetch</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">KeySetRefresh</span><span class="p">{</span><span class="nx">KeyID</span><span class="p">:</span><span class="w"> </span><span class="nx">kid</span><span class="p">,</span><span class="w"> </span><span class="nx">At</span><span class="p">:</span><span class="w"> </span><span class="nx">now</span><span class="p">}</span>

<span class="w">    </span><span class="nx">body</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">ctx</span><span class="p">)</span>
<span class="w">    </span><span class="c1">// ...</span>
<span class="p">}</span></code></pre></figure>
<p>The two callers differ in exactly two arguments. The request path reaches it through the key lookup, with the <code>kid</code> the token named and <code>arrivingToken</code>. The schedule reaches it with no <code>kid</code> at all:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">j</span><span class="w"> </span><span class="o">*</span><span class="nx">JWKS</span><span class="p">)</span><span class="w"> </span><span class="nx">scheduledRefresh</span><span class="p">(</span><span class="nx">ctx</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">Context</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="nx">KeySetRefresh</span><span class="p">,</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">j</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Lock</span><span class="p">()</span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">j</span><span class="p">.</span><span class="nx">refresh</span><span class="p">(</span><span class="nx">ctx</span><span class="p">,</span><span class="w"> </span><span class="s">""</span><span class="p">,</span><span class="w"> </span><span class="nx">ownSchedule</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>The empty key id is not a placeholder. It is reported as-is through <code>OnRefresh</code>, which is how an operator reading the fetch log tells a rotation the server went looking for from one a token dragged out of it: <code>KeyID</code> set means some request wanted that key, <code>KeyID</code> empty means the ticker.</p>
<h2 id="what-both-kinds-share-is-the-record-of-the-attempt">What both kinds share is the record of the attempt<a class="hlink" href="#what-both-kinds-share-is-the-record-of-the-attempt" title="Link to this section">#</a></h2>
<p>This part took me a second pass to get right. The gate is skipped for the scheduled fetch, but the line above it, <code>j.attempted = now</code>, is not. Every fetch records its attempt, whoever asked for it.</p>
<p>That is deliberate, and it follows from what the request path is actually reading. It is not reading <em>who</em> asked. It is reading <em>when the issuer was last asked</em>, because that is the only thing that bounds load at the issuer. A scheduled fetch that went out two seconds ago has already spent that round trip, and a token arriving now with an unknown key id has no claim to another one. So a tick holds the request path off for the whole interval, exactly like a token-triggered fetch would:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// The attempt it recorded is what holds the request path off: a key id the</span>
<span class="c1">// refreshed set does not name does not start a fetch of its own.</span>
<span class="nx">clock</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">now</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Minute</span><span class="p">)</span>
<span class="k">if</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">set</span><span class="p">.</span><span class="nx">Key</span><span class="p">(</span><span class="nx">context</span><span class="p">.</span><span class="nx">Background</span><span class="p">(),</span><span class="w"> </span><span class="s">"rsa-9"</span><span class="p">);</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">t</span><span class="p">.</span><span class="nx">Fatal</span><span class="p">(</span><span class="s">"a key id nobody published was answered"</span><span class="p">)</span>
<span class="p">}</span>
<span class="k">if</span><span class="w"> </span><span class="nx">got</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">issuer</span><span class="p">.</span><span class="nx">count</span><span class="p">();</span><span class="w"> </span><span class="nx">got</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">t</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"the issuer was asked %d times after the scheduled fetch recorded its attempt, want twice"</span><span class="p">,</span><span class="w"> </span><span class="nx">got</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>The same reasoning covers the failure cases, and that is where the ordering matters most: the attempt gets written down before the fetch goes out, not after. A fetch that got a 500, and a fetch whose request context was cancelled before it finished, both spend the interval, because what costs the issuer is the attempt and not the result. An issuer that is down is precisely the situation where refetching on every arriving token does the most damage.</p>
<p>Both refusals carry the wait they imply, so the HTTP layer does not have to guess:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="nx">tooSoon</span><span class="p">)</span><span class="w"> </span><span class="nx">RetryAfter</span><span class="p">()</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Duration</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">interval</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">since</span><span class="w"> </span><span class="p">}</span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">u</span><span class="w"> </span><span class="nx">unreachable</span><span class="p">)</span><span class="w"> </span><span class="nx">RetryAfter</span><span class="p">()</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Duration</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">u</span><span class="p">.</span><span class="nx">interval</span><span class="w"> </span><span class="p">}</span></code></pre></figure>
<p>A declined fetch names what is left of the interval. A fetch that went out and failed names the whole of it. The guard turns an <code>ErrUnavailable</code> into a 503 with that <code>Retry-After</code>, rounding the division up, because a client told to wait <code>0</code> comes straight back and is told <code>0</code> again. Note also that the floor is inclusive: a gap of exactly <code>MinRefresh</code> is no longer shorter than it, so a client that waits precisely as long as it was told gets through rather than being told to wait the interval over again.</p>
<h2 id="a-ticker-is-infrastructure-so-it-gets-checked-like-infrastructure">A ticker is infrastructure, so it gets checked like infrastructure<a class="hlink" href="#a-ticker-is-infrastructure-so-it-gets-checked-like-infrastructure" title="Link to this section">#</a></h2>
<p>The schedule is written out in the package rather than left to the caller, and what that mainly gets you is sane behaviour when things break.</p>
<p><code>Start(ctx, every)</code> validates the key set URL <em>before</em> it starts the goroutine. A key set URL that is not HTTPS is refused at startup, as a returned error, rather than producing a goroutine that fails quietly once a period into a log nobody is reading. A zero <code>every</code> becomes <code>CacheFor</code>, 15 minutes by default, which makes that field the ticker period and nothing else.</p>
<p>Nothing is fetched before the first tick. The request path still fetches on demand, so startup is not blocked on the issuer being up. The ticker is there to meet rotations without a token having to ask for them.</p>
<p>On each tick the goroutine checks <code>ctx.Err()</code> before refreshing, because a tick and a stop can both be ready at once, and a fetch on a cancelled context fails immediately while still spending <code>MinRefresh</code> for every request queued behind it. Then stopping:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="k">return</span><span class="w"> </span><span class="kd">func</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">cancel</span><span class="p">()</span>
<span class="w">    </span><span class="o">&lt;-</span><span class="nx">done</span>
<span class="p">},</span><span class="w"> </span><span class="kc">nil</span></code></pre></figure>
<p><code>stop</code> cancels the derived context and waits on the done channel, so a fetch in flight has returned before <code>stop</code> does. A server that has shut down is not still asking its issuer for keys, and a test that stops the ticker is not racing a goroutine that still holds the mutex.</p>
<h2 id="what-i-would-weigh-differently">What I would weigh differently<a class="hlink" href="#what-i-would-weigh-differently" title="Link to this section">#</a></h2>
<p>The cost of recording the attempt before the fetch is that recovery waits for the interval to lapse rather than for the next request. When the issuer comes back up, the first arriving token inside the interval is still refused with a 503 even though a fetch would now succeed. I took that trade knowingly, since the alternative is that an outage plus traffic equals a retry storm, but it is a real cost, and on a key set with a long <code>MinRefresh</code> it is probably the thing that will look like a bug to whoever is on call.</p>
<p>The other tension is that one <code>attempted</code> field serves two policies. Separating them, a budget for scheduled fetches and another for token-triggered ones, is not what this package does, and I would want a concrete reason before adding it, because the moment the two are independent the issuer no longer has a single bound on how often it is asked.</p>
<p>One clock, two triggers, one record of the attempt. The gate knows who asked; the interval does not care.</p>]]></content:encoded>
  </item>
  <item>
    <title>Two weeks into the rewrite I tried to prove it was correct, and I…</title>
    <link>https://polycratia.com/blog/2026-11-18-two-weeks-into-the-rewrite-i-tried-to-pr/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-two-weeks-into-the-rewrite-i-tried-to-pr/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>Two weeks into the rewrite I tried to prove it was correct, and I couldn&#x27;t.</description>
    <content:encoded><![CDATA[<p>Two weeks into the rewrite I tried to prove it was correct, and I couldn't.</p>
<p>Inherited monolith, Python 2, one module that priced and posted charges. I was standing up the replacement as a separate service, with the plan every engineer makes: run both against the same input, diff the results, cut over when the diff goes empty.</p>
<p>Then I read the old code properly.</p>
<p>It didn't just compute. It wrote. Asking it for a price advanced counters, stamped records, consumed a sequence. There was no way to ask that system a question without changing the answer.</p>
<p>So shadow mode was out. The diff was out. The only verification left was deploy it and watch.</p>
<p>Pulling the write side out of the read path, in Python 2 code I was about to delete anyway, cost me more calendar time than writing the new service did. All of it spent buying the right to compare.</p>
<p>People ask five questions before a rewrite: what does it cost, who owns it, what's the deadline, what's the risk, what do we freeze. I now ask a different one first: can the old system answer a question without side effects?</p>
<p>If the answer is no, then the work that comes first is not new code. It's making the old thing observable: a pure read path, replayable inputs, an output you can set next to another output.</p>
<p>Rewrites rarely die in the writing. They die at the moment you can no longer tell which version is right, and the decision turns into a vote instead of a measurement.</p>
<p>If you've replaced a money-adjacent service, I'd like to know how you actually proved the new one matched: shadow traffic, replay of historical inputs, or a slow cutover with someone checking by hand.</p>]]></content:encoded>
  </item>
  <item>
    <title>Three seconds on an API response is rarely slow code. It&#x27;s a chain of…</title>
    <link>https://polycratia.com/blog/2026-11-18-three-seconds-on-an-api-response-is-rare/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-three-seconds-on-an-api-response-is-rare/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>Three seconds on an API response is rarely slow code. It&#x27;s a chain of network calls that someone decided the request had to block on.</description>
    <content:encoded><![CDATA[<p>Three seconds on an API response is rarely slow code. It's a chain of network calls that someone decided the request had to block on.</p>
<p>When I profile an endpoint like that, the handler itself is usually trivial. The time sits in two places.</p>
<p>First, serialization. The query looks fine on its own, then the serializer walks relations and fires one more query per row. That loop never shows up in a diff, so review tends to slide past it. It exists only at runtime, and it scales with the data a real account carries, not the data the fixtures had.</p>
<p>Second, outbound calls that ended up inline because at the time that was the simplest thing: a verification provider, a carrier rate quote, a price lookup. Each one is acceptable by itself. Stacked in sequence inside one request, they define the response time, and your service inherits the worst latency among the dependencies it touches.</p>
<p>So the fix is not usually an optimization pass. It's a modeling decision: what does the caller actually need to know right now, and what does the system merely need to know eventually.</p>
<p>In payment and compliance flows I treat that split as part of the design. Persist the intent, return an explicit state, and hand the slow dependency to a worker that drives that state forward. The endpoint gets fast because it stopped pretending to be synchronous, not because the code got cleverer. Each outbound call gets a timeout and a state of its own, so a degraded provider shows up as a pending record instead of a hanging request.</p>
<p>The measurement habit that makes this visible: stop tracking endpoint total latency as one number. Track time per dependency inside the request. Total latency tells you that something is slow. Per-dependency timing tells you which decision to reverse.</p>
<p>Most of the multi-second endpoints I've inherited were not performance problems. They were boundary problems wearing a performance costume.</p>
<p>There is probably a dependency sitting in your request path right now that doesn't need to be there before you respond...</p>]]></content:encoded>
  </item>
  <item>
    <title>The ledger was off by a few cents on one repayment, and I came close…</title>
    <link>https://polycratia.com/blog/2026-11-18-the-ledger-was-off-by-a-few-cents-on-one/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-the-ledger-was-off-by-a-few-cents-on-one/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>The ledger was off by a few cents on one repayment, and I came close to closing the ticket as rounding noise.</description>
    <content:encoded><![CDATA[<p>The ledger was off by a few cents on one repayment, and I came close to closing the ticket as rounding noise.</p>
<p>The deal had multiple investors, each owed a pro-rata share of what the borrower paid. The code did the straightforward thing: take each investor's fraction, multiply, round to the nearest minor unit. Every payout looked right. Taken alone, each one was right.</p>
<p>The sum was not. Depending on the split, the parts landed a hair over or under the amount actually received. No exception, no alert, just a ledger that quietly stopped conserving money, once per repayment, across every active deal.</p>
<p>It surfaced during reconciliation against the bank side, and by then I was not holding a bug report. I was holding a backfill: recompute historical distributions, decide who absorbs each residual, and explain the drift to people whose entire job is trusting those numbers.</p>
<p>So distribution I now treat as an allocation problem rather than a multiplication problem. Integer minor units only. Compute the shares, then hand out the leftover units deterministically by a fixed ordering rule that someone wrote down on purpose. Assert that the parts sum to the whole before anything is persisted, and fail loudly when they do not.</p>
<p>Rounding bugs hide well because every individual row passes review. The damage lives in the sum, and no normal code path ever looks at the sum.</p>
<p>If you split money between parties, your residual goes somewhere: I think it is worth checking whether that somewhere was documented, or just accidental...</p>]]></content:encoded>
  </item>
  <item>
    <title>The agent passed every test I wrote. Then it went live against the…</title>
    <link>https://polycratia.com/blog/2026-11-18-the-agent-passed-every-test-i-wrote-then/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-the-agent-passed-every-test-i-wrote-then/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>The agent passed every test I wrote. Then it went live against the real catalog feed and started confidently describing products that did not exist.</description>
    <content:encoded><![CDATA[<p>The agent passed every test I wrote. Then it went live against the real catalog feed and started confidently describing products that did not exist.</p>
<p>Not hallucinating from nothing. Hallucinating from garbage.</p>
<p>My fixtures were product descriptions I had picked by hand: clean text, one language, complete attributes. Production input came from retail catalog parsers, and parsers break quietly. A truncated field. Leftover HTML. A size chart flattened onto one line. Two languages in the same description. A unit that changed without anyone noticing.</p>
<p>The model never objected. It has no concept of "this is not a product description." Whatever arrived, it produced something fluent and plausible, and fluent-and-plausible is what a reviewer skims past...</p>
<p>The cost was pulling a batch of listings back and reprocessing them. What stung more was losing the assumption that output could be sampled instead of checked.</p>
<p>What I do differently now: the model sits in the middle of the pipeline, never at the edge. A gate in front decides whether an input is even eligible to be processed. Hard post-conditions sit behind it: numbers, units, SKUs and required attributes have to survive the transformation unchanged, and anything that fails gets routed to review instead of published. My test inputs are sampled from live parser output now, not written by me.</p>
<p>The uncomfortable part is that my test set was not too small. It was too clean.</p>
<p>If you run an LLM step in production, I'd want to know what you assert about its output that does not depend on a human reading it.</p>]]></content:encoded>
  </item>
  <item>
    <title>In payments, the service that gets missed in an audit is the one off…</title>
    <link>https://polycratia.com/blog/2026-11-18-in-payments-the-service-that-gets-missed/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-in-payments-the-service-that-gets-missed/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>In payments, the service that gets missed in an audit is the one off the request path: the nightly reconciliation batch, the notification worker, the file service nobody has redeployed in a year.</description>
    <content:encoded><![CDATA[<p>In payments, the service that gets missed in an audit is the one off the request path: the nightly reconciliation batch, the notification worker, the file service nobody has redeployed in a year.</p>
<p>India Today reports that researchers, after PHP, turned AI on Java and say 90% of Fortune 500 companies may be at risk. Separately, BleepingComputer reports that hackers stole payment card details entered at Fandango's checkout. Two unrelated stories, and scanning your main app answers neither of them: you need a list of every JVM process you actually run, and of every page where a card number actually gets typed.</p>
<p>I take that audit on, over payment and account flows end to end: services and dependencies mapped, then patched. A small scope takes days, not months. Message me and you get cost and timeline within a day.</p>]]></content:encoded>
  </item>
  <item>
    <title>A key that can trade can usually move money too, and someone made…</title>
    <link>https://polycratia.com/blog/2026-11-18-a-key-that-can-trade-can-usually-move-mo/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-18-a-key-that-can-trade-can-usually-move-mo/</guid>
    <pubDate>Wed, 18 Nov 2026 09:00:00 +0000</pubDate>
    <description>A key that can trade can usually move money too, and someone made that design decision quietly.</description>
    <content:encoded><![CDATA[<p>A key that can trade can usually move money too, and someone made that design decision quietly.</p>
<p>CoinDesk reports that the decentralized exchange Lighter suspended all withdrawals after a $10.4 million exploit. The team blamed a leaked trading API key and said its smart contracts were unaffected, while investigators traced the funds to Hyperliquid. Three independent sources covered it.</p>
<p>So the contract held and the credential did not. That is the part worth sitting with: audits read on-chain logic, but the keys live in your backend, your CI, your ops scripts and someone's laptop. In the custodial wallet and exchange work I have done, the controls that actually hold are unglamorous: keys scoped to exactly one capability, IP-bound where the venue allows it, withdrawal paths kept apart from trading paths, per-window limits that fail closed, and a kill switch someone has actually pulled in a drill rather than written into a runbook.</p>
<p>I do this as a focused review of key custody and withdrawal flows for exchange, wallet and payment systems: where every credential lives, what it is allowed to do, and what happens in the first ten minutes after one leaks. Scope is honest. A review like this takes days, not months, and you get cost and timeline in writing within a day.</p>
<p>If you run a venue or a wallet and want that review, message me and tell me which flows move money.</p>]]></content:encoded>
  </item>
  <item>
    <title>core.fsmonitor is a program exactly when git cannot read it as a boolean</title>
    <link>https://polycratia.com/blog/core-fsmonitor-is-a-program-exactly-when-git-cannot-read-it-as-a-boole/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/core-fsmonitor-is-a-program-exactly-when-git-cannot-read-it-as-a-boole/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>A repository arrives as plain files, and it brings its own configuration with it. A few of those keys name programs git starts before you have typed anything more adventurous than git status.…</description>
    <content:encoded><![CDATA[<p>A repository arrives as plain files, and it brings its own configuration with it. A few of those keys name programs git starts before you have typed anything more adventurous than <code>git status</code>. <code>core.fsmonitor</code> is the awkward one: the same key holds either a boolean that selects the monitor built into git, or the command git runs as a hook. Which one you are looking at is not a judgement call, it is a parse, and the only parser whose opinion counts is the one inside the binary that will later execute the value.</p>
<p>That is the rule I kept landing on while writing <a href="https://github.com/polycratia/gitdisarm">gitdisarm</a>, which reads a repository's git configuration and prints every program that configuration would make git run, each with the override that takes it away. With <code>core.fsmonitor</code> the temptation is a heuristic: if the value looks like a path, or has a slash in it, or is not one of six known words, call it a command. A heuristic of that shape is wrong in both directions at once. It flags a <code>TRUE</code> that git reads as a plain boolean, and it waves through a value git refuses to read as a boolean and therefore hands to a shell. Both failures are quiet. Only one of them matters.</p>
<h2 id="two-rows-for-one-key-and-the-order-between-them">Two rows for one key, and the order between them<a class="hlink" href="#two-rows-for-one-key-and-the-order-between-them" title="Link to this section">#</a></h2>
<p>The catalog in <code>catalog/catalog.go</code> is data: one row per key pattern, carrying the family the key belongs to and the override that disarms it. <code>core.fsmonitor</code> gets two rows. The first is conditional, matching only when the value is one git reads as a boolean, and it classifies the entry <code>Inert</code>. The second is unconditional, classifies the entry <code>Executes</code>, and carries the override <code>-c core.fsmonitor=false</code>. <code>Classify</code> walks the rows in order, and the first match wins.</p>
<p>The ordering is the design. The narrow conditional row is the exception, the broad unconditional row is the rule, and the fallthrough points towards reporting rather than away from it. A value the condition cannot vouch for does not land in some third bucket of things the tool is unsure about. It lands on the row that says git will run this.</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="nx">Rule</span><span class="p">)</span><span class="w"> </span><span class="nx">matches</span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="nx">gitconfig</span><span class="p">.</span><span class="nx">Entry</span><span class="p">)</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">Section</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">section</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">key</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s">""</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">Key</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">key</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">sub</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">withSub</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">Subsection</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">""</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">sub</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">withoutSub</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">Subsection</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s">""</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">switch</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">value</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">boolValue</span><span class="p">:</span>
<span class="w">        </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">parseBool</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">Value</span><span class="p">)</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">ok</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">bangValue</span><span class="p">:</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">HasPrefix</span><span class="p">(</span><span class="nx">strings</span><span class="p">.</span><span class="nx">TrimLeft</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">Value</span><span class="p">,</span><span class="w"> </span><span class="s">" \t"</span><span class="p">),</span><span class="w"> </span><span class="s">"!"</span><span class="p">)</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span>
<span class="p">}</span></code></pre></figure>
<p>Look at what the <code>boolValue</code> case throws away. It discards the truth and keeps only <code>ok</code>. Whether the monitor ends up enabled or disabled is git's business and no concern of a tool that is not going to run anything. Whether the value was readable as a boolean at all is the only question the catalog has to answer. The same two-row shape serves <code>pager.&lt;name&gt;</code> and <code>alias.&lt;name&gt;</code>, where the condition is <code>bangValue</code> (an expansion beginning with <code>!</code>) instead.</p>
<h2 id="the-condition-is-gits-grammar-including-the-parts-nobody-quotes">The condition is git's grammar, including the parts nobody quotes<a class="hlink" href="#the-condition-is-gits-grammar-including-the-parts-nobody-quotes" title="Link to this section">#</a></h2>
<p><code>parseBool</code> mirrors git's reader rather than approximating it:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="kd">func</span><span class="w"> </span><span class="nx">parseBool</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nx">truth</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="kt">bool</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">switch</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">ToLower</span><span class="p">(</span><span class="nx">v</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="s">""</span><span class="p">:</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="s">"true"</span><span class="p">,</span><span class="w"> </span><span class="s">"yes"</span><span class="p">,</span><span class="w"> </span><span class="s">"on"</span><span class="p">:</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="s">"false"</span><span class="p">,</span><span class="w"> </span><span class="s">"no"</span><span class="p">,</span><span class="w"> </span><span class="s">"off"</span><span class="p">:</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">parseInt</span><span class="p">(</span><span class="nx">v</span><span class="p">)</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span>
<span class="p">}</span></code></pre></figure>
<p>Two things in those few lines are easy to get wrong. The empty value is a boolean, and it is false: a line reading <code>core.fsmonitor =</code> is not an empty command. And the words are matched whole, against the whole value lowercased, with no trimming at all. Git compares the words and scans the integers, and those are different operations with different tolerances. The scanner below accepts leading whitespace, the comparison above does not. A tool that helpfully trimmed before comparing would disagree with the thing that executes the value, which is the only disagreement that can hurt anyone.</p>
<h2 id="the-integer-scanner-is-where-the-surprises-live">The integer scanner is where the surprises live<a class="hlink" href="#the-integer-scanner-is-where-the-surprises-live" title="Link to this section">#</a></h2>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="kd">func</span><span class="w"> </span><span class="nx">parseInt</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kt">int64</span><span class="p">,</span><span class="w"> </span><span class="kt">bool</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">31</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span>
<span class="w">    </span><span class="nx">s</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">TrimLeft</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span><span class="w"> </span><span class="s">" \t\n\v\f\r"</span><span class="p">)</span>
<span class="w">    </span><span class="nx">negative</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">s</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s">""</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="p">(</span><span class="nx">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="sc">'+'</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="sc">'-'</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">negative</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="sc">'-'</span>
<span class="w">        </span><span class="nx">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">base</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">10</span>
<span class="w">    </span><span class="k">switch</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">HasPrefix</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="w"> </span><span class="s">"0x"</span><span class="p">),</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">HasPrefix</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="w"> </span><span class="s">"0X"</span><span class="p">):</span>
<span class="w">        </span><span class="nx">base</span><span class="p">,</span><span class="w"> </span><span class="nx">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">16</span><span class="p">,</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="sc">'0'</span><span class="p">:</span>
<span class="w">        </span><span class="nx">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">n</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">digitRun</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="w"> </span><span class="nx">base</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">val</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">strconv</span><span class="p">.</span><span class="nx">ParseUint</span><span class="p">(</span><span class="nx">s</span><span class="p">[:</span><span class="nx">n</span><span class="p">],</span><span class="w"> </span><span class="nx">base</span><span class="p">,</span><span class="w"> </span><span class="mi">64</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">factor</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kt">uint64</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="w">    </span><span class="k">switch</span><span class="w"> </span><span class="nx">unit</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">s</span><span class="p">[</span><span class="nx">n</span><span class="p">:];</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">unit</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s">""</span><span class="p">:</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">EqualFold</span><span class="p">(</span><span class="nx">unit</span><span class="p">,</span><span class="w"> </span><span class="s">"k"</span><span class="p">):</span>
<span class="w">        </span><span class="nx">factor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1024</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">EqualFold</span><span class="p">(</span><span class="nx">unit</span><span class="p">,</span><span class="w"> </span><span class="s">"m"</span><span class="p">):</span>
<span class="w">        </span><span class="nx">factor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1024</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">1024</span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="nx">strings</span><span class="p">.</span><span class="nx">EqualFold</span><span class="p">(</span><span class="nx">unit</span><span class="p">,</span><span class="w"> </span><span class="s">"g"</span><span class="p">):</span>
<span class="w">        </span><span class="nx">factor</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1024</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">1024</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">1024</span>
<span class="w">    </span><span class="k">default</span><span class="p">:</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">val</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nx">max</span><span class="o">/</span><span class="nx">factor</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">val</span><span class="w"> </span><span class="o">*=</span><span class="w"> </span><span class="nx">factor</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">negative</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="o">-</span><span class="kt">int64</span><span class="p">(</span><span class="nx">val</span><span class="p">),</span><span class="w"> </span><span class="kc">true</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="kt">int64</span><span class="p">(</span><span class="nx">val</span><span class="p">),</span><span class="w"> </span><span class="kc">true</span>
<span class="p">}</span></code></pre></figure>
<p>Four properties of that function decide what the report says. The prefix picks the base: <code>0x</code> or <code>0X</code> is hexadecimal, and a leading <code>0</code> followed by at least one more digit is octal. <code>digitRun</code> demands at least one digit valid in that base, so a value that is all prefix and no number gets refused. Exactly one <code>k</code>,<code>m</code> or <code>g</code>, in either case, multiplies by 1024, 1024 squared or 1024 cubed, and anything else left over after the digits refuses the whole value: a second unit, a stray letter, a space that survived quoting. Finally the result is bounded by <code>1&lt;&lt;31 - 1</code> after the factor is applied, tested as <code>val &gt; max/factor</code> before the multiplication, so you cannot jump the bound by overflowing into it.</p>
<p>That last property has a consequence worth stating plainly: <code>1g</code> is a boolean and <code>2g</code> is not.</p>
<table>
<thead>
<tr>
<th>value</th>
<th>git's reader</th>
<th>gitdisarm</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>TRUE</code>,<code>On</code>,<code>no</code>, empty</td>
<td>boolean</td>
<td>inert, not printed</td>
</tr>
<tr>
<td><code>0x10</code></td>
<td>hexadecimal 16, true</td>
<td>inert</td>
</tr>
<tr>
<td><code>010</code></td>
<td>octal 8, true</td>
<td>inert</td>
</tr>
<tr>
<td><code>08</code></td>
<td>base 8 stops after <code>0</code>; <code>8</code> is trailing text</td>
<td>reported as a program</td>
</tr>
<tr>
<td><code>16k</code></td>
<td>16384, true</td>
<td>inert</td>
</tr>
<tr>
<td><code>1g</code></td>
<td>1073741824, true</td>
<td>inert</td>
</tr>
<tr>
<td><code>2g</code></td>
<td>over the <code>1&lt;&lt;31 - 1</code> bound</td>
<td>reported as a program</td>
</tr>
<tr>
<td><code>-1</code></td>
<td>integer, non-zero, true</td>
<td>inert</td>
</tr>
<tr>
<td><code>"true "</code> quoted, trailing space</td>
<td>neither a whole word nor an integer</td>
<td>reported as a program</td>
</tr>
</tbody>
</table>
<p>The quoted row is the one that earns the exactness. The configuration parser decides which bytes reach the value, and a quoted value keeps its spaces. A value of <code>true</code> is therefore reachable from a file, is not a boolean to git, and is text git would hand to a shell. <code>2g</code> looks absurd in a report until you remember that the overflow bound is part of the grammar. A tool that treats that grammar as roughly numbers and some words will disagree with git exactly at the values an attacker gets to choose.</p>
<h2 id="what-the-report-can-say-because-the-condition-is-exact">What the report can say because the condition is exact<a class="hlink" href="#what-the-report-can-say-because-the-condition-is-exact" title="Link to this section">#</a></h2>
<p>Two things hold at once here, and no heuristic delivers them together. A mixed-case <code>TRUE</code>, an <code>Off</code>, a <code>16k</code> come back inert and never get printed, which is what keeps a report short enough that somebody reads it. And everything the grammar refuses is printed as a finding whose text says git cannot read the value as a boolean, carrying the value exactly as the file wrote it, with the override beside it.</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="nx">f</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">catalog</span><span class="p">.</span><span class="nx">Classify</span><span class="p">(</span><span class="nx">entry</span><span class="p">)</span>
<span class="k">if</span><span class="w"> </span><span class="nx">f</span><span class="p">.</span><span class="nx">Class</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nx">catalog</span><span class="p">.</span><span class="nx">Executes</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"%s runs %q — disarm with: git %s …\n"</span><span class="p">,</span><span class="w"> </span><span class="nx">catalog</span><span class="p">.</span><span class="nx">Name</span><span class="p">(</span><span class="nx">entry</span><span class="p">),</span><span class="w"> </span><span class="nx">entry</span><span class="p">.</span><span class="nx">Value</span><span class="p">,</span><span class="w"> </span><span class="nx">f</span><span class="p">.</span><span class="nx">Disarm</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>The override on that row is <code>-c core.fsmonitor=false</code> rather than an emptied value. Both disarm it, since the empty string is a boolean too, but <code>false</code> reads as a decision rather than as a deletion that stopped halfway.</p>
<p>None of this makes a short report a clean report, and the tool is written not to let anyone believe otherwise. <code>NotCovered</code> names what can still end in a program being run and what the catalog has no verdict for (<code>hook.&lt;name&gt;.command</code>,<code>include.path</code>,<code>core.gitProxy</code>,<code>uploadpack.packObjectsHook</code>, a template directory, the executables already sitting in <code>.git/hooks</code>), and it is printed at the end of every run. Exit status 0 is documented as no key this catalog covers names a program, which is a narrower claim than safe and is meant to be.</p>
<h2 id="what-i-would-do-differently">What I would do differently<a class="hlink" href="#what-i-would-do-differently" title="Link to this section">#</a></h2>
<p>Mirroring another program's parser is a dated claim, and I treat it as one: the catalog carries its own version and the git version its rows were read from, so a stale mirror is at least legible as stale. What I would tighten is the direction of verification. <code>internal/gitoracle/bool.go</code> exists because the mirror is worth only as much as the comparison against the original, and I would rather have the boolean grammar checked against an installed git than against my reading of its source, every time, than treat that check as a nicety. The source moves, and a copy that nobody re-compares probably drifts into a heuristic wearing a parser's name.</p>
<p>The shape of this mistake is not specific to git. In payment integrations I have watched the same thing happen: a sender normalizes a value, trims it, re-cases it, pads a decimal, and the receiver parses the original grammar, and the bug lives in the gap between the two readings rather than in either one. Whenever one program decides whether a value is data and another decides whether to execute it, the second one owns the grammar. Copy it, bound and units and all, or do not claim to know.</p>]]></content:encoded>
  </item>
  <item>
    <title>Your checkout page runs code you did not write, loaded at runtime…</title>
    <link>https://polycratia.com/blog/2026-11-17-your-checkout-page-runs-code-you-did-not/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-your-checkout-page-runs-code-you-did-not/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>Your checkout page runs code you did not write, loaded at runtime from vendors you have not audited.</description>
    <content:encoded><![CDATA[<p>Your checkout page runs code you did not write, loaded at runtime from vendors you have not audited.</p>
<p>BleepingComputer reports a suspected supply chain attack on Optimizely that loaded malicious JavaScript onto sites including Zoom, CNN, Fox News, BBC, Salesforce and GitLab. The same outlet reports Check Point Security Gateway instances hit by PHP2Shell attacks that deployed data-wiping malware on VMware ESXi hosts. And BigGo Finance reports unauthorized access at Japan's GoQSystem leaked personal data of 4.37 million people, along with 4.98 million e-commerce orders.</p>
<p>So take the boring step this week. Inventory every third-party tag your site loads, pin versions and add subresource integrity where the vendor allows it, enforce a content security policy, and keep marketing tags off the pages where people type card or wallet details.</p>
<p>I run that as a scoped review of payment and account flows: about a week of work, not a quarter. Send me the list of what your checkout loads and you get cost and timeline back within a day.</p>]]></content:encoded>
  </item>
  <item>
    <title>When a shared transfer rail stalls, your users do not blame the rail.…</title>
    <link>https://polycratia.com/blog/2026-11-17-when-a-shared-transfer-rail-stalls-your/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-when-a-shared-transfer-rail-stalls-your/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>When a shared transfer rail stalls, your users do not blame the rail. They blame your app.</description>
    <content:encoded><![CDATA[<p>When a shared transfer rail stalls, your users do not blame the rail. They blame your app.</p>
<p>The Sunday Guardian reports US users hitting Zelle payment transfer, app login, transaction and mobile banking issues, checked against Downdetector status, and the same outage got picked up by 11 independent sources.</p>
<p>If one rail carries your money movement, the protection is boring and structural. Queue the request instead of firing it inline. Make retries idempotent so a reattempt can never double-send, and show an honest degraded state instead of a spinner that quietly fails. I audit payment flows for exactly these three gaps and build the missing layer: a focused review takes days, not months, and you get cost and timeline in writing before anything starts.</p>
<p>Send me your payment flow and I will tell you where it breaks when the rail does.</p>]]></content:encoded>
  </item>
  <item>
    <title>Three seconds to 200 milliseconds, and almost none of that came from…</title>
    <link>https://polycratia.com/blog/2026-11-17-three-seconds-to-200-milliseconds-and-al/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-three-seconds-to-200-milliseconds-and-al/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>Three seconds to 200 milliseconds, and almost none of that came from making the code faster.</description>
    <content:encoded><![CDATA[<p>Three seconds to 200 milliseconds, and almost none of that came from making the code faster.</p>
<p>The endpoint was waiting on other people's systems. One page assembled itself from live calls out: carrier rate lookups per shipping option, a provider check, a lookup after that. Each one sequential, each one fast enough on its own, and the tails added up. Your p99 becomes the worst p99 among your vendors, and no amount of query tuning touches that.</p>
<p>Two changes did the work.</p>
<p>First, stop serializing calls that do not depend on each other. Five sequential 300ms calls are a second and a half. The same five in parallel are 300ms plus noise.</p>
<p>Second, and this is the one that actually matters: decide a freshness window per field, not per request. Not everything on a page has to be true as of this millisecond. A shipping rate can be minutes old. A catalog attribute can be hours old. Price and stock usually cannot. Once you write that down explicitly, most of the fan-out leaves the request path and becomes a background refresh.</p>
<p>The latency number is the visible part. The real win is that the endpoint stops failing when a vendor has a bad afternoon.</p>
<p>I think your API has at least one field you would admit does not need to be live...</p>]]></content:encoded>
  </item>
  <item>
    <title>The endpoint was fast when I ran it. Fast in staging too. In…</title>
    <link>https://polycratia.com/blog/2026-11-17-the-endpoint-was-fast-when-i-ran-it-fast/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-the-endpoint-was-fast-when-i-ran-it-fast/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>The endpoint was fast when I ran it. Fast in staging too. In production it took three seconds, but only during the hours the ops team was working in the admin panel.</description>
    <content:encoded><![CDATA[<p>The endpoint was fast when I ran it. Fast in staging too. In production it took three seconds, but only during the hours the ops team was working in the admin panel.</p>
<p>I started where you start: the queries. They were fine. The handler's own work was a small slice of those three seconds. The rest of the time the request was doing nothing at all. It sat there waiting to get a database connection out of the pool.</p>
<p>The pool was being drained by an export in the admin panel. Long transaction, opened a connection, then called an external carrier API from inside that transaction and held the connection for the whole round trip. A few of those running at once and ordinary catalog traffic simply queued behind them.</p>
<p>Two changes. The external call moved outside the transaction boundary, so no connection is held while you wait on someone else's network. And the reporting path got its own pool, so an operator's export can't starve customer requests.</p>
<p>What I do differently now: I instrument connection acquisition as its own span, separate from query time. Without that split, waiting in line looks exactly like slow code, and you lose a week optimizing the wrong thing.</p>
<p>So when your endpoint is slow, I'd measure the time it spends waiting for a resource someone else is holding...</p>]]></content:encoded>
  </item>
  <item>
    <title>The cost of a bug is not its wrongness. It is how long it can keep…</title>
    <link>https://polycratia.com/blog/2026-11-17-the-cost-of-a-bug-is-not-its-wrongness-i/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-the-cost-of-a-bug-is-not-its-wrongness-i/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>The cost of a bug is not its wrongness. It is how long it can keep looking plausible.</description>
    <content:encoded><![CDATA[<p>The cost of a bug is not its wrongness. It is how long it can keep looking plausible.</p>
<p>Rounding in money splits is the clearest case. Split a payout pro-rata between several investors, round each share to the minor unit, and the shares stop summing to the amount you received. Off by one minor unit, sometimes two.</p>
<p>Nothing crashes. Every row passes validation. Every share looks correct to the person it belongs to.</p>
<p>So the system keeps running, and the gap repeats per payout, across every deal, for as long as nobody reconciles against what the bank actually settled. When it finally surfaces there is no broken row to point at: none of them is broken on its own. The defect lives in the relationship between them, and that relationship is the one thing row-level validation never looks at.</p>
<p>I treat splits as allocation rather than arithmetic: compute the parts, push the remainder onto one deterministic recipient by a stable ordering, and assert that the parts sum to the whole inside the same transaction that writes them. A failed assert at write time costs you minutes. The same discrepancy found later in a settlement report costs an investigation across everything that ran in between.</p>
<p>The real size of a bug is wrongness times volume times time to detection. Crashes are cheap because that last term sits close to zero.</p>
<p>Where to enforce parts-equal-the-whole I am still not sure about: in the allocation code, as a database constraint, or in a nightly reconciliation job...</p>]]></content:encoded>
  </item>
  <item>
    <title>An agent that passes testing got graded on its answer. Production…</title>
    <link>https://polycratia.com/blog/2026-11-17-an-agent-that-passes-testing-got-graded/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-17-an-agent-that-passes-testing-got-graded/</guid>
    <pubDate>Tue, 17 Nov 2026 09:00:00 +0000</pubDate>
    <description>An agent that passes testing got graded on its answer. Production grades its side effects.</description>
    <content:encoded><![CDATA[<p>An agent that passes testing got graded on its answer. Production grades its side effects.</p>
<p>That gap is the whole failure mode.</p>
<p>In a test run the world sits frozen. Fixtures don't move, every tool returns, and the only thing checked is the final output. In production the agent's plan is a sequence of writes into other people's systems, spread over minutes: a payout triggered, a status flipped, a document sent for signature. The run can die at step 5 with steps 1 through 4 already applied, and then the orchestrator retries from the top.</p>
<p>Three things I hold a production agent to.</p>
<p>Every write tool takes a caller-supplied idempotency key derived from the intent, not generated per attempt. Retries collapse instead of duplicating.</p>
<p>Preconditions get checked at the write, not at the decision. An agent that read a balance at step 1 and moves money at step 7 is acting on a number that has aged.</p>
<p>Reads and writes are split at the tool boundary. The agent explores freely, and the dangerous surface stays small enough to audit.</p>
<p>Evaluation follows from that. Scoring final answers tells you the model is fine. Replaying traces tells you what state the world was left in when a run stopped halfway.</p>
<p>If you run agents against real systems, I'd start with the thing that happens today when yours stops at step 5.</p>]]></content:encoded>
  </item>
  <item>
    <title>Fee-on-transfer tokens: the only honest number is the balance</title>
    <link>https://polycratia.com/blog/fee-on-transfer-tokens-the-only-honest-number-is-the-balance/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/fee-on-transfer-tokens-the-only-honest-number-is-the-balance/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>A token transfer can deliver less than it was asked to deliver, and nothing in the transaction says so. The amount sitting in the call data is the amount I asked for, the return value only tells you…</description>
    <content:encoded><![CDATA[<p>A token transfer can deliver less than it was asked to deliver, and nothing in the transaction says so. The amount sitting in the call data is the amount I asked for, the return value only tells you the call did not revert, and a token that deducts on the way through writes that deduction in neither place. So a payout pipeline that credits the number it requested is crediting a number the chain never agreed to.</p>
<p>The fix is not clever: read the recipient's balance before the transfer and again after it, then treat the difference as the only number worth writing down. What makes it worth writing down is what the measurement refuses to do, and that is most of this post. The code comes from erc20-transfers (https://github.com/polycratia/erc20-transfers), a package I maintain for the parts of the ERC-20 surface that actually bite.</p>
<h2 id="the-two-places-the-fee-is-not">The two places the fee is not<a class="hlink" href="#the-two-places-the-fee-is-not" title="Link to this section">#</a></h2>
<p>Call data first. A <code>transfer</code> is a four-byte selector and two words, and there is no room in it for the token to answer back:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="kn">from</span><span class="w"> </span><span class="nn">erc20_transfers</span><span class="w"> </span><span class="kn">import</span> <span class="n">encode_transfer</span>

<span class="n">data</span> <span class="o">=</span> <span class="n">encode_transfer</span><span class="p">(</span><span class="n">to</span><span class="o">=</span><span class="n">bob</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="mi">1_500_000</span><span class="p">)</span></code></pre></figure>
<p>That 1,500,000 is an input. It is what the sender wants moved. A token whose implementation credits the recipient with less and keeps the rest accepts exactly this call data, unchanged, and the bytes on chain afterwards still say 1,500,000. The call data is a request, and I have watched people read it back out of a block explorer as if it were a settlement record.</p>
<p>Then the return value, which is where the optimism usually creeps in. The standard says <code>transfer</code> returns a bool. The tokens that predate the finalised standard, USDT among them, return nothing at all and revert on failure instead. So the package reads the return data defensively rather than coercing it:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">read_transfer_return</span><span class="p">(</span><span class="n">data</span><span class="p">:</span> <span class="nb">bytes</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">TransferReturn</span><span class="p">:</span>
    <span class="n">raw</span> <span class="o">=</span> <span class="nb">bytes</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">raw</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">TransferReturn</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="sa">b</span><span class="s2">""</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">raw</span><span class="p">)</span> <span class="o">!=</span> <span class="n">WORD_SIZE</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">NonBooleanReturn</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>  <span class="c1"># not a word and not empty</span>

    <span class="n">word</span> <span class="o">=</span> <span class="nb">int</span><span class="o">.</span><span class="n">from_bytes</span><span class="p">(</span><span class="n">raw</span><span class="p">,</span> <span class="s2">"big"</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">word</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">NonBooleanReturn</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>  <span class="c1"># not a boolean either</span>

    <span class="k">return</span> <span class="n">TransferReturn</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">word</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span></code></pre></figure>
<p>Even at its most cooperative, that boolean carries one bit: the call did not revert. Empty data and <code>True</code> mean the same thing here, and neither of them is a quantity. Anything that is neither empty nor a boolean word gets refused rather than read as truthy, because a token returning something else is not described by the interface it claims to implement, and guessing at what it meant is how a fee turns into a rounding error in someone's ledger.</p>
<p>So: the amount is mine, the boolean is binary, and the fee is in neither.</p>
<h2 id="two-reads-one-receipt">Two reads, one receipt<a class="hlink" href="#two-reads-one-receipt" title="Link to this section">#</a></h2>
<p>Since the token will not say, the recipient's balance has to. Two <code>balanceOf</code> reads around the transfer, and the difference between them is what arrived:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="kn">from</span><span class="w"> </span><span class="nn">erc20_transfers</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
    <span class="n">decode_uint256</span><span class="p">,</span>
    <span class="n">encode_balance_of</span><span class="p">,</span>
    <span class="n">encode_transfer</span><span class="p">,</span>
    <span class="n">measure_received</span><span class="p">,</span>
<span class="p">)</span>

<span class="k">def</span><span class="w"> </span><span class="nf">pay</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">recipient</span><span class="p">,</span> <span class="n">amount</span><span class="p">):</span>
    <span class="n">before</span> <span class="o">=</span> <span class="n">decode_uint256</span><span class="p">(</span><span class="n">eth_call</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">encode_balance_of</span><span class="p">(</span><span class="n">account</span><span class="o">=</span><span class="n">recipient</span><span class="p">)))</span>
    <span class="n">send</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">encode_transfer</span><span class="p">(</span><span class="n">to</span><span class="o">=</span><span class="n">recipient</span><span class="p">,</span> <span class="n">amount</span><span class="o">=</span><span class="n">amount</span><span class="p">))</span>
    <span class="n">after</span> <span class="o">=</span> <span class="n">decode_uint256</span><span class="p">(</span><span class="n">eth_call</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">encode_balance_of</span><span class="p">(</span><span class="n">account</span><span class="o">=</span><span class="n">recipient</span><span class="p">)))</span>

    <span class="k">return</span> <span class="n">measure_received</span><span class="p">(</span><span class="n">sent</span><span class="o">=</span><span class="n">amount</span><span class="p">,</span> <span class="n">balance_before</span><span class="o">=</span><span class="n">before</span><span class="p">,</span> <span class="n">balance_after</span><span class="o">=</span><span class="n">after</span><span class="p">)</span></code></pre></figure>
<p>You want those two reads pinned to blocks rather than taken whenever the node feels like answering: the block before the transfer landed, and the block it landed in. A read taken a few blocks late on a busy recipient address is measuring the recipient's week, not this transfer.</p>
<p>What comes back is a <code>TransferReceipt</code>, which is two integers plus the arithmetic that would otherwise be scattered across three services:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="nd">@property</span>
<span class="k">def</span><span class="w"> </span><span class="nf">fee</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sent</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">received</span>

<span class="nd">@property</span>
<span class="k">def</span><span class="w"> </span><span class="nf">has_fee</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">received</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">sent</span>

<span class="nd">@property</span>
<span class="k">def</span><span class="w"> </span><span class="nf">fee_bps</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Decimal</span><span class="p">:</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">sent</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Decimal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">Decimal</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fee</span> <span class="o">*</span> <span class="n">_BPS_PER_UNIT</span><span class="p">)</span> <span class="o">/</span> <span class="n">Decimal</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sent</span><span class="p">)</span></code></pre></figure>
<p><code>_BPS_PER_UNIT</code> is 10_000, and the share is a <code>Decimal</code> rather than a float because a fee expressed in basis points is going to end up next to money. The zero branch is there because a zero-value transfer is a legitimate thing to send and dividing by it is not.</p>
<p>The receipt says what happened in one sentence:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="n">receipt</span> <span class="o">=</span> <span class="n">pay</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">bob</span><span class="p">,</span> <span class="mi">1_500_000</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">receipt</span><span class="o">.</span><span class="n">explain</span><span class="p">())</span>
<span class="c1"># 1485000 of 1500000 units arrived; the token kept 15000 (100.00 bps), so</span>
<span class="c1"># anything downstream that was promised 1500000 is short</span></code></pre></figure>
<p>and for a well-behaved token, <code>all 1500000 units arrived; this token takes no fee</code>.</p>
<p>Measuring is not the same as deciding, so the raise is a separate call:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="n">receipt</span><span class="o">.</span><span class="n">raise_for_fee</span><span class="p">()</span>  <span class="c1"># FeeOnTransfer, carrying the receipt</span></code></pre></figure>
<p><code>FeeOnTransfer</code> is a <code>RuntimeError</code> that keeps the receipt on the exception, so the handler gets the numbers and not just a message. Whether 100 bps is a problem depends on what the transfer was for. Routing through a pool, it is a cost of doing business. Paying out against an invoice that names a figure, it is a breach, and the right move is to fail the payout loudly rather than book the shortfall as drift.</p>
<h2 id="the-two-readings-it-refuses">The two readings it refuses<a class="hlink" href="#the-two-readings-it-refuses" title="Link to this section">#</a></h2>
<p>A fee measured from the wrong pair of reads is worse than no measurement at all, because it looks like a fact. Two pairs get rejected outright.</p>
<p>A balance that fell is not a pair of reads around an incoming transfer:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="n">measure_received</span><span class="p">(</span><span class="n">sent</span><span class="o">=</span><span class="mi">1_500_000</span><span class="p">,</span> <span class="n">balance_before</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">balance_after</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="c1"># ValueError: the recipient balance fell from 10 to 5 across a transfer in;</span>
<span class="c1"># the two reads are not around the same transfer</span></code></pre></figure>
<p>Something else moved tokens out between the reads, or the reads were taken against the wrong account or the wrong blocks. Either way the subtraction would go negative, and the only sane answer is to refuse rather than clamp to zero and report a 100% fee.</p>
<p>A balance that grew by more than was sent is refused in <code>TransferReceipt.__post_init__</code>:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="n">measure_received</span><span class="p">(</span><span class="n">sent</span><span class="o">=</span><span class="mi">1_000</span><span class="p">,</span> <span class="n">balance_before</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">balance_after</span><span class="o">=</span><span class="mi">1_500</span><span class="p">)</span>
<span class="c1"># ValueError: 1500 arrived out of 1000 sent; a balance that grew by more than</span>
<span class="c1"># the transfer means a rebase or a second transfer landed in between, so this</span>
<span class="c1"># measurement says nothing</span></code></pre></figure>
<p>That one is the interesting refusal. A rebasing token moves every holder's balance on its own schedule, and a second transfer landing in the same window is ordinary on a hot recipient address. Both produce a difference that is arithmetically fine and semantically meaningless. Returning a negative fee, or quietly reporting "no fee", would hide the fact that the measurement window was not clean.</p>
<p>Underneath, every input goes through the same <code>_uint256</code> check: a bool or a non-int raises <code>TypeError</code>, a negative value or one above <code>UINT256_MAX</code> raises <code>ValueError</code>. The bool rejection earns its place in Python specifically, since <code>True</code> is an <code>int</code>, and a <code>balance_after=True</code> slipping out of a truthy helper would otherwise measure as one unit arriving.</p>
<h2 id="units-are-not-money-yet">Units are not money yet<a class="hlink" href="#units-are-not-money-yet" title="Link to this section">#</a></h2>
<p>The receipt counts in the token's smallest unit, which is the only scale the chain knows. A fee of 15000 units is 0.015 on a six-decimal token and 0.000000000000015 on an eighteen-decimal one, so the number is not fit to show anyone until the token has said how to read it:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="kn">from</span><span class="w"> </span><span class="nn">erc20_transfers</span><span class="w"> </span><span class="kn">import</span> <span class="n">decode_decimals</span><span class="p">,</span> <span class="n">encode_decimals</span><span class="p">,</span> <span class="n">from_units</span>

<span class="n">decimals</span> <span class="o">=</span> <span class="n">decode_decimals</span><span class="p">(</span><span class="n">eth_call</span><span class="p">(</span><span class="n">token</span><span class="p">,</span> <span class="n">encode_decimals</span><span class="p">()))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">from_units</span><span class="p">(</span><span class="n">receipt</span><span class="o">.</span><span class="n">fee</span><span class="p">,</span> <span class="n">decimals</span><span class="o">=</span><span class="n">decimals</span><span class="p">))</span></code></pre></figure>
<p>Nothing in the package has a default for <code>decimals</code>, and <code>TokenAmount</code> exists so an on-chain integer can travel with the scale it was read at. A fee-on-transfer bug and a decimals bug compound in the same direction: both produce a payout that is confidently wrong and already final.</p>
<h2 id="why-grossing-up-is-left-out">Why grossing up is left out<a class="hlink" href="#why-grossing-up-is-left-out" title="Link to this section">#</a></h2>
<p>The obvious next feature is "send enough extra that the net comes out exact", and the package does not offer it. Inverting a fee schedule nobody published is guesswork. The rate is whatever the contract's owner last set it to, and it can change between the block I measured and the block my transaction lands in. Some tokens deduct on receive, some on send, some exempt particular addresses, and none of them are obliged to tell me which. And a grossed-up transfer still has to be measured afterwards, so the inversion buys nothing except a second number to be wrong about.</p>
<p>Measure, report, raise. If the exact net matters, the thing to adjust is the agreement with the counterparty, not the arithmetic.</p>
<h2 id="what-i-would-do-differently">What I would do differently<a class="hlink" href="#what-i-would-do-differently" title="Link to this section">#</a></h2>
<p>On earlier payout work I credited the requested amount at the moment of broadcast and let reconciliation catch any difference later. That holds up until a token with a transfer fee enters the asset list, and then it fails in the worst shape available: small per-transfer gaps that pile up quietly in an account nobody watches, surfacing as an unexplained reconciliation delta long after the transactions are irreversible.</p>
<p>What I do now is narrower. The ledger entry takes <code>received</code>, never <code>sent</code>. The difference, when there is one, gets written as an explicit fee line with its basis points next to it, so it is a recorded fact rather than a discrepancy somebody has to explain later. And I do not keep a <code>fee_on_transfer</code> boolean per token in a config table, because that table is a snapshot of what was true when somebody last looked, and the behaviour it describes is controlled by someone else's contract. Two <code>balanceOf</code> reads cost almost nothing next to a transfer. Paying for them every time is cheaper than being wrong once.</p>
<p>The general habit this is a special case of: read what the chain says, and do not stand in for it with a constant. The amount in my call data is a constant I chose. The balance is what happened.</p>]]></content:encoded>
  </item>
  <item>
    <title>Three seconds down to 200 milliseconds, and not one line of the slow…</title>
    <link>https://polycratia.com/blog/2026-11-16-three-seconds-down-to-200-milliseconds-a/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-three-seconds-down-to-200-milliseconds-a/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>Three seconds down to 200 milliseconds, and not one line of the slow code got any faster.</description>
    <content:encoded><![CDATA[<p>Three seconds down to 200 milliseconds, and not one line of the slow code got any faster.</p>
<p>The endpoint returned shipping options for a cart. It called several carriers for live rates, one after another, inside the request. Each integration was fine on its own. The response time was the sum of the worst day every provider was having.</p>
<p>The contract changed. The code path stayed where it was.</p>
<p>A rate is not a fact you have to fetch: it is a quote with a validity window. So the quotes moved out of the request, computed on a schedule and on cart changes, stored together with the inputs they were derived from (origin, destination, weight bracket, service level), and served from storage. The request became a lookup.</p>
<p>The calls that still had to be live got a budget. Hard per-carrier timeout, run concurrently, and whoever misses the window is simply absent from the options list instead of holding the whole response hostage. One missing carrier is a much smaller problem than a three-second checkout.</p>
<p>The part that is easy to skip: you need an explicit rule for stale. A quote served past its window has to be refreshed or marked, because the price you show is the price you are expected to honour when the label is actually bought.</p>
<p>My take: most slow endpoints in commerce backends are not slow. They are synchronous aggregation of other people's latency, and you fix them by deciding what the response is allowed to not know yet.</p>
<p>The line you draw there, cached quotes with a validity window or blocking on live rates, is the decision I'd actually argue about...</p>]]></content:encoded>
  </item>
  <item>
    <title>The withdrawal was marked sent. The chain had no record of it. Both…</title>
    <link>https://polycratia.com/blog/2026-11-16-the-withdrawal-was-marked-sent-the-chain/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-the-withdrawal-was-marked-sent-the-chain/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>The withdrawal was marked sent. The chain had no record of it. Both were right: they were reading different nodes.</description>
    <content:encoded><![CDATA[<p>The withdrawal was marked sent. The chain had no record of it. Both were right: they were reading different nodes.</p>
<p>On my machine the custodial wallet talked to a single node. Broadcast, then ask for the account nonce, and the answer included what I had just sent, every time. Sequential, consistent, free.</p>
<p>Production talked to an endpoint sitting in front of a pool of nodes. The write landed on one, and the next read was served by another that had not seen the pending transaction yet. So the allocator handed out the same nonce twice. The second transaction replaced the first. A payout silently stopped existing.</p>
<p>The expensive part was not the fix. It was working out which withdrawals had actually landed: reconciling my own records against the chain, one by one, because that state had been derived from a read I did not control.</p>
<p>Now I do not derive a nonce from a remote read at send time. Nonces are allocated from my own database, one allocator per wallet, inside a transaction. The chain confirms in-flight state, it is not the source of it.</p>
<p>The gap between my machine and production was never config. It was a consistency guarantee a laptop gives you for free and no distributed system ever promised.</p>
<p>If you run custodial wallets or on-chain payouts, I would go looking for the place where your system still trusts a read-after-write it cannot guarantee...</p>]]></content:encoded>
  </item>
  <item>
    <title>Any integration you handed admin rights to is now reachable at…</title>
    <link>https://polycratia.com/blog/2026-11-16-any-integration-you-handed-admin-rights/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-any-integration-you-handed-admin-rights/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>Any integration you handed admin rights to is now reachable at machine speed, not at human speed.</description>
    <content:encoded><![CDATA[<p>Any integration you handed admin rights to is now reachable at machine speed, not at human speed.</p>
<p>BleepingComputer reports that the FBI is warning that hackers are using an AI-powered penetration testing framework called ARTEX to automate cyberattacks against organizations worldwide, including a recent ConnectWise supply-chain breach that compromised Microsoft 365 customer environments.</p>
<p>The attack itself is familiar. What changed for you is the timing. An integration with standing admin rights used to be a risk somebody would get around to exploiting eventually. Once the probing and the pivoting run automatically, the gap between "reachable" and "used" closes faster than your quarterly review cycle.</p>
<p>So the work is inventory, not panic. Every remote-management agent and every vendor integration that holds admin rights in your environment, written down, with an owner and an answer to one question: does this need standing access, or access on request? The ones with no answer are where you rotate tokens first.</p>
<p>I run that audit as a small engagement: mapping remote access and vendor integrations across payment and account flows, then cutting standing privileges back to what the work actually requires. Days, not months, and you get cost and timeline in writing before anything starts.</p>
<p>If you want that audit scoped for your setup, message me and describe what has admin rights today.</p>]]></content:encoded>
  </item>
  <item>
    <title>A repayment came in whole and left the system one cent short.</title>
    <link>https://polycratia.com/blog/2026-11-16-a-repayment-came-in-whole-and-left-the-s/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-a-repayment-came-in-whole-and-left-the-s/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>A repayment came in whole and left the system one cent short.</description>
    <content:encoded><![CDATA[<p>A repayment came in whole and left the system one cent short.</p>
<p>No error, no alert. The deal page looked fine, every investor saw a plausible number, and the payouts just did not add up to what the borrower had paid.</p>
<p>It was a rounding call inside a pro-rata split. Several investors fund one loan, a repayment lands, and each share gets rounded to the smallest currency unit. Round every share on its own and the parts stop summing to the whole. One cent per repayment, across every active deal, on every schedule date.</p>
<p>The money was never the problem. The damage sat downstream: reconciliation against what the bank actually settled stopped being an exact match. So someone widened the tolerance to make the report green. That is the real cost. Once you accept a tolerance, a genuinely missed payment hides inside the noise you trained yourself to ignore.</p>
<p>What I do differently now. Money is integers in minor units, never floats. A split function returns the parts and an explicit remainder, and it is not allowed to silently discard anything. The remainder goes somewhere by a stated rule, not by whichever row the loop happened to touch last. And the write is guarded in the same transaction: parts must sum to the total, or nothing is written.</p>
<p>The idea I keep coming back to: a split is not arithmetic. It is an allocation decision, and someone has to own the leftover, explicitly, in code you can point at during an audit.</p>
<p>If your system divides money between parties, I would want to know who owns the remainder, and whether that rule is written down anywhere outside a rounding function.</p>]]></content:encoded>
  </item>
  <item>
    <title>A passport scan is the one credential your user cannot rotate after a…</title>
    <link>https://polycratia.com/blog/2026-11-16-a-passport-scan-is-the-one-credential-yo/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-a-passport-scan-is-the-one-credential-yo/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>A passport scan is the one credential your user cannot rotate after a leak. Apps built on Lovable exposed passport details and more than 200,000 private messages. The Hacker News reports that a…</description>
    <content:encoded><![CDATA[<p>A passport scan is the one credential your user cannot rotate after a leak. Apps built on Lovable exposed passport details and more than 200,000 private messages. The Hacker News reports that a stolen Fireworks AI key led to the Cursor data breach and a trojanized Claude Code extension.</p>
<p>The rule I keep on payment platforms: the server decides access per record against the session identity. Ids get enumerated, and any key that has left your own infrastructure counts as burned, so narrow the scope and schedule the rotation. If you shipped an MVP fast, I review the account and data-access paths and put real server-side checks in, and that scope is days, not months. Tell me the endpoint you trust least and you get cost and timeline within a day.</p>]]></content:encoded>
  </item>
  <item>
    <title>A 3-second API response is rarely 3 seconds of work. Usually it is a…</title>
    <link>https://polycratia.com/blog/2026-11-16-a-3-second-api-response-is-rarely-3-seco/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-16-a-3-second-api-response-is-rarely-3-seco/</guid>
    <pubDate>Mon, 16 Nov 2026 09:00:00 +0000</pubDate>
    <description>A 3-second API response is rarely 3 seconds of work. Usually it is a few milliseconds of work, and the rest is time spent waiting in line.</description>
    <content:encoded><![CDATA[<p>A 3-second API response is rarely 3 seconds of work. Usually it is a few milliseconds of work, and the rest is time spent waiting in line.</p>
<p>The mechanics are boring and consistent. Your web process has a bounded pool of database connections. Something else in the same system borrows them and holds on: a reconciliation job, a report export, a batch writer keeping a transaction open while it waits on a third-party HTTP call. The request handler never got slower. It just could not start.</p>
<p>That is why profiling the endpoint finds nothing. The span you measure begins when the query runs, and the lost time happened before that. Timing inside the handler is blind to the queue sitting in front of it.</p>
<p>So I measure connection checkout wait separately from query execution, and pool saturation over time instead of averages. Once those are two different numbers, the shape gives itself away: p50 flat, p99 cliff-shaped, lining up with a background schedule rather than with traffic.</p>
<p>What fixes this is isolation rather than optimization. Separate pools for the request path and the worker path. A hard ceiling on how long any transaction may hold a connection. No external calls inside an open transaction, ever. That is where a slow partner becomes your outage.</p>
<p>Latency work goes wrong when you treat it as a code problem. Most of it, I think, is a capacity-sharing problem between the things that serve users and the things that run behind them.</p>
<p>So when your p99 spikes, the number worth having is whether that time went to executing or to waiting...</p>]]></content:encoded>
  </item>
  <item>
    <title>Why extending an acceptance deadline is a write, not a recomputation</title>
    <link>https://polycratia.com/blog/why-extending-an-acceptance-deadline-is-a-write-not-a-recomputation/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/why-extending-an-acceptance-deadline-is-a-write-not-a-recomputation/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>Pausing an acceptance clock changes a deadline, and a deadline that gets recomputed on demand belongs to whoever recomputed it last. In a two-party contracting engagement that is not an academic…</description>
    <content:encoded><![CDATA[<p>Pausing an acceptance clock changes a deadline, and a deadline that gets recomputed on demand belongs to whoever recomputed it last. In a two-party contracting engagement that is not an academic worry: the client and the contractor each replay the same journal on their own machine, and if the deadline is derived rather than recorded, one calendar revision or one zoneinfo update between the two runs is enough to make them disagree about whether a milestone was accepted by silence. In <a href="https://github.com/polycratia/engageledger">engageledger</a> I record an extension the way I record the original delivery deadline: computed once, on the calendar version the running window already named, written into the journal as a single UTC instant, and read back later instead of derived again.</p>
<h2 id="the-three-inputs-that-can-move-underneath-a-running-window">The three inputs that can move underneath a running window<a class="hlink" href="#the-three-inputs-that-can-move-underneath-a-running-window" title="Link to this section">#</a></h2>
<p>The acceptance clock counts business days. <code>bizday.Calendar{version, zone, weekend, holidays, close}</code> carries the zone the client counts days in, which weekdays are weekend, the holidays the contract lists, and the local time a business day ends. <code>Calendar.Deadline(deliveredAt, n)</code> gives you the close of the n-th business day after a delivery as one UTC instant: a delivery past close counts from the next business day, weekends and listed holidays are skipped, and the wall-clock close maps to a different instant on either side of a daylight-saving transition.</p>
<p>It is a pure function, which is why calling it twice is dangerous. Three of its inputs can change while a window is open:</p>
<ul>
<li><strong>The calendar version.</strong> <code>calendar.revised</code> appends another calendar version with its own hash. Deliveries recorded after it count on it; windows opened before it keep the version they named.</li>
<li><strong>The zone database.</strong> The zoneinfo database is embedded so that the same journal reads the same deadline on any host, but embedded is not frozen: a rebuild can carry a corrected transition.</li>
<li><strong>The days.</strong> Every effective hold adds business days to the window it pauses.</li>
</ul>
<p>Any one of those can move a deadline by a day, and a day is the difference between a milestone still open and one that is <code>deemed_accepted</code>.</p>
<h2 id="a-delivery-fixes-its-deadline-once">A delivery fixes its deadline once<a class="hlink" href="#a-delivery-fixes-its-deadline-once" title="Link to this section">#</a></h2>
<p><code>acceptance.Deliver</code> writes a <code>milestone.delivered</code> event carrying the computed instant as <code>deadline_utc</code> next to the <code>calendar_hash</code> it was computed on. The fold reads both and stores them, and it does not call <code>Deadline</code> again. The delivery side is also where the rule about <em>which</em> calendar version may be named gets enforced:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// fix reads the deadline a delivery recorded and the calendar version it counts</span>
<span class="c1">// on, which must be the one in force where the delivery stands. The instant</span>
<span class="c1">// enters the window as recorded: nothing here recomputes it, so a revised</span>
<span class="c1">// calendar or an updated zone database cannot move a deadline the journal</span>
<span class="c1">// already carries. Verify is what reports a recorded deadline this replay</span>
<span class="c1">// would compute elsewhere.</span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="o">*</span><span class="nx">folder</span><span class="p">)</span><span class="w"> </span><span class="nx">fix</span><span class="p">(</span><span class="nx">w</span><span class="w"> </span><span class="o">*</span><span class="nx">Window</span><span class="p">,</span><span class="w"> </span><span class="nx">body</span><span class="w"> </span><span class="kt">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">any</span><span class="p">)</span><span class="w"> </span><span class="kt">error</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">head</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">f</span><span class="p">.</span><span class="nx">head</span><span class="p">()</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// a contract that opened on no calendar may carry no deadline at all</span>
<span class="w">        </span><span class="p">...</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">hash</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">readText</span><span class="p">(</span><span class="nx">body</span><span class="p">,</span><span class="w"> </span><span class="s">"calendar_hash"</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">err</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">hash</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">head</span><span class="p">.</span><span class="nx">Hash</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">old</span><span class="p">,</span><span class="w"> </span><span class="nx">known</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">versionOf</span><span class="p">(</span><span class="nx">f</span><span class="p">.</span><span class="nx">calendars</span><span class="p">,</span><span class="w"> </span><span class="nx">hash</span><span class="p">);</span><span class="w"> </span><span class="nx">known</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">return</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"counts on calendar %s, which the revision at event %d superseded with %s"</span><span class="p">,</span>
<span class="w">                </span><span class="nx">short</span><span class="p">(</span><span class="nx">old</span><span class="p">.</span><span class="nx">Hash</span><span class="p">),</span><span class="w"> </span><span class="nx">head</span><span class="p">.</span><span class="nx">Seq</span><span class="p">,</span><span class="w"> </span><span class="nx">short</span><span class="p">(</span><span class="nx">head</span><span class="p">.</span><span class="nx">Hash</span><span class="p">))</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"names calendar %s, which no contract.opened or calendar.revised before it appended"</span><span class="p">,</span><span class="w"> </span><span class="nx">short</span><span class="p">(</span><span class="nx">hash</span><span class="p">))</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">at</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">readInstant</span><span class="p">(</span><span class="nx">body</span><span class="p">,</span><span class="w"> </span><span class="s">"at"</span><span class="p">)</span>
<span class="w">    </span><span class="p">...</span>
<span class="w">    </span><span class="nx">w</span><span class="p">.</span><span class="nx">Delivered</span><span class="p">,</span><span class="w"> </span><span class="nx">w</span><span class="p">.</span><span class="nx">Deadline</span><span class="p">,</span><span class="w"> </span><span class="nx">w</span><span class="p">.</span><span class="nx">CalendarHash</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">at</span><span class="p">,</span><span class="w"> </span><span class="nx">deadline</span><span class="p">,</span><span class="w"> </span><span class="nx">hash</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span>
<span class="p">}</span></code></pre></figure>
<p>A new delivery has to count on the <strong>head</strong> calendar. That is the asymmetry to hold on to, because an extension does the opposite.</p>
<h2 id="an-extension-counts-on-the-windows-calendar-not-on-the-head">An extension counts on the window's calendar, not on the head<a class="hlink" href="#an-extension-counts-on-the-windows-calendar-not-on-the-head" title="Link to this section">#</a></h2>
<p>A hold is a pause of one milestone between two dates. <code>hold.proposed</code> names it, <code>hold.approved</code> carries one party's approval of that proposal, evidenced the way a change order's approval is, and the hold pauses nothing until every party has approved. Only then does it get an <code>Effective</code> flag set and days worth adding:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// Hold is one pause of the acceptance clock of a milestone: the dates it</span>
<span class="c1">// covers, the business days of the calendar the paused window counts on that</span>
<span class="c1">// fall between them, and the approvals behind it.</span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Hold</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">Seq</span><span class="w">         </span><span class="kt">int64</span>
<span class="w">    </span><span class="nx">ID</span><span class="w">          </span><span class="kt">string</span>
<span class="w">    </span><span class="nx">Milestone</span><span class="w">   </span><span class="kt">string</span>
<span class="w">    </span><span class="nx">Start</span><span class="w">       </span><span class="kt">string</span>
<span class="w">    </span><span class="nx">End</span><span class="w">         </span><span class="kt">string</span>
<span class="w">    </span><span class="nx">Days</span><span class="w">        </span><span class="kt">int64</span>
<span class="w">    </span><span class="nx">Approvals</span><span class="w">   </span><span class="p">[]</span><span class="nx">approval</span><span class="p">.</span><span class="nx">Approval</span>
<span class="w">    </span><span class="nx">Effective</span><span class="w">   </span><span class="kt">bool</span>
<span class="w">    </span><span class="nx">EffectiveBy</span><span class="w"> </span><span class="kt">int64</span>
<span class="w">    </span><span class="nx">ExtendedBy</span><span class="w">  </span><span class="kt">int64</span>
<span class="p">}</span></code></pre></figure>
<p>The body of the extension is computed by one party, once:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// Extension is the deadline.extended body of the hold id, computed once with</span>
<span class="c1">// the calendar version the paused window recorded: the close of the business</span>
<span class="c1">// day the term of that window reaches once the days the hold covered are added</span>
<span class="c1">// to it.</span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="nx">Acceptance</span><span class="p">)</span><span class="w"> </span><span class="nx">Extension</span><span class="p">(</span><span class="nx">id</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kt">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">any</span><span class="p">,</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">h</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Hold</span><span class="p">(</span><span class="nx">id</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: hold %q is not in the journal"</span><span class="p">,</span><span class="w"> </span><span class="nx">id</span><span class="p">)</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">h</span><span class="p">.</span><span class="nx">Effective</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: hold %q carries %d approvals, so it pauses nothing"</span><span class="p">,</span><span class="w"> </span><span class="nx">id</span><span class="p">,</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">h</span><span class="p">.</span><span class="nx">Approvals</span><span class="p">))</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">h</span><span class="p">.</span><span class="nx">ExtendedBy</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: event %d already extended the deadline on hold %q"</span><span class="p">,</span><span class="w"> </span><span class="nx">h</span><span class="p">.</span><span class="nx">ExtendedBy</span><span class="p">,</span><span class="w"> </span><span class="nx">id</span><span class="p">)</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">m</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Find</span><span class="p">(</span><span class="nx">h</span><span class="p">.</span><span class="nx">Milestone</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: milestone %q is not in the contract"</span><span class="p">,</span><span class="w"> </span><span class="nx">h</span><span class="p">.</span><span class="nx">Milestone</span><span class="p">)</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">w</span><span class="p">,</span><span class="w"> </span><span class="nx">open</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Open</span><span class="p">()</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">open</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: milestone %q is %s, with no open acceptance window to extend"</span><span class="p">,</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">ID</span><span class="p">,</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">describe</span><span class="p">())</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">v</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">versionOf</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">Calendars</span><span class="p">,</span><span class="w"> </span><span class="nx">w</span><span class="p">.</span><span class="nx">CalendarHash</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="o">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Errorf</span><span class="p">(</span><span class="s">"acceptance: the window of milestone %q counts on calendar %s, which the journal does not carry"</span><span class="p">,</span>
<span class="w">            </span><span class="nx">m</span><span class="p">.</span><span class="nx">ID</span><span class="p">,</span><span class="w"> </span><span class="nx">short</span><span class="p">(</span><span class="nx">w</span><span class="p">.</span><span class="nx">CalendarHash</span><span class="p">))</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="nx">deadline</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">Calendar</span><span class="p">.</span><span class="nx">Deadline</span><span class="p">(</span><span class="nx">w</span><span class="p">.</span><span class="nx">Delivered</span><span class="p">,</span><span class="w"> </span><span class="nx">w</span><span class="p">.</span><span class="nx">Days</span><span class="o">+</span><span class="nx">w</span><span class="p">.</span><span class="nx">Held</span><span class="o">+</span><span class="nx">h</span><span class="p">.</span><span class="nx">Days</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="kt">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">any</span><span class="p">{</span>
<span class="w">        </span><span class="s">"milestone"</span><span class="p">:</span><span class="w">     </span><span class="nx">m</span><span class="p">.</span><span class="nx">ID</span><span class="p">,</span>
<span class="w">        </span><span class="s">"hold"</span><span class="p">:</span><span class="w">          </span><span class="nx">id</span><span class="p">,</span>
<span class="w">        </span><span class="s">"deadline_utc"</span><span class="p">:</span><span class="w">  </span><span class="nx">deadline</span><span class="p">.</span><span class="nx">Format</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">RFC3339</span><span class="p">),</span>
<span class="w">        </span><span class="s">"calendar_hash"</span><span class="p">:</span><span class="w"> </span><span class="nx">w</span><span class="p">.</span><span class="nx">CalendarHash</span><span class="p">,</span>
<span class="w">    </span><span class="p">},</span><span class="w"> </span><span class="kc">nil</span>
<span class="p">}</span></code></pre></figure>
<p>Two details carry the whole design. The lookup is <code>versionOf(a.Calendars, w.CalendarHash)</code>, the version the window named at delivery, not the head. If a revision landed while the window was running, the extension still counts on the older version, because the window's term was measured on that one and a pause should not quietly re-measure the term. Then the day count: <code>w.Days + w.Held + h.Days</code>, where <code>Held</code> is the days earlier holds already added, so a second pause extends the window the first one produced rather than the one the delivery produced.</p>
<p>The append is a thin wrapper. The body it writes is what every later reader will trust:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="c1">// Extend appends the deadline.extended event of the hold id. A hold that pauses</span>
<span class="c1">// nothing leaves the journal untouched.</span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="nx">Acceptance</span><span class="p">)</span><span class="w"> </span><span class="nx">Extend</span><span class="p">(</span><span class="nx">ap</span><span class="w"> </span><span class="o">*</span><span class="nx">journal</span><span class="p">.</span><span class="nx">Appender</span><span class="p">,</span><span class="w"> </span><span class="nx">id</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nx">journal</span><span class="p">.</span><span class="nx">Event</span><span class="p">,</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">body</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Extension</span><span class="p">(</span><span class="nx">id</span><span class="p">)</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">journal</span><span class="p">.</span><span class="nx">Event</span><span class="p">{},</span><span class="w"> </span><span class="nx">err</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">ap</span><span class="p">.</span><span class="nx">Append</span><span class="p">(</span><span class="nx">EventExtended</span><span class="p">,</span><span class="w"> </span><span class="nx">body</span><span class="p">)</span>
<span class="p">}</span></code></pre></figure>
<p>From the caller's side you get three appends and a re-read:</p>
<figure class="code"><figcaption><span class="lang">go</span></figcaption><pre><code><span class="k">if</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">ap</span><span class="p">.</span><span class="nx">Append</span><span class="p">(</span><span class="s">"hold.proposed"</span><span class="p">,</span><span class="w"> </span><span class="nx">acceptance</span><span class="p">.</span><span class="nx">HoldBody</span><span class="p">(</span><span class="s">"h1"</span><span class="p">,</span><span class="w"> </span><span class="s">"m2"</span><span class="p">,</span><span class="w"> </span><span class="s">"2026-03-09"</span><span class="p">,</span><span class="w"> </span><span class="s">"2026-03-11"</span><span class="p">));</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">err</span>
<span class="p">}</span>
<span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">partyApprovals</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// one per party of the contract</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">ap</span><span class="p">.</span><span class="nx">Append</span><span class="p">(</span><span class="s">"hold.approved"</span><span class="p">,</span><span class="w"> </span><span class="nx">acceptance</span><span class="p">.</span><span class="nx">ApprovalBody</span><span class="p">(</span><span class="s">"h1"</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">));</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nx">err</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>
<span class="nx">st</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">acceptance</span><span class="p">.</span><span class="nx">Fold</span><span class="p">(...)</span><span class="w"> </span><span class="c1">// re-read: the hold is Effective only now</span>
<span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">err</span>
<span class="p">}</span>
<span class="k">if</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">st</span><span class="p">.</span><span class="nx">Extend</span><span class="p">(</span><span class="nx">ap</span><span class="p">,</span><span class="w"> </span><span class="s">"h1"</span><span class="p">);</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">err</span>
<span class="p">}</span></code></pre></figure>
<h2 id="what-the-replay-refuses">What the replay refuses<a class="hlink" href="#what-the-replay-refuses" title="Link to this section">#</a></h2>
<p>Writing the instant down moves the risk instead of removing it: a recorded deadline is a number someone chose. So the replay does not recompute the extension, it audits the claim. Its <code>extend</code> step refuses the event unless all of this holds:</p>
<ul>
<li>the milestone has an open window, since an extension cannot reopen a settled acceptance;</li>
<li>the hold is in the journal and pauses <em>this</em> milestone;</li>
<li>the hold is <code>Effective</code>, so one party cannot extend a deadline alone;</li>
<li>the hold has not been extended before, which is what <code>ExtendedBy</code> is for (otherwise the same approved pause could be spent twice);</li>
<li>the hold's <code>Seq</code> is not below the window's <code>Seq</code>, so a hold proposed before this window opened cannot extend it;</li>
<li>the event's <code>calendar_hash</code> equals the window's <code>CalendarHash</code>, so an extension computed on a revised calendar is not accepted against a window counting on the old one;</li>
<li>the recorded <code>deadline_utc</code> is not before the window's current <code>Deadline</code>.</li>
</ul>
<p>Only then does it add <code>h.Days</code> to <code>Held</code>, set <code>Deadline</code> to the recorded instant, and mark the hold <code>ExtendedBy</code> the event's <code>Seq</code>.</p>
<p>That last check is the one I would keep if I had to drop the other six. A pause is supposed to give the client more time to review, so an extension that moves the deadline earlier is a shortening wearing an extension's event type. Monotonic-forward is a property the replay can check without redoing the arithmetic, and it is probably the property a party would try to violate.</p>
<h2 id="drift-is-reported-never-applied">Drift is reported, never applied<a class="hlink" href="#drift-is-reported-never-applied" title="Link to this section">#</a></h2>
<p><code>Acceptance.Verify</code> is the audit path. It recomputes every recorded deadline with the calendar version whose hash the delivery named and the window's own <code>w.Days + w.Held</code>, and it returns a <code>Drift{seq, milestone, recorded, recomputed, cause}</code> for each one that comes out elsewhere: <code>tzdata</code> where the day the window closes on holds and only the instant of that close moves, <code>calendar</code> where the day itself moves. It writes none of them back.</p>
<p>Because <code>Held</code> is part of that recomputation, an extension gets audited on exactly the same terms as the original delivery deadline: a window that was paused is still checkable against a fresh computation, and a disagreement surfaces as a reported fact instead of as a silently different answer on two machines. Neither a zoneinfo update nor a calendar correction can shorten a window that is already running. What they can do is make the drift visible, which is what a contract dispute actually needs.</p>
<h2 id="what-i-would-do-differently">What I would do differently<a class="hlink" href="#what-i-would-do-differently" title="Link to this section">#</a></h2>
<p>The first shape I reach for in a model like this is the derived one: store the hold's start and end dates, store the calendar, compute the deadline whenever someone asks. Less code, and it cannot go stale. It also makes the deadline a function of the reader's environment, and once money and acceptance-by-silence hang off that instant, "a function of the reader's environment" is the bug.</p>
<p>What I underrated at first was the asymmetry: a delivery must name the head calendar, an extension must name the version its window already recorded, and the two rules live in different functions with similar-looking hash comparisons. Today I would make that pair explicit in the naming of the two reads rather than leaving the distinction to the error strings, because it is the one place where a plausible-looking change ("just use the head everywhere") quietly re-measures a term that is already running...</p>
<p>The general rule I carry out of this: if a value decides whether a party owes something, record the value, not the recipe. Keep the recipe for auditing, and let it report rather than correct.</p>]]></content:encoded>
  </item>
  <item>
    <title>Three seconds, roughly, and every month it took a little longer.</title>
    <link>https://polycratia.com/blog/2026-11-15-three-seconds-roughly-and-every-month-it/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-three-seconds-roughly-and-every-month-it/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>Three seconds, roughly, and every month it took a little longer.</description>
    <content:encoded><![CDATA[<p>Three seconds, roughly, and every month it took a little longer.</p>
<p>It returned an account balance in a lending system. No slow join, no missing index: it summed the account's entire entry history on every call. At launch that was a few hundred rows. A year in it was far more. The query plan never changed. The input did.</p>
<p>What made it visible was the caller. A moderation screen listed accounts and asked for each balance in its own request, so one page load fired dozens of full-history sums at once. Alone the endpoint looked tolerable. Under its real access pattern it was the slowest thing in the product.</p>
<p>So I stopped treating it as a query problem. Balances now come from a stored total per closed period: the period keeps its computed value, and a read sums only the entries recorded after it. The scan is bounded by recent activity, not by the lifetime of the account. That moved the response into the low hundreds of milliseconds and, more importantly, kept it there.</p>
<p>The lesson I carry is that response time is a slope and not a number. Anything priced in rows since inception is a scheduled outage with no date on it yet.</p>
<p>When you look at latency, I'd ask whether you check how it moved over the last six months, or only today's p95...</p>]]></content:encoded>
  </item>
  <item>
    <title>The slow endpoint had a 9 ms database query. I checked it three times…</title>
    <link>https://polycratia.com/blog/2026-11-15-the-slow-endpoint-had-a-9-ms-database-qu/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-the-slow-endpoint-had-a-9-ms-database-qu/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>The slow endpoint had a 9 ms database query. I checked it three times because I did not believe the number.</description>
    <content:encoded><![CDATA[<p>The slow endpoint had a 9 ms database query. I checked it three times because I did not believe the number.</p>
<p>This was a cross-border shopping platform. A product page endpoint took seconds, and the first assumption on the call was "the catalog table needs an index". It did not. Postgres was answering fast and the query plan was boring.</p>
<p>What was actually happening: that one request fanned out to things that were not the database. Delivery rate lookups across multiple carriers, done one after another. A translated-copy call for the localized catalog text. A currency figure. Each one was fine alone: a couple hundred milliseconds, sometimes more when a carrier was having a bad afternoon. Run serially, inside a request a user was waiting on, they added up into a page that felt broken.</p>
<p>The cost was not just the latency. My availability became the worst availability of everyone I called. One slow carrier API and the whole product page degraded, which is a strange thing to explain to a business owner whose own servers are healthy.</p>
<p>What I do differently now: I treat any third-party call inside a user-facing request as something that has to justify itself. Delivery rates get fetched asynchronously and cached per route and weight bracket, with a last-known value served while a refresh happens behind the request. Translated catalog copy comes out of the LLM pipeline ahead of time and gets stored, not generated on read. Anything genuinely live gets a timeout short enough that it can fail without taking the page with it, plus a defined fallback (a stale rate with a label beats a spinner).</p>
<p>The useful habit is measuring where the wall-clock time goes, not where you expect it to go. Count the outbound calls per request before you touch an index. Most "slow API" tickets I have picked up were fast code waiting politely in line for someone else's network.</p>
<p>If you run integration-heavy backends, I would like to hear what finally forced you to move third-party calls out of the request path...</p>]]></content:encoded>
  </item>
  <item>
    <title>The fiat-to-crypto onramp passed verification on my laptop every run.…</title>
    <link>https://polycratia.com/blog/2026-11-15-the-fiat-to-crypto-onramp-passed-verific/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-the-fiat-to-crypto-onramp-passed-verific/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>The fiat-to-crypto onramp passed verification on my laptop every run. Approved, instantly, each time.</description>
    <content:encoded><![CDATA[<p>The fiat-to-crypto onramp passed verification on my laptop every run. Approved, instantly, each time.</p>
<p>In production that same code left people suspended mid-signup.</p>
<p>The difference was not config, and it was not the code. It was which states the provider was willing to emit. The sandbox I had built against answered with a decision. The live integration answered with pending manual review, then came back later through a webhook, sometimes asking the user to resubmit a document, sometimes expiring the session before anyone looked at it.</p>
<p>My state model had two outcomes. The real system had a queue with humans in it.</p>
<p>The cost was not an outage, which is why it took a while to feel urgent. It was a stall: users who had already committed money sitting in a state my code had no transition out of, and me moving them along by hand while I rebuilt the flow around the states I had never seen.</p>
<p>What I do differently now: I map the dependency's full state machine before I write the happy path, and I keep fixtures for the inconvenient states (pending, resubmit required, expired, approved after an initial rejection). Local success has to mean my environment reproduced those, not that the call returned 200.</p>
<p>A vendor sandbox is a simulator of the vendor's optimism.</p>
<p>If you integrate KYC or payment providers, your state model probably comes from their documentation rather than from the responses you have actually observed in production...</p>]]></content:encoded>
  </item>
  <item>
    <title>One compromised system inside your network can be enough to put the…</title>
    <link>https://polycratia.com/blog/2026-11-15-one-compromised-system-inside-your-netwo/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-one-compromised-system-inside-your-netwo/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>One compromised system inside your network can be enough to put the whole customer table in someone else&#x27;s hands.</description>
    <content:encoded><![CDATA[<p>One compromised system inside your network can be enough to put the whole customer table in someone else's hands.</p>
<p>BleepingComputer reports that Japanese furniture retailer Nitori found an attacker had accessed a system on its network, and that 37.62 million customer records may have been stolen. CISA added CVE-2026-20320, an XML external entity flaw in Cisco BroadWorks, to its catalog of vulnerabilities exploited in the wild: an unauthenticated remote attacker could use it to read sensitive configuration information. The Block reports Kava Lend lost roughly $3.8 million to a hack, with the KAVA token down nearly 40 percent.</p>
<p>The entry point varies. What you control is the blast radius: inventory every service, job and admin tool that can read the full customer table, cut that list down, and set retention limits on order and profile data. I do security reviews of payment and account flows, access paths included. Send me yours and you get scope, cost and timeline within a day.</p>]]></content:encoded>
  </item>
  <item>
    <title>A verification check came back invalid on a document that had been…</title>
    <link>https://polycratia.com/blog/2026-11-15-a-verification-check-came-back-invalid-o/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-a-verification-check-came-back-invalid-o/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>A verification check came back invalid on a document that had been signed months earlier.</description>
    <content:encoded><![CDATA[<p>A verification check came back invalid on a document that had been signed months earlier.</p>
<p>Nothing in the signing path had changed. The signature was intact, and the certificate and the keys were fine.</p>
<p>What changed was the renderer. A template tweak moved a field in the layout: cosmetic, reviewed, harmless. But the signature had been computed over a PDF rendered on demand, not over a stored artifact. Re-render, new bytes, new hash, failed verification.</p>
<p>So every document signed before that deploy was suddenly unverifiable. That is not something you quietly repair. A signature is a claim about exact bytes, and you cannot go back and ask people to sign history again.</p>
<p>The bug itself was one design decision made long before: hash what gets rendered instead of hashing what gets stored.</p>
<p>Now anything signed is frozen first. Render once, persist the exact bytes, hash those bytes, sign the hash. Verification does not re-render. The template can change as often as anyone wants, because the signed object no longer depends on it.</p>
<p>The general version I keep coming back to: if a value is part of a legal or financial guarantee, it has to be stored, not recomputed. Recomputation makes that guarantee a function of your current code, and your current code moves.</p>
<p>Somewhere in your system there is probably still a value you recompute when you should have stored it...</p>]]></content:encoded>
  </item>
  <item>
    <title>A scan has no opinion about which server matters to you. It only sees…</title>
    <link>https://polycratia.com/blog/2026-11-15-a-scan-has-no-opinion-about-which-server/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-15-a-scan-has-no-opinion-about-which-server/</guid>
    <pubDate>Sun, 15 Nov 2026 09:00:00 +0000</pubDate>
    <description>A scan has no opinion about which server matters to you. It only sees which version answers.</description>
    <content:encoded><![CDATA[<p>A scan has no opinion about which server matters to you. It only sees which version answers.</p>
<p>BigGo Finance reports that North Korea's Lazarus went after 30 South Korean servers through a PHP vulnerability, aiming at government and finance. The Record reports the City of Osaka warned of a breach impacting 1 million, and warned about emails impersonating the city.</p>
<p>If you run inherited software, own two things: what is installed and reachable, and how your users verify that a message is yours. Every undocumented system I have taken over had a reachable service listed nowhere, so inventory comes before patch plans, with migration off versions that no longer get fixes after that. Describe what you inherited and you will get a written estimate with scope and timeline.</p>]]></content:encoded>
  </item>
  <item>
    <title>Decimal is exact only inside its context, so size the context per operation</title>
    <link>https://polycratia.com/blog/decimal-is-exact-only-inside-its-context-so-size-the-context-per-opera/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/decimal-is-exact-only-inside-its-context-so-size-the-context-per-opera/</guid>
    <pubDate>Sat, 14 Nov 2026 09:00:00 +0000</pubDate>
    <description>Switching a balance from float to Decimal removes binary rounding, but it does not make the arithmetic exact. Decimal is exact only within the precision of the context it runs in, and the default…</description>
    <content:encoded><![CDATA[<p>Switching a balance from <code>float</code> to <code>Decimal</code> removes binary rounding, but it does not make the arithmetic exact. <code>Decimal</code> is exact only within the precision of the context it runs in, and the default context stops at 28 significant digits. An ETH balance held in wei carries more than that the moment you multiply it by anything, so a sum you believed was exact gets rounded silently, and a quantize you believed was safe raises instead.</p>
<p>That is the problem the arithmetic in <a href="https://github.com/polycratia/cryptomoney">cryptomoney</a> is built around. The package refuses floats at the door, which is the part everybody writes about. The quieter half: refusing floats buys you nothing if the decimal arithmetic that follows runs in a context too narrow for the numbers you hold.</p>
<h2 id="28-digits-is-not-a-lot-of-money">28 digits is not a lot of money<a class="hlink" href="#28-digits-is-not-a-lot-of-money" title="Link to this section">#</a></h2>
<p>Every <code>Money</code> is padded to its asset's full precision at construction, so an ETH amount always spends eighteen digits on its fraction. Ten digits are left for whole ether before the default context starts cutting:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="o">&gt;&gt;&gt;</span> <span class="kn">from</span><span class="w"> </span><span class="nn">decimal</span><span class="w"> </span><span class="kn">import</span> <span class="n">Decimal</span><span class="p">,</span> <span class="n">getcontext</span><span class="p">,</span> <span class="n">localcontext</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">getcontext</span><span class="p">()</span><span class="o">.</span><span class="n">prec</span>
<span class="mi">28</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">balance</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1234567890.123456789123456789"</span><span class="p">)</span>   <span class="c1"># 28 digits, at the limit</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">balance</span> <span class="o">*</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1.5"</span><span class="p">)</span> <span class="o">==</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1851851835.1851851836851851835"</span><span class="p">)</span>
<span class="kc">False</span></code></pre></figure>
<p>The exact product needs 29 digits. Under <code>prec=28</code> the last one is dropped. No exception, no flag anybody reads, just a wei that stopped existing between two lines of code. Widen the context and the same expression comes out exact:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="o">&gt;&gt;&gt;</span> <span class="k">with</span> <span class="n">localcontext</span><span class="p">()</span> <span class="k">as</span> <span class="n">ctx</span><span class="p">:</span>
<span class="o">...</span>     <span class="n">ctx</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="mi">60</span>
<span class="o">...</span>     <span class="n">balance</span> <span class="o">*</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1.5"</span><span class="p">)</span> <span class="o">==</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1851851835.1851851836851851835"</span><span class="p">)</span>
<span class="kc">True</span></code></pre></figure>
<p>Quantizing is the loud half of the same problem. <code>quantize</code> will not produce a result whose coefficient is longer than the context allows:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="o">&gt;&gt;&gt;</span> <span class="n">Decimal</span><span class="p">(</span><span class="s2">"1851851835.1851851836851851835"</span><span class="p">)</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">"1E-18"</span><span class="p">))</span>
<span class="n">decimal</span><span class="o">.</span><span class="n">InvalidOperation</span><span class="p">:</span> <span class="p">[</span><span class="o">&lt;</span><span class="k">class</span><span class="w"> </span><span class="err">'</span><span class="nc">decimal</span><span class="o">.</span><span class="n">InvalidOperation</span><span class="s1">'&gt;]</span></code></pre></figure>
<p>So the two things a money type does constantly (combine amounts, then fit the result back to the asset's precision) are the two things the default context is too narrow for. One fails quietly, the other fails violently. In a ledger neither is acceptable.</p>
<h2 id="counting-the-digits-an-operation-actually-needs">Counting the digits an operation actually needs<a class="hlink" href="#counting-the-digits-an-operation-actually-needs" title="Link to this section">#</a></h2>
<p>The fix is not a bigger global precision. You compute, per operation, how many digits that operation needs, and you run it in a local context of that size. Everything rests on one small function:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">_digits_of</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
    <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">as_tuple</span><span class="p">()</span><span class="o">.</span><span class="n">digits</span><span class="p">)</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">as_tuple</span><span class="p">()</span><span class="o">.</span><span class="n">exponent</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span></code></pre></figure>
<p>Coefficient digits plus any positive exponent. The second term matters more than it looks: <code>Decimal("1E+20")</code> carries a single digit of coefficient but occupies twenty-one digit positions once you write it out, and a context sized from the coefficient alone would round it away.</p>
<p>On top of that sits the bound for the steps that must not be rounded at all:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">_exact_precision</span><span class="p">(</span><span class="o">*</span><span class="n">values</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
<span class="w">    </span><span class="sd">"""Enough digits that a sum, difference or product is never rounded.</span>

<span class="sd">    Every amount carries its asset's full precision, so the default context of</span>
<span class="sd">    28 digits is not wide enough for a large balance in wei.</span>
<span class="sd">    """</span>
    <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">_digits_of</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">)</span> <span class="o">+</span> <span class="mi">10</span></code></pre></figure>
<p>Summing the operands is the worst case of the three operations this serves. A product of an m-digit and an n-digit number needs at most m + n digits; a sum or difference needs at most max(m, n) + 1. One expression covers both, with ten digits of headroom on top. <code>__add__</code>, <code>__sub__</code>, <code>__mul__</code>, <code>multiply</code> and <code>convert</code> all size their context with it, so none of them can be rounded by whatever context they happen to be running in.</p>
<p>That is the real payoff. Whether a result loses a digit stops being a function of ambient process state; it gets decided in exactly one place, by the rounding mode the caller passed to the quantization at the end.</p>
<h2 id="division-and-quantization-get-target-places-plus-a-guard-band">Division and quantization get target places plus a guard band<a class="hlink" href="#division-and-quantization-get-target-places-plus-a-guard-band" title="Link to this section">#</a></h2>
<p>Division cannot be exact, so it gets a different bound, one sized from the target rather than from the full product:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">_working_precision</span><span class="p">(</span><span class="n">places</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="o">*</span><span class="n">values</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">int</span><span class="p">:</span>
    <span class="k">return</span> <span class="nb">max</span><span class="p">(</span><span class="n">_digits_of</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">values</span><span class="p">)</span> <span class="o">+</span> <span class="n">places</span> <span class="o">+</span> <span class="mi">10</span></code></pre></figure>
<p><code>divide</code> uses this for the quotient and sets <code>ctx.rounding</code> to the caller's mode, so the single rounding that happens is the one that was asked for. <code>_quantize</code> uses it again:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="k">def</span><span class="w"> </span><span class="nf">_quantize</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">Decimal</span><span class="p">,</span> <span class="n">places</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">rounding</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Decimal</span><span class="p">:</span>
    <span class="n">quantum</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,),</span> <span class="o">-</span><span class="n">places</span><span class="p">))</span>
    <span class="k">with</span> <span class="n">localcontext</span><span class="p">()</span> <span class="k">as</span> <span class="n">ctx</span><span class="p">:</span>
        <span class="n">ctx</span><span class="o">.</span><span class="n">prec</span> <span class="o">=</span> <span class="n">_working_precision</span><span class="p">(</span><span class="n">places</span><span class="p">,</span> <span class="n">amount</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">amount</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">quantum</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">rounding</span><span class="p">)</span></code></pre></figure>
<p>This is the function that would otherwise raise <code>InvalidOperation</code> on a long product. Because the context is sized from the amount actually in hand plus the places being quantized to, the result fits, and the only thing deciding the last base unit is <code>rounding</code>. The same helper runs inside <code>_to_decimal</code>, so even a plain constructor call pads to the asset's precision under a context wide enough to do it.</p>
<p>Which is why nothing in the package has a default rounding mode. If the only rounding in the system is the one the caller asked for, then the caller has to ask:</p>
<figure class="code"><figcaption><span class="lang">python</span></figcaption><pre><code><span class="kn">from</span><span class="w"> </span><span class="nn">decimal</span><span class="w"> </span><span class="kn">import</span> <span class="n">ROUND_DOWN</span><span class="p">,</span> <span class="n">ROUND_HALF_UP</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">cryptomoney</span><span class="w"> </span><span class="kn">import</span> <span class="n">BTC</span><span class="p">,</span> <span class="n">Money</span>

<span class="n">Money</span><span class="p">(</span><span class="s2">"0.5"</span><span class="p">,</span> <span class="n">BTC</span><span class="p">)</span> <span class="o">/</span> <span class="mi">3</span>                                      <span class="c1"># TypeError: / is refused</span>
<span class="n">Money</span><span class="p">(</span><span class="s2">"0.5"</span><span class="p">,</span> <span class="n">BTC</span><span class="p">)</span><span class="o">.</span><span class="n">divide</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_DOWN</span><span class="p">)</span>           <span class="c1"># 0.16666666 BTC</span>
<span class="n">Money</span><span class="p">(</span><span class="s2">"1"</span><span class="p">,</span> <span class="n">BTC</span><span class="p">)</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="s2">"0.015"</span><span class="p">,</span> <span class="n">rounding</span><span class="o">=</span><span class="n">ROUND_HALF_UP</span><span class="p">)</span>  <span class="c1"># 0.01500000 BTC</span></code></pre></figure>
<h2 id="validate-the-operand-before-you-size-a-context-from-it">Validate the operand before you size a context from it<a class="hlink" href="#validate-the-operand-before-you-size-a-context-from-it" title="Link to this section">#</a></h2>
<p><code>multiply</code>, <code>divide</code> and <code>convert</code> run their checks in a fixed order: <code>_check_rounding</code> first, which refuses any mode outside the eight names from <code>decimal</code>, then <code>_operand</code>, which raises through <code>_refuse_float</code> for a float and <code>ValueError</code> for anything not finite. <code>divide</code> raises <code>ZeroDivisionError</code> for a zero divisor; <code>convert</code> raises <code>ValueError</code> for a rate at or below zero.</p>
<p>The order is not decoration. A context sized from an unvalidated operand is a context sized from whatever the caller happened to pass. A NaN does not even have an integer exponent, so <code>_digits_of</code> on one fails with an error that tells the caller nothing about what they did wrong. Validate first and the precision calculation only ever sees a finite decimal, and every error message names the actual mistake.</p>
<h2 id="the-global-context-is-never-touched">The global context is never touched<a class="hlink" href="#the-global-context-is-never-touched" title="Link to this section">#</a></h2>
<p>The shortcut for all of this is one line at import time: <code>getcontext().prec = 60</code>. It works, and it turns your library's correctness into a property of the whole process. Another library can set it back. Application code can wrap a call site in its own <code>localcontext()</code> and silently narrow you. A notebook can do it by accident. The results of a money type should not move because of code it has never heard of.</p>
<p>So no code path in the package alters the global context. Every widening happens inside a <code>with localcontext()</code> block that unwinds on exit, and results come back through <code>Money.__post_init__</code>, which runs <code>_to_decimal</code> like any other constructed amount. One door in, one normalization, whichever operation produced the number.</p>
<h2 id="what-i-would-do-differently">What I would do differently<a class="hlink" href="#what-i-would-do-differently" title="Link to this section">#</a></h2>
<p>The <code>+ 10</code> guard band is a bare literal in two functions. It should be a named constant with a sentence explaining what it covers, because "ten" currently reads as superstition when it is in fact headroom for the extra digits <code>quantize</code> carries while it decides the last one.</p>
<p><code>_exact_precision</code> also sums every operand when the operation is addition, where <code>max + 1</code> would do. I would keep the loose bound (a few extra digits in a short-lived local context cost nothing measurable, and one bound serving three operators is one fewer thing to get wrong), but the docstring should say it is deliberately generous rather than leaving the next reader to wonder whether the author confused the two cases.</p>
<p>The last one is a dependency I would write down. An asset's precision is capped at 36 decimals; <code>Asset("TOOFINE", 37)</code> raises. That cap bounds the fractional side of every operand these functions ever see, which is what keeps the computed precisions sane. Today it reads as an input validation rule. It is actually load-bearing for the precision strategy, and it belongs in the same paragraph as <code>_exact_precision</code>.</p>
<h2 id="close">Close<a class="hlink" href="#close" title="Link to this section">#</a></h2>
<p><code>Decimal</code> does not hand you exactness. It hands you a way to ask for it, one operation at a time, and the default answer is 28 digits because somebody had to pick a number for the general case. Money is not the general case. How many digits a step needs is computable from its operands, and computing it costs a few microseconds, considerably less than finding out the hard way, from a reconciliation report, which wei went missing and when...</p>]]></content:encoded>
  </item>
  <item>
    <title>Three seconds down to 200 milliseconds, and almost none of that came…</title>
    <link>https://polycratia.com/blog/2026-11-14-three-seconds-down-to-200-milliseconds-a/</link>
    <guid isPermaLink="true">https://polycratia.com/blog/2026-11-14-three-seconds-down-to-200-milliseconds-a/</guid>
    <pubDate>Sat, 14 Nov 2026 09:00:00 +0000</pubDate>
    <description>Three seconds down to 200 milliseconds, and almost none of that came from making the query faster.</description>
    <content:encoded><![CDATA[<p>Three seconds down to 200 milliseconds, and almost none of that came from making the query faster.</p>
<p>The endpoint returned a user's balance, and it computed that balance the honest way: read every ledger entry for the account, sum them, apply holds. Correct and auditable, and linear in account age. So the oldest accounts were the slowest, which is backwards, since those are the users you least want to keep waiting.</p>
<p>The fix was to stop computing a value that only changes when something writes it. The balance became a row updated in the same transaction as the ledger entry that moved it. One row read, no aggregation, no cache in front, no invalidation to get wrong.</p>
<p>Most of the work lands somewhere else: proving the stored number still equals the derived one. So the ledger stays the source of truth, the maintained balance is an optimization, and a reconciliation job recomputes from entries and alerts on drift instead of quietly serving a wrong figure. Drift you detect is an incident. Drift you miss is a dispute months later.</p>
<p>The rule I keep coming back to: when a read is slow, ask first whether it is re-deriving something a write already knew. Indexes make derivation cheaper. They do not make it unnecessary.</p>
<p>So I'd want to know where your authoritative balance lives, derived on read, or maintained on write with a checker sitting behind it...</p>]]></content:encoded>
  </item>
</channel>
</rss>
