<?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: Sparsh Prajapati</title>
    <description>The latest articles on DEV Community by Sparsh Prajapati (@prajapatisparsh).</description>
    <link>https://dev.to/prajapatisparsh</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%2F4279789%2F9034767e-de2a-4dfe-a3d7-7dfe9cbbe96b.jpg</url>
      <title>DEV Community: Sparsh Prajapati</title>
      <link>https://dev.to/prajapatisparsh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prajapatisparsh"/>
    <language>en</language>
    <item>
      <title>Six AI agents run a software team. The seventh rewrites their rules.</title>
      <dc:creator>Sparsh Prajapati</dc:creator>
      <pubDate>Thu, 03 Dec 2026 06:36:37 +0000</pubDate>
      <link>https://dev.to/prajapatisparsh/six-ai-agents-run-a-software-team-the-seventh-rewrites-their-rules-31mk</link>
      <guid>https://dev.to/prajapatisparsh/six-ai-agents-run-a-software-team-the-seventh-rewrites-their-rules-31mk</guid>
      <description>&lt;p&gt;Multi-agent demos usually stop at "the agents talked to each other and produced something". I wanted to know what happens on the second run. If an agent made a mistake, does the system learn from it, or does it make the same mistake forever?&lt;/p&gt;

&lt;p&gt;AI Factory is my experiment in that. It is open source: &lt;a href="https://github.com/prajapatisparsh/AI-factory" rel="noopener noreferrer"&gt;github.com/prajapatisparsh/AI-factory&lt;/a&gt;. It is written in Python with a Streamlit interface, and it runs on Groq and Gemini models.&lt;/p&gt;

&lt;h2&gt;
  
  
  The team
&lt;/h2&gt;

&lt;p&gt;Six agents each play one role in a small software team:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PM&lt;/strong&gt; writes the product requirements (the PRD).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tech Lead&lt;/strong&gt; turns them into a technical plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dev&lt;/strong&gt; writes the code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;QA&lt;/strong&gt; writes the tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reviewer&lt;/strong&gt; reviews the result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coach&lt;/strong&gt; watches how the others performed and changes how they work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each agent's behaviour comes from a plain markdown rules file, for example &lt;code&gt;agents/qa/rules.md&lt;/code&gt;. The rules are data, not code, and that is what makes the next part possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  The loop: evaluate, then rewrite the rules
&lt;/h2&gt;

&lt;p&gt;After a run, an evaluator scores the output. The Coach reads the evaluation, finds which agent caused the weakest part, and edits that agent's rules file. The next run uses the new rules.&lt;/p&gt;

&lt;p&gt;Here is a real change from one run. The evaluator found that QA's tests skipped edge cases. The Coach rewrote QA's test-planning rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;  ## Test planning
&lt;span class="gd"&gt;- Write test cases for the feature.&lt;/span&gt;
&lt;span class="gi"&gt;+ Write one test per acceptance criterion in the PRD.&lt;/span&gt;
&lt;span class="gi"&gt;+ Add a negative case for every user input.&lt;/span&gt;
&lt;span class="gi"&gt;+ Cite the PRD section id in each test name.&lt;/span&gt;
  ## Output format
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next run's evaluation went from 6.1 to 8.4 out of 10.&lt;/p&gt;

