<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:discourse="http://discourse.org/rss/modules/discourse/">
<channel>
<title>RAG on Roque</title>
<link>https://j-roque.com/tags/rag/</link>
<description>Recent content in RAG on Roque</description>
<generator>Hugo -- gohugo.io</generator>
<language>en</language>
<managingEditor>joao.roque.94@gmail.com (João Roque)</managingEditor>
<webMaster>joao.roque.94@gmail.com (João Roque)</webMaster>
<copyright>© 2026 João Roque</copyright>
<lastBuildDate>Wed, 02 Dec 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://j-roque.com/tags/rag/index.xml" rel="self" type="application/rss+xml"/>
<item>
<title>Persistent Memory for MES Agents: Approved Rules and Semantic Search</title>
<link>https://j-roque.com/posts/20261202-agentmemory/</link>
<pubDate>Wed, 02 Dec 2026 00:00:00 +0000</pubDate>
<author>joao.roque.94@gmail.com (João Roque)</author>
<guid>https://j-roque.com/posts/20261202-agentmemory/</guid>
<description>Last week a person answered the agent&amp;rsquo;s question in a notification. This week the agent turns that answer into a rule, a process engineer approves it in the MES, and the same ambiguous file is posted without a review. We do it twice: once with an index of rules in the prompt, and once with RAG, through a custom MCP tool that searches the embeddings MES v12 already keeps.</description>
<content:encoded>&lt;p&gt;&lt;img src="https://j-roque.com/posts/20261202-agentmemory/featured.png" alt="Persistent Memory for MES Agents: Approved Rules and Semantic Search" /&gt;&lt;/p&gt;&lt;p&gt;An agent should not ask a person the same question twice. It should learn from the answer, and to do that it needs memory.&lt;/p&gt;
&lt;p&gt;In &lt;a
href="https://j-roque.com/posts/20261125-humanintheloop/"
target="_blank"
&gt;last week&amp;rsquo;s post&lt;/a&gt; the agent asked, a person answered, and the answer helped exactly one file. In this post the answer becomes an MES rule that the agent uses on every file after it.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 class="relative group"&gt;Overview
&lt;/h2&gt;
&lt;p&gt;The setup is the one from &lt;a
href="https://j-roque.com/posts/20261118-agenticframework/"
target="_blank"
&gt;the agentic framework post&lt;/a&gt;: a &lt;code&gt;Connect IoT&lt;/code&gt; controller watches a folder, the &lt;strong&gt;File Parser for DataCollection&lt;/strong&gt; agent maps each file to a Data Collection, and when it is unsure it asks a person through a notification.&lt;/p&gt;
&lt;p&gt;For this post I dropped the same file into the controller three times on a local MES v12 stack:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Run&lt;/th&gt;
&lt;th&gt;What the agent has&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;No rules&lt;/td&gt;
&lt;td&gt;&lt;code&gt;needs_review&lt;/code&gt;, a person answers, the agent proposes a rule&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;The approved rule, sent in the prompt&lt;/td&gt;
&lt;td&gt;Posted without a review&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;No rules in the prompt, plus a search tool over six rules in the MES&lt;/td&gt;
&lt;td&gt;Posted without a review&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The post follows that order. Part 1 is about &lt;strong&gt;rules&lt;/strong&gt;: memory kept as approved MES data. Part 2 is about &lt;strong&gt;RAG&lt;/strong&gt;: the agent searches for the rule that fits its case.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I always shoutout to this great article from IBM about what is RAG (retrieval augmented generation), check it out to learn more on the concept &lt;a
href="https://www.ibm.com/think/topics/retrieval-augmented-generation"
target="_blank"
&gt;What is RAG (Retrieval Augmented Generation)? | IBM&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 class="relative group"&gt;The Inspection Report
&lt;/h2&gt;
&lt;p&gt;The file is a full inspection report of housing &lt;code&gt;A12&lt;/code&gt;. The top face flatness was measured twice, once per probe.&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;HOUSING A12 - FULL INSPECTION REPORT
Feature                         Actual    Unit
Bore D1 Diameter                12.004    mm
Bore D2 Diameter                7.998     mm
Bore Distance D1-D2             41.998    mm
Bore D1 Roundness               0.0040    mm
Bore D2 True Position           0.021     mm
Flatness Top Face (probe 1)     0.012     mm
Flatness Top Face (probe 2)     0.018     mm
Parallelism Top to Base         0.010     mm
Overall Length                  84.512    mm
Overall Width                   32.006    mm&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Data Collection&amp;rsquo;s &lt;code&gt;Flatness&lt;/code&gt; parameter takes one reading per sample, and both values fit it. &lt;strong&gt;The agent&amp;rsquo;s instructions say that when two values in the file could fit the same parameter, it must not choose one&lt;/strong&gt;. A simple parser would take the first match and post it without any warning.&lt;/p&gt;
&lt;p&gt;In this example, probe 1 is the reference probe and probe 2 sits next to the fixture clamp, where clamping can bend the face, so its reading is for information only. The plant&amp;rsquo;s quality engineer decides which probe is the reference, so a person approves every rule the agent proposes.&lt;/p&gt;
&lt;h2 class="relative group"&gt;Part 1: Rules
&lt;/h2&gt;
&lt;h3 class="relative group"&gt;Run 1: The Agent Asks for a Review
&lt;/h3&gt;
&lt;p&gt;The agent answered with &lt;code&gt;needs_review&lt;/code&gt;, and the &lt;code&gt;Run Agent&lt;/code&gt; task opened a notification. Both flatness readings are under &lt;em&gt;Unmapped&lt;/em&gt;, &lt;code&gt;Flatness&lt;/code&gt; is under &lt;em&gt;Missing&lt;/em&gt;, and &lt;strong&gt;Applied Rules&lt;/strong&gt; is empty.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="The review notification"
src="https://image.j-roque.com/posts/20261202-agentmemory/notification-needs-review.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;Run 1: &lt;code&gt;needs_review&lt;/code&gt;, two flatness candidates, no applied rules.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In the Respond wizard the agent explains the problem: two values, one reading per sample, and &lt;em&gt;&amp;ldquo;I did not choose between them&amp;rdquo;&lt;/em&gt;.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="The agent&amp;rsquo;s request"
src="https://image.j-roque.com/posts/20261202-agentmemory/respond-request.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;The agent&amp;rsquo;s request in the Respond wizard.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A person can answer for this file only (&amp;ldquo;use 0.012&amp;rdquo;) or for every report like it. To get a rule out of it, the answer has to be the second kind:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="The answer"
src="https://image.j-roque.com/posts/20261202-agentmemory/answer-typed.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;&amp;ldquo;This is a general rule for every Housing A12 inspection report, not only this file… Please propose this as a reusable rule.&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The agent ran again with the answer and returned &lt;code&gt;ok&lt;/code&gt;. The task cleared the notification and posted the Data Collection with &lt;code&gt;Flatness Top Face = 0.012&lt;/code&gt;.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="The request is closed"
src="https://image.j-roque.com/posts/20261202-agentmemory/request-closed.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;After one answer the request is closed.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Run 1 posted"
src="https://image.j-roque.com/posts/20261202-agentmemory/dc-run1.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;&lt;code&gt;DC_Housing_A12_FullInspection-000000015&lt;/code&gt;: Flatness Top Face 0.012, the probe 1 reading.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Up to here this is the flow from last week. The controller log shows one new line, between clearing the notification and posting the data:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;09:35:51 RunAgent: HITL-12cef2f8-… cleared, the agent&amp;#39;s result needs no further review
09:35:51 RunAgent: learned rule &amp;#39;housing-a12-flatness-use-probe-1-4a35&amp;#39; persisted (waiting for approval)
09:35:52 dataCollection: Successfully performed data into Resource FileParserResource.&lt;/code&gt;&lt;/pre&gt;
&lt;h3 class="relative group"&gt;The Agent Proposes the Rule, a Person Approves It
&lt;/h3&gt;
&lt;p&gt;The agent&amp;rsquo;s reply now has a &lt;code&gt;proposedRules&lt;/code&gt; array next to the mappings. When a result is accepted &lt;strong&gt;after at least one human answer&lt;/strong&gt;, the &lt;code&gt;Run Agent&lt;/code&gt; task creates one &lt;code&gt;AgentMemory&lt;/code&gt; instance per proposed rule. &lt;code&gt;AgentMemory&lt;/code&gt; is a custom MES entity type.&lt;/p&gt;
&lt;p&gt;The agent memory entity will store all the information that was added to the contextual information reality provides. The entity will store context and rules so that the agents know if for their case these rules apply.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="The proposed rule"
src="https://image.j-roque.com/posts/20261202-agentmemory/rule-proposed.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;The rule the agent wrote from the answer, with IsActive set to No.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Property&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;What the agent cites in &lt;code&gt;appliedRules&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;AppliesTo&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The process the rule belongs to (lookup, here &lt;code&gt;FileIntegration&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The kind of outcome it leads to (lookup, here &lt;code&gt;DataCollection&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;One line that is always sent to the agent, enough to tell whether the rule is relevant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;When&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The situation the rule is for&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Guidance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;What to do in that situation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Optional input and output&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;IsActive&lt;/strong&gt;, &lt;strong&gt;ApprovedBy&lt;/strong&gt;, &lt;strong&gt;ApprovedOn&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Approval and who gave it&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The situation (&lt;strong&gt;When&lt;/strong&gt;) and the instruction (&lt;strong&gt;Guidance&lt;/strong&gt;) are separate properties on purpose. Part 2 embeds the situation and the one-line Summary, not the full Guidance.&lt;/p&gt;
&lt;p&gt;The task creates the rule inactive, because one conversation with one person is not enough to change how every future file is read. A process engineer opens it under &lt;strong&gt;Administration → Agent Memory&lt;/strong&gt;, checks it, and approves it.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Approving the rule"
src="https://image.j-roque.com/posts/20261202-agentmemory/rule-approve.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;Approval is an edit: IsActive on, ApprovedBy and ApprovedOn set.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Agents that write their own memory have a known problem. In reflection and similar designs, the agent writes lessons from its failures and reuses them in later prompts, and a wrong lesson keeps being applied until someone notices. Some memory systems such let the agent edit its long-term memory itself. The approval step prevents this here, because the agent does not use a rule until a person approves it. In our scenario we want a human in the loop for the creation of rules.&lt;/p&gt;
&lt;p&gt;I keep the rules as an MES entity, rather than in a text file or a chat history, because then they get what every other MES record gets: history of every change, security on who can edit and approve, the same Create/Save/Terminate lifecycle, and master data export to other environments. A process engineer can see every rule the agent uses and who approved it. This is sometimes called a procedural memory (how to do a task) kept as MES data instead of model weights.&lt;/p&gt;
&lt;h3 class="relative group"&gt;How Rules Reach the Agent
&lt;/h3&gt;
&lt;p&gt;The agent keeps no state between runs, so the rules have to be in the prompt. The &lt;code&gt;RunAgentPrompt&lt;/code&gt; service takes the run&amp;rsquo;s &lt;code&gt;AppliesTo&lt;/code&gt; and &lt;code&gt;Scope&lt;/code&gt;, selects the active rules for them, newest approval first, and appends an &lt;code&gt;&amp;lt;agent_memory&amp;gt;&lt;/code&gt; block to the prompt:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;&amp;lt;agent_memory&amp;gt;
Rules from earlier human reviews. If one matches the case, read it in full with get_object_by_name
(entity type AgentMemory) before applying it unless its guidance is shown, follow it over your own
judgement and name it in appliedRules. If none matches and you are unsure, ask for review as you normally would.
housing-a12-flatness-use-probe-1-4a35 · DataCollection · Housing A12 reports with Flatness Top Face per probe: post probe 1 to Flatness, list probe 2 as unmapped. · Map the probe 1 reading to the Flatness parameter with high confidence. …
&amp;lt;/agent_memory&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The MES service adds this block after the controller has sent its prompt.&lt;/p&gt;
&lt;p&gt;There is one line per rule. The Guidance is included when it is short (up to 300 characters by default). For a longer one, the agent reads the full rule with the stock &lt;code&gt;get_object_by_name&lt;/code&gt; tool of the CM MES MCP server. The goal is to provide just enough amount of context, in order for the agent to make decisions, but provide also a way for the agent if he so requires to grab the full information from the MES.&lt;/p&gt;
&lt;h3 class="relative group"&gt;Run 2: Posted Without a Review
&lt;/h3&gt;
&lt;p&gt;The same file, with the rule approved:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;09:38:15 RunAgent: calling agent &amp;#39;File Parser for DataCollection&amp;#39; (prompt: 932 characters)
09:38:36 RunAgent: agent &amp;#39;File Parser for DataCollection&amp;#39; answered in 21549 ms (response: 1954 characters)
09:38:36 dataCollection: Successfully performed data into Resource FileParserResource.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This time the task created no notification. Run 1 stopped on the same file without the rule, so now with the rules for that &lt;strong&gt;AppliesTo&lt;/strong&gt; and &lt;strong&gt;Scope&lt;/strong&gt; the agent had enough information to make a decision without requesting a human in the loop intervention. With different files or different information, those rules may not be enough, in that case the agent will ask the human what to do and in turn generate new rules.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Run 2 posted"
src="https://image.j-roque.com/posts/20261202-agentmemory/dc-run2-index.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;&lt;code&gt;-000000016&lt;/code&gt;: posted on the first pass with the value the rule gives.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;For many plants this is enough. However, every run gets every active rule of its AppliesTo and Scope, relevant or not, and rules are selected by those two labels, not by what the file contains. With five rules that is fine. Beyond that, the cap leaves the oldest rules out, and raising it makes every prompt longer. Even below the cap, a long prompt costs accuracy: models use information in the middle of a long context worse than information at its start or end.&lt;/p&gt;
&lt;h2 class="relative group"&gt;Part 2: RAG and Embeddings
&lt;/h2&gt;
&lt;p&gt;Part 1 was leveraging information that we already knew about the scenario and using that to extract and provide immediately all the memory rules. In Part 2 we are going to show a totally different approach, but using the exact same rules.&lt;/p&gt;
&lt;h3 class="relative group"&gt;Searching Rules by Meaning
&lt;/h3&gt;
&lt;p&gt;An &lt;strong&gt;embedding&lt;/strong&gt; is a list of numbers (a vector) that a model computes from a text. Texts with similar meaning get vectors that are close to each other. To search, you compute the vector of the question and return the stored texts with the smallest cosine distance to it (0 means the same direction, larger means less related).&lt;/p&gt;
&lt;p&gt;That search is the &lt;strong&gt;retrieval&lt;/strong&gt; in &lt;strong&gt;Retrieval-Augmented Generation&lt;/strong&gt; (RAG). The agent no longer gets every rule in the prompt. When it is stuck, it describes its case and gets back the few rules closest to it.&lt;/p&gt;
&lt;h3 class="relative group"&gt;MES v12 Already Keeps the Vectors
&lt;/h3&gt;
&lt;p&gt;MES v12 ships a RAG service, &lt;code&gt;core-rag&lt;/code&gt;. It indexes every entity type that has &lt;strong&gt;Semantic Search Enabled&lt;/strong&gt; and whose operations publish events to Kafka. When an instance is created, saved or terminated, &lt;code&gt;core-rag&lt;/code&gt; asks the MES for the instance&amp;rsquo;s texts, computes the embedding with the stack&amp;rsquo;s embedding model and stores the vector in ClickHouse. Every instance that is not terminated is indexed, inactive rules included, and terminated ones are removed. Filtering on IsActive happens later, in the MCP server. For &lt;code&gt;AgentMemory&lt;/code&gt;, an install-time process rule turns on Semantic Search and the Kafka events, so we do not run a vector database of our own.&lt;/p&gt;
&lt;p&gt;We had to decide two things ourselves.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What gets embedded.&lt;/strong&gt; By default &lt;code&gt;core-rag&lt;/code&gt; embeds the Name and Description. For a rule, the Name is a slug and the Description is not enough. But we can with a simple DEE enrich this a bit more. A DEE on the Post action group of &lt;code&gt;GetEmbeddingAndContextTextsForEntityType&lt;/code&gt; replaces both texts for &lt;code&gt;AgentMemory&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-cs"&gt;texts[id] = new EmbeddingAndContextText
{
    // what a query is matched against: the situation, not the fix
    EmbeddingText = $&amp;#34;# When\n{Value(&amp;#34;When&amp;#34;)}\n\n# Summary\n{Value(&amp;#34;Summary&amp;#34;)}&amp;#34;,
    // what the search returns: the full rule, in a fixed format the MCP server parses
    ContextText =
        $&amp;#34;# Rule: {Value(&amp;#34;Name&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;AppliesTo: {Value(&amp;#34;AppliesTo&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;Scope: {Value(&amp;#34;Scope&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;IsActive: {(rule.GetNativeValue&amp;lt;bool?&amp;gt;(&amp;#34;IsActive&amp;#34;) == true ? &amp;#34;true&amp;#34; : &amp;#34;false&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;\n## Summary\n{Value(&amp;#34;Summary&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;\n## When\n{Value(&amp;#34;When&amp;#34;)}\n&amp;#34; &amp;#43;
        $&amp;#34;\n## Guidance\n{Value(&amp;#34;Guidance&amp;#34;)}&amp;#34; &amp;#43;
        (string.IsNullOrWhiteSpace(example) ? &amp;#34;&amp;#34; : $&amp;#34;\n\n## Example\n{example}&amp;#34;),
};&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The agent searches with a description of its problem, so the embedded text is the rule&amp;rsquo;s &lt;strong&gt;When&lt;/strong&gt; and its one-line &lt;strong&gt;Summary&lt;/strong&gt;, which carries a short form of the fix. The full Guidance is left out.  If we embedded the full Guidance, rules would be matched mostly by the fix they prescribe, and the agent does not know the fix when it searches.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Who can search.&lt;/strong&gt; &lt;code&gt;core-rag&lt;/code&gt; answers retrieval requests over HTTP, but none of the stock MCP tools searches the instances of a custom entity type. We wrote a small ASP.NET Core MCP server with one tool, &lt;code&gt;search_agent_memory(query, appliesTo?, scope?, topK)&lt;/code&gt;. It forwards the caller&amp;rsquo;s MES token to &lt;code&gt;core-rag&lt;/code&gt;, so the user&amp;rsquo;s data groups and security still apply. It asks for the 20 closest rules, keeps the active ones with the right AppliesTo and Scope, and returns them as JSON with their distances.&lt;/p&gt;
&lt;h3 class="relative group"&gt;Does It Find the Right Rule?
&lt;/h3&gt;
&lt;p&gt;With only one rule stored, any search returns it, so before run 3 I added five more approved rules for the same process and category. They are realistic neighbors: a Bore D1 measured before and after deburring, a dial indicator with no recorded unit, two roughness traces, a fixture offset on another part, and a repeated leak test.&lt;/p&gt;
&lt;p&gt;I searched them with two different descriptions of the flatness case, and with one about the bore:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Closest rules (cosine distance)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;&amp;ldquo;Flatness Top Face reported twice, probe 1 and probe 2: which reading goes to Flatness?&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;flatness probe rule &lt;strong&gt;0.253&lt;/strong&gt;, dial indicator unit 0.420, roughness traces 0.525, … leak test 0.654&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;&amp;ldquo;top face flatness measured by two probes, 0.012 and 0.018, one reading per sample&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;flatness probe rule &lt;strong&gt;0.274&lt;/strong&gt;, dial indicator unit 0.438, roughness traces 0.488, … leak test 0.680&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;&amp;ldquo;Bore D1 diameter as found and after deburr&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;deburr rule &lt;strong&gt;0.282&lt;/strong&gt;, dial indicator unit 0.499, fixture offset 0.536, … flatness probe rule 0.623&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Both flatness questions put the flatness rule first with a clear gap to the next one, and the bore question puts it near the bottom. These questions share words with the rule (&lt;em&gt;flatness&lt;/em&gt;, &lt;em&gt;top face&lt;/em&gt;, &lt;em&gt;probe&lt;/em&gt;), though, so they do not show whether the search matches meaning or only shared words. The rule&amp;rsquo;s embedded text is its When, &lt;em&gt;&amp;ldquo;A Housing A12 inspection report gives Flatness Top Face twice, once per probe (probe 1 and probe 2), and the Flatness parameter accepts only 1 reading per sample.&amp;rdquo;&lt;/em&gt;, plus its Summary, &lt;em&gt;&amp;ldquo;Housing A12 reports with Flatness Top Face per probe: post probe 1 to Flatness, list probe 2 as unmapped.&amp;rdquo;&lt;/em&gt; I also searched with two paraphrases that share no content word with either:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Closest rules (cosine distance)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;&amp;ldquo;two sensors disagree on how even the upper surface is&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;flatness probe rule &lt;strong&gt;0.496&lt;/strong&gt;, roughness traces 0.523, dial indicator unit 0.536, …&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;&amp;ldquo;the gauge beside the clamp reads higher than the other one on the lid, which value counts?&amp;rdquo;&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;dial indicator unit &lt;strong&gt;0.487&lt;/strong&gt;, flatness probe rule 0.538, deburr rule 0.539, …&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Without shared words the right rule is still near the top, but the margin is small or gone. That is why the agent&amp;rsquo;s instructions tell it to put the label as written in the file into its query, and why a rule&amp;rsquo;s When should use the words the files use. The search returns five rules, and the agent is instructed to read each one&amp;rsquo;s When and apply it only when it matches, so a right rule in second place is still among those it receives. Of course in a real scenario we may have thousands of rules, here will be where the semantic search really becomes important.&lt;/p&gt;
&lt;h3 class="relative group"&gt;When the Prompt and the Search Disagree
&lt;/h3&gt;
&lt;p&gt;If we keep the rules from the &lt;strong&gt;Scope&lt;/strong&gt; and &lt;strong&gt;Applies To&lt;/strong&gt;, with rules coming from two places, they can contradict each other. The agent&amp;rsquo;s instructions decide when to search and which rule wins:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;* When a mapping is still ambiguous after the rules in the &amp;lt;agent_memory&amp;gt; block, or the message has no
  block, call search_agent_memory (MCP server MESProject Agent Memory) with query = a short description of the
  case: the label as written in the file, its value and unit, and what is ambiguous. Read each returned
  rule&amp;#39;s when and apply it only when it matches the case.
* Rules provided in the prompt (the &amp;lt;agent_memory&amp;gt; block and the rules you fetched for it) take
  precedence over rules returned by search_agent_memory. When both cover the case and disagree,
  follow the prompt&amp;#39;s rule. Use a searched rule only for what the prompt&amp;#39;s rules do not settle.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rules in the prompt were selected for this process and category. Remember, the search returns the closest matches by meaning, which can include a rule written for a different case. This is one of the great advantages of using RAG which means we can leverage global rule context to make decisions, the downside is that we are giving the responsibility to the model to interpret if that rule applies and makes sense for his use case. That&amp;rsquo;s why we need to provide as much valuable context and meaning as possible in order for it to make decisions.&lt;/p&gt;
&lt;h3 class="relative group"&gt;Run 3: The Agent Searches
&lt;/h3&gt;
&lt;p&gt;To show the search on its own, I switched the prompt index off for this run by setting the index cap to 1 character. No rule line fits, and when no line fits the service adds no &lt;code&gt;&amp;lt;agent_memory&amp;gt;&lt;/code&gt; block at all, so the prompt is the same as in run 1. This means that no rules will be provided to the prompt, even if they have a matching Scope or AppliesTo.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Index switched off"
src="https://image.j-roque.com/posts/20261202-agentmemory/config-index-off.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;For the demo only: &lt;code&gt;IndexMaxCharacters&lt;/code&gt; set to 1, so the prompt has no rules.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Then I added the tool to the agent. MCP servers are MES entities, and the AI Agent page has a wizard to choose which of their tools an agent may use:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Attaching the tool"
src="https://image.j-roque.com/posts/20261202-agentmemory/mcp-attach.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;MESProject Agent Memory → &lt;code&gt;search_agent_memory&lt;/code&gt;, next to the stock CM MES tools.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The controller log for the same file, with the six rules stored:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;11:44:37 RunAgent: calling agent &amp;#39;File Parser for DataCollection&amp;#39; (prompt: 932 characters)
11:45:01 RunAgent: agent &amp;#39;File Parser for DataCollection&amp;#39; answered in 24461 ms (response: 2021 characters)
11:45:02 dataCollection: Successfully performed data into Resource FileParserResource.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The MCP server&amp;rsquo;s log shows the tool call. The tool returns five rules by default, the closest five of the six it retrieved:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-text"&gt;info: ModelContextProtocol.Server.McpServer
      Server (Cmf.Custom.MESProject.AgentMemoryMCPServer 1.0.0.0) method &amp;#39;tools/call&amp;#39; request handler called.
dbug: Cmf.Custom.MESProject.AgentMemoryMCPServer.Tools.AgentMemoryTools
      Agent memory search returned 5 of 6 retrieved rules.
info: ModelContextProtocol.Server.McpServer
      Server (Cmf.Custom.MESProject.AgentMemoryMCPServer 1.0.0.0) method &amp;#39;tools/call&amp;#39; request handler completed in 940.861ms.&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The server logs its outgoing messages at Trace level but not the incoming arguments, so the query the agent wrote is not in the log. The answer it received, in order:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Distance&lt;/th&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0.232&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;housing-a12-flatness-use-probe-1-4a35&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.412&lt;/td&gt;
&lt;td&gt;housing-a12-di2-roundness-micrometres&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.489&lt;/td&gt;
&lt;td&gt;housing-a12-roughness-two-traces&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.558&lt;/td&gt;
&lt;td&gt;housing-a12-bore-d1-after-deburr&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.565&lt;/td&gt;
&lt;td&gt;bracket-b7-length-fixture-offset&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;As in run 1, the prompt had no rules. The agent&amp;rsquo;s query put the flatness rule first at 0.232, closer than any of my test queries, and the file was posted without a review.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center"&gt;&lt;figure&gt;&lt;img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="Run 3 posted"
src="https://image.j-roque.com/posts/20261202-agentmemory/dc-run3-rag.png"
&gt;&lt;/figure&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center"&gt;&lt;em&gt;Flatness Top Face 0.012, posted on the first pass after the search returned the flatness rule first.&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 class="relative group"&gt;Rules or RAG?
&lt;/h2&gt;
&lt;p&gt;We use both, for different cases.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Rule index in the prompt&lt;/th&gt;
&lt;th&gt;Search through RAG&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;How rules are selected&lt;/td&gt;
&lt;td&gt;By &lt;strong&gt;AppliesTo&lt;/strong&gt; and &lt;strong&gt;Scope&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;By meaning of the case, then filtered by AppliesTo, Scope and IsActive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number of rules&lt;/td&gt;
&lt;td&gt;About five with inline Guidance at the default 2000-character cap (adjustable)&lt;/td&gt;
&lt;td&gt;Not limited by the prompt, since only the closest are returned. Tested here with six; with many similar rules, queries must use the files&amp;rsquo; own words (see the paraphrase results)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What each run adds to the prompt&lt;/td&gt;
&lt;td&gt;Every rule of the category, every run&lt;/td&gt;
&lt;td&gt;No rules in the prompt; one tool call when a mapping is still ambiguous&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;When they disagree&lt;/td&gt;
&lt;td&gt;The prompt&amp;rsquo;s rule wins&lt;/td&gt;
&lt;td&gt;Used for what the prompt does not cover&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost of the lookup in this demo&lt;/td&gt;
&lt;td&gt;None beyond the longer prompt&lt;/td&gt;
&lt;td&gt;The search took 0.94 s, and the requirement to call external MCPs will probably incur a bigger token cost&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Rules that apply to most files of a process belong in the index. A large archive of narrow rules, each for one supplier or one report layout, belongs in the search.&lt;/p&gt;
&lt;h2 class="relative group"&gt;Final Thoughts
&lt;/h2&gt;
&lt;p&gt;Last week the human in the loop kept the agent from guessing. With memory, a person&amp;rsquo;s answer becomes a rule, and the agent uses it on the next file without asking again.&lt;/p&gt;
</content:encoded>
<enclosure url="https://j-roque.com/posts/20261202-agentmemory/featured.png" type="image/png" length="0"/>
<media:content url="https://j-roque.com/posts/20261202-agentmemory/featured.png" type="image/png" medium="image"/>
</item>
</channel>
</rss>