Skip to content

Latest commit

 

History

History
111 lines (85 loc) · 6.2 KB

AGENTS.md

File metadata and controls

111 lines (85 loc) · 6.2 KB

AGENTS.md

Guidance for AI coding agents (especially Codex) working in this repository.

Scope

These instructions apply to the entire repository unless a deeper AGENTS.md overrides them.

Project Shape

  • This is a React Native TurboModule SDK.
  • Public JS API should stay small and stable (setup(...), flushNow()) unless explicitly requested.
  • Native C++ install is internal and should run as part of native setup flow.

Native Logging Rules (Important)

  • Do not add direct android.util.Log calls in SDK runtime code.
  • On Android/Kotlin, use AnalyticsLogger.
  • AnalyticsLogger forwards to C++ via JNI (nativeLog(...)) and is the canonical logging path.
  • In C++, use margelo::analytics::logger::log(...) from cpp/logging/AnalyticsLogger.hpp.
  • Avoid NSLog/print for regular SDK logging paths. If logging is needed from ObjC++/C++, route through the shared C++ logger.

Native Integration Rules

  • Keep C++ binding install tied to setup lifecycle:
    • Android: MargeloAnalyticsSdkModule.setup(...) installs bindings before SDK setup.
    • iOS (new architecture): install via RCTTurboModuleWithJSIBindings + setup guard.
  • Keep cpp/entrypoint/InstallMargeloAnalyticsSdk.cpp minimal unless feature work explicitly requires adding behavior.
  • When adding C++ files, ensure both platforms include them:
    • Android: update android/CMakeLists.txt includes/sources if needed.
    • iOS: ensure CallstackPerformanceAnalyticsReactNative.podspec includes the new sources/headers.

JNI Bridging Style (Android)

  • Use fbjni for JNI method registration and bridging.
  • Prefer facebook::jni::registerNatives(...) + makeNativeMethod(...) for non-hybrid classes.
  • Prefer fbjni::HybridClass (registerNatives/registerHybrid) for hybrid module bindings.
  • Keep JNI_OnLoad minimal: only orchestrate registration calls.
  • Avoid manual raw JNI registration (FindClass/RegisterNatives tables in JNI_OnLoad) unless there is a concrete technical blocker.
  • Avoid adding new extern "C" Java_* entrypoints for regular SDK bridging when fbjni registration can be used.

How jsi::Runtime Reaches install(...)

  • Android path:
    • MargeloAnalyticsSdkModule.setup(...) calls installCppBindingsIfNeeded().
    • installCppBindingsIfNeeded() reads reactApplicationContext.javaScriptContextHolder.
    • javaScriptContextHolder.get() returns the raw JS runtime pointer as Long.
    • Kotlin calls native install(jsRuntimePointer, callInvokerHolder).
    • In JNI (JMargeloAnalyticsSdkModule::install), jlong is cast to facebook::jsi::Runtime* and forwarded to margelo::analytics::install(*runtime, callInvoker).
  • iOS new-architecture path:
    • MargeloAnalyticsSdk conforms to RCTTurboModuleWithJSIBindings.
    • React Native calls installJSIBindingsWithRuntime:(jsi::Runtime&)runtime.
    • The module stores the runtime pointer and later calls margelo::analytics::install(*_runtime, callInvoker) from setup guard logic.
  • Important safety constraints:
    • Treat the runtime pointer/reference as borrowed, never owned.
    • Never cache runtime globally in shared C++ static state without explicit lifecycle handling.
    • Do not use runtime from background/native threads directly; schedule JS work through CallInvoker.

Files To Reuse

  • Android JNI loader: android/src/main/java/com/margeloanalyticssdk/JNIOnLoad.kt
  • Android JSI install bridge: android/src/main/java/com/margeloanalyticssdk/MargeloAnalyticsSdkModule.kt
  • Android C++ JNI bindings: android/src/main/cpp/*
  • Shared C++ entrypoint: cpp/entrypoint/InstallMargeloAnalyticsSdk.*
  • Shared C++ logger: cpp/logging/AnalyticsLogger.*

Session and Upload Lifecycle (Do Not Break)

  • Treat one app process lifetime as one session.
  • Keep one active session file in current/ and append all samples for that session to the same file.
  • On startup/early init, move stale files from current/ to pending/, excluding the active session file.
  • Upload only completed session files from pending/.
  • Never upload the active in-progress session file from current/.
  • Keep Android and iOS lifecycle behavior mirrored unless a platform-specific exception is explicitly required.

Metrics Schema Contract (Cross-Platform)

  • Metric keys must encode units in the name (...Bytes, ...Ms, ...Percent, ...Flag).
  • Do not mix implicit or ambiguous units (for example raw /proc ticks or Android PSS KB values without conversion).
  • Maintain cross-platform baseline keys for core reporting:
    • Memory: processMemoryFootprintBytes, systemTotalMemoryBytes, systemAvailableMemoryBytes
    • CPU: cpuUserTimeMs, cpuSystemTimeMs, cpuTotalTimeMs, cpuUsagePercent, threadCount
  • Platform-specific extra keys are allowed, but baseline keys should remain stable.
  • If a schema/key rename is unavoidable, prefer dual-writing old+new keys for a transition release.

Platform Pitfalls

  • Android:
    • Do not keep Context (or context-owning classes) in static fields; use applicationContext safely.
    • Guard PackageInfo.getLongVersionCode() with API >= 28; use versionCode fallback for minSdk compatibility.
    • Keep SDK logging controlled by margeloAnalyticsSdkEnableLogging -> BuildConfig.ANALYTICS_LOGGING_ENABLED.
  • iOS:
    • Use robust task VM memory fallback logic (phys_footprint when available, else resident_size).
    • Always release task_threads allocations with vm_deallocate on both success and failure paths.

Validation Checklist

Run relevant checks after native changes:

  1. yarn typecheck
  2. Android example build: cd apps/example-react-native/android && ./gradlew :app:assembleDebug -x lint -x test --no-daemon
  3. iOS packages: yarn example ios:setup. Check podspec changes in the Expo CocoaPods example.
  4. iOS example build (when possible):
    • cd apps/example-react-native/ios && xcodebuild -project MargeloAnalyticsSdkExample.xcodeproj -scheme MargeloAnalyticsSdkExample -configuration Debug -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' build

Editing Discipline

  • Keep changes focused and minimal.
  • Do not introduce unrelated refactors.
  • Preserve existing API behavior unless explicitly requested to change it.
  • If a native dependency or podspec changes, include any required lockfile updates from pod install.