&lt;p&gt;The new rule is better in a specific way: it is checkable. "Write test cases" can't be verified. "One test per acceptance criterion, cite the section id" can be, by the evaluator and by a human reading the diff.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why rules files instead of fine-tuning
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You can read every change.&lt;/strong&gt; Each improvement is a diff in a markdown file. If the Coach makes a rule worse, you can see it and revert it, like any other commit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Changes are small and local.&lt;/strong&gt; The Coach changes one agent's rules at a time, so a regression is easy to trace to its cause.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It costs nothing to train.&lt;/strong&gt; The models stay the same; only their instructions improve.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An agent system is only as good as its evaluator.&lt;/strong&gt; If the score rewards the wrong thing, the Coach will optimise for the wrong thing with real enthusiasm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specific rules beat clever prompts.&lt;/strong&gt; Most improvements came from turning vague instructions into checkable ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep a human in the loop for rule changes&lt;/strong&gt; that touch more than one agent. Small, reviewable diffs make that cheap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code is on GitHub, and there is a short interactive version of the rule diff on my portfolio.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm Sparsh Prajapati, a full-stack engineer at Cloudzent in Gurugram, India. More of my work: &lt;a href="https://sparshprajapati.com" rel="noopener noreferrer"&gt;sparshprajapati.com&lt;/a&gt; · &lt;a href="https://www.linkedin.com/in/prajapatisparsh/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; · &lt;a href="https://github.com/prajapatisparsh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>agents</category>
      <category>python</category>
    </item>
    <item>
      <title>Moving 37 million rows over a connection that drops</title>
      <dc:creator>Sparsh Prajapati</dc:creator>
      <pubDate>Thu, 03 Dec 2026 06:36:22 +0000</pubDate>
      <link>https://dev.to/prajapatisparsh/moving-37-million-rows-over-a-connection-that-drops-1go3</link>
      <guid>https://dev.to/prajapatisparsh/moving-37-million-rows-over-a-connection-that-drops-1go3</guid>
      <description>&lt;p&gt;A publicly listed Indian steel manufacturer kept its sales history in SQL Server: 37 million rows. I was building a CRM intelligence layer on top of it (lead scoring, plus a different view for each of 12 roles from field officer to CMO), and that layer runs on PostgreSQL. So the first job was getting 37 million rows from SQL Server into Postgres.&lt;/p&gt;

&lt;p&gt;The hard part was not the volume. It was the link. The connection between the two databases dropped, and a long copy that dies at row 21 million either starts over, or worse, restarts halfway and writes the same rows twice.&lt;/p&gt;

&lt;p&gt;The rule I set was simple: pull the plug at any moment, and the sync must recover with zero duplicates and no lost rows. Here is the design that met it, built with FastAPI and SQLAlchemy.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Copy in chunks, one transaction per chunk
&lt;/h2&gt;

&lt;p&gt;Instead of one huge copy, the sync reads the source in fixed-size chunks ordered by the source table's primary key. Each chunk is written to Postgres inside a single transaction. Either the whole chunk lands, or none of it does.&lt;/p&gt;

&lt;p&gt;Keyset pagination (&lt;code&gt;WHERE id &amp;gt; :last ORDER BY id&lt;/code&gt;) is better than &lt;code&gt;OFFSET&lt;/code&gt; here. &lt;code&gt;OFFSET&lt;/code&gt; gets slower the deeper you go, and it can skip or repeat rows if the source changes while you read.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Commit the checkpoint in the same transaction
&lt;/h2&gt;

&lt;p&gt;This is the part that makes it resumable. A small checkpoint table in Postgres records the last source id that was written. It is updated inside the same transaction as the chunk:&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="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;sqlalchemy&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sync_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""src: a SQL Server connection. dst: the Postgres engine."""&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT TOP (:n) id, account, amount, updated_at FROM sales "&lt;/span&gt;
             &lt;span class="s2"&gt;"WHERE id &amp;gt; :last ORDER BY id"&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="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;last_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;size&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mappings&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="nb"&gt;all&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="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;rows&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;None&lt;/span&gt;  &lt;span class="c1"&gt;# done&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# one transaction: the rows and the checkpoint commit together&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"INSERT INTO sales (source_id, account, amount, updated_at) "&lt;/span&gt;
                 &lt;span class="s2"&gt;"VALUES (:id, :account, :amount, :updated_at) "&lt;/span&gt;
                 &lt;span class="s2"&gt;"ON CONFLICT (source_id) DO UPDATE SET "&lt;/span&gt;
                 &lt;span class="s2"&gt;"account = EXCLUDED.account, amount = EXCLUDED.amount, updated_at = EXCLUDED.updated_at"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"UPDATE sync_checkpoint SET last_id = :last WHERE job = 'sales'"&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="s2"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&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="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Table and column names are simplified for the post.)&lt;/p&gt;

