Closes #671 `TunnelSessionManager` was test infrastructure living in `jvmMain`. Nothing in any `main` source set ever constructed it, it never had a Koin binding, and the collector that actually ships is `TunnelForegroundService.collectIncomingSessions`. Sitting in a production source set it read as a second, competing collector one wiring change away from being used, and its comments argued production error-reporting policy for a code path that never ran. The class is deleted. The collect-into-handler helper its tests needed moves to `core/bridge/src/jvmTest/.../SessionCollection.kt`, next to its only callers, where it makes no claims about production; `:app`'s integration test collects inline instead, because `:core:bridge`'s test source set is not on `:app`'s unit-test compile classpath. Not a coverage regression, despite how the diff reads. This removes `TunnelError.UnhandledTlsState` escape-pinning *from the manager* only. `SessionHandlerDefectVisibilityTest` still pins exactly that property, in the same module, for `SessionHandler` -- the class that does ship -- and it is pinned end-to-end at four shipped layers: thrown in `TlsAcceptor`, surviving to the handler boundary, rethrown by `SessionHandler`, and becoming a crash report in `TunnelBoundaryFailureReportingTest`. What is deleted was a fifth, parallel pin on a class nothing constructed. Please do not revert this as a lost guard. The one property that needed migrating is now better covered than before. "One bad session must not take the collector down" was pinned only against the manager, so it was not actually covered on the path that ships. It moves to `TunnelBoundaryFailureReportingTest` as `an ordinary session failure does not stop the collector`, driving the real service through the real Koin graph. Every pre-existing test there delivers exactly one stream and so stays green on a boundary that handles its first failure and then stops collecting forever; only a second stream can tell the difference, which is why the new test emits two. Verified by ablation, not by reading: removing the per-stream `launch` and the `catch` turns it red. `ToolCallViaSniPassthroughTest`'s kdoc claimed the manager came "from the production Koin graph" while it was hand-constructed, because no binding existed. The kdoc now enumerates precisely which pieces are production (`SessionHandler`, `CertificateStore`) and which are scaffolding (`TunnelClientImpl`, built with a fixture websocket factory). `TunnelSessionManagerTest` is deleted for free: its 103-line helper block was byte-identical to `ClientPassthroughTest`'s and its sole test a variable-rename clone. Its two extra `initialize should succeed` assertions are folded into the survivor, and its three detekt baseline entries go with it. Docs: four references updated in `docs/design/android-app.md` (20, 33, 321) and `docs/test-coverage-audit.md:79`. That last one credited "✓ Good" coverage to a class that ships nowhere -- one instance of the pattern issue #654 tracks. This removes that instance only; #654 is a broader audit and stays open. No `docs/ux-decisions.md` entry: `.claude/rules/ux-changes.md:21` exempts documentation updates that describe already-shipped behaviour. Reviewed-by: reviewerbot
32 KiB
Android App Design
Reflects shipped behavior on main. Source paths and line numbers are cited so this doc can be re-validated against the tree.
Architecture Overview
The Android app is a single-Activity Compose app that hosts an on-device MCP server. The app shell wires together a Koin graph, a Compose Navigation host, and a foreground tunnel service. MCP integrations register through a single contract (McpIntegration) and the app is the only module that knows about all the others.
Runtime topology of a live session:
AI client ──TLS──▶ relay (SNI passthrough) ──mux/WebSocket──▶
TunnelForegroundService.collectIncomingSessions (in :work) ──▶
SessionHandler.handleStream (in :core:bridge) ──▶
McpSession (in :core:mcp) ──▶ McpIntegration.provider (one per integration)
TunnelForegroundService collects TunnelClient.incomingSessions itself and calls sessionHandler.handleStream(stream) directly, one child coroutine per stream (work/src/main/kotlin/com/rousecontext/work/TunnelForegroundService.kt:254-279).
That service collector is the only one. :core:bridge used to also hold a TunnelSessionManager that collected incomingSessions and dispatched to the same SessionHandler, in jvmMain — but no main source set ever constructed it and it never had a Koin binding, so it read as a second, competing production collector that was one wiring change away from being used, and its catch (_: Exception) documented a policy that ran nowhere. It was test scaffolding in a production source set; #671 deleted it and moved the collect-into-handler helper its tests needed to core/bridge/src/jvmTest/.../SessionCollection.kt. For where session errors actually surface, see Session handling and failure discrimination.
Per-integration audit, notification, and permission state surfaces in the app UI; the integration itself only supplies metadata, an MCP provider, and an availability check.
Module Map
Canonical list from settings.gradle.kts:25-35. Eleven modules; eight ship in the APK (:core:testfixtures, :device-tests, and :e2e are test-only).
| Module | Path | Role | Project deps |
|---|---|---|---|
:app |
app/ |
Single Activity, Koin graph, navigation, integration registry, all setup/manage screens. Only module that knows about every other module. | :core:tunnel, :core:mcp, :core:bridge, :api, :integrations, :notifications, :work |
:core:tunnel |
core/tunnel/ |
KMP. Mux protocol, WebSocket client, CertificateStore, OnboardingFlow, CertProvisioningFlow, RelayApiClient. No MCP knowledge. |
(none) |
:core:mcp |
core/mcp/ |
KMP. McpSession, McpServerProvider, OAuth device-code flow, token store, HTTP routing, AuditListener. |
(none) |
:core:bridge |
core/bridge/ |
KMP. Wires :core:tunnel mux streams to :core:mcp sessions: SessionHandler, McpSessionFactory, HttpHeaderInjector, TlsCertProvider. |
:core:tunnel, :core:mcp |
:api |
api/ |
The McpIntegration interface plus the supporting IntegrationStateStore / NotificationSettingsProvider contracts. |
:core:mcp |
:integrations |
integrations/ |
Hosts every shipped MCP server: health, notifications, outreach, usage. Each subpackage exports an McpServerProvider plus its own data layer. |
:core:mcp, :api, :notifications |
:notifications |
notifications/ |
Notification channels, foreground notification builder, audit Room database, post-session decisioning. | :core:tunnel, :core:mcp, :api |
:work |
work/ |
TunnelForegroundService, TunnelFailureReporting, GracefulTunnelShutdown, push dispatch (FcmDispatch + WakeDispatcher — the FirebaseMessagingService itself lives in :app, see below), WorkManager workers (CertRenewalWorker, SecurityCheckWorker), IntegrationSecretsSynchronizer, IdleTimeoutManager, WakelockManager, WakeReconnectDecider, SessionActivityTracker. |
:api, :core:tunnel, :core:bridge, :notifications |
:core:testfixtures |
core/testfixtures/ |
Test-only. TestRelayFixture for booting the real relay binary in integration tests. |
(test) |
:device-tests |
device-tests/ |
Instrumented tests. | (test) |
:e2e |
e2e/ |
Cold-start and end-to-end harnesses. | (test) |
Dependency graph
Edges below are derived from the build.gradle.kts file in each module.
:core:tunnel (no project deps)
:core:mcp (no project deps)
:core:bridge ──▶ :core:tunnel, :core:mcp
:api ──▶ :core:mcp
:integrations ──▶ :core:mcp, :api, :notifications
:notifications ──▶ :core:tunnel, :core:mcp, :api
:work ──▶ :api, :core:tunnel, :core:bridge, :notifications
:app ──▶ :core:tunnel, :core:mcp, :core:bridge, :api,
:integrations, :notifications, :work
:core:bridge exposes both of its dependencies with api(...) rather than implementation(...), so :core:mcp types reach :work transitively even though work/build.gradle.kts does not name :core:mcp.
There is no separate :health, :outreach, :usage, or :notifications-mcp module; all four MCP integrations live as subpackages of :integrations (integrations/src/main/{java,kotlin}/com/rousecontext/integrations/{health,notifications,outreach,usage}).
Integration Contract
The single contract is McpIntegration in api/src/main/kotlin/com/rousecontext/api/McpIntegration.kt:11-45:
interface McpIntegration {
val id: String // e.g. "health"
val displayName: String // e.g. "Health Connect"
val description: String // shown in the Add picker
val path: String // URL path prefix, e.g. "/health"
val provider: McpServerProvider
suspend fun isAvailable(): Boolean
val onboardingRoute: String // legacy field (see below)
val settingsRoute: String // legacy field (see below)
}
What's not on this interface (and previous revisions of this doc claimed):
- No
registerNavigation(NavGraphBuilder, ...). Integrations do not own nav graph entries. Setup and manage screens for every integration live in:app's nav graph (HealthConnectSetupDestination,NotificationSetupDestination,OutreachSetupDestination,UsageSetupDestination,IntegrationManageDestination). - No
requiredPermissions(). Permissions are integration-specific. Health Connect derives its set fromRecordTypeRegistry.allPermissions(integrations/src/main/java/com/rousecontext/integrations/health/RecordTypeRegistry.kt:336); the others compute their own. onboardingRoute/settingsRoutesurvive on the interface but are not used to route — the app navigates by integrationidto fixed routes (see Navigation below).
The four McpIntegration implementations live in :app (not :integrations) so that the app can wire Context, settings stores, scopes, and notifiers without :integrations taking a dependency on :app-owned types:
app/src/main/java/com/rousecontext/app/registry/HealthConnectIntegration.ktapp/src/main/java/com/rousecontext/app/registry/NotificationIntegration.ktapp/src/main/java/com/rousecontext/app/registry/OutreachIntegration.ktapp/src/main/java/com/rousecontext/app/registry/UsageIntegration.kt
Each delegates to the corresponding *McpProvider/*McpServer in :integrations.
Supporting interfaces in :api
interface IntegrationStateStore {
suspend fun isUserEnabled(integrationId: String): Boolean
suspend fun setUserEnabled(integrationId: String, enabled: Boolean)
fun observeUserEnabled(integrationId: String): Flow<Boolean>
suspend fun wasEverEnabled(integrationId: String): Boolean
fun observeEverEnabled(integrationId: String): Flow<Boolean>
fun observeChanges(): Flow<Unit>
}
interface NotificationSettingsProvider {
suspend fun settings(): NotificationSettings
// plus a reactive Flow view of the same settings
}
The active subdomain is not exposed as a flow. CertificateStore offers suspend fun getSubdomain(): String?, and per-integration URLs are built through McpUrlProvider (app/src/main/java/com/rousecontext/app/UrlBuilder.kt), a :app-owned wrapper whose buildUrl(integrationId) / buildHostname(integrationId) combine the stored subdomain with that integration's secret prefix. The MCP endpoint path is always /mcp, so the URL path component is not what selects an integration — the hostname is. McpRouting.resolveIntegration (core/mcp/src/jvmMain/kotlin/com/rousecontext/mcp/core/McpRouting.kt:216-224) parses the first Host label as {secret}-{integration} and hands the extracted name to ProviderRegistry.providerForPath, whose production map is keyed on McpIntegration.path with the leading / stripped (app/src/main/java/com/rousecontext/app/registry/IntegrationProviderRegistry.kt:41, consumed at :77-81). So path is still the routing key — changing an integration's path would break its routing; it is simply no longer consumed as a URL path component.
Navigation
Single Activity, Compose Navigation. All routes are defined as constants in Routes (app/src/main/java/com/rousecontext/app/ui/navigation/AppNavigation.kt:42-111) and registered as composables in AppNavigation().
| Route constant | Pattern | Purpose |
|---|---|---|
ONBOARDING |
onboarding?autostart={autostart} |
Welcome screen + autostart trigger; same composable for both modes (see Onboarding below). |
ONBOARDING_BASE |
onboarding |
NavHost start destination; resolves to the ONBOARDING composable because the arg is nullable. |
ONBOARDING_AUTOSTART |
onboarding?autostart=true |
Concrete URL used by NotificationPreferences Continue (#392). |
NOTIFICATION_PREFERENCES |
onboarding/notification_preferences |
Post-session mode picker, plus inline POST_NOTIFICATIONS permission request. |
BACKGROUND_DELIVERY |
background_delivery?settings={settings} |
UnifiedPush distributor picker (#463). Registered unconditionally, navigated to only in the foss flavor. BACKGROUND_DELIVERY_BASE / BACKGROUND_DELIVERY_SETTINGS are the onboarding-step and Settings-entry URLs. |
HOME |
home |
Main dashboard. |
AUDIT |
audit?provider={provider}&scrollToCallId={scrollToCallId} |
Audit list, optionally filtered + scrolled. |
AUDIT_DETAIL |
audit_detail/{entryId} |
Single audit row detail. |
SETTINGS |
settings |
App settings, trust status, subdomain rotation. |
ADD_INTEGRATION |
add_integration |
Picker for integrations not yet enabled. |
INTEGRATION_MANAGE |
integration/{integrationId} |
Per-integration manage screen: URL, recent activity, authorized clients, disable. |
INTEGRATION_SETUP |
integration_setup/{integrationId} |
Cert/wiring spinner shown after a fresh enable, before the integration-specific setup. |
HEALTH_CONNECT_SETUP |
health_connect_setup/{mode} |
Health Connect permission + record type picker. |
NOTIFICATION_SETUP |
notification_setup/{mode} |
Notifications-MCP setup. |
OUTREACH_SETUP |
outreach_setup/{mode} |
Outreach (installed-apps) setup. |
USAGE_SETUP |
usage_setup/{mode} |
Usage stats setup. |
INTEGRATION_ENABLED |
integration_enabled/{integrationId} |
Confirmation screen showing the URL + waiting-for-client state. |
AUTH_APPROVAL |
auth_approval |
OAuth device-code approve/deny. |
ALL_CLIENTS |
all_clients/{integrationId} |
Authorized clients list, per integration. |
{mode} on the four *_setup routes is a SetupMode enum (initial onboarding vs. post-onboarding management).
There are no integration-owned routes. Each *_setup destination is registered in :app (app/src/main/java/com/rousecontext/app/ui/navigation/destinations/) and the integration only supplies an id plus an McpServerProvider.
Bottom nav: Home, Audit, Settings (3 tabs). The bottom bar and top bar are hidden during the onboarding routes — which include the two background-delivery routes (ONBOARDING_ROUTES at AppNavigation.kt:113-119, applied at AppNavigation.kt:142-183).
Device Onboarding
Three relay hops, one Compose flow, one shared OnboardingViewModel.
Relay sequence
OnboardingFlow.execute() (core/tunnel/src/jvmMain/kotlin/com/rousecontext/tunnel/OnboardingFlow.kt:53-61) chains:
POST /request-subdomain— relay reserves a single-word subdomain keyed by the Firebase UID (short TTL).POST /register— consumes the reservation, returns the assigned subdomain plus the per-integration secret map. Subdomain + secrets are persisted viaCertificateStore.POST /register/certs(viaCertProvisioningFlow) — mints the ACME server cert (DNS-01 through Cloudflare) and the relay-CA client cert. Added in #389 so a device never lands in a half-configured "subdomain but no certs" state.
Failure semantics:
- Step 1 failure: no persisted state. Reservation expires on its own.
- Step 2 failure: no persisted state.
- Step 3 failure: subdomain + secrets stay (#163). The user can retry just the cert hop without burning a new subdomain reservation.
UI flow
Drawn from OnboardingViewModel.kt:67-138 and the destinations under app/src/main/java/com/rousecontext/app/ui/navigation/destinations/:
Welcome ──▶ NotificationPreferences ──▶ onboarding?autostart=true ──▶ Home
(ONBOARDING) (NOTIFICATION_PREFERENCES) (ONBOARDING_AUTOSTART)
Step-by-step:
- Welcome (
OnboardingDestination) — first-run intro. On Get Started, navigates toNOTIFICATION_PREFERENCES. - Notification preferences (
NotificationPreferencesDestination) — pick post-session mode (summary / each-usage / suppress), and on Android 13+ inline-prompt forPOST_NOTIFICATIONS. On Continue, navigates toONBOARDING_AUTOSTART(poppingONBOARDINGinclusive). - Autostart re-entry — the same
OnboardingDestinationrecomposes withautostart=trueand triggersOnboardingViewModel.startOnboarding()on the destination's own VM. This is the #392 invariant: previously two separateOnboardingViewModelinstances existed (one for Welcome, one for NotificationPreferences) which caused the Welcome screen to never observe the relay registration completing. There is now exactly oneOnboardingViewModelfor the whole flow. - Registering —
OnboardingState.InProgress(Registering)while Firebase anon auth + FCM token +POST /request-subdomain+POST /registerrun. UI shows a spinner with "Registering" copy. - Provisioning certificates —
OnboardingState.InProgress(ProvisioningCerts)whilePOST /register/certsruns (multi-second ACME hop). UI flips to "Provisioning certificates" copy. - Onboarded — navigates to
HOME.
There is no separate "generating keys" UI step; key generation happens inside CertProvisioningFlow while the UI is in ProvisioningCerts. The two OnboardingStep values in OnboardingViewModel.kt:39-42 are exhaustive.
The decision to run cert provisioning at Continue (rather than deferring to the first integration add) is logged in docs/ux-decisions.md under the 2026-04-24 entry.
Failure surfaces:
OnboardingState.RateLimited— relay or ACME rate-limit; UI shows the formatted retry date.OnboardingState.Failed— terminal error with retry button; on cert-provisioning failures,registrationStatus.markComplete()still fires so a retry from Settings can re-run only the cert hop.
OnboardingViewModel.startOnboarding() launches on an appScope (Application-scoped) coroutine so the multi-second cert hop survives the user backgrounding the app or recomposition tearing down viewModelScope.
Foreground Service & Tunnel Lifecycle (:work)
:work owns every Android-lifecycle concern around the tunnel. Bridging TLS and MCP is :core:bridge's job, invoked from inside the service — but :work is not MCP-free: TunnelForegroundService injects McpSession and ProviderRegistry, WakeDispatcher gates on ProviderRegistry.enabledPaths(), SessionActivityAuditListener implements :core:mcp's AuditListener, and GracefulTunnelShutdown takes an McpSession. Those types reach :work transitively through :core:bridge's api(project(":core:mcp")).
TunnelForegroundService
- Started by
WakeDispatcheron atype: "wake"push, and only when at least one integration is enabled. startForeground()is the first non-trivial call inonCreate(#325); a start blocked by the Android 15dataSyncbudget posts anFgsLimitNotifiernotification and stops the service, andonTimeout()does the same mid-run (#450, #451).- Holds the singleton
TunnelClientfrom Koin; callsconnect(). - Posts and updates the foreground notification as
TunnelStatechanges, with defensive reconciles afterawaitReady()and after a successfulconnect()(#510). - Reconnects with exponential backoff on an unexpected
DISCONNECTED, giving up after 5 minutes. - Stops via
IdleTimeoutManagerafter the configured idle window with no active streams.
Session handling and failure discrimination
collectIncomingSessions (TunnelForegroundService.kt:254-279) collects tunnelClient.incomingSessions, launches one child coroutine per mux stream, and calls sessionHandler.handleStream(stream) inside a per-stream try. Catching per stream is what keeps one bad session from taking the tunnel down.
What happens to a throwable that escapes handleStream is decided by work/src/main/kotlin/com/rousecontext/work/TunnelFailureReporting.kt, which classifies into three kinds — not the binary "report it or swallow it" the older layout implied (#642, #650):
| Kind | What it means | What the boundary does |
|---|---|---|
Cancellation |
The scope is shutting down (service destroyed, idle timeout, tunnel teardown). | Rethrown, so the coroutine completes as cancelled. Never logged as an error, never reported. |
PeerOrTransport |
The peer, network, or relay did something ordinary — hung up, aborted mid-handshake, reset a stream. | Log.i with the exception class and message, so the rate stays visible in logcat. No crash report. |
Defect |
This layer reached a state it has no handling for. | Log.e and CrashReporter.logCaughtException. |
classifyTunnelFailure matches in this order:
CancellationException→Cancellation. It must come first:CancellationExceptionextendsIllegalStateException, so any later arm could otherwise swallow it.TunnelError.UnhandledTlsState→Defect. By its own kdoc this is a defect in our TLS/mux code, not anything the peer did (#616).TunnelError.ConnectionFailed→ depends on the cause.TunnelClientImpl.connectwraps anything non-TunnelErrorthat escapes it, so anullorIOExceptioncause is routine (PeerOrTransport) while any other cause is a laundered defect and stays loud.TunnelError.TlsHandshakeFailed,WebSocketClosed,StreamRefused,StreamReset, and plainIOException→PeerOrTransport.- Everything else →
Defect.
The quiet set is a closed allowlist and the else arm is Defect, deliberately: an unanticipated throwable — the IllegalStateException from SessionHandler.handleStream's missing-cert error(...), an NPE, TunnelError.ProtocolError, InternalError, CertificateError, InvalidStateTransition — is by definition something this layer did not plan for, so the default has to be loud or the policy decays into "report nothing". TunnelError extends Exception rather than IOException, which is what keeps the is IOException arm from capturing a tunnel error.
The same policy is applied at the connect boundary: connectToRelay's catch around tunnelClient.connect(relayUrl) calls reportTunnelFailure too, so a phone that woke with no network is quiet while a cert/provisioning bug is loud. Two nearby sites rethrow cancellation by hand for the same reason: the best-effort disconnect() inside connectToRelay, and the backoff loop in launchReconnect (which logs other attempt failures at Log.w, since the loop itself is the recovery).
Below this boundary, SessionHandler's two copy loops rethrow CancellationException and TunnelError.UnhandledTlsState and treat every other exception as a quiet EOF (#616, #626, #630). :core:bridge is a KMP jvm target with no Log or CrashReporter, so propagating to TunnelForegroundService is how a defect down there becomes observable.
Push dispatch (FcmDispatch, WakeDispatcher, FcmReceiver)
The receiver is flavor-specific; the routing is not.
FcmDispatch.resolve(data)(in:work) is pure: it mapstypetoFcmAction.StartService/EnqueueRenewal/Ignore.WakeDispatcher(in:work, extracted in #463) executes the action. Before acting it callsProviderRegistry.awaitReadyBlocking(2s)(#414) and drops the push if the registry never becomes ready or if no integration is enabled — avoiding pointless foreground-service starts and ACME quota burn.wakestartsTunnelForegroundService;renewenqueuesCertRenewalWorkerwithExistingWorkPolicy.KEEP; unknown types are logged and ignored.FcmReceiver, theFirebaseMessagingServicesubclass, lives inapp/src/google/java/com/rousecontext/app/push/FcmReceiver.kt, not:work— so the shared:workmodule links nofirebase-messaging(#476). ItsonNewToken()callsFcmTokenRegistrar(still in:work) to update the relay.- The
fossflavor'sUnifiedPushReceiver(app/src/foss/java/com/rousecontext/app/push/) routes the identical relay payloads through the sameWakeDispatcher.
WorkManager workers
CertRenewalWorker— periodic, daily, network-constrained. Reads cert expiry fromCertificateStore; if the cert is expired or withinDEFAULT_RENEWAL_WINDOW_DAYS(21) of expiry, callsPOST /renew(mTLS while the cert is still valid, credential-signed once it has expired). Schedules backoff on failure; honorsretry_afteronrate_limited.SecurityCheckWorker— periodic self-check against the device's own cert and crt.sh. Persists results viaSecurityCheckPreferences. Triggered bySecurityCheckScheduler.
Wakelock and reconnect logic
WakelockManagerobservesTunnelClient.state: CONNECTING and ACTIVE acquirePARTIAL_WAKE_LOCK; DISCONNECTING and DISCONNECTED release it immediately; CONNECTED schedules a release after a 3 s grace (CONNECTED_GRACE_MS), cancelled if ACTIVE arrives first — so the relay's first request frame after the handshake is not deferred by Doze on non-exempt devices. Idempotent: never double-acquires or double-releases.WakeReconnectDeciderdecides whether awakeFCM should reconnect immediately or be treated as spurious (SpuriousWakeRecorderkeeps the rolling history).
Integration secret synchronization
IntegrationSecretsSynchronizer keeps the device's stored integration secrets in sync with the relay's view. Run on connect and after rotate-secret events so a freshly-rotated integration secret on one device propagates without a full re-register.
Idle timeout
IdleTimeoutManager (work/src/main/kotlin/com/rousecontext/work/IdleTimeoutManager.kt) arms the timer on each entry into CONNECTED and cancels it on each transition to ACTIVE. On expiry it invokes an injected onTimeout, which AppModule wires to gracefulTunnelShutdown(mcpSession, tunnelClient) rather than a bare disconnect().
The duration is adaptive: a substantive wake cycle (one that issued at least one tools/call, tracked by SessionActivityTracker) gets the user-facing "Idle timeout" (idle_timeout_minutes, default 5); a discovery-only or spurious wake gets the much shorter "Quick disconnect" (quick_disconnect_seconds, default 30), so a lightweight wake does not hold the foreground service up and burn the Android 15 dataSync budget. The "disable timeout" toggle makes timeoutProvider return null so the timer never arms; it is gated on the device being battery-optimization-exempt. Completed wake cycles are reported to SpuriousWakeRecorder.
Audit & Notifications (:notifications)
Audit persistence
Room database, schema in notifications/src/main/.../audit/. RoomAuditListener implements AuditListener from :core:mcp so every tool call/response surfaces with timestamps, arguments JSON, result JSON, duration, session ID, and provider ID; :work's SessionActivityAuditListener wraps it so each tools/call also marks the wake cycle substantive for the adaptive idle timeout. There is no automatic retention window: AuditDao.deleteOlderThan exists but the only production caller is the user-driven "clear history" action in AuditHistoryViewModel, and the UI says so ("Audit history is kept until you clear it manually").
Notification taps deep-link into AUDIT?provider={id}&scrollToCallId={id} so the user lands on the specific call.
Notification channels
Eight channels, all created by NotificationChannels.createAll() (notifications/src/main/.../NotificationChannels.kt):
| Channel id | Name | Importance |
|---|---|---|
rouse_foreground |
Foreground Service | LOW |
rouse_session |
Session Activity | DEFAULT |
rouse_error |
Errors | HIGH |
rouse_alert |
Security Alerts | HIGH |
rouse_auth_request |
Authorization Requests | DEFAULT |
rouse_session_summary |
Session Summaries | LOW |
rouse_outreach_launch |
Outreach Launch Requests | DEFAULT |
rouse_fgs_limit |
Foreground Service Limit | HIGH |
The Session Summaries channel is the one controlled by the post_session_mode setting (summary / each_usage / suppress).
Foreground notification builder
ForegroundNotifier.build(context, message: String): Notification — an object in :notifications, called directly by TunnelForegroundService rather than injected through Koin, because a foreground-service notification must be returned to startForeground() rather than posted. TunnelForegroundService maps TunnelState to the message string itself. Always posted while the service is running, regardless of post_session_mode.
Cross-Cutting Concerns
Koin DI
The Koin graph is assembled in app/src/main/java/com/rousecontext/app/di/AppModule.kt. The four McpIntegration instances are registered as named singles and aggregated into a List<McpIntegration>:
single<McpIntegration>(named("health")) { HealthConnectIntegration(androidContext()) }
single<McpIntegration>(named("outreach")) { OutreachIntegration(...) }
single<McpIntegration>(named("notifications")) { NotificationIntegration(...) }
single<McpIntegration>(named("usage")) { UsageIntegration(androidContext()) }
single<List<McpIntegration>> {
buildList {
add(get(named("notifications")))
add(get(named("outreach")))
add(get(named("usage")))
add(get(named("health")))
getKoin().getOrNull<McpIntegration>(named("test"))?.let { add(it) }
}
}
Other key bindings:
single<TunnelClient> { ... }— the singleton consumed by:work.single<CertificateStore> { ... }— file/Keystore-backed.single<TokenStore> { ... }— Room-backed.single<IntegrationStateStore> { DataStoreIntegrationStateStore(...) }.single<NotificationSettingsProvider> { DataStoreNotificationSettingsProvider(...) }.single<AuditListener> { ... }—SessionActivityAuditListenerwrapping the Room-backedRoomAuditListener; consumed by:core:mcp.single<TlsCertProvider> { CertStoreTlsCertProvider(...) },single<McpSessionFactory> { SharedMcpSessionFactory(...) }, andsingle<SessionHandler> { SessionHandler(certProvider, mcpSessionFactory) }— the:core:bridgetrio the foreground service consumes.SessionHandleris the whole bridge wiring::core:bridgeexposes no collector to bind, because the only collector lives inTunnelForegroundService(see the Architecture Overview and #671).single { IdleTimeoutManager(...) },single { WakelockManager(...) },single<SpuriousWakeRecorder> { ... }— the:workcollaborators the service injects.
Integration state machine
Derived from IntegrationStateStore and TokenStore:
- Available —
!userEnabled, never set up. Shows in Add picker. - Disabled —
!userEnabled, previously set up. Shows in Add picker. - Pending —
userEnabled, no tokens. Shown on dashboard. - Active —
userEnabled, ≥1 token. Shown on dashboard. - Unavailable —
!isAvailable(). Greyed out.
Transitions:
Available ──[setup]────────▶ Pending ──[client authorizes]──▶ Active
Disabled ──[re-enable]────▶ Pending ──[client authorizes]──▶ Active
Active ──[user disable]─▶ Disabled
Pending ──[user disable]─▶ Disabled
Active ──[tokens revoked]▶ Pending
Setup flow (post-onboarding)
- Add picker → user taps an available integration.
- App navigates to
INTEGRATION_SETUP/{id}(cert/wiring spinner; cert provisioning already ran during onboarding, so this is mostly an integration-specific bootstrap). - App navigates to the integration-specific setup destination (
HEALTH_CONNECT_SETUP,NOTIFICATION_SETUP,OUTREACH_SETUP, orUSAGE_SETUP) withmode = Setup. - On complete:
IntegrationStateStore.setUserEnabled(id, true), thenINTEGRATION_ENABLED/{id}shows the URL and waits for the first client. - On cancel: back to Add picker.
Battery optimization
BatteryOptimization.isExempt() wraps PowerManager.isIgnoringBatteryOptimizations(). When the device is not exempt and the active delivery transport needs wake-ups, MainDashboardViewModel surfaces a BatteryOptimizationBanner card on Home that deep-links to the system dialog; Settings carries the same action as a row. The banner is derived from live state on every emission — it has no dismissed flag persisted anywhere, and there is no OEM-specific (Samsung/Xiaomi/Huawei) guidance in the app.
Security monitoring (Settings → Trust Status)
- Self-check timestamp + result (verified / warning / alert).
- CT-log (crt.sh) check timestamp + result.
- Truncated SHA-256 cert fingerprint (tap to expand).
- Overall status: green / amber / red.
Warning (amber) is non-blocking: shows "Unable to verify certificate — will retry." Alert (red) blocks new MCP sessions until acknowledged and offers View details + Rotate address actions.
Persistence: SecurityCheckPreferences in :work (DataStore-backed).
Settings (DataStore)
Keys as they appear in the *PreferencesKey declarations, grouped by owning store.
AppStatePreferences (:app):
idle_timeout_minutes: Int(default 5)idle_timeout_disabled: Boolean(battery-opt-exempt only)quick_disconnect_seconds: Int(default 30 — the non-substantive wake timeout)ignore_daily_time_limit: Booleansecurity_check_interval_hours: Inthas_launched_before: Boolean
DataStoreNotificationSettingsProvider (:app):
post_session_mode: String(summary|each_usage|suppress)show_all_mcp_messages: Boolean
SecurityCheckPreferences (:work):
last_check_time: Longself_cert_result: String/ct_log_result: Stringcert_fingerprint: String- per-source
warning_streak_*,notified_for_streak_*,last_streak_increment_*
Also ThemePreference (theme_mode), CertRenewalPreferences (last_attempt_time, last_outcome), SpuriousWakePreferences (total_wake_count, spurious_wake_count_total, spurious_wake_timestamps), and per-integration keys under IntegrationSettingsStore / DataStoreIntegrationStateStore.
Subdomain rotation
In Settings: "Generate new address" button. Confirmation warns "All connected clients will lose access. Once per 30 days." On confirm, POST /register with force_new: true; old subdomain invalidated, all tokens revoked, certs re-provisioned, UI updates.
ACME rate-limit UX
When the relay returns rate_limited for cert issuance: notification "Certificate issuance delayed. Will retry on [date].", onboarding shows the same retry date, CertRenewalWorker schedules retry honoring retry_after.
Still Needs Design
- Third-party provider discovery — bound-service intent filter, verification, trust UI. Not v1.