<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aman </title>
    <description>The latest articles on DEV Community by Aman  (@amanm006).</description>
    <link>https://dev.to/amanm006</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4004632%2F9ace8ac5-8bd3-4909-9237-80b334c30d22.jpg</url>
      <title>DEV Community: Aman </title>
      <link>https://dev.to/amanm006</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amanm006"/>
    <language>en</language>
    <item>
      <title>I Built an AI-Agent Guardrail, Then Discovered My Observer Was Watching Itself</title>
      <dc:creator>Aman </dc:creator>
      <pubDate>Sat, 25 Jul 2026 19:57:37 +0000</pubDate>
      <link>https://dev.to/amanm006/i-built-an-ai-agent-guardrail-then-discovered-my-observer-was-watching-itself-3j4d</link>
      <guid>https://dev.to/amanm006/i-built-an-ai-agent-guardrail-then-discovered-my-observer-was-watching-itself-3j4d</guid>
      <description>&lt;p&gt;Reactive AI cost monitoring has an awkward limitation: by the time a chart shows a runaway agent, the tokens are already gone. I wanted to test a different idea for the Agents of SigNoz hackathon: make a small execution contract &lt;em&gt;before&lt;/em&gt; an AI coding agent starts work, then measure whether its actual behavior stays inside that contract.&lt;/p&gt;

&lt;p&gt;That became &lt;strong&gt;Preflight&lt;/strong&gt;. Given a coding task, it predicts a model tier, effort level, and expected file footprint. Those predictions become observable expectations: when the agent writes files, Preflight measures scope drift, emits OpenTelemetry, and surfaces the result in SigNoz. It is deliberately a prototype, not a universal security sandbox. The useful part is that the prediction, the observed actions, and the misses are all inspectable.&lt;/p&gt;

&lt;p&gt;The most valuable lesson did not come from the happy path. It came from discovering that my external observer was counting its own tooling metadata as agent work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The contract comes before the agent
&lt;/h2&gt;

&lt;p&gt;A task such as 'rename one local variable' should not automatically receive the same model budget or repository-wide scope as 'introduce tenant-aware authorization across billing, reporting, migrations, tests, and rollout documentation.' Preflight classifies the task first and creates a compact contract:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Contract field&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Tier&lt;/td&gt;
&lt;td&gt;small / medium / large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Effort&lt;/td&gt;
&lt;td&gt;low / medium / high&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Blast radius&lt;/td&gt;
&lt;td&gt;isolated / moderate / wide&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;File ceiling&lt;/td&gt;
&lt;td&gt;3 / 10 / 25 files&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I use a cheap &lt;code&gt;gpt-5.6-luna&lt;/code&gt; structured classification call for that small JSON decision, capped at 250 completion tokens. This is intentional: the guard should practice the same 'predict cheap when appropriate' principle it recommends to other agents.&lt;/p&gt;

&lt;p&gt;Preflight then has two honest runtime modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In-process interception&lt;/strong&gt; evaluates actions from Preflight's built-in agent while it runs. When drift gets high, it can pause and propose a human-confirmed remediation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External watch&lt;/strong&gt; predicts before launching another coding CLI, recursively observes writes and deletes in a selected workspace, then calculates drift after the process exits. This mode is post-hoc; it cannot preempt the child process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That distinction matters. A filesystem watcher cannot prove semantic safety, see short-lived files, or protect paths outside the watched workspace. Calling those modes equivalent would make the demo look stronger than it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What SigNoz sees
&lt;/h2&gt;

&lt;p&gt;I exported stable OpenTelemetry span names instead of putting task text or model names into span names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;preflight.predict
  -&amp;gt; preflight.execute
    -&amp;gt; preflight.action.evaluate
    -&amp;gt; preflight.drift.check
    -&amp;gt; preflight.signal      (only when something is wrong)
    -&amp;gt; preflight.complete    (successful session end)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attributes carry the changing details: session ID, predicted tier, runtime mode, action target, verdict, and drift score. The metrics cover classifier cost, modeled savings versus an always-large baseline, tier distribution, action count, and drift. 'Modeled savings' is explicitly a comparison assumption, not a billing claim.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5wgcw2jguqasr1fuja7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5wgcw2jguqasr1fuja7m.png" alt="Preflight dashboard in self-hosted SigNoz" width="799" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I used those signals across several SigNoz features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a Preflight dashboard for classification cost, drift, routing tiers, policy verdicts, session cost, and modeled savings;&lt;/li&gt;
