Guidance for AI coding agents (especially Codex) working in this repository.
These instructions apply to the entire repository unless a deeper AGENTS.md overrides them.
- 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.
- Do not add direct
android.util.Logcalls in SDK runtime code. - On Android/Kotlin, use
AnalyticsLogger. AnalyticsLoggerforwards to C++ via JNI (nativeLog(...)) and is the canonical logging path.- In C++, use
margelo::analytics::logger::log(...)fromcpp/logging/AnalyticsLogger.hpp. - Avoid
NSLog/printfor regular SDK logging paths. If logging is needed from ObjC++/C++, route through the shared C++ logger.
- Keep C++ binding install tied to setup lifecycle:
- Android:
MargeloAnalyticsSdkModule.setup(...)installs bindings before SDK setup. - iOS (new architecture): install via
RCTTurboModuleWithJSIBindings+ setup guard.
- Android:
- Keep
cpp/entrypoint/InstallMargeloAnalyticsSdk.cppminimal unless feature work explicitly requires adding behavior. - When adding C++ files, ensure both platforms include them:
- Android: update
android/CMakeLists.txtincludes/sources if needed. - iOS: ensure
CallstackPerformanceAnalyticsReactNative.podspecincludes the new sources/headers.
- Android: update
- Use
fbjnifor 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_OnLoadminimal: only orchestrate registration calls. - Avoid manual raw JNI registration (
FindClass/RegisterNativestables inJNI_OnLoad) unless there is a concrete technical blocker. - Avoid adding new
extern "C"Java_*entrypoints for regular SDK bridging whenfbjniregistration can be used.
- Android path:
MargeloAnalyticsSdkModule.setup(...)callsinstallCppBindingsIfNeeded().installCppBindingsIfNeeded()readsreactApplicationContext.javaScriptContextHolder.javaScriptContextHolder.get()returns the raw JS runtime pointer asLong.- Kotlin calls native
install(jsRuntimePointer, callInvokerHolder). - In JNI (
JMargeloAnalyticsSdkModule::install),jlongis cast tofacebook::jsi::Runtime*and forwarded tomargelo::analytics::install(*runtime, callInvoker).
- iOS new-architecture path:
MargeloAnalyticsSdkconforms toRCTTurboModuleWithJSIBindings.- 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.
- 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.*
- 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/topending/, 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.
- Metric keys must encode units in the name (
...Bytes,...Ms,...Percent,...Flag). - Do not mix implicit or ambiguous units (for example raw
/procticks 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
- Memory:
- 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.
- Android:
- Do not keep
Context(or context-owning classes) in static fields; useapplicationContextsafely. - Guard
PackageInfo.getLongVersionCode()with API >= 28; useversionCodefallback for minSdk compatibility. - Keep SDK logging controlled by
margeloAnalyticsSdkEnableLogging->BuildConfig.ANALYTICS_LOGGING_ENABLED.
- Do not keep
- iOS:
- Use robust task VM memory fallback logic (
phys_footprintwhen available, elseresident_size). - Always release
task_threadsallocations withvm_deallocateon both success and failure paths.
- Use robust task VM memory fallback logic (
Run relevant checks after native changes:
yarn typecheck- Android example build:
cd apps/example-react-native/android && ./gradlew :app:assembleDebug -x lint -x test --no-daemon - iOS packages:
yarn example ios:setup. Check podspec changes in the Expo CocoaPods example. - 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
- 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.