Back to blog
EngineeringDate unavailable· min read

When Your Telemetry Has Been Silent Since Launch

I shipped a memory system for Sabine and the Dream Team agents months ago. Every time an agent reads a memory entry, we're supposed to increment access_count and update last_accessed_at so the system can surface frequently-used memories and decay stale ones. That's the design.

Except it's been failing silently since the day I deployed it. All 4,020 rows in agent_memory have an access_count of zero. None have a last_accessed_at timestamp. The fire-and-forget RPC call to increment_memory_access was calling a function that didn't exist in production, and the code was swallowing the error with a bare except: pass.

How This Happened

Migration 010 created the increment_memory_access function in my local development database. I tested it. It worked. I committed it. But when I deployed to production, that migration never ran—its history row doesn't exist in the migrations table and the function is absent from pg_proc. I don't know why. Maybe I skipped it during a deployment. Maybe I thought I'd applied it when I hadn't.

The result: every memory_read call and every MemoryInjector fetch has been making an HTTP RPC call that returns 404, and the code was treating that as non-fatal and moving on. The memory reads still worked. The agents got their context. But the telemetry that would let the system learn which memories matter? Silent.

The Fix

Migration 044 ships the function as a new migration instead of editing 010, because production's migration history doesn't list 010. The new version has the same signature so no caller code needs to change, but it's hardened in five ways:

SECURITY INVOKER instead of DEFINER, so the function runs with the caller's permissions and respects the existing RLS policies. If we ever tighten agent_memory's UPDATE policy, this function won't bypass it.

Only current rows (valid_to IS NULL) are updated. Superseded rows are history and shouldn't look recently used.

Batches larger than 100 IDs are rejected with a program_limit_exceeded error. memory_read and MemoryInjector return at most 20 rows each, so 100 is headroom, not a constraint—but it prevents an anon caller from turning this into an unbounded bulk write.

The 009 trigger that sets updated_at on every UPDATE now checks whether the update changed only access_count or last_accessed_at. If so, it preserves the old updated_at. Otherwise, counter bumps from reads would pollute the ordering field that's supposed to reflect actual edits.

EXECUTE privileges are explicit: anon and service_role can call it, authenticated and PUBLIC cannot. The tool code logs RPC failures at WARNING instead of swallowing them, so if the grants are wrong or the function disappears again, it shows up in the logs instead of failing silently for another six months.

What This Means

The function is now in production. Future memory reads will increment counters and timestamps. But the existing 4,020 rows still have zero access counts, so the memories that were read early (when the function was missing) still rank higher in confidence-then-access_count ordering, which means they'll keep being read, which means their counts will grow while older memories stay at zero.

That's fine for now—confidence is the primary sort key anyway. But it's why decay_stale_memory (also missing from production) needs to ship right after this. That's tracked separately and should land within days, not weeks.

What's Next

Ship decay_stale_memory so the access counters actually influence memory ranking. Audit the other fire-and-forget RPC calls in the codebase to see if any others are failing silently. Consider switching memory_read and MemoryInjector to use the service role key instead of anon for these background calls, then revoke anon's EXECUTE grant as a tightening step. And add a monitoring check that increment_memory_access is actually running in production—some rows should have access_count > 0 within hours of this migration landing.