Skip to content

: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.

fix(cli): hash the injected snippet variant in event mode (#92250)

Aleksander Błaszkiewicz0a93bb9d822a
diff --git a/cli/.sampo/changesets/event-mode-hash-snippet-variant.md b/cli/.sampo/changesets/event-mode-hash-snippet-variant.md
new file mode 100644
index 000000000000..8be465be2059
--- /dev/null
+++ b/cli/.sampo/changesets/event-mode-hash-snippet-variant.md
@@ -0,0 +1,5 @@
+---
+cargo/posthog-cli: patch
+---
+
+Fix `--release-mode event` keeping a stale source map for a chunk that gains a release. The content hash ignored which snippet the chunk carried. The release snippet is longer than the chunk-id snippet, so it shifts the generated columns the uploaded map records. The two uploads therefore shared one hash, the server kept the first map, and later frames resolved to the wrong source positions. The hash now covers the snippet variant. It still ignores the release id itself, so a new release does not re-upload every chunk.
diff --git a/cli/src/sourcemaps/content.rs b/cli/src/sourcemaps/content.rs
index b6ca3e773a29..541f8f276ac1 100644
--- a/cli/src/sourcemaps/content.rs
+++ b/cli/src/sourcemaps/content.rs
@@ -396,6 +396,14 @@ impl MinifiedSourceFile {
None
}
+ /// Whether the snippet injected for `chunk_id` carries a release id.
+ ///
+ /// The two snippet variants have different lengths, so the choice shifts every generated
+ /// column the sourcemap records for the injected chunk.
+ pub fn has_release_snippet(&self, chunk_id: &str) -> bool {
+ find_release_snippet(&self.inner.content, chunk_id).is_some()
+ }
+
pub fn remove_chunk_id(&mut self, chunk_id: String) -> Result<SourceMap> {
let (new_source_content, source_adjustment) = {
// Update source content with chunk ID
diff --git a/cli/src/sourcemaps/source_pairs.rs b/cli/src/sourcemaps/source_pairs.rs
index e66fbeaa5c2b..03c05df6caed 100644
--- a/cli/src/sourcemaps/source_pairs.rs
+++ b/cli/src/sourcemaps/source_pairs.rs
@@ -148,12 +148,16 @@ impl SourcePair {
/// only the content hash depends on the mode.
///
/// In event mode the hash is computed over the pair with the injection undone (snippet and
- /// chunk-id comment removed, sourcemap adjustment reversed), because the injected bytes vary
- /// while the chunk id does not: the embedded release id changes every release, and a chunk
- /// injected before a release could be resolved carries a shorter snippet (and a differently
- /// adjusted sourcemap) than the same chunk after a later run adds the release. The server
- /// keys its skip-or-conflict decision on this hash per chunk id, so any injection-state
- /// dependence turns an unchanged chunk into a `content_hash_mismatch` rejection.
+ /// chunk-id comment removed, sourcemap adjustment reversed), because the embedded release id
+ /// changes every release while the chunk id does not. The server keys its skip-or-conflict
+ /// decision on this hash per chunk id, so hashing the embedded id would re-upload every chunk
+ /// on every release.
+ ///
+ /// The hash does cover which snippet variant is embedded. The release variant is longer, so it
+ /// shifts every generated column in the uploaded sourcemap. Two builds of one unchanged chunk,
+ /// one before a release could be resolved and one after, therefore ship different maps. Equal
+ /// hashes would make the server keep the first map and resolve later frames to the wrong
+ /// source positions.
///
/// In symbol-set mode no hash is set and the upload layer hashes the raw payload, matching
/// the hashes the server already stores for previous uploads.
@@ -167,6 +171,12 @@ impl SourcePair {
let content_hash = match release_mode {
ReleaseMode::Event => {
+ // Read the variant before the removal, which erases the evidence.
+ let snippet_variant: &[u8] = if self.source.has_release_snippet(&chunk_id) {
+ b"with-release"
+ } else {
+ b"chunk-id-only"
+ };
self.remove_chunk_id(chunk_id.clone())?;
let pristine_map = serde_json::to_string(&self.sourcemap.inner.content)?;
// JSON serialization never contains a raw NUL, so it unambiguously separates
@@ -175,6 +185,8 @@ impl SourcePair {
self.source.inner.content.as_bytes(),
b"\0".as_slice(),
pristine_map.as_bytes(),
+ b"\0".as_slice(),
+ snippet_variant,
]))
}
ReleaseMode::SymbolSet => None,
diff --git a/cli/tests/sourcemap.rs b/cli/tests/sourcemap.rs
index bbd83582f1f8..255f05d59aad 100644
--- a/cli/tests/sourcemap.rs
+++ b/cli/tests/sourcemap.rs
@@ -472,11 +472,11 @@ fn test_upload_set() {
}
#[test]
-fn test_event_mode_content_hash_is_stable_across_release_states() {
- // The hash must not depend on which snippet variant is embedded. A chunk injected while
- // no release was resolvable, the same chunk injected with a release, and the transition
- // between the two all keep one chunk id, so they must hash identically or the server
- // rejects the later upload as a content_hash_mismatch.
+fn test_event_mode_content_hash_tracks_the_snippet_variant() {
+ // The hash must ignore which release id is embedded, or every release re-uploads every
+ // chunk. It must still track whether a release id is embedded at all: the release snippet is
+ // longer, so it shifts the generated columns the uploaded map records. Equal hashes there
+ // make the server keep the first map and resolve later frames to the wrong positions.
let case_path = get_case_path("inject");
let load = || {
read_pairs(vec![case_path.clone()], vec![], vec![], &None).expect("Failed to read pairs")
@@ -505,8 +505,14 @@ fn test_event_mode_content_hash_is_stable_across_release_states() {
)
};
- assert_eq!(releaseless, with_release);
- assert_eq!(releaseless, transitioned);
+ assert_ne!(
+ releaseless, with_release,
+ "a chunk that gains a release ships a different map, so it must not reuse the stored one"
+ );
+ assert_eq!(
+ with_release, transitioned,
+ "a different release id leaves the map identical, so the hash must not change"
+ );
}
const BUNDLER_DEBUG_ID: &str = "11111111-2222-4333-8444-555555555555";