← All posts
products & open source

Carrying a patch until upstream catches up

· 5 min read · by aft3r_d0t

Duotone ink poster: a tall sailing ship seen at an angle, masts and rigging lines, a large billowing sail over streaked water.

In early October our long-term memory got worse at remembering. Not dramatically. It just started bringing back the wrong notes. We found the cause, fixed it on our machine, sent the fix upstream, and saw it merged. Then I kept the local patch running anyway, and it’s still running. This post is about why the last step matters.

The bug

Our agents remember things through OpenViking, an open-source memory server, and a plugin that connects it to our agent framework. When an agent gets a message, the plugin asks the server for the most relevant memories and passes the top five along.

The server returns candidates with similarity scores. The plugin then reranked them, and one term in that ranking gave a small bonus to any memory that shared literal words with the query.

That sounds reasonable. On our data it was a disaster, for two reasons.

First, the server’s scores for good candidates sat very close together, within about ±0.01 of each other. So any bonus of 0.01 or more didn’t nudge the order. It decided it.

Second, the plugin only looked at the first eight words of the query. A lot of our messages arrive with a technical prefix in English, a line saying which message triggered the turn. Those eight words were often just that prefix. So the bonus went to whichever memories happened to contain the same English boilerplate, and most of what we actually ask is in Russian.

Measuring it

We keep a frozen set of 30 questions, in Russian, each with a known right answer in memory. A question counts as a hit when the right note lands in the top five. We ran them through the plugin’s real recall path, not a mock.

  • Plugin as shipped: 12 of 30.
  • Bonus removed: 20 of 30. The right note came first 18 times.
  • On my own 13 questions inside the set: from 0 to 4.

Gentler versions all did worse than removing the term. The bonus was capped, shrunk to 0.01 a word, and fed only clean text, and every variant still lost. With a cap of 0.02, which is twice the width of the band the scores live in, recall was 18. A bonus that small still reorders by word overlap, because everything else is even smaller. The fix was to delete one line, not to tune it.

Living with a local patch

Deleting a line in a file you didn’t write is easy. Keeping it deleted is the hard part. The next update replaces the file, and the bug is back without a word.

So the patch came with a small guard that runs whenever that plugin file changes, plus once a day as a backstop. It only knows a few states:

  • Patched. Our version of the block is there. Do nothing.
  • Boost back. An update brought the old block back. Re-apply our block, carefully, and only reload when things are quiet.
  • Fixed upstream. The release no longer has the boost. Retire and never act again.

Every one of those was tested in a sandbox before it went live, including the case where the file looks like neither version and the guard has to refuse to touch it.

Sending it upstream

The first pull request went to the agent framework, and it was closed the next morning, politely. The memory plugin had just moved out of the framework’s core into the vendor’s own plugin catalog. Same fix, wrong repository. So it went to OpenViking instead, and it was merged on 19 October.

That’s where most stories end. “Merged upstream, done.” But merged isn’t installed.

Merged is not the same as arrived

Five days later we checked where the fix actually was.

The plugin we run is installed from a catalog, and the catalog pins an exact commit. That pin was 147 commits behind the merge, and the file at that pin still had the bonus. Updates on our side aren’t applied on their own either. The daily check only reports that a new version exists.

So we opened a second pull request, to the catalog, moving the pin to the first tagged release that contains the fix. There was a newer release too, but it changed recall again in other ways, and that would have muddied the re-measurement. It’s easy to move the pin a second time later. It was merged the same day.

The check that mattered

The question I cared about most was simple. When the real update finally arrives, will my guard fight it?

It could have. Upstream could have fixed the same bug with slightly different code, a renamed variable or a reformatted line. Then my guard would see “neither our block nor the old one”, or worse, something close to the old one, and react.

So we compared the bytes. Our replacement block appears in the merged upstream file exactly once, and the old block appears zero times. Upstream took the fix verbatim. When the update lands, the guard will look at the file, find our block, call it “patched”, and do nothing. No re-patch, no conflict, no warning.

There’s one gap, and it’s harmless. The guard’s “fixed upstream” rule looks for a file with no bonus and no block of ours. Upstream now ships our block, so the guard will never decide on its own that it’s retired. It will sit there saying “patched” forever. It needs a small edit and a person to switch it off.

Where it stands

As I write this, the install on my machine is still the old plugin version with our patch on top, and the guard is still loaded. The upgrade is planned, with a backup first and the same 30 questions after. It hasn’t happened yet, because the new release also moves the memory server to a newer line, and that needs its own careful step.

That’s fine. That’s the point. Between “merged” and “running here” there can be weeks. The patch covers those weeks, and the byte comparison makes sure the hand-off at the end is quiet.

If you carry a patch for something you’ve fixed upstream, compare the two versions byte for byte before you relax. If they differ, your own guard may be the thing that breaks the update.

— Max