rouse-context/app
Jason Monk 3071bf787f
Fix #419: Apply awaitReady pattern to securityAlertCheck + OutreachIntegration + DeviceRegistrationStatus (#421)
Three sibling instances of the cold-start async-state-not-loaded-yet race
that #416 fixed for ProviderRegistry (issue #414). Each used the same
shape: async state seeded into a StateFlow with a default value, read
synchronously by callers that fire during the cold-start window between
process spawn and the first DataStore emission.

Finding #1 (HIGH) - securityAlertCheck

The McpSession `securityAlertCheck` lambda was `() -> Boolean` reading
`alertFlag.value` from a `combine().stateIn(..., initialValue = false)`
flow. On cold-start FCM wake the very first request arrived before the
flow had emitted, so a device whose security check reported `alert`
answered the request anyway. Subsequent requests refused correctly.

Fix: introduce SecurityAlertGate (mirrors ProviderRegistry's awaitReady
shape) that suspends until the underlying combined flow has emitted
once, then exposes `suspend fun isAlerting()`. McpRouting's
`securityAlertCheck` lambda type changes to `suspend () -> Boolean`;
existing test lambdas (`{ true }` / `{ false }`) auto-cast.

Finding #2 (MEDIUM) - OutreachIntegration._directLaunchEnabled

`canLaunchDirectly` was a `() -> Boolean` reading
`_directLaunchEnabled.value` from a MutableStateFlow seeded by an
`appScope.launch{}` collector. First tool call after cold-start could
mis-route through the notification fallback even when the user had
opted into direct launch.

Fix: convert `canLaunchDirectly` to `suspend () -> Boolean` through the
chain (LaunchAppTool, OpenLinkTool, launchActivitySafely). Add
`awaitReady` / `awaitReadyBlocking` to OutreachIntegration; the lambda
becomes `{ isDirectLaunchAllowed() }` which suspends on awaitReady
before reading the flow value.

Finding #3 (MEDIUM, latent) - DeviceRegistrationStatus

Constructed with `initiallyRegistered = false` and an
`appScope.launch{ if (certStore.getSubdomain() != null) markComplete() }`.
Currently safe only because IntegrationSetupViewModel.awaitRegistrationIfNeeded
(issue #242) consults the cert store directly; future readers that add
`.complete.value` without copying the workaround re-introduce the bug.

Fix: add `DeviceRegistrationStatus.create(scope, suspend isRegistered)`
factory that runs the check in `scope` and signals `awaitReady` once it
resolves. AppModule's Koin binding switches to `create()`. The
synchronous constructor is preserved for tests; both code paths signal
readiness eagerly when the answer is known up front.

Per #419 hard rules:
- Workaround in IntegrationSetupViewModel.awaitRegistrationIfNeeded
  left in place; no UX-flow files touched.
- No runBlocking added to production code.

Tests:
- SecurityAlertGateTest (new): slow-flow regression + idempotent
  awaitReady + empty-first-emission ready signal.
- DeviceRegistrationStatusTest (extended): create() defers awaitReady
  until check resolves; both true/false verdicts unblock.
- OutreachIntegrationTest (extended): mockk-based slow settings store
  proves awaitReady waits for first emission.
- HttpRoutingTest (extended): suspend-awaiting securityAlertCheck still
  rejects with 503.

Verification:
- `./gradlew :app:testDebugUnitTest :work:testDebugUnitTest :core:mcp:jvmTest :integrations:testDebugUnitTest` all green.
- `./gradlew ktlintCheck detekt` clean.

Co-authored-by: Claude Opus 4 (1M context) <noreply@anthropic.com>
2026-04-26 14:18:17 -04:00
..
screenshots Fix #383: ToolCallRow multi-line padding + top-align time + never render ciphertext fallback (#384) 2026-04-23 16:04:40 -04:00
src Fix #419: Apply awaitReady pattern to securityAlertCheck + OutreachIntegration + DeviceRegistrationStatus (#421) 2026-04-26 14:18:17 -04:00
build.gradle.kts Fix #277: Integration test for CertRenewalWorker scheduling (#282) 2026-04-19 11:40:43 -04:00
google-services.json Add Firebase: google-services.json, Google Services plugin, debug applicationIdSuffix 2026-04-05 15:38:40 -04:00
proguard-rules.pro Add ProGuard rules for Ktor JMX references 2026-04-11 15:07:22 -04:00