&lt;li&gt;three warning alert rules for scope drift, cost spikes, and the hard ceiling;&lt;/li&gt;
&lt;li&gt;a trace funnel with &lt;code&gt;preflight.predict -&amp;gt; preflight.execute -&amp;gt; preflight.drift.check -&amp;gt; preflight.complete&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Query Builder aggregation to find the tiers actually showing high drift.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The dashboard query was not just a screenshot. In the saved v5 query, I calculated &lt;code&gt;avg(preflight.drift_score)&lt;/code&gt;, grouped by &lt;code&gt;preflight.predicted_tier&lt;/code&gt;, and applied &lt;code&gt;HAVING avg_drift_score &amp;gt; 0.5&lt;/code&gt;. In the current data, only the medium tier surfaced, at 0.542 average drift. That is a real query result, not a synthetic metric.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5096fotw0rpbb9i5a1jh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5096fotw0rpbb9i5a1jh.png" alt="One real Preflight trace waterfall in SigNoz" width="799" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A three-session live batch reached every stable funnel stage: predict, execute, drift check, and complete. The classification cost for that batch was $0.001852. I also hit two practical SigNoz integration details while building it: dashboard group-by needed &lt;code&gt;groupBy.key&lt;/code&gt; rather than an older &lt;code&gt;groupBy.name&lt;/code&gt; shape, and v5 aggregation aliases must be supplied through the alias field instead of inline SQL-style aliasing. Those are the kinds of details that only show up when a dashboard is actually wired to data.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug: my observer was watching itself
&lt;/h2&gt;

&lt;p&gt;For the external-watch proof, I wrapped the installed Codex CLI and asked it to create exactly one Markdown file. The first raw result looked like a success: the watcher saw changed files and drift rose.&lt;/p&gt;

&lt;p&gt;But the requested file had not been created.&lt;/p&gt;

&lt;p&gt;The culprit was a globally installed Codex plugin named Foresight. It wrote its own &lt;code&gt;.foresight&lt;/code&gt; contract and history files inside the temporary workspace. My watcher dutifully counted those internal files as if the external coding agent had edited user code.&lt;/p&gt;

&lt;p&gt;That was invalid evidence. I kept the failed attempts in the repository, but I did not call them successful runs.&lt;/p&gt;

&lt;p&gt;The fix is intentionally narrow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;_INTERNAL_WATCH_DIRECTORIES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;frozenset&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.foresight&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="n"&gt;relative_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;relative_to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;workspace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;_INTERNAL_WATCH_DIRECTORIES&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;relative_path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also made the proof runner fail closed. A run is accepted only when the external CLI returns zero, the exact requested output file exists, and its contents exactly match the expected text. Every rerun receives a fresh workspace. This is not a broad hidden-file exclusion; it filters one verified plugin metadata directory and leaves ordinary project files observable.&lt;/p&gt;

&lt;p&gt;After the correction, the committed result contains two real Codex CLI runs. Each produced exactly one requested user file, each returned exit code 0, and each had one observed file write with 0.33 drift against a three-file contract. The two live Luna predictions used 203 input tokens each, 50 and 54 output tokens, and cost $0.001030 total.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9024s3ujpo0jpoehf8wj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9024s3ujpo0jpoehf8wj.png" alt="Three configured Preflight warning alerts" width="799" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Evidence instead of a perfect-looking score
&lt;/h2&gt;

&lt;p&gt;I wanted the evaluation story to be more useful than a single flattering percentage. The initial 18-task live benchmark was 61.11% accurate on both tier and blast-radius labels. That exposed a real weakness: short requests underweighted authentication, validation, regression coverage, and durable operational work.&lt;/p&gt;

&lt;p&gt;I applied one scope-rubric calibration and reran the same corpus. The result was 18/18, or 100%, but I label it clearly as a same-corpus calibrated result rather than independent validation. Then I froze a separate eight-task held-out set before running it. That held-out run was 8/8 for tier and blast radius. It is encouraging, not a universal guarantee.&lt;/p&gt;

