Skip to content

harden(app): route capture timers, streams and listeners through CaptureLifetime - #14375

Closed
Git-on-my-level wants to merge 6 commits into
task/msd-core-c1-compositionfrom
task/msd-core-c1-disposal
Closed

Git-on-my-level wants to merge 6 commits into
task/msd-core-c1-compositionfrom
task/msd-core-c1-disposal

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What changed and why

C1 cut (3): CaptureController now owns a CaptureLifetime wrapping the injected scheduler, so every _scheduling.once / periodic, the constructor connectivity listen, BLE finalized/stream subscriptions, and CaptureProvider's live-segment addListener cancel when dispose runs. The first commit restores the two why-comments removed to hold 2,710 lines (mute restore; custom-STT buffering UI) and pays for them with BleBridgeCaptureListeners. Production no-argument defaults stay.

This is the first C1 cut that changes behavior on purpose: the 15s voice-command timeout used to survive dispose and could still fire. It no longer does. Evidence class: 05c43c014d (late timer after teardown), 348d5f31dc (_bleButtonStream leak — already cancelled in dispose; now bag-owned so it cannot regress).

Stacked on the composition tip (task/msd-core-c1-composition / forthcoming PR stacked on #14361). origin/main does not yet contain #14361. capture_controller.dart shrinks 2710 → 2698.

No spine markers retired: remaining C1 contracts are generation/join, adoption, FGS, persistence — not this dispose drain.

Resource inventory (capture_controller.dart / capture_provider.dart)

Resource Lines Cancelled on dispose before? This PR
_keepAliveTimer (_scheduling.periodic 15s) field 107; create 1997; pause-cancel 2680 yes bag (_scheduling is the lifetime)
_inProgressConversationRefreshTimer field 112; create 2116 yes bag
_autoSyncFallbackTimer (30s ConversationEvent fallback) field 466; create 2252 yes bag — class of 05c43c014d
_voiceCommandTimeoutTimer (15s) field 518; create 966 no bag — the leak this cut closes
connectivity _connectivity.changes.listen ctor 245 yes lifetime.listen
BLE addBatchRecordingFinalizedListener ctor 246–252 yes (remove in dispose) lifetime.own
_bleBytesStream field 509; adopt 1165 yes lifetime.own + takeSubscription
_blePhotoStream field 510; adopt 1491 yes same
_bleButtonStream field 514; adopt 986 yes (348d5f31dc) same
_storageStream was dead, never assigned n/a removed
CaptureProvider.addListener(_persistLiveSegments) provider 28–29 yes (removeListener in dispose) lifetime.own; provider dispose override deleted
CaptureMetricsTracker Timer.periodic 5s capture_metrics_tracker.dart:124 yes via _metrics.dispose() left — uses dart:async Timer directly, not CaptureScheduling. Still cancelled in dispose. Migrating it would pull the metrics helper onto capture's clock; listed rather than expanded.
_socket?.unsubscribe(this) dispose 1576 yes left — socket-service listener, not a dart:async Timer
Future.delayed(20ms) image chunks now image_utils.dart emitBase64ImageChunks in-flight loop, not retained left — not a Timer held across dispose
_peopleRefreshFuture field 104; nulled on dispose future not cancelled left — async generation, next cut
unawaited(_captureSessionLocation / auth refresh) 1747, 1854, 1926, 1966, 2613 n/a left — generation/join, not lifetime

Product invariants affected

none

How it was verified

Not exercised as a live device recording session.

Commands run:

  • python3 scripts/check_spine_contracts.py — 47 pending markers; 17 protected files. No spine-test edits.
  • python3 scripts/check_mobile_boundaries.py --config app/contracts/capture/boundary-baseline.json — 65 changed paths; 0 errors.
  • TZ=UTC flutter test test/services/capture/capture_disposal_test.dart test/services/capture/capture_lifetime_test.dart test/spine/c1_lifetime_test.dart — 16 passed.
  • TZ=UTC bash app/test.sh — exit 0.
  • make mobile-verify ARGS="fast --paths app/lib/services/capture/capture_controller.dart app/lib/providers/capture_provider.dart" — executed 20, passed 20, failed 0 (full-suite fallback because capture_provider.dart is outside the capture-wal path rule; j5_capture_interruption_reconnect ran and passed).
  • OMI_PR_BODY_FILE=.local/pr-body-c1-disposal.md make preflight — 21 checks passed in 185.65s.

capture_controller.dart touched line ranges (working tree, 2698 lines):

  • 15: drop ble_bridge import (listeners go through BleBridgeCaptureListeners)
  • 27: import capture_lifetime.dart
  • 126–127: lifetime wraps the injected scheduler
  • 241–243: restored mute-restore comment
  • 245–252: lifetime.listen / lifetime.own for connectivity, BLE finalized, BLE streams
  • 519: dead _storageStream removed
  • 966: voice-command timeout still _scheduling.once (now bag-tracked)
  • 986–990: _bleButtonStream via takeSubscription
  • 1165: _bleBytesStream via takeSubscription
  • 1491–1512: _blePhotoStream via takeSubscription; image chunks extracted
  • 1572–1579: dispose — socket unsubscribe + metrics + lifetime.close()
  • 1987–1992: debugArmVoiceCommandTimeout test seam

CaptureConnectivityBoundary.production() remains the no-argument default.

Tests

Builder regressions in app/test/services/capture/capture_disposal_test.dart:

  • dispose cancels the voice-command timeout that old dispose leaked (arm, dispose, elapse 15s, no pending timers) — fails on the old code
  • a timeout armed after dispose never fires
  • dispose drops the connectivity listener so a later event does not mutate isConnected

capture_lifetime_test.dart: close() cancels every owned timer before the caller awaits (needed because dispose is void).

Failure class (fixes)

No fix: commits. This is a harden: adoption of the lifetime bag. The voice-command leak was unreleased C1 code, not a live production bug report.

Failure-Class: none


Part of the mobile self-driving program (App core lane, C1 step 3). Stacked on composition / #14361 / #14327. Implemented by Cursor Grok 4.6. Coordinator: metrics sampler, socket unsubscribe, in-flight Futures, and location/auth unawaited work are listed not migrated.


Part of the mobile self-driving program (App core lane, third C1 PR). Stacked on #14367. Implemented by Cursor cursor-grok-4.6-high. Coordinator verification on the head: spine check passes with no edits to protected tests; the disposal and lifetime tests pass (16); capture_controller.dart shrank from 2,710 to 2,698 lines; the two explanatory comments dropped in #14367 are restored. One genuinely leaked timer is closed here (_voiceCommandTimeoutTimer, 15 s, never cancelled on dispose). Resources deliberately left for later cuts are listed in the inventory above with the reason.

🤖 Generated with Claude Code

Review in cubic

Git-on-my-level and others added 5 commits September 17, 2026 23:57
Those comments record why-knowledge this file breaks without: mute must
survive process death so streamDeviceRecording can re-apply it, and custom
STT buffering is surfaced so the UI shows offline instead of Listening.
Pay for the lines by extracting BleBridgeCaptureListeners.

Co-authored-by: Cursor <[email protected]>
…ureLifetime

Wrap the injected scheduler in CaptureLifetime so once/periodic, connectivity
listen, BLE finalized/stream subscriptions, and the live-segment listener
cancel on dispose. close() now drains every sync cancel before returning so
void dispose() stays synchronous. Extract image-chunk emission so the
controller shrinks.

Co-authored-by: Cursor <[email protected]>
The only CaptureController timer that dispose did not cancel was the
15s voice-command timeout (the class of 05c43c0). After dispose,
advancing fake time leaves no pending timers. Also lock close() to
cancel every owned timer before the caller awaits.

Co-authored-by: Cursor <[email protected]>
Formatting only; required by the pre-push dart format gate.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…posal

Co-authored-by: Cursor <[email protected]>

# Conflicts:
#	app/lib/services/capture/capture_controller.dart
#	app/lib/services/capture/capture_lifetime.dart
#	app/test/services/capture/capture_lifetime_test.dart
@Git-on-my-level
Git-on-my-level deleted the branch task/msd-core-c1-composition September 18, 2026 10:47
@github-actions

Copy link
Copy Markdown
Contributor

Hey @Git-on-my-level 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our product principles and locked invariants

Before your next PR, please skim:

  • PRODUCT.md — product north star
  • Product invariants — locked rules (shared chat, memory tiers, agent control plane, integrations, brand)

If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing.

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community!

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Auto-closed when base branch was deleted after its parent squash-merged. Head unchanged at 2ec51f99d; continued as #14549 targeting main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant