DEV Community

Ted
Ted

Posted on Originally published at tedagentic.com

In a Chat Message, the Formatting Is Part of the Data

A lot of my work runs through one Slack channel. I post numbered requests, REQ-141, REQ-142 and so on, and a browser agent picks them up, does the work, and answers in the thread. A small script posts a weekly request of its own every Monday, and before posting it works out the next free number by reading the channel history.

On Monday the script posted REQ-143. There was already a REQ-143. I had posted it myself at one in the morning.

The underscore is a word character

The numbering code was one line:

nums = [int(n) for m in history for n in re.findall(r"\bREQ-(\d{3})\b", m["text"])]
Enter fullscreen mode Exit fullscreen mode

That looks right. \b is a word boundary, so it stops XREQ-143 from matching, and the trailing \b stops it from reading the first three digits of REQ-1430.

But the requests I post go through a connector that writes my titles in italics, and Slack stores italics as underscores. My request's text began:

_REQ-143: Go ahead with part B now_
Enter fullscreen mode Exit fullscreen mode

A word boundary is the point between a word character and a non-word character. In regex, word characters are letters, digits and the underscore. So between _ and R there is no boundary. Both sides count as "word". \bREQ never matched, my request was invisible to the counter, and the script happily took 143 again.

To the parser, the markup isn't around the message. It is the message.

I had been thinking of _..._ as "how it looks". The regex was reading what was actually stored, and what was stored was an underscore glued to the front of the thing I was searching for.

The fix keeps the guard I actually needed, digits after the number, and drops the one that assumed clean text before it:

re.findall(r"REQ-(\d{3})(?!\d)", text)
Enter fullscreen mode Exit fullscreen mode

I tested it against the shapes that really occur in the channel, not the one I'd imagined:

>>> re.findall(r"REQ-(\d{3})(?!\d)", "_REQ-143: x *REQ-142* REQ-1234")
['143', '142']
Enter fullscreen mode Exit fullscreen mode

Italic, bold and plain all match, and a four-digit number doesn't produce a false 123.

The language tag that ate a line

The same day, a request listed fifteen properties in a code block, so the agent would see a clean list:

```1. First property (city)
2. Second property
...
```
Enter fullscreen mode Exit fullscreen mode

The agent answered items 2 to 15. I resent the missing one as part of the next request, formatted the same way. It went missing again.

Text on the same line as an opening triple backtick isn't content. It's the language tag, the thing that tells the renderer to highlight python or js. My first item had been sent as a language called 1. First property (city), which the renderer treated as metadata and quietly didn't show. Nothing errored. The list simply started at 2.

The fix is a newline after the opening fence, every time:

```
1. First property (city)
2. Second property
```
Enter fullscreen mode Exit fullscreen mode

The same mistake twice

Both bugs came from writing for the renderer and forgetting that something else reads the source. I wrote italics so a title would look like a title. I put a list on the fence line so the message would look compact. In both cases the thing reading the text downstream, a regex in one, a markdown parser in the other, saw characters I'd stopped noticing.

What I do now:

  • Match against the stored form, not the displayed one. If a channel stores formatting as characters (_, *, ~, backticks), assume any line you parse may be wrapped in them. Don't lean on \b beside text that might start with an underscore.
  • Test parsers with real messages from the channel. My regex test strings were all clean, plain-text REQ-142. The channel has never contained a clean one.
  • Put a newline after every opening code fence in anything a machine will read. The first line belongs to the tag.

Neither bug was clever. Both sat in the gap between what a message looks like and what it contains.

Top comments (0)