&lt;p&gt;The repository keeps v1, v2, held-out data, raw token counts, costs, failed external attempts, and successful external results. The interesting engineering outcome is not 'the model never misses.' It is that a miss, a drift event, or a broken observer leaves a trail another person can inspect.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell my past self
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Predict before spending.&lt;/strong&gt; A cheap preflight decision can bound a much more expensive agent run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State runtime guarantees precisely.&lt;/strong&gt; In-process interception and post-hoc external watch are useful for different reasons.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Treat failed evidence as evidence.&lt;/strong&gt; The Foresight metadata bug made the project stronger only because I kept the bad run, explained it, and tightened the test.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make observability falsifiable.&lt;/strong&gt; Dashboards are more valuable when they connect to committed raw data, stable spans, and explicit boundaries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Preflight is my attempt at making agent scope and cost visible before they become cleanup work. The full repository, Foundry deployment files, SigNoz dashboard evidence, and benchmark artifacts are available at &lt;a href="https://github.com/AmanM006/Preflight" rel="noopener noreferrer"&gt;AmanM006/Preflight&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>opentelemetry</category>
      <category>ai</category>
      <category>devops</category>
      <category>observability</category>
    </item>
    <item>
      <title>Building Sovereign: A Dual-Database Creator Liquidity Exchange on AWS Aurora DSQL + DynamoDB</title>
      <dc:creator>Aman </dc:creator>
      <pubDate>Fri, 26 Jun 2026 23:43:55 +0000</pubDate>
      <link>https://dev.to/amanm006/building-sovereign-a-transactional-creator-liquidity-exchange-on-aws-aurora-dsql-5327</link>
      <guid>https://dev.to/amanm006/building-sovereign-a-transactional-creator-liquidity-exchange-on-aws-aurora-dsql-5327</guid>
      <description>&lt;p&gt;Digital creators hold billions of dollars in illiquid assets - from newsletters and YouTube channels to SaaS repositories and Notion templates. How do you fractionalize ownership with instant, atomic trading settlements and absolute data integrity under heavy concurrent load?&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;Sovereign&lt;/strong&gt;: a real-time Creator Liquidity Exchange built on a dual-database architecture - AWS Aurora DSQL for transactional correctness and Amazon DynamoDB for high-throughput telemetry.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: Distributed Contention on the Order Book
&lt;/h2&gt;

&lt;p&gt;When multiple buyers attempt to match against the same resting sell orders concurrently, they generate a high-contention race condition:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Over-Allocation Threat&lt;/strong&gt;: If two buy transactions read the same sell order's remaining balance simultaneously, both believe they can buy it. This creates phantom shares - double spending where total ledger allocation exceeds asset supply.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Latency Trap&lt;/strong&gt;: Solving this with distributed locks slows execution significantly across regions, raising latency to hundreds of milliseconds.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Solution: Optimistic Concurrency Control with DSQL
&lt;/h2&gt;

&lt;p&gt;Instead of locking rows, Sovereign uses &lt;code&gt;SERIALIZABLE&lt;/code&gt; isolation on Aurora DSQL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every matching event executes in a single database transaction. The engine reads counter-orders, computes fill quantities, and updates buyer and seller ledger rows atomically.&lt;/li&gt;
&lt;li&gt;If another transaction modified the order concurrently, DSQL throws a &lt;code&gt;40001&lt;/code&gt; serialization conflict. The engine catches it, applies jittered exponential backoff, and retries:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isOCCCollision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cause&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;cause&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;cause&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;40001&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;cause&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;serialization failure&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Retry loop&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;MAX_RETRIES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;attemptMatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isOCCCollision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;attempt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Dual-Database Architecture
&lt;/h2&gt;

&lt;p&gt;The most important architectural decision: two databases, each doing what it's best at.&lt;/p&gt;