&lt;p&gt;If the connection dies mid-chunk, Postgres rolls back both the partial rows and the checkpoint update. The checkpoint still points at the end of the last complete chunk, so the next run starts exactly there:&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="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT last_id FROM sync_checkpoint WHERE job = 'sales'"&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="nf"&gt;scalar_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nf"&gt;sync_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CHUNK_SIZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Make retries harmless with an upsert on source_id
&lt;/h2&gt;

&lt;p&gt;Transactions protect you from half-written chunks. They do not protect you from a retry that re-sends a chunk that actually committed (for example, the commit succeeded but the acknowledgement was lost). The fix is a unique key on &lt;code&gt;source_id&lt;/code&gt; and &lt;code&gt;INSERT ... ON CONFLICT (source_id) DO UPDATE&lt;/code&gt;. Writing the same chunk twice leaves the table exactly as if it had been written once. Retries can't duplicate.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Test it by actually cutting the cable
&lt;/h2&gt;

&lt;p&gt;I did not trust this until I had broken it on purpose: kill the connection in the middle of a chunk, resume, then compare row counts and checksums between source and target. On my portfolio there is an interactive version of this: a simulated sync you can cut mid-chunk and resume, with a duplicates counter that stays at zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same idea, one layer up: scores that survive an outage
&lt;/h2&gt;

&lt;p&gt;The CRM layer scores every lead with an LLM (Groq). LLM APIs stall too, so scoring follows the same philosophy as the sync: a failure must degrade, not break. When the model times out, rule-based scoring takes over (for example, "3+ orders in 90 days and a quote accepted within 7 days" means hot) and the screen says that rules, not the model, produced the score. Users always get an answer, and they always know where it came from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Chunk the work and make each chunk atomic.&lt;/li&gt;
&lt;li&gt;Store progress in the same transaction as the data it describes.&lt;/li&gt;
&lt;li&gt;Make writes idempotent with a natural key and an upsert, so retries are safe.&lt;/li&gt;
&lt;li&gt;Break it on purpose before you trust it.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I'm Sparsh Prajapati, a full-stack engineer at Cloudzent in Gurugram, India. More of my work, including the interactive sync demo: &lt;a href="https://sparshprajapati.com" rel="noopener noreferrer"&gt;sparshprajapati.com&lt;/a&gt; · &lt;a href="https://www.linkedin.com/in/prajapatisparsh/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; · &lt;a href="https://github.com/prajapatisparsh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>postgres</category>
      <category>sqlserver</category>
      <category>dataengineering</category>
    </item>
    <item>
      <title>Proving consent without asking anyone to trust our database</title>
      <dc:creator>Sparsh Prajapati</dc:creator>
      <pubDate>Thu, 03 Dec 2026 06:25:13 +0000</pubDate>
      <link>https://dev.to/prajapatisparsh/proving-consent-without-asking-anyone-to-trust-our-database-50k0</link>
      <guid>https://dev.to/prajapatisparsh/proving-consent-without-asking-anyone-to-trust-our-database-50k0</guid>
      <description>&lt;p&gt;India's Digital Personal Data Protection Act (DPDP, 2023) says a company must be able to show that a person consented before it processed their data. Most consent tools answer that with a database row: &lt;code&gt;visitor 7f3a, analytics = granted, 09:14&lt;/code&gt;. The trouble is who can edit that row. Anyone with admin access to the database, including the company being audited, can change it later, and nothing in the row would show it.&lt;/p&gt;

&lt;p&gt;I'm the primary developer of PrivacySuraksha, a DPDP compliance platform that is live in production. One of the problems I had to solve was this one: how do you prove a consent record is genuine without asking the auditor to trust our database?&lt;/p&gt;

&lt;p&gt;The answer we shipped is a ledger nobody can quietly edit. Every consent event is hashed. Every day, all of that day's hashes are sealed into one SHA-256 Merkle root. The root is signed with Ed25519 and timestamped on Bitcoin with OpenTimestamps. An auditor can then verify any single record without trusting us.&lt;/p&gt;

