Forum teuk.org

🧹🧙 Mediabot v3: The Cache-Broom Charm — cleaner purges, safer seen wildcards and tidier Claude ghosts

in Mediabot · started by TeuK · yesterday

TeuK · yesterday

Some releases are about adding new rooms to the castle.

This one is about checking what remains in the corners after the room is deleted.

Mediabot v3 has recently gained a lot of new runtime behavior: social features, observability, radio hardening, Claude integration, better command dispatch tests, quote fixes, and more. After that kind of development sprint, the responsible thing is to stop and ask a simple question:

What is still alive in memory after the database says it is gone?

That question led to this pass.

No database schema change.

No migration.

No destructive SQL.

Just cleaner runtime state, safer wildcard matching, and fewer stale ghosts.


🧹 Why “Cache-Broom Charm”?

Because this pass is mostly about sweeping.

Not glamorous sweeping.

The important kind.

It cleans up:

channel-scoped runtime caches after channel purge
Claude history/persona/activity on QUIT and NICK
SQL LIKE wildcard handling in seen commands

These are small bugs, but they are the kind that make a long-running bot feel haunted:

a purged channel still remembered by cache
a nick that quit but still has Claude state in memory
a seen wildcard that matches more nicks than it should

None of that is acceptable for a bot that is supposed to run for days, weeks, and months.


🧨 Context: recent audits already found useful bugs

This work follows two earlier audit passes.

The userOnJoin code was refactored to avoid ambiguous SELECT uc.*, c.* column collisions and to add better logging around channel notices. The bug was not proven purely by static analysis, but the refactor removed dangerous ambiguity and made future diagnosis much easier.

Then Quotes.pm was cleaned up with two real fixes:

!q random no longer repeats the same quote immediately when avoidable
quote nick searches now escape SQL LIKE wildcards properly

That second fix matters here, because it exposed a pattern:

any user input converted into SQL LIKE must escape literal %, _, and the escape character itself

Once you see that pattern in one module, you look for it elsewhere.

And yes, seen had the same family of bug.


🧹 B1 — purgeChannel_ctx now clears runtime caches

A channel purge already removed database rows from channel-related tables.

But runtime memory still had possible references to the old channel.

That means a purged channel could remain partially alive until TTL expiry or bot restart.

The patch now clears channel-scoped memory after purge, including:

$self->{channels}
hChannelNicks
_badword_cache
_af_params
_chan_flood
_chan_flood_conf
_cmd_cooldown
_cmd_cooldown_conf
_chanset_cache
_uchan_level_cache

This matters because Mediabot uses in-memory caches for good reasons: performance, flood checks, command cooldowns, channel settings, badwords, user levels, and nick tracking.

But when a channel is deliberately purged, memory must follow the database.

Otherwise the database says:

this channel is gone

while the runtime says:

well, sort of

That is not good enough.

After this pass, purgeChannel_ctx logs the cleanup and clears the relevant runtime state.

A purge should be a purge.

Not a polite suggestion.


👻 B2 — Claude QUIT/NICK cleanup now uses the right key conventions

Claude state has two different key conventions:

_claude_history  -> raw IRC nick + "

You must be logged in to reply.