<?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: Ada@Cognitia</title>
    <description>The latest articles on DEV Community by Ada@Cognitia (@ada_cognitia).</description>
    <link>https://dev.to/ada_cognitia</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%2F4218924%2F419dbca6-2888-4ee4-ae9b-216766f694b9.png</url>
      <title>DEV Community: Ada@Cognitia</title>
      <link>https://dev.to/ada_cognitia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ada_cognitia"/>
    <language>en</language>
    <item>
      <title>Late facts shouldn't overwrite newer ones: handling out-of-order state in a temporal knowledge graph</title>
      <dc:creator>Ada@Cognitia</dc:creator>
      <pubDate>Sat, 31 Oct 2026 12:30:49 +0000</pubDate>
      <link>https://dev.to/ada_cognitia/late-facts-shouldnt-overwrite-newer-ones-handling-out-of-order-state-in-a-temporal-knowledge-graph-4i1p</link>
      <guid>https://dev.to/ada_cognitia/late-facts-shouldnt-overwrite-newer-ones-handling-out-of-order-state-in-a-temporal-knowledge-graph-4i1p</guid>
      <description>&lt;p&gt;When you extract facts from someone's messages with an LLM, they don't arrive in order. A re-extraction, a history backfill or an out-of-order batch will happily deliver &lt;em&gt;yesterday's truth&lt;/em&gt; after &lt;em&gt;today's&lt;/em&gt;. If your store decides "what is current" by arrival order, a late fact quietly takes over.&lt;/p&gt;

&lt;p&gt;Here's the pattern we ended up with in a personal knowledge graph I work on. It's small, but it took us a few wrong turns to get there.&lt;/p&gt;

&lt;h2&gt;
  
  
  The data model
&lt;/h2&gt;

&lt;p&gt;Facts are triples with a type:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;state&lt;/code&gt;: an ongoing truth ("Alex works_at Acme")&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;event&lt;/code&gt;: something that happened at a point in time&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;preference&lt;/code&gt;: a preference or habit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each row carries two clocks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;World time&lt;/strong&gt;: &lt;code&gt;valid_at&lt;/code&gt; / &lt;code&gt;invalid_at&lt;/code&gt;, when the fact was true in the world&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System time&lt;/strong&gt;: &lt;code&gt;observed_at&lt;/code&gt; / &lt;code&gt;expired_at&lt;/code&gt;, when we learned it and when we stopped treating it as current&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a given subject and predicate there is at most &lt;strong&gt;one open state row&lt;/strong&gt;, meaning no &lt;code&gt;invalid_at&lt;/code&gt;, no &lt;code&gt;expired_at&lt;/code&gt; and no &lt;code&gt;superseded_by&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The naive rule (and the bug)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;New state for the same subject + predicate arrives → close the open row, insert the new one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's arrival order. Suppose we already know "Alex works_at Acme (valid from March)". Then a backfill delivers "Alex works_at Initech (valid from January)". The naive rule makes Initech current and stamps Acme's &lt;code&gt;invalid_at&lt;/code&gt; with January, so Acme's world &lt;em&gt;ends before it starts&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: file it as history
&lt;/h2&gt;

&lt;p&gt;Before closing the open row, compare world times:&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hasDeadlineValidAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;      &lt;span class="c1"&gt;// see below&lt;/span&gt;
  &lt;span class="nf"&gt;isClosableCurrentState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openFact&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
  &lt;span class="nx"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;validAt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;openFact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;validAt&lt;/span&gt;    &lt;span class="c1"&gt;// the incoming fact describes an earlier world&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;invalid_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;openFact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valid_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// true until the newer state began&lt;/span&gt;
    &lt;span class="na"&gt;superseded_by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;openFact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// expired_at stays null: nothing was retracted, it just isn't the latest&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="c1"&gt;// and it must NOT be used to close anything else&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details turned out to matter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The history row must never be open.&lt;/strong&gt; Our "find the current state" probe reads one open row. A second open row for the same subject and predicate would hide the real current state from the next insert.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It must not act as a closer.&lt;/strong&gt; We have a second pass that ends &lt;em&gt;other&lt;/em&gt; states based on incoming facts (e.g. a "finished onboarding" event closes "onboarding pending"). A history row isn't new information about the present, so it's kept out of that pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mark it pre-resolved for review tools.&lt;/strong&gt; Our contradiction review restores a fact by clearing &lt;code&gt;invalid_at&lt;/code&gt; and &lt;code&gt;superseded_by&lt;/code&gt;. Running that on a history row would recreate exactly the second open row from point 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The exception: deadlines
&lt;/h2&gt;

&lt;p&gt;For some predicates &lt;code&gt;valid_at&lt;/code&gt; isn't "when it became true" but a &lt;strong&gt;deadline&lt;/strong&gt; ("has_pending_reminder … due Friday"). Moving a deadline &lt;em&gt;earlier&lt;/em&gt; is a legitimate update, not a stale fact. Applying the world-time rule there would pin the old deadline. So those predicates are excluded first, and the rest of the rule applies to everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Duplicates still count
&lt;/h2&gt;

&lt;p&gt;If the exact triple is already recorded, we don't insert it again, but we still feed it to the closing pass. A duplicate observation of "onboarding completed" should still be allowed to close "onboarding pending" if that wasn't closed the first time.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Decide "current" by world time, not arrival time.&lt;/li&gt;
&lt;li&gt;Late facts are history: give them an &lt;code&gt;invalid_at&lt;/code&gt;, never an open slot.&lt;/li&gt;
&lt;li&gt;Keep a short list of predicates where &lt;code&gt;valid_at&lt;/code&gt; means something else (deadlines).&lt;/li&gt;
&lt;li&gt;Watch your repair tools: anything that "restores" a fact can reintroduce the invariant you just protected.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This article was written with the help of AI. I described the design and reviewed the text for accuracy, and the code is simplified from our implementation.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>architecture</category>
      <category>llm</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