&lt;p&gt;Here is how the pieces fit, and why each one is there.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Hash every event
&lt;/h2&gt;

&lt;p&gt;A consent event is a small, canonical string: record id, a pseudonymous visitor id, the purpose, the decision and a timestamp.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cs_0142 visitor 1c9e marketing=denied 2026-11-30T09:20Z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its SHA-256 hash becomes a leaf of the day's tree. In the browser or on Cloudflare Workers, WebCrypto does this in a few lines:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;enc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Uint8Array&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;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subtle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SHA-256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// a 0x00 prefix for leaves and 0x01 for inner nodes (as in RFC 6962), so a leaf can never pass as a node&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;leaf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;sha256&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;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&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="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="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;sha256&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;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;r&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change one character of the record and the hash changes completely. That property is what everything else builds on.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Seal a day into one Merkle root
&lt;/h2&gt;

&lt;p&gt;Storing thousands of hashes does not help an auditor much on its own. A Merkle tree turns them into a single 32-byte value: hash leaves in pairs, then hash those results in pairs, until one hash is left.&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;merkleRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leaves&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&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="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leaves&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&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="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&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="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// odd count: pair the last node with itself&lt;/span&gt;
      &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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;node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&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="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&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="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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;p&gt;If any record from that day is edited, added or removed, the root changes. So instead of protecting every row, we only have to protect one value per day.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Sign the root
&lt;/h2&gt;

&lt;p&gt;A root on its own proves the data has not changed since the root was computed. It does not say who computed it. Signing the daily root with an Ed25519 key ties it to us: an auditor can check the signature with our public key, and a forged root will not verify.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Anchor it on Bitcoin
&lt;/h2&gt;

&lt;p&gt;A signature still leaves one gap: we hold the key, so in theory we could rebuild a day's tree with an edited record, sign the new root and backdate it. Timestamping closes that gap. We submit the signed root to OpenTimestamps, which commits it into a Bitcoin transaction. Once that transaction is in a block, anyone can show the root existed at that time, and no one, including us, can change it afterwards without the mismatch showing.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Verify one record without the rest
&lt;/h2&gt;

&lt;p&gt;An auditor who wants to check one consent does not need the whole day's data. They need the record, the sibling hashes on its path to the root (an inclusion proof, about log2(n) hashes), and the signed, anchored root.&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="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;proof&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Step&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="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;h&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;leaf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;record&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;proof&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;left&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;node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&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;node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hash&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="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the recomputed root matches the signed, Bitcoin-anchored root, the record is exactly what was there on that day. If one letter changed, it will not match. That is the whole point: the check does not depend on trusting our database, our servers or us.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would tell someone building this
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Keep personal data out of the leaves.&lt;/strong&gt; Use pseudonymous ids. The ledger proves what happened; it should not become another copy of the personal data you are trying to protect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefix leaves and nodes differently.&lt;/strong&gt; Without the 0x00 / 0x01 prefixes, an attacker can present an inner node as if it were a leaf. It is a one-byte fix.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make the record format canonical.&lt;/strong&gt; If two services serialise the same event differently, their hashes differ and proofs fail. Pick one exact format and test it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch, then anchor.&lt;/strong&gt; One root per day keeps signing and anchoring cheap no matter how many events there are.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Show the failure.&lt;/strong&gt; On my portfolio, the hero is a live Merkle tree: change one letter of a consent record and you can watch the proof fail. It explains the idea faster than any diagram.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can try that demo at &lt;a href="https://sparshprajapati.com" rel="noopener noreferrer"&gt;sparshprajapati.com&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm Sparsh Prajapati, a full-stack engineer at Cloudzent in Gurugram, India. I build product interfaces and the systems underneath them. More of my work: &lt;a href="https://sparshprajapati.com" rel="noopener noreferrer"&gt;sparshprajapati.com&lt;/a&gt; · &lt;a href="https://www.linkedin.com/in/prajapatisparsh/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; · &lt;a href="https://github.com/prajapatisparsh" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>cryptography</category>
      <category>privacy</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
