All insights

My hook read the message after someone rewrote it

A forwarding rule in my chat assistant failed for weeks because it saw the message too late, and the assistant confirmed every forward anyway.

I run a personal assistant on an open-source agent gateway, reachable over two chat apps. One rule matters a lot to me: a message that starts with a short prefix goes straight to my coding agent instead of the assistant's own model. For weeks that rule worked in testing and failed in daily use, and every time it failed the assistant replied with a check mark saying the message had been forwarded. Last Saturday I rebuilt it. Most of the fix was moving the interception point three steps earlier.

What the hook actually received

The first version hung on an event the gateway fires when the assistant starts working on a message. It looked like the obvious place. It was the wrong one, for four reasons I only found by logging what the hook actually received:

  • The text was cut at 500 characters.
  • It was the prepared text. If I sent a photo, a generated description of the image stood in front of my words. If I replied to an earlier message, the quoted text stood in front. My prefix was no longer at the start, so the rule never matched.
  • Looking up the full message in the gateway's database could not help either. The gateway waits for the hook before it runs the turn, and the message row is written after the turn. The hook was asking for a record that did not exist yet.
  • Only the first attachment was passed along, and messages sent while the assistant was busy did not fire the event at all.

In testing I sent short plain messages, so all four stayed invisible. In daily use I send photos with captions and replies to earlier messages, which is exactly where it broke.

The check mark came from the wrong place

Every failed forward was acknowledged. The assistant's model saw a message starting with the prefix, knew from its instructions what the prefix meant and wrote back that it had passed the message on. It had not. The model was describing what should happen, and nothing in the system checked whether it had.

A confirmation should come from the component that did the work. Anything else is a description of the plan.

Moving the interception point

The gateway also offers a plugin point before dispatch, before any preparation runs. There the event is still raw: the text as typed, all attachments with photo albums already merged, the quoted message as a separate field. The new router sits there.

  • It checks authorization itself, because this point runs before the gateway's own check.
  • A matched message skips the assistant's model entirely. The router hands it to the coding agent and sends the receipt itself, including the mode and the number of attachments, so I can see what arrived.
  • The model's instruction is turned around. If it ever sees a message with the prefix, the router missed it, and the model answers with a warning that the message did not arrive.

Building it turned up two more things. The pattern that detects the prefix could be made to backtrack: a message with the prefix followed by 20,000 spaces took about a second to check, before authorization, so anyone could send it. Putting the separator into its own group fixed that. And in threaded chats every reply is its own session, so the context the router attached pulled lines that were days old. It now only takes lines from the last 24 hours. Checked against real data, 28 thread sessions older than a day now return no lines where they returned two to eight before.

The gap I left open

One case still fails. If a message arrives while the assistant is in the middle of a turn, the chat adapter catches it before the plugin point and either feeds it into the running turn or queues it as a follow-up. Neither path passes the router. Closing that would mean patching the gateway's own code, and the gateway updates itself, which would overwrite a local patch without telling me.

So the gap is documented, and the inverted instruction covers it: the model sees the prefixed message, answers that it did not arrive, and I send it again. That is a worse experience than a working router and a much better one than a confident check mark on a message that went nowhere.

Key

Before trusting a hook, log once what it really receives with the inputs you actually send, photos and replies included. Intercept where the data is still raw, let the component that did the work send the receipt, and write down the case you cannot close together with what happens when it occurs.

Hung Mai

Hung Mai is an independent consultant in Weilmünster, Germany, mostly working remotely. He plans, builds and monitors AI automation for marketing and sales for companies without a digital team. About Hung Mai

Let's build something real.

Let's talkResponse within 24h.