Skip to content

refactor(tunnel): PR-1 — remove memory-triggered reconnect, gate legacy diagnostics#1

Open
mrmidi wants to merge 1 commit into
masterfrom
pr-1/measurement-safety
Open

refactor(tunnel): PR-1 — remove memory-triggered reconnect, gate legacy diagnostics#1
mrmidi wants to merge 1 commit into
masterfrom
pr-1/measurement-safety

Conversation

@mrmidi

@mrmidi mrmidi commented Jul 20, 2026

Copy link
Copy Markdown
Owner

PR-1 — Measurement Safety

Purpose: Stop legacy telemetry from contaminating PR0/PR1 native memory measurements.

Changes (5 files)

File Change
PacketTunnelProvider.swift Removed triggerMemoryPressureReconnect(). evaluateMemoryPressure() is telemetry-only (throttled os_log, no bridge action). recordProviderEvent() uses @autoclosure + #if FPTN_MEASUREMENT_BUILD — interpolated strings never constructed in Measurement builds. updateDiagnosticsHeartbeat() returns before building the full snapshot. emitTelemetry() samples only Mach memory counters in Measurement builds. Removed memoryEmergencyReconnectInProgress and all references.
TunnelDiagnosticsStore.swift Added providerRecordingEnabled static flag as runtime defense-in-depth. recordProviderEvent() and writeHeartbeat() no-op when disabled. App-side read path and MetricKit unaffected.
TunnelLogger.swift RingLogSink.tunnel.write() gated behind #if DEBUG. TunnelLogLevelStore forces .warning in Measurement builds so info/debug never reach formatter/redactor.
project.yml Added Measurement: release build configuration with FPTN_MEASUREMENT_BUILD compilation condition on the tunnel target. Build with -configuration Measurement.
FptnVPNTests.swift Added memoryEmergencyIsTelemetryOnly test: verifies emergency classification at/above threshold, warning just below.

Measurement configuration

xcodebuild build -scheme FptnVPNTunnel -configuration Measurement

This is a real, reproducible build configuration — no manual uncommenting required.

What Measurement builds eliminate

  • String interpolation at every recordProviderEvent call site (@autoclosure + #if)
  • Full TunnelRuntimeSnapshot construction on 15s telemetry ticks (memory-only path)
  • Native status queries, ISO-8601 dates, string copies for heartbeat
  • JSON encoding, JSONL file reads, string arrays, atomic file rewrites
  • 1 MiB ring buffer + 512 KiB file sink (Release #if DEBUG gate)
  • Info/debug log formatting and regex redaction (forced .warning level)
  • Bridge destruction + recreation at 42 MiB (removed entirely)

What remains active

  • os_log warning/error output (sparse, transient formatting cost only)
  • Crash signal installer (async-signal-safe POSIX writes)
  • Mach task_info memory sampling (2 syscalls per 15s tick)
  • Thresholds retained as telemetry-only markers (recalibration deferred)

Verification

  • Debug build: succeeded
  • Measurement build: succeeded
  • Tests: 22/22 passed

Part of the memory reduction sequence

PR-1  Measurement safety          ← this PR
PR0   Production native build     (C++23, MinSizeRel, dead code removal)
PR1A  Native fixed baseline       (socket buffers, Beast reserve, logging)
PR1B  Queue and batch bounds
PR1C  Native teardown ownership
PR2   Wrapper/provider integration
PR3   C-based binary flight recorder

@mrmidi
mrmidi force-pushed the pr-1/measurement-safety branch from 476b55e to 4fb85bb Compare July 20, 2026 07:32
…nostics

PR-1 (Measurement Safety) — stop legacy telemetry from contaminating
native memory baselines before PR0/PR1 optimization work.

Memory pressure:
- Remove triggerMemoryPressureReconnect(): the emergency threshold
  (42 MiB) no longer destroys the WebSocket bridge and schedules a
  full reconnect. Recreating TLS, queues and buffers near the memory
  ceiling increased peak usage and obscured the actual growth source.
- evaluateMemoryPressure() is now telemetry-only: both warning and
  emergency emit a throttled os_log entry. No bridge action, no JSONL
  event recording, no heartbeat rewrite originates from this method.
- Remove memoryEmergencyReconnectInProgress state and all references.

Measurement configuration:
- Add a real Measurement build configuration (release-based) to
  project.yml with FPTN_MEASUREMENT_BUILD compilation condition.
- recordProviderEvent() uses @autoclosure + #if gate: interpolated
  message strings are never constructed in Measurement builds.
- updateDiagnosticsHeartbeat() returns before constructing the full
  runtime snapshot (native status query, ISO-8601 dates, string
  copies) in Measurement builds.
- emitTelemetry() samples only Mach memory counters in Measurement
  builds, skipping the full TunnelRuntimeSnapshot construction.
- TunnelLogLevelStore forces .warning in Measurement builds so
  info/debug messages never reach the formatter or redactor.
- RingLogSink.tunnel.write() gated behind #if DEBUG: the 1 MiB ring
  buffer and 512 KiB file sink are compiled out of Release builds.
  Release builds do not retain provider text in the application-owned
  ring or shared log file; sparse messages continue through os_log.
- TunnelDiagnosticsStore.providerRecordingEnabled runtime guard
  remains as defense-in-depth for non-Measurement builds.

Thresholds (30/42 MiB) retained as telemetry-only markers.
Recalibration deferred until real-device measurements post-PR0.

Tests: 22/22 passed. Debug + Measurement configurations build.
@mrmidi
mrmidi force-pushed the pr-1/measurement-safety branch from 4fb85bb to 1ce6510 Compare July 20, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant