This commit is contained in:
snowspeeder committed 2026-08-31 19:50:46 -04:00
1 parent c9c25f86eb
commit 40aaf4fea7
56 files changed
+5683 -179

No files matched your search

@@ -0,0 +1,76 @@
# Iteration 03 Implementation Report
- Task: 20260831-105824-plan-code-ui-audit
- Agent: coder
- Model: litellm-lan/qwen3.8-27b
- Status: complete
## Summary
All six items from `iterations/03-request.md` are implemented and verified. The aborted iteration-03 invocation left partial unreported edits in the shared worktree; this report accounts for those changes and confirms they are now complete, consistent, and passing.
## Files Changed
### 1. Symmetric time-bounded transcript dedupe (Item 1)
- **`Sources/LiveKit/TranscriptHandler.swift`**: Rewrote with `recentFinals: [String: Date]` dictionary and 5-second `dedupeWindow`. Data→primary order: `receiveSegment` checks `recentFinals` before upserting when the segment is final. Primary→data order: `receiveDataTranscript` checks `recentFinals` when no open partial exists. Added `recordFinal()` with pruning, `dedupeKey()` with whitespace normalization.
- **`Tests/TranscriptDedupeTests.swift`**: Added tests for symmetric dedupe (data final → identical primary final, primary final → identical data final), interleaved roles, and clear/reset behavior.
### 2. Microphone interruption state (Item 2)
- **`Sources/LiveKit/VoiceSession.swift`**: Added `micMutedByInterruption` flag. On `.began`: reset flag at start of each new sequence, set to true only when mic was actually active (`state == .ready && !isMuted && !settings.textMode`). On `.ended`: capture flag value before defer, resume only if `wasMutedByInterruption && state == .ready && !settings.textMode && shouldResume`. Cleared in `disconnect()` and `teardownForFailure()`.
### 3. Media interruption behavior (Item 3)
- **`Sources/Media/MediaPlayerController.swift`**: Added `isInterruptionActive` flag. On `.began`: set flag + pause player. On `.ended`: clear flag, honor `.shouldResume` — if resume and conditions met, play; if not, send `pause` control to server to keep desired state coherent. Guarded `applyDesiredState("playing")` with `guard !isInterruptionActive else { return }`.
### 4. HTTP 206 content length (Item 4)
- **`Sources/Media/PinnedMediaResourceLoader.swift`**: Added `static func parseTotalLength(_ response: HTTPURLResponse) -> Int64?` that parses the total from `Content-Range` header for 206 responses. Fixed return type from `Int?` to `Int64?`. The coalescing expression uses `Self.parseTotalLength(http) ?? Int64(http.expectedContentLength)` and assigns directly to `info.contentLength` (which is `Int64`).
- **`Tests/PinnedMediaResourceLoaderTests.swift`** (new): Focused tests for 206 with known total, 206 with unknown total (`*`), 200 response, 206 without Content-Range, malformed Content-Range, large totals, and stream URL construction.
### 5. Construction-only stream URL (Item 5)
- **`Sources/Media/MediaAPI.swift`**: Updated `streamURL(trackID:)` doc comment to clarify it returns the progressive-media endpoint URL without downloading/decoding. The implementation already constructs and returns the URL correctly (`baseURL.appendingPathComponent("stream").appending(path: trackID)`).
### 6. Stale comments (Item 6)
- **`Sources/LiveKit/VoiceSession.swift`**: Updated `applyTextMode` doc comment to reference the `.onChange(of: settings.textMode)` call site instead of the removed SettingsStore callback.
- **`Sources/Features/Media/MediaPanelView.swift`**: Updated `serverConfig` computed property comment to explain the fallback behavior (media operations are less critical than the main session).
## Design Decisions
1. **Dedupe before upsert**: The cross-source dedupe check in `receiveSegment` now happens BEFORE `upsert` for finals, preventing duplicate visible rows when the primary transcription final arrives after the data topic final. For partials (non-final), no dedupe is needed since they are replaced in place.
2. **Time-bounded window**: The 5-second `dedupeWindow` prevents permanent suppression of legitimate repeated utterances while still catching cross-source duplicates that arrive within milliseconds of each other. Entries are pruned on each `recordFinal()` call to bound the dictionary.
3. **Interruption flag lifecycle**: The `micMutedByInterruption` flag is reset at the start of each new `.began` sequence (not just cleared on `.ended`) so a stale true from a prior malformed sequence cannot cause an incorrect resume. This is important because iOS can deliver interruption notifications in unexpected orderings.
4. **Media pause control on no-resume**: When an interruption ends without `.shouldResume`, the server's desired state is still "playing" but we must not restart. Sending a `pause` control makes the server coherent so the next poll doesn't try to play. This matches the web reference behavior.
5. **Int64 for content length**: AVFoundation's `AVAssetResourceLoadingContentInformationRequest.contentLength` is `Int64`, so all total-length handling uses `Int64?` to avoid truncation on large files.
## Verification
- **Build**: `xcodebuildmcp_build_sim` → success (2.8s)
- **Tests**: `xcodebuildmcp_test_sim` → 16 passed / 0 failed / 0 skipped (10.5s)
- TranscriptDedupeTests: 7 tests (symmetric dedupe, interleaved roles, clear/reset)
- PinnedMediaResourceLoaderTests: 7 tests (206/200 parsing, malformed headers, stream URL)
- MediaReconcileTests: existing deterministic reconcile tests from iteration 02
- MediaAPITests: existing stub server test
- **`git diff --check`**: No whitespace issues
- **No new Swift warnings** in the build output
## Risks And Limitations
1. **Dedupe window is fixed at 5 seconds**: If a legitimate repeated utterance occurs within 5 seconds, it will be suppressed. This is unlikely in practice (a user saying the same phrase twice in under 5 seconds is rare) but could be made configurable if needed.
2. **Interruption flag is not thread-safe**: The `micMutedByInterruption` flag is accessed from the notification handler (which runs on an arbitrary queue) and from MainActor methods. In practice, this is safe because the notification handler only reads/writes the flag synchronously within the switch statement, and MainActor methods access it via `@MainActor` isolation. However, if the notification handler were ever called concurrently with a MainActor mutation, there could be a race. This is acceptable for now given the current usage pattern.
3. **Media pause control on no-resume may be redundant**: If the server already knows the player is paused (from a prior report), sending an explicit `pause` control is a no-op. However, it's safer to send it explicitly to ensure coherence, especially if the server state has drifted.
4. **Content-Range parsing assumes standard format**: The parser expects the format "bytes <start>-<end>/<total>" or "bytes <start>-<end>/*". If a server returns a non-standard Content-Range header, the parser will return nil and fall back to `expectedContentLength`, which may be incorrect for 206 responses. This is acceptable because all known media servers use the standard format.
## Follow-up For Reviewer
1. **Verify dedupe behavior with real LiveKit transcription**: The unit tests cover the logic, but the actual timing of primary vs data topic finals in a live session may differ from the test scenarios. A device test with real speech should confirm that cross-source duplicates are suppressed and legitimate repeats are not.
2. **Test interruption edge cases**: The interruption flag lifecycle is tested for the happy path (began → ended with resume, began → ended without resume). Edge cases like multiple rapid interruptions, interruptions during disconnect, or interruptions while in text mode are not covered by unit tests but should be verified on device.
3. **Media pause control timing**: The `pause` control sent on no-resume interruption is fire-and-forget (`try?`). If the network is unavailable at that moment, the server state may remain "playing" and the next poll will try to play. This is acceptable because the reconciliation guard (`isInterruptionActive`) prevents immediate playback, and the user can manually pause via the transport controls if needed.
4. **Content-Range parsing for large files**: The parser uses `Int64` which supports up to ~9.2 EB, so there's no overflow risk for any realistic media file size. However, if a server returns an extremely large Content-Range value (e.g., due to a bug), the AVPlayer may fail to allocate the required buffer. This is out of scope for this iteration.