Agent fleet field notes / Field essay 74

A faster port can be a weaker security boundary.

Porting agent hooks from Python to Go cut median latency from 34.3 ms to 5.8 ms, and a literal port would have weakened secret redaction. How to catch semantic drift before it ships.

Published

FIELD NOTE: MIGRATION SAFETY
FIELD ESSAY 74 / AGENT FLEET FIELD NOTES

A faster port can be a weaker security boundary

The pattern to watch for: you port a slow component to a faster language, the benchmark comes back several times better, and the code reads the same line for line. Somewhere in that component is a security check written as a pattern or a parser. In the new language it looks identical and behaves differently on inputs nobody ever wrote a test for. The benchmark shows the gain. Nothing shows the loss.

The case behind this note is a port of agent hooks from Python to Go. The speed gain was real. A literal port would also have been less safe, in a way no speed benchmark would ever reveal.

Why the port looked attractive

Five hooks fire on every tool call the coding agents make. Each one is a Python process that starts, reads a blob of JSON from standard input, appends a line to a write-ahead log, syncs it to disk and exits. On the main workstation the installed pre-tool hook took 34.3 ms at the median and 60.6 ms at the 95th percentile. Multiply that by five hooks, then by every file read, search and edit across a fleet of agents, and the harness starts to feel like a drag on everything.

The goal was to move the hooks to a compiled language that works across all three agent runtimes in use, not just one. Go was chosen. One rule went with it: no production code until the plan passed review.

Inventory and two small experiments

The first step was an inventory, before anything clever. It found 20 Python units, about 13.3k lines of production code and 733 test methods. Roughly 400 tests were black-box and could run unchanged against a Go binary. About 330 were white-box and would need Go equivalents. The inventory also listed the contracts any rewrite must keep:

  • git and the code-host CLI are run from the search path, because the test shims depend on that.
  • Canonical JSON has to stay byte-identical, because digests and directory names are derived from it.
  • Five regex sites use lookarounds and backreferences, so they cannot be carried over as they are.
  • Python and Go processes have to agree on file locking.
  • The installs had already drifted: two machines were not running the same commit.

Then came two small experiments. I think they were the most valuable hour of the whole effort.

Startup. A minimal Go hook (go1.27.3, static build, trimmed paths) did the same work as the real one: read JSON, append JSON, sync. Over 40 runs it took 5.8 ms at the median and 7.1 ms at the 95th percentile, against 34.3 and 60.6 ms for the installed Python hook. The plan put the gain at roughly 25 to 50 ms per hook on each call. That is a real, named workload: one machine, one toolchain, 40 runs, one operation. It is not a fleet-wide benchmark, and the end-to-end effect on agent wall-clock time has not been measured.

Semantics. The second experiment was about ten lines of Go. It compiled the harness's secret-redaction pattern and ran it against a handful of edge-case strings. The result: the two regex engines treat some shorthand character classes differently when Unicode is involved. In the Go version, the same pattern matched less input than in Python. Some secrets the Python hook masked would have passed through the Go hook into the log in clear text. The plan called this what it is: unsafe.

The same experiment turned up three more differences. None was dangerous, but each would break parity:

  • Another class difference made Go redact more than Python in some cases. That direction is safe, but it is still a behavior change.
  • Go's standard JSON encoder escapes some HTML characters and one Unicode line separator by default, which breaks byte-identical canonical JSON.
  • Python's timestamp formatting drops microseconds when they are zero. A fixed Go format string does not.

The plan drew the right conclusion. None of this argued for a different compiled language, because any non-Python language needs the same compatibility shims and differential fuzzing. The experiment also showed that the Python harness could not run on one major desktop operating system at all, because it imports a POSIX-only locking module. The port would be adding that platform, not preserving it.

Why the weakness hides

The protection lives in the engine's semantics, not in the test suite. The redaction pattern was correct in Python because of how Python's engine defines a character class. No test ever pinned that behavior down, because nothing ever needed to. A new engine with a different definition breaks the protection while every existing test still passes.

Parsers carry the same risk. In a later code review of the Go foundations, the Go JSON decoder, documented as parsing "as Python's would", turned out to repair malformed Unicode escapes silently where Python preserved them. That means two different documents could decode to the same value. Once a later phase hashes decoded documents, that becomes digests that differ from Python's for the same input, and distinct inputs that share a digest. The reviewer called it fail-open integrity for digests. The test fixtures never covered it, because Go strings cannot even represent the original value. The recommended fix is to detect the malformed escape and fail closed. It was rated medium and not yet blocking, because nothing dispatched to Go and no Python code imported the decoder.

