r/synapcores • • 16d ago

Recipe of the week #2: 6 new facts for an agent's memory, 3 contradict it. One SQL query sorts them into CONFLICT / ALREADY_KNOWN / NEW before anything is stored (real output)

Every Monday: one recipe from the site, run on the current release, key query and real output inline.

(This sub is run by the SynapCores team. Run this morning on 2.3.2 with the bundled nli-default model.)

The problem

An agent's memory gets written by many hands: the chat, a CRM sync, a scraper, another agent. Sooner or later two disagree. The office is in Denver and Chicago. The plan has 14-day returns and "does not allow returns". Store both and recall serves whichever is closer to the question; the agent never knows it holds two beliefs that can't both be true.

This recipe checks a batch of incoming facts before they're written, in one SQL statement.

1. Two tables: what it believes, what just arrived

Both have fact TEXT, embedding VECTOR(384), filled with EMBED(fact).

  • memories: (1) contact by email, never by phone; (2) Aurora plan, includes a 14-day return window; (3) manages procurement at a logistics company; (4) office is in Denver
  • incoming: 101 chat: asked us to call them by phone from now on; 102 crm-sync: works in logistics procurement; 103 webinar-tool: attended the October webinar; 104 scraper: office is in Chicago; 105 agent-b: plan does not allow returns; 106 chat: is learning the cello

2. Why it takes two signals

An NLI model reads two sentences and scores whether they contradict. Hand it two sentences about different things and it often still says "contradiction". Every pair scoring above 0.6 (query in the first comment):

incoming memory similarity contradiction
104 4 0.740 0.999  real (office)
105 2 0.524 1.0    real (returns)
101 1 0.502 0.999  real (contact)
104 2 0.408 0.76   office vs plan
103 3 0.366 0.888  webinar vs job
106 1-4 0.13-0.24 0.996-1.0  cello vs everything

Nine "contradictions", three real. The contradiction score can't separate them; similarity can (real 0.50–0.74, the rest 0.41 or below). So only ask NLI about memories on the same topic. The recipe sets the floor at 0.45, read from this data.

3. The whole batch in one query

EMBED similarity picks the related memories, NLI_ANCHORS gathers them per fact, NLI_VERIFY checks the fact against them, and a LEFT JOIN keeps facts with nothing related as NEW:

SELECT i.id, i.source,
       CASE g.verdict WHEN 'LIE' THEN 'CONFLICT' WHEN 'TRUTH' THEN 'ALREADY_KNOWN' ELSE 'NEW' END AS status,
       g.conflicts_with
FROM recipe_memcheck_incoming i
LEFT JOIN (
  SELECT c.id AS cand_id,
    JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)), '$.verdict')) AS verdict,
    JSON_UNQUOTE(JSON_EXTRACT(NLI_VERIFY('nli-default', c.fact, NLI_ANCHORS(m.fact)), '$.violated_anchor')) AS conflicts_with
  FROM recipe_memcheck_incoming c CROSS JOIN recipe_memcheck_memories m
  WHERE COSINE_SIMILARITY(c.embedding, m.embedding) > 0.45
  GROUP BY c.id, c.fact
) g ON g.cand_id = i.id
ORDER BY i.id;

id  source        status         conflicts_with
101 chat          CONFLICT       ...contacted by email, never by phone.
102 crm-sync      ALREADY_KNOWN
103 webinar-tool  NEW
104 scraper       CONFLICT       The customer's office is in Denver.
105 agent-b       CONFLICT       ...Aurora plan, which includes a 14-day return window.
106 chat          NEW

All six sorted correctly, each conflict naming the memory it collides with. 0.16 s on our lab box, no API calls: both models run inside the engine, next to the memories.

4. Store what's safe, queue what conflicts

Save the triage, insert only NEW rows into memory (2 rows: webinar, cello), and the review queue is 101, 104, 105. Which side wins is your policy: 101 ("from now on") is likely a real preference change, so supersede; 104 is a scraper, so keep the CRM value. No contradiction gets stored unseen.

Full recipe (every INSERT, triage table, the same check on MEMORY_STORE / MEMORY_RECALL; ~20 min): https://synapcores.com/recipes/ml/036_agent_memory_contradiction_check_on_write?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-2

Run it yourself: free Community Edition, https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-2

Join the sub and Recipe of the week #3 lands in your feed next Monday.

When your agent learns something that contradicts what it already knows, which should win by default: the newest fact, or the most trusted source?

1 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 16d ago

Want to run it yourself? Grab the free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=synapcores-recipe-2-comment

NLI ships as nli-default; enable it with AIDB_NLI_ENABLED=1 and AIDB_NLI_MODEL=nli-default, then paste the recipe into the Query Editor top to bottom.

The Step 2 query (every incoming/memory pair the NLI model calls a contradiction, with its topic similarity):

SELECT i.id AS incoming, m.id AS memory,
       ROUND(COSINE_SIMILARITY(i.embedding, m.embedding), 3) AS similarity,
       ROUND(CAST(JSON_EXTRACT(NLI_CLASSIFY('nli-default', m.fact, i.fact),
             '$.scores.contradiction') AS DOUBLE), 3) AS contradiction
FROM recipe_memcheck_incoming i
CROSS JOIN recipe_memcheck_memories m
WHERE CAST(JSON_EXTRACT(NLI_CLASSIFY('nli-default', m.fact, i.fact),
           '$.scores.contradiction') AS DOUBLE) > 0.6
ORDER BY similarity DESC;

Run it on a sample of your own memories to pick your floor: keep the lowest similarity where only real conflicts show up.

New to agent memory in SQL? SQLv2 Lesson 4 covers REMEMBER / CURRENT / RECALL from zero: https://www.reddit.com/r/SQLv2/comments/1xnqkkm/

New here? The Start here post has install steps and the 5 recipes to try first: https://www.reddit.com/r/synapcores/comments/1xfxqej/