9 of 102 min

How I protect the project from chaos, duplicates and chance

The most unpleasant thing in projects like this isn’t errors but quiet accumulating chaos.

When the same event arrives twice. When media is saved again. When the system gradually starts contradicting itself.

From the outside it’s almost invisible. But this is exactly how a project loses its shape.

The first thing I had to do was stop duplicates at the lowest level:

UNIQUE(chat_id, msg_id, media_type)

The database simply won’t let the same thing be written twice. No extra logic and no hope that someone will sort it out by hand later.

Then comes the second layer:

INSERT OR IGNORE INTO ann_message_media

If the event has already happened, it just doesn’t go any further. No extra state is created.

But duplicates are only part of the problem.

There’s another form of chaos: repeated actions. When the system replies once too often, publishes once too often, or starts again something that no longer needs starting.

Here comes:

cooldown_until

The system stores the moment before which it must do nothing. This is no longer about functionality but about discipline of behaviour.

The next principle concerns heavy operations. They can’t be performed right at the moment of a reply. If you recalculate the profile for every line, the system becomes overly sensitive and starts making noise.

So part of the logic is moved to the background: queues, deferred updates, accumulating material before acting.

And finally, failures.

If an external layer goes down, the project shouldn’t turn into either silence or garbage. So there are pauses, fallback scenarios and state recovery here, and the database works through WAL so that writing doesn’t block the whole system.

In the end, protection is built in layers:

  • the database doesn’t let duplicates in
  • the code doesn’t amplify repetition
  • cooldown holds back excess activity
  • heavy logic is moved out of the dialogue
  • a failure doesn’t destroy the whole flow of work

And only after that can you talk about the quality of replies at all.

How I protect the project from chaos, duplicates and chance: illustration 1How I protect the project from chaos, duplicates and chance: illustration 2How I protect the project from chaos, duplicates and chance: illustration 3How I protect the project from chaos, duplicates and chance: illustration 4
Series contents
  1. How the idea of a book gave rise to a space for memory, conversation and recirculation
  2. What this project is really made of
  3. Memory, profile and context: what the bot rests on
  4. The logic by which the bot answers people
  5. How I made a system that doesn't let content vanish
  6. How the project's philosophy is sewn into the code
  7. Why this project lives not only in text
  8. How the bot gathers and updates people's profiles
  9. How I protect the project from chaos, duplicates and chance
  10. Why I wrote this code not for the sake of automation