The speed result arrives first and sounds like the answer. A clean benchmark is satisfying. A semantics corpus is tedious. If the plan lets one ship without the other, the faster one wins the argument.

The related note Port agent controls across runtimes without assuming hook parity is about parity between agent runtimes. The same caution applies one level down, to the language the hooks are written in.

How to detect it

The plan review is where this was caught, and it caught a lot. The first independent red-team review blocked the plan with 15 findings, and the owner checked and accepted every one. A selection:

  • Some Python modules import each other in-process, so a per-component switch to Go would be bypassed silently.
  • Flipping that switch mid-workflow would let one run mix implementations.
  • The install backstop failed open.
  • A Go panic exits with code 2, and in the hook protocol 2 means "block". This was built and run on go1.27.3 to confirm it.
  • The trust root was circular, and the binary would be replaced in place. On one platform, an in-place write to a signed binary gets new processes killed, which would block every hook.
  • New-platform support was overstated.
  • The baseline was already stale: the checkout had moved to a newer commit with 749 tests.

A second review blocked round two with eleven findings, and a third blocked round three with eight. An acceptance review of revision 5 finally passed it. Each round found real problems. My takeaway is not that review was slow. The first plan described a Go port, and what it needed to describe was a migration of a security boundary.

One more gap showed up in a larger plan to rebuild the whole harness in Go. The file where owners recorded their test commands, and the file holding their result, were both excluded by the worktree's ignore rules, so staging everything did not reliably keep them in the commit. The recorded test lines were also passed to the runner without a counting parser, so a parser that exited cleanly could stand in for tests that never ran. Neither was a Go bug. Both are the kind of plumbing gap that lets a port claim parity it never demonstrated. The general problem is the subject of A merge gate must prove the tests ran.

How to prevent it

These are the changes the plan adopted, and the ones I would start with on any port:

Risk Prevention
Engine or library semantics differ An explicit compatibility shim for every difference found, plus differential fuzzing between the two implementations
Mixed implementations in one run Switch groups with an importer table, implementation pinned per workflow, flips refused while group processes run
Crash read as a decision A top-level recover that turns a panic into exit 1, with a golden test per subcommand
Fail-open backstop Repoint the backstop to a fail-closed Go wrapper before deleting anything
In-place binary replacement Write to a temp file, then rename. Never overwrite in place
New platform with weaker guarantees Document it as new support, and start git-mutating helpers fail-closed there until they have their own tests
Invisible test receipts A counting parser, refusal when zero tests ran, and committed result manifests outside scratch

One item stayed open at the time of the review: some of the new-platform paths were documented as fail-closed but not yet guarded in code.

Plan for the work stopping halfway

The port stalled, and not on a technical problem. One owner agent hit its weekly usage limit with draft changes open and building not yet started. The written next step was "resume after reset". The cross-runtime support itself shipped separately in the policy docs and installer.

A long multi-phase migration run by agents needs a plan for an owner running out of capacity partway through: smaller phases that can resume cleanly, with the next step written down before the limit hits. Meanwhile, keep the old implementation as the fallback until each new component has soaked live. The 6 ms is tempting. It does not outrank a working redaction.

Checklist

[ ] Before porting any security-relevant pattern or parser, build a
    differential corpus covering Unicode and non-ASCII input around
    every character class, boundary and escape the code relies on.
[ ] Run that corpus against both implementations in CI, on every
    change.
[ ] Treat "behaves like the old version" in a doc comment as a claim
    that needs a failing control.
[ ] Deliver the speed experiment and the semantics experiment
    together, so the plan cannot argue for one without the other.
[ ] Name the workload behind any speed claim: machine, toolchain, run
    count, operation.
[ ] Make sure crashes cannot be read as a valid hook decision.
[ ] Replace binaries by temp file and rename, never in place.
[ ] Commit test manifests outside ignored paths, and refuse any pass
    that counted zero tests.
[ ] Pin one implementation per workflow; refuse switches mid-run.
[ ] Split the migration into resumable phases sized to agent usage
    limits.
[ ] Keep the old implementation as fallback until the new one has
    soaked live.

A port can be faster and less safe at the same time, and a benchmark only shows you the first half.

Go deeper: the Transition2 course covers how to verify protections on the artifact you actually ship.

CONNECTED READINGForward and back
Keep reading

The answer deserves
another experiment.

Back to the journal