:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(cdp): derive hog invocation result version from a monotonic clock (#70245)
Paul D'Ambra49f47e154d2c
diff --git a/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.test.ts b/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.test.tsindex ae8d57a2389e..1dc1d4fce150 100644--- a/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.test.ts+++ b/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.test.ts@@ -272,16 +272,22 @@ describe('HogInvocationResultsService', () => {describe('version monotonicity', () => {it('produces strictly increasing version values across successive rows for the same invocation', async () => {+ // Queued back-to-back with no delay: both rows land in the same wall-clock+ // millisecond, so this only passes if version is clamped to strictly exceed the+ // last value issued — the property that keeps ReplacingMergeTree from dropping the+ // terminal row. No sleep, so it can't flake on wall-clock jitter.const invocation = createExampleInvocation()service.queueLifecycleRow(invocation, 'running')- // Tick by 1 ms — version is now64(6) in microseconds.- await new Promise((r) => setTimeout(r, 2))service.queueLifecycleRow(invocation, 'succeeded')await service.flush()+ // flush() produces rows concurrently (Promise.all), so their produce order is not+ // the queue order — match by status rather than array index.const rows = parseProducedRows(outputs)expect(rows).toHaveLength(2)- expect(BigInt(rows[1].version)).toBeGreaterThan(BigInt(rows[0].version))+ const running = rows.find((r) => r.status === 'running')!+ const succeeded = rows.find((r) => r.status === 'succeeded')!+ expect(BigInt(succeeded.version)).toBeGreaterThan(BigInt(running.version))})})diff --git a/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.ts b/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.tsindex 3813cd2cd255..664fe398ba63 100644--- a/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.ts+++ b/nodejs/src/cdp/services/monitoring/hog-invocation-results.service.ts@@ -82,20 +82,35 @@ const isHogFunctionInvocation = (invocation: CyclotronJobInvocation): invocationconst isHogFlowInvocation = (invocation: CyclotronJobInvocation): invocation is CyclotronJobInvocationHogFlow =>'hogFlow' in invocation-// In-process monotonic counter that breaks ties when consecutive lifecycle-// rows for the same invocation are produced within the same millisecond.-// Without this, the 'running' + terminal rows of a fast invocation can share a-// `version` value, and ReplacingMergeTree keeps one arbitrarily — potentially-// leaving the runs UI showing a permanently 'running' status. We layer-// `performance.now()`'s sub-ms fractional component on top of `Date.now()` to-// stay monotonic within a process; the worst remaining tie window is two-// rows produced at the exact same `performance.now()` sample, which is below-// the precision the clock actually delivers.+// Monotonic microsecond timestamp used as the row `version`. ReplacingMergeTree keeps the+// row with the max `version` per key; without a monotonic version the 'running' + terminal+// rows of a fast invocation can share or invert a version, and CH keeps one arbitrarily —+// leaving the runs UI stuck on 'running' (and, via the rerun paginator's+// `argMax(status, version)` in-flight check, that invocation un-rerunnable).+//+// The rows being compared are produced across DIFFERENT processes — the 'running' row by+// the events consumer, the terminal row by a cyclotron worker — so the base must be a+// clock that stays synchronised across processes. `Date.now()` is continuously+// NTP-disciplined, so cross-process skew stays well under the queue latency between the+// two rows. (`performance.timeOrigin + performance.now()` is monotonic within a process+// but freezes each process's wall-clock offset at start and never re-syncs, so an+// NTP step or VM pause would invert versions across pods — worse for the comparison that+// matters.)+//+// A wall clock alone can still tie, or briefly step backward under NTP, for two rows+// produced close together in the SAME process (e.g. the flaky monotonicity test). Clamp to+// strictly exceed the last value this process issued: that adds strict in-process+// monotonicity on top of the NTP-anchored cross-process ordering.+//+// This module-level counter is process-global and never resets, so a test using+// `jest.setSystemTime()` to a time earlier than an already-issued stamp will see clamped+// `last + 1` values, not the fake time. Don't rewind the clock below a prior stamp in tests.+let lastVersionMicros = 0nconst microsecondsSinceEpoch = (): string => {// BigInt avoids the 53-bit cap so the number lines up with ClickHouse UInt64.- const ms = BigInt(Date.now())- const subMs = BigInt(Math.floor((performance.now() % 1) * 1000))- return (ms * 1000n + subMs).toString()+ const wallMicros = BigInt(Date.now()) * 1000n+ lastVersionMicros = wallMicros > lastVersionMicros ? wallMicros : lastVersionMicros + 1n+ return lastVersionMicros.toString()}const isoMicroseconds = (date: Date): string => {