&lt;h3&gt;
  
  
  Aurora DSQL (Stockholm, eu-north-1) - Transactional Core
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Orders, trades, ownership ledger, assets&lt;/li&gt;
&lt;li&gt;Requires &lt;code&gt;SERIALIZABLE&lt;/code&gt; isolation and global consistency&lt;/li&gt;
&lt;li&gt;OCC collision resolution IS the product mechanic&lt;/li&gt;
&lt;li&gt;Flat schema, no FK constraints (DSQL active-active requirement)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CREATE INDEX ASYNC&lt;/code&gt; (synchronous indexes not supported on distributed clusters)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Amazon DynamoDB (eu-north-1) - Activity Firehose
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Trade events, OCC collision logs, stampede telemetry&lt;/li&gt;
&lt;li&gt;Single-table design with 3 GSIs&lt;/li&gt;
&lt;li&gt;30-day TTL auto-expiry&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PAY_PER_REQUEST&lt;/code&gt; billing&lt;/li&gt;
&lt;li&gt;No transactions needed - pure append-only throughput DSQL for correctness. DynamoDB for throughput.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every trade fires a fire-and-forget write to DynamoDB:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// After successful DSQL match - never blocks critical path&lt;/span&gt;
&lt;span class="nf"&gt;writeFirehose&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;eventType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TRADE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;assetId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;asset_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;sharesTraded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;priceExecuted&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;latencyMs&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt; &lt;span class="c1"&gt;// silent fail - DynamoDB is best-effort&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  System Architecture Flow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              ┌───────────────────────────────┐
              │      Client Web App (UI)      │
              └───────────────┬───────────────┘
                              │ HTTPS / SSE
                              ▼
              ┌───────────────────────────────┐
              │     Next.js Server Actions    │
              └───────┬───────────────┬───────┘
                      │               │
  Serializable Tx     │               │ Async Telemetry Write
 (Postgres pg client) │               │ (AWS SDK PutCommand)
                      ▼               ▼
              ┌───────────────┐┌───────────────┐
              │  OCC Engine   ││  DynamoDB     │
              │  (Aurora DSQL)││  Firehose     │
              └───────────────┘└───────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Stress Testing: The Stampede Load Simulator
&lt;/h2&gt;

&lt;p&gt;To prove integrity under load, we built a stampede simulator:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Seed 100 sell orders from market-maker&lt;/li&gt;
&lt;li&gt;Fire 100 concurrent buy orders simultaneously&lt;/li&gt;
&lt;li&gt;DSQL generates serialization failures - engine retries with jitter&lt;/li&gt;
&lt;li&gt;Audit: &lt;code&gt;SUM(shares_owned)&lt;/code&gt; must equal &lt;code&gt;total_shares&lt;/code&gt; exactly&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Recent Run Results:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Settled&lt;/strong&gt;: 100/100 orders&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OCC Collisions Resolved&lt;/strong&gt;: 432&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time elapsed&lt;/strong&gt;: 1,450ms&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Balance Delta&lt;/strong&gt;: 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Audit Verdict&lt;/strong&gt;: ✅ PASSED - ZERO OVER-ALLOCATION&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mathematical proof: if two buyers both got share #47, delta would be nonzero. Delta is always 0. DSQL OCC makes this impossible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Implementation Challenges
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Drizzle wraps native errors&lt;/strong&gt;: Drizzle puts the real Postgres error in &lt;code&gt;err.cause&lt;/code&gt;, not &lt;code&gt;err&lt;/code&gt; directly. Our retry loop initially missed all &lt;code&gt;40001&lt;/code&gt; codes. Fix: inspect &lt;code&gt;err.cause?.code&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DSQL DDL restrictions&lt;/strong&gt;: No &lt;code&gt;SERIAL&lt;/code&gt; types, no synchronous indexes. Required a custom migration runner using &lt;code&gt;CREATE INDEX ASYNC&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel timeout&lt;/strong&gt;: 300 concurrent orders to Stockholm DSQL exceeded Vercel's 60s function timeout. Restructured to 3 batches of 20 with timeout escape hatch.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Live Demo &amp;amp; Code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Live Demo&lt;/strong&gt;: &lt;a href="https://sovereign-dex.vercel.app/" rel="noopener noreferrer"&gt;sovereign-dex.vercel.app&lt;/a&gt; - click &lt;strong&gt;DEMO LOGIN&lt;/strong&gt;, no signup needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run Stampede&lt;/strong&gt;: Hit &lt;strong&gt;RUN STAMPEDE&lt;/strong&gt; in the telemetry console to see OCC collisions resolve live. Watch the DynamoDB activity feed populate on the market home page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository&lt;/strong&gt;: &lt;a href="https://github.com/AmanM006/sovereign" rel="noopener noreferrer"&gt;AmanM006/sovereign&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I created this article for the H0: Hack the Zero Stack hackathon.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>nextjs</category>
      <category>database</category>
      <category>h0hackathon</category>
    </item>
  </channel>
</rss>
