HalfLife 3

We demote a memory when the code it describes changes not only by time

Inspiration

A decision from six months ago about code nobody has touched is still correct. A decision from yesterday about a function someone rewrote this morning is junk. Time decay can't tell those apart - and it ranks the junk first, because it's recent.

What it does

When the agent saves a decision, it also saves a hash of the code that decision was about. When it looks the decision up, it checks whether that code still looks the same. If it doesn't, the memory drops down the ranking.

One question to three agents — "how do we sort search results?" The team picked relevance in June, switched to newest-first two days ago, and someone rewrote that function this morning.

no memory          "I have no record of your team's decisions."
time decay only    "Newest-first."  <- wrong, and it gives the same answer
                                       whether the code changed or not
ours               "saveMessages changed since. Going with relevance."

How we built it

| EVAL time_decay  = DECAY(created_at, NOW(), 1080 hours)
| EVAL structural  = CASE(code_ref == "saveMessages"
                          AND code_fingerprint != "11425ae0...", 0.15, 1.0)
| EVAL final_score = _score * time_decay * structural

Elasticsearch holds the memories, Mastra runs the agents, Graft hashes the code. Graft stores nothing — it re-reads the repo every time, so it can't be out of date itself. That's the part memory can't do on its own.

Challenges we ran into

Our staleness check went stale. It cached the file it reads, so a memory stayed flagged as out-of-date after we'd already undone the change - which looks exactly like the feature being broken.

The search also returned relevance scores so close together (0.024 to 0.029 across 54 memories) that recency swamped them. An infrastructure note ranked second for a pricing question.

What we learned

An old decision doesn't become wrong, it stops applying. Timestamps can't tell you which - you have to go and look at the thing the decision was about.

Built With

Share this project:

Updates

Submission history