diff --git a/.changeset/v4-remove-replay-wireframe.md b/.changeset/v4-remove-replay-wireframe.md index 655ee824e6..6bee1e552a 100644 --- a/.changeset/v4-remove-replay-wireframe.md +++ b/.changeset/v4-remove-replay-wireframe.md @@ -8,6 +8,5 @@ - Masking covers text, inputs, images and web views. Custom-drawn content (custom `draw(_:)` views, maps, video, charts) is now visible in recordings: wrap anything sensitive in `postHogMask()` or tag it `ph-no-capture`. - Apps whose window root is a SwiftUI view, which previously recorded nothing, now record sessions. - `PostHogSessionReplayConfig.screenshotMode` is deprecated and has no effect: delete the assignment. Setting it to `false` no longer prevents screenshots; use the masking options instead. -- `screenshotModeBackgroundCapture` now applies without setting `screenshotMode`. - The replay screen name now names the visible screen (SwiftUI screens report their view name), and is also sent when `screenshotMode` was enabled. It comes from the view controller's type name only: a plain `UIViewController`'s `title` is no longer sent, since it can contain user data. - Secure-entry text fields now stay masked inside a `postHogNoMask()` or `ph-no-mask` subtree. diff --git a/.changeset/v4-remove-screenshot-background-capture.md b/.changeset/v4-remove-screenshot-background-capture.md new file mode 100644 index 0000000000..8096e942e1 --- /dev/null +++ b/.changeset/v4-remove-screenshot-background-capture.md @@ -0,0 +1,5 @@ +--- +'posthog-ios': major +--- + +**Breaking:** remove the experimental `PostHogSessionReplayConfig.screenshotModeBackgroundCapture` option. Delete the assignment. Screenshots are now always captured on the main thread, where rendering is fast enough that the off-main workaround is no longer needed. diff --git a/PostHog/Replay/PostHogMaskSettleAnalysis.swift b/PostHog/Replay/PostHogMaskSettleAnalysis.swift index 227685250c..24bbbb6cef 100644 --- a/PostHog/Replay/PostHogMaskSettleAnalysis.swift +++ b/PostHog/Replay/PostHogMaskSettleAnalysis.swift @@ -110,37 +110,6 @@ return oldRects } - /// Per-owner union of two samples taken either side of a render: covers wherever the - /// content sat while the render ran. An owner present in only one sample was still on - /// screen for part of that window, so its rect is emitted as collected rather than - /// discarding the frame — a scroll that recycles cells changes the owner set constantly, - /// and dropping every such frame freezes the recording across the whole interaction. - /// Unlike `pairedOldRects` the result is a mask list, not positionally tied to `after`. - /// nil only for a repeated owner within one sample, where the pairing is genuinely - /// ambiguous and there is no safe rect to emit. - static func sweptRects(before: [MaskedRegion]?, after: [MaskedRegion]?) -> [CGRect]? { - guard let before, let after, let beforeByOwner = rectsByOwner(before) else { - return nil - } - - var rects: [CGRect] = [] - rects.reserveCapacity(max(before.count, after.count)) - var seenAfterOwners: Set = [] - seenAfterOwners.reserveCapacity(after.count) - for region in after { - guard seenAfterOwners.insert(region.owner).inserted else { - return nil - } - // No counterpart means the owner appeared mid-render; cover where it landed. - rects.append(beforeByOwner[region.owner].map { $0.union(region.rect) } ?? region.rect) - } - // Owners that went away mid-render were still displayed for part of it. - for region in before where !seenAfterOwners.contains(region.owner) { - rects.append(region.rect) - } - return rects - } - /// Banded by measured drift: /// - still (≤ tolerance): rects as collected. /// - drift (≤ budget): each mask inflated to the swept region (both sampled positions diff --git a/PostHog/Replay/PostHogReplayIntegration.swift b/PostHog/Replay/PostHogReplayIntegration.swift index 114f1c64aa..a20cb18f81 100644 --- a/PostHog/Replay/PostHogReplayIntegration.swift +++ b/PostHog/Replay/PostHogReplayIntegration.swift @@ -1163,9 +1163,6 @@ /// All regions to redact in `window`: heuristic widgets from the hierarchy walk plus the /// live regions of `postHogMask()` reporters. Returns nil when a reporter hasn't laid out /// yet — the caller must skip the frame rather than capture it under-masked. - /// Pre-existing limitation with `screenshotModeBackgroundCapture` (off by default): pixels - /// render after this collection, so any rect source can go stale for content committed in - /// between. private func collectMaskedRegions(in window: UIWindow) -> [MaskedRegion]? { guard !window.hasCameraForReplay() else { return nil @@ -1212,8 +1209,8 @@ } // The settled path renders here so the pixels come from the same main-thread tick that - // measured the mask rects — any later and the presentation tree has moved on. Callers - // that render themselves (background capture, the bridge's first frame) pass false. + // measured the mask rects — any later and the presentation tree has moved on. The + // bridge's first frame renders itself and passes false. let image = renderImage ? window.toImage(preferFidelityRenderer: preferFidelityRenderer) : nil return ScreenshotCapture(wireframe: wireframe, windowSize: window.bounds.size, timestampDate: Date(), image: image) @@ -1441,49 +1438,6 @@ ) } - /// The render sits between two mask samples instead of one: measure geometry, render - /// off-main, measure again, mask the per-owner union — provably covering wherever the - /// content sat while the render ran, so no threshold is needed. Always uses - /// `drawHierarchy`, never the presentation-tree renderer: that reads `layer.presentation()`, - /// which is main-only, and this path's other reads already sit inside `main.sync`. - @discardableResult - private func performBracketedBackgroundCapture(window: UIWindow, screenName: String?, postHog: PostHogSDK) -> Bool { - defer { finishScreenshotRender() } - - guard let before = DispatchQueue.main.sync(execute: { self.collectMaskedRegions(in: window) }) else { - return false - } - // Off-main on purpose, and the reason this mode exists: drawHierarchy on main was too - // slow to keep up. UIKit documents it as main-thread-only, so it stays experimental - // behind `screenshotModeBackgroundCapture` — the bracketing above is what keeps masks - // aligned with pixels despite the render happening on this thread. - let image = window.toImage(preferFidelityRenderer: true) - let capture = DispatchQueue.main.sync { () -> ScreenshotCapture? in - let after = self.collectMaskedRegions(in: window) - guard let rects = Self.sweptRects(before: before, after: after) else { - hedgeLog("[Session Replay] Skipping snapshot: mask samples could not be paired") - return nil - } - // No renderer preference: `renderImage: false` means this call never renders — the - // image was already taken off-main above. - return self.collectScreenshotMetadata(window, overrideMaskRects: rects, renderImage: false) - } - - guard let capture, let image, postHog.isSessionReplayActive() else { - return false - } - - return renderAndEnqueueScreenshot( - capture.wireframe, - window: window, - windowSize: capture.windowSize, - screenName: screenName, - postHog: postHog, - timestampDate: capture.timestampDate, - image: image - ) - } - /// Settle-then-shoot: after one display-pipeline depth, unchanged mask geometry proves the /// displayed frame identical to the current tree, so full-fidelity drawHierarchy is safe /// (blur/video/Metal intact); drift within budget keeps drawHierarchy with masks swept to @@ -1533,13 +1487,7 @@ .flatMap(UIViewController.getViewControllerTypeName) .flatMap { PostHogScreenNameSanitizer.sanitize(rawScreenName: $0) } - if postHog.config.sessionReplayConfig.screenshotModeBackgroundCapture { - PostHogReplayIntegration.dispatchQueue.async { [weak self] in - self?.performBracketedBackgroundCapture(window: window, screenName: screenName, postHog: postHog) - } - } else { - scheduleSettledCapture(window: window, screenName: screenName, postHog: postHog) - } + scheduleSettledCapture(window: window, screenName: screenName, postHog: postHog) } private func handleEventCaptured(event: String) { diff --git a/PostHog/Replay/PostHogSessionReplayConfig.swift b/PostHog/Replay/PostHogSessionReplayConfig.swift index 3748b2248f..76419a3e5c 100644 --- a/PostHog/Replay/PostHogSessionReplayConfig.swift +++ b/PostHog/Replay/PostHogSessionReplayConfig.swift @@ -45,16 +45,6 @@ set {} // swiftlint:disable:this unused_setter_value } - /// Schedule screenshot image capture on a background queue. - /// - /// Warning: Enabling this option will trigger Main Thread Checker warnings and may - /// briefly freeze the app the first time a screenshot is captured. Consider disabling - /// Main Thread Checker in your scheme's run diagnostics when enabling this. - /// - /// Experimental. - /// Default: false - @objc public var screenshotModeBackgroundCapture: Bool = false - /// Throttle delay used to reduce the number of snapshots captured and reduce performance impact /// This is used for capturing the screenshot /// The lower the number more snapshots will be captured but higher the performance impact diff --git a/PostHog/Replay/UIView+Util.swift b/PostHog/Replay/UIView+Util.swift index c0d01ab06d..d67232a3ef 100644 --- a/PostHog/Replay/UIView+Util.swift +++ b/PostHog/Replay/UIView+Util.swift @@ -111,7 +111,7 @@ } func toImage(afterScreenUpdates: Bool = false, preferFidelityRenderer: Bool = true) -> UIImage? { - // Background capture also enters here; controller traversal belongs on main. + // Replay callers render on main; the sync is defensive, since controller traversal belongs on main. let hasCamera = { (self as? UIWindow ?? self.window)?.hasCameraForReplay() ?? false } diff --git a/PostHogTests/PostHogMaskScenarioTest.swift b/PostHogTests/PostHogMaskScenarioTest.swift index 2d3c4f8558..cc5fa23b8f 100644 --- a/PostHogTests/PostHogMaskScenarioTest.swift +++ b/PostHogTests/PostHogMaskScenarioTest.swift @@ -497,116 +497,5 @@ #expect(inflated[0].minY <= 95) #expect(inflated[0].maxY >= 125.31) } - - @Test("swept union covers both sample positions, in after order") - func sweptRectsCoversBothSamplesInAfterOrder() throws { - let a = NSObject() - let b = NSObject() - let before = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(b, CGRect(x: 0, y: 100, width: 100, height: 20)), - ] - // b moves; a is stationary. after is in swapped order vs before, so - // ordering must be recovered from `owner`, not index. - let after = [ - region(b, CGRect(x: 0, y: 130, width: 100, height: 20)), - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - ] - let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after)) - #expect(swept.count == 2) - - // swept[0] pairs with after[0] (b): union of y=100..120 and y=130..150. - #expect(swept[0] == CGRect(x: 0, y: 100, width: 100, height: 50)) - - // swept[1] pairs with after[1] (a): stationary, so it collapses to a's rect. - #expect(swept[1] == CGRect(x: 0, y: 0, width: 100, height: 20)) - } - - @Test("nil before makes sweptRects nil") - func sweptRectsNilBefore() { - let a = NSObject() - let after = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))] - #expect(PostHogReplayIntegration.sweptRects(before: nil, after: after) == nil) - } - - @Test("nil after makes sweptRects nil") - func sweptRectsNilAfter() { - let a = NSObject() - let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))] - #expect(PostHogReplayIntegration.sweptRects(before: before, after: nil) == nil) - } - - @Test("an owner that appears mid-render is covered, not dropped") - func sweptRectsCountMismatch() throws { - let a = NSObject() - let b = NSObject() - let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))] - let after = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(b, CGRect(x: 0, y: 100, width: 100, height: 20)), - ] - // b appeared mid-render, so it was displayed for part of the window and is covered - // rather than costing the whole frame. - let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after)) - #expect(swept == [ - CGRect(x: 0, y: 0, width: 100, height: 20), - CGRect(x: 0, y: 100, width: 100, height: 20), - ]) - } - - @Test("duplicate owner in before makes sweptRects nil") - func sweptRectsDuplicateOwnerInBefore() { - let a = NSObject() - let b = NSObject() - let before = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - ] - let after = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(b, CGRect(x: 0, y: 100, width: 100, height: 20)), - ] - #expect(PostHogReplayIntegration.sweptRects(before: before, after: after) == nil) - } - - @Test("duplicate owner in after makes sweptRects nil") - func sweptRectsDuplicateOwnerInAfter() { - let a = NSObject() - let b = NSObject() - let before = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(b, CGRect(x: 0, y: 100, width: 100, height: 20)), - ] - let after = [ - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - region(a, CGRect(x: 0, y: 0, width: 100, height: 20)), - ] - #expect(PostHogReplayIntegration.sweptRects(before: before, after: after) == nil) - } - - @Test("a fully recycled owner set covers both samples, not nil") - func sweptRectsDisjointOwnerSet() throws { - let a = NSObject() - let b = NSObject() - let before = [region(a, CGRect(x: 0, y: 0, width: 100, height: 20))] - let after = [region(b, CGRect(x: 0, y: 200, width: 100, height: 20))] - // Neither owner can be paired, but both were on screen during the render window: - // `after` entries come first, then the ones that went away. - let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after)) - #expect(swept == [ - CGRect(x: 0, y: 200, width: 100, height: 20), - CGRect(x: 0, y: 0, width: 100, height: 20), - ]) - } - - @Test("a stationary owner's swept rect equals its rect unchanged") - func sweptRectsStationaryOwnerUnchanged() throws { - let a = NSObject() - let rect = CGRect(x: 10, y: 20, width: 100, height: 20) - let before = [region(a, rect)] - let after = [region(a, rect)] - let swept = try #require(PostHogReplayIntegration.sweptRects(before: before, after: after)) - #expect(swept == [rect]) - } } #endif diff --git a/api/posthog-ios.public-api.txt b/api/posthog-ios.public-api.txt index 354c055859..946179d2e5 100644 --- a/api/posthog-ios.public-api.txt +++ b/api/posthog-ios.public-api.txt @@ -344,7 +344,6 @@ PostHog | PostHogSessionReplayConfig.maskAllSandboxedViews | property | @objc va PostHog | PostHogSessionReplayConfig.maskAllTextInputs | property | @objc var maskAllTextInputs: Bool | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)maskAllTextInputs PostHog | PostHogSessionReplayConfig.sampleRate | property | @objc var sampleRate: NSNumber? { get set } | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)sampleRate PostHog | PostHogSessionReplayConfig.screenshotMode | property | @objc var screenshotMode: Bool { get set } | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)screenshotMode -PostHog | PostHogSessionReplayConfig.screenshotModeBackgroundCapture | property | @objc var screenshotModeBackgroundCapture: Bool | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)screenshotModeBackgroundCapture PostHog | PostHogSessionReplayConfig.throttleDelay | property | @objc var throttleDelay: TimeInterval | c:@M@PostHog@objc(cs)PostHogSessionReplayConfig(py)throttleDelay PostHog | PostHogSessionReplayConsoleLogConfig | class | @objc class PostHogSessionReplayConsoleLogConfig | c:@M@PostHog@objc(cs)PostHogSessionReplayConsoleLogConfig PostHog | PostHogSessionReplayConsoleLogConfig.logSanitizer | property | @objc var logSanitizer: (String) -> PostHogConsoleLogEntry? | c:@M@PostHog@objc(cs)PostHogSessionReplayConsoleLogConfig(py)logSanitizer diff --git a/test-audit/replay.md b/test-audit/replay.md index 234d955bd6..cdbf18c236 100644 --- a/test-audit/replay.md +++ b/test-audit/replay.md @@ -383,14 +383,6 @@ Production owner: presentation-aware mask collection. The harness requires visib | 450 | `emptyArraysStill` | R — legitimate no-mask geometry settles. | | 457 | `driftInflationCoversSweepInAfterOrder` | R — swept coverage and owner ordering; catches wrong pairing/under-inflation. | | 486 | `driftInflationCoversTrailBehindBefore` | R — displayed-pixel lag requires trailing inflation. | -| 502 | `sweptRectsCoversBothSamplesInAfterOrder` | R — union and owner ordering independently specified. | -| 526 | `sweptRectsNilBefore` | R — missing sample cannot manufacture a safe union. | -| 533 | `sweptRectsNilAfter` | R — same later-sample guard. | -| 540 | `sweptRectsCountMismatch` | R — newly appearing owner remains covered. | -| 558 | `sweptRectsDuplicateOwnerInBefore` | R — ambiguous duplicate pairing returns nil. | -| 573 | `sweptRectsDuplicateOwnerInAfter` | R — symmetric ambiguity guard. | -| 588 | `sweptRectsDisjointOwnerSet` | R — recycled owners cover both samples instead of losing disappeared content. | -| 603 | `sweptRectsStationaryOwnerUnchanged` | R — stationary union does not distort mask. | ### 5. `PostHogMaskSnapshotTest.swift`