In the previous chapter, Contextual Recall Mechanism, we built a "smart librarian" that finds the right memory file for a user's question.
But there is a danger lurking in that library. What if the book the librarian hands you is from 1998?
Code changes fast. Memories do not update automatically. If a memory says, "The login function is in auth.js," but you renamed that file to auth.ts yesterday, the memory is now a lie. This is called Memory Drift.
In this chapter, we will learn how memdir solves this by treating memories like perishable goods (like milk) rather than permanent facts (like diamonds).
Imagine this scenario:
util.quickSort() for sorting."util.fastSort(). You forgot to update the memory file.
Without Verification:
The AI reads the memory. It confidently writes code using util.quickSort(). Your code crashes because that function no longer exists.
With Verification:
The AI reads the memory but sees a "Warning Label" because the memory is 14 days old. Instead of blindly trusting it, the AI thinks: "This memory is old. I should check if util.quickSort still exists." It runs a grep command, fails to find it, finds fastSort instead, and writes the correct code.
To handle this, we don't delete old memories (they might still contain valid reasoning). Instead, we tag them based on their age.
We categorize memories into two states:
Let's see how the system alters what the AI reads based on the file's modification date.
Input (File System):
memory/team/api_endpoints.mdSystem Processing: The system calculates the age. Since 20 days > 1 day, it injects a warning.
Output (What the AI sees):
<system-reminder>
This memory is 20 days old. Memories are point-in-time observations.
Claims about code behavior or file:line citations may be outdated.
Verify against current code before asserting as fact.
</system-reminder>
User: Where is the API endpoint?
Because of that <system-reminder>, the AI lowers its confidence in the specific file paths mentioned in the memory and looks at the real code first.
How does this happen automatically? It happens right before the memory content is sent to the AI.
We need a simple utility to convert a timestamp (milliseconds) into "Days Ago."
In src/memoryAge.ts:
// src/memoryAge.ts
export function memoryAgeDays(mtimeMs: number): number {
const oneDay = 86_400_000 // milliseconds
// Math.max ensures we don't get negative numbers due to clock skew
return Math.max(0, Math.floor((Date.now() - mtimeMs) / oneDay))
}
Explanation: We take the current time, subtract the file's modification time, and divide by the number of milliseconds in a day.
If the file is old, we generate the text that warns the AI.
// src/memoryAge.ts
export function memoryFreshnessText(mtimeMs: number): string {
const days = memoryAgeDays(mtimeMs)
// If it was modified today or yesterday, it's fresh. No warning.
if (days <= 1) return ''
// Otherwise, warn the AI.
return (
`This memory is ${days} days old. ` +
`Memories are point-in-time observations... ` +
`Verify against current code before asserting as fact.`
)
}
Explanation: This function returns an empty string for fresh files (reducing noise) but a strong warning for anything older than 48 hours.
The warning label is useless if the AI doesn't know how to verify things. We explicitly teach it this behavior in the System Prompt.
In src/memoryTypes.ts, we add a section called TRUSTING_RECALL_SECTION.
// src/memoryTypes.ts (Simplified)
export const TRUSTING_RECALL_SECTION = [
'## Before recommending from memory',
'',
'A memory that names a specific function or file is a claim that it existed *when the memory was written*.',
'',
'- If the memory names a file path: check the file exists.',
'- If the memory names a function: grep for it.',
'',
'"The memory says X exists" is not the same as "X exists now."',
]
This is the most critical part. We act proactively. We tell the AI that Memory โ Reality. We force it to treat memory as a "lead" or a "hint" that must be confirmed by looking at the actual hard drive.
When the system combines these pieces, it ensures the AI never hallucinates based on old data.
mtime.grep or ls) to confirm the memory is still valid before answering you.In this chapter, we learned:
<system-reminder> warning for any file older than 1 day.We have now covered how to structure memories, where to store them, how to find them, and how to verify them.
But there is one final piece of the puzzle. If the AI is reading and writing files on your computer, how do we make sure it doesn't accidentally read your passwords or delete your operating system?
Next Chapter: Path Security and Sandboxing
Generated by Code IQ