posthog icon indicating copy to clipboard operation
posthog copied to clipboard

fix(hogvm): treat replaceOne/replaceAll replacement literally

Open winklemad opened this issue 4 months ago • 0 comments

Problem

replaceOne / replaceAll in the TypeScript HogVM (common/hogvm/typescript/src/stl/stl.ts) and the Hog→JS compiler (posthog/hogql/compiler/javascript_stl.py) pass the replacement to JS String.replace / .replaceAll as a plain string. JS then interprets $$, $&, $`, $' as special replacement patterns, so the result diverges from the Python VM, the Rust VM, and ClickHouse — all of which treat the replacement literally.

The same Hog program returns different output depending on the runtime:

  • replaceAll('price', 'price', '$$') → '$' on TS (dollar silently halved), '$$' on Python / Rust / ClickHouse
  • replaceAll('a_b', '_', '$&') → 'a_b' on TS, 'a$&b' correct

The TS VM runs in plugin-server (production ingestion) while the Python VM backs previews/tests, so a hog-function transformation can preview correctly and run wrong — a silent data-integrity divergence, reachable via ordinary currency/price strings.

Changes

  • stl.ts: replaceOne / replaceAll now use a replacer function (() => replacement), whose return value is never $-processed, so the replacement is always literal.
  • javascript_stl.py: same fix in the two generated function bodies; regenerated the committed stl.js / hogqlx.js snapshots to match.
  • Added a regression test through the real bytecode path covering $$, $&, $`, $' plus an ordinary-replacement guard. The full HogVM TS suite passes (97/97), and the output matches the Python VM byte-for-byte.

🤖 Agent context

Investigated with the help of an AI coding agent (Claude Code). I verified the cross-runtime divergence and the fix myself against the real HogVM code (Python vs TS vs Rust), ran the HogVM TypeScript jest suite (97/97), and confirmed red→green on the new regression test through the real bytecode path. I understand and own this change.

winklemad avatar Jul 18 '26 11:07 winklemad