SessionCapture

class SessionCapture(maxActions: Int? = null, maxLogicEvents: Int? = null, maxLogs: Int? = null, redactor: StateRedactor? = null, redactSensitiveKeys: Boolean = true, sensitiveKeys: Set<String> = DEFAULT_SENSITIVE_KEYS)(source)

Captures session data for crash reports, manual export, and DevTools streaming.

SessionCapture is the shared nexus for all tooling signals: dispatched actions, traced logic events, and crashes all flow through this single instance. Capture calls only enqueue a record; a background worker performs JSON encoding and storage writes off the dispatch path, batching consecutive records into single storage writes.

Events are stored in file-backed JSONL storage (when filesystem is available) to avoid holding large state snapshots in memory. Falls back to in-memory storage on platforms without filesystem access (e.g., wasmJs browser).

Usage:

val capture = SessionCapture(maxActions = 500, maxLogicEvents = 1000)
capture.start("client-id", "MyApp", "Android")

// Capture events as they happen (typically called by middleware/observers)
capture.captureDispatchedAction(action, resultState)
capture.captureLogicStarted(logicEvent)

// Crashes are reported once and fan out to every consumer
capture.reportCrash(exception)

// Manual export (flushes pending records first)
val json = capture.exportSession()

The exported JSON format is compatible with DevTools ghost device import.

Parameters

maxActions

Maximum number of actions to retain (older actions are dropped)

maxLogicEvents

Maximum number of logic events to retain (older events are dropped)

Constructors

Link copied to clipboard
constructor(maxActions: Int? = null, maxLogicEvents: Int? = null, maxLogs: Int? = null, redactor: StateRedactor? = null, redactSensitiveKeys: Boolean = true, sensitiveKeys: Set<String> = DEFAULT_SENSITIVE_KEYS)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val actions: SharedFlow<CapturedAction>

Live stream of captured actions, emitted by the worker after encoding. DevTools consumes this instead of re-serializing state per action.

Link copied to clipboard
val crashes: SharedFlow<CrashInfo>

Live stream of reported crashes. Every crash source (traced logic failures, platform uncaught-exception handlers, manual reports) funnels through here.

Link copied to clipboard
val extensions: StateFlow<Map<String, JsonElement>>
Link copied to clipboard
Link copied to clipboard
val logs: SharedFlow<CapturedLog>
Link copied to clipboard
val markers: SharedFlow<SessionMarker>
Link copied to clipboard
Link copied to clipboard
var stateJson: Json
Link copied to clipboard
val stateReads: SharedFlow<StateRead>

Functions

Link copied to clipboard
fun addMarker(label: String, note: String = "", source: String = "device", timestampMs: Long? = null, afterActionIndex: Int = -1)
Link copied to clipboard
fun attachStateSerializers(serializersModule: SerializersModule)

Provides the store's serializers so the worker can encode module states. Called by ToolingLogic during initialization.

Link copied to clipboard

Captures a pre-built action event.

Link copied to clipboard

Captures a dispatched action and its resulting module state. State encoding happens on the worker, off the dispatch path.

Link copied to clipboard

Captures the initial full state snapshot at session start. Encoding happens on the worker, off the dispatch path.

Link copied to clipboard
fun captureLog(level: String, category: String, message: String)
Link copied to clipboard
fun captureLogicCompleted(event: LogicMethodCompleted)

Captures a logic method completed event.

Link copied to clipboard
fun captureLogicFailed(event: LogicMethodFailed)

Captures a logic method failed event.

Link copied to clipboard
fun captureLogicStarted(event: LogicMethodStart)

Captures a logic method started event.

Link copied to clipboard
fun captureStateRead(read: StateRead)
Link copied to clipboard
suspend fun clear()

Clears all captured data but keeps the session active.

Link copied to clipboard
Link copied to clipboard

Encodes a state tree one module at a time with the encoder the capture uses for its deltas.

Link copied to clipboard
suspend fun exportCrashSession(throwable: Throwable): String

Reports the throwable as a crash and exports the session including it. Typically called by platform crash handlers for uncaught exceptions.

Link copied to clipboard
suspend fun exportSession(crash: CrashInfo? = null): String

Exports the current session as a JSON string.

Link copied to clipboard
suspend fun flush()

Suspends until every record enqueued before this call has been processed.

Link copied to clipboard

Gets the captured crash info if any.

Link copied to clipboard

Gets the client ID for this session.

Link copied to clipboard

Gets the captured initial state JSON.

Link copied to clipboard

Gets the current session history.

Link copied to clipboard

Checks if session capture has been started.

Link copied to clipboard

Records a completed network exchange, materialising its full bodies before they age out.

Link copied to clipboard

Reports a crash to the nexus: stores it for export and emits it on crashes.

fun reportCrash(throwable: Throwable, origin: CrashOrigin = CrashOrigin.MANUAL)

Reports a crash from a throwable.

Link copied to clipboard
fun start(clientId: String, clientName: String, platform: String, metadata: ClientMetadata? = null)

Starts a new session capture and its background worker.

Link copied to clipboard
suspend fun stop()

Stops the session capture and its worker. Drains pending records before deleting storage so a mid-flight batch cannot resurrect deleted data.

Link copied to clipboard
fun suggestFileName(prefix: String? = null): String

Suggests an export file name carrying client identity and app version. The prefix defaults to crash when a crash has been captured, session otherwise.