Repository navigation
fix(desktop): defer native PTT evidence attach until its journal row exists - #13235
Conversation
…exists OCR can finish before the streaming journal inserts the user row. The direct UPDATE then failed with a missing-row RPC and dropped native evidence (#13205). Co-authored-by: Cursor <[email protected]>
There was a problem hiding this comment.
3 issues found across 6 files
Confidence score: 2/5
RealtimeHubController+PushToTalk.swiftcan issue the deferred attach UPDATE before the streaming record because the direct UPDATE is not included in the streaming tail, creating an ordering race; serialize the attach through the streaming ledger so it runs after the record.RealtimeHubController+PushToTalk.swiftmay treat a populatedjournalUserTurnIDas evidence that a row exists even when streaming admission was rejected, allowing an UPDATE before fallback admission creates the record; only permit the update after confirmed record creation.RealtimeTurnEvidenceTests.swiftmutates process-globalUserDefaults.standardand restores it withdefer, so concurrent tests or production paths using.authUserIdcould observe the temporary owner; isolate the test setting from global defaults.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+PushToTalk.swift">
<violation number="1" location="desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+PushToTalk.swift:81">
P2: When streaming admission is rejected, `journalUserTurnID` may already be populated even though its record task never created a row. This guard then allows a direct evidence UPDATE before the fallback admission runs. Track record acceptance separately and bind the producing ID only after admission succeeds, or keep the evidence deferred until that result is known.</violation>
<violation number="2" location="desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+PushToTalk.swift:91">
P1: When streaming starts after the second `contains` check, this direct UPDATE is still in flight and is not part of the streaming tail. Serialize the deferred attach with the streaming ledger so it runs after the record task, rather than sampling the ledger only before the await.</violation>
</file>
<file name="desktop/macos/Desktop/Tests/RealtimeTurnEvidenceTests.swift">
<violation number="1" location="desktop/macos/Desktop/Tests/RealtimeTurnEvidenceTests.swift:653">
P3: This helper mutates the process-global `UserDefaults.standard` `.authUserId` key to install a test owner, restoring it only via `defer`. Because `currentOwnerId()` and many production paths (`AuthService`, `AuthOwnerTransition`) read that same shared key, the synthetic owner leaks to any concurrently running test in the same process during the async `body`, and stays persisted if the process dies mid-test. Use the dedicated test hook `RuntimeOwnerIdentity.applyAutomationOwnerOverride` instead, which the codebase already documents for injecting a synthetic owner without touching real auth state.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| self.enqueueNativeEvidenceUpdate(continuityKey: continuityKey, evidence: current) | ||
| return | ||
| } | ||
| let accepted = await self.attachResolvedNativeUserEvidence( |
There was a problem hiding this comment.
P1: When streaming starts after the second contains check, this direct UPDATE is still in flight and is not part of the streaming tail. Serialize the deferred attach with the streaming ledger so it runs after the record task, rather than sampling the ledger only before the await.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+PushToTalk.swift, line 91:
<comment>When streaming starts after the second `contains` check, this direct UPDATE is still in flight and is not part of the streaming tail. Serialize the deferred attach with the streaming ledger so it runs after the record task, rather than sampling the ledger only before the await.</comment>
<file context>
@@ -67,20 +74,21 @@ extension RealtimeHubController {
+ self.enqueueNativeEvidenceUpdate(continuityKey: continuityKey, evidence: current)
+ return
+ }
+ let accepted = await self.attachResolvedNativeUserEvidence(
surface: surface,
ownerID: ownerID,
</file context>
| enqueueNativeEvidenceUpdate(continuityKey: continuityKey, evidence: evidence) | ||
| return | ||
| } | ||
| guard let surface = entry.surface, let userTurnID = entry.journalUserTurnID else { return } |
There was a problem hiding this comment.
P2: When streaming admission is rejected, journalUserTurnID may already be populated even though its record task never created a row. This guard then allows a direct evidence UPDATE before the fallback admission runs. Track record acceptance separately and bind the producing ID only after admission succeeds, or keep the evidence deferred until that result is known.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+PushToTalk.swift, line 81:
<comment>When streaming admission is rejected, `journalUserTurnID` may already be populated even though its record task never created a row. This guard then allows a direct evidence UPDATE before the fallback admission runs. Track record acceptance separately and bind the producing ID only after admission succeeds, or keep the evidence deferred until that result is known.</comment>
<file context>
@@ -67,20 +74,21 @@ extension RealtimeHubController {
enqueueNativeEvidenceUpdate(continuityKey: continuityKey, evidence: evidence)
return
}
+ guard let surface = entry.surface, let userTurnID = entry.journalUserTurnID else { return }
Task { @MainActor [weak self] in
guard let self,
</file context>
| let defaults = UserDefaults.standard | ||
| let previous = defaults.string(forKey: .authUserId) | ||
| let ownerID = "evidence-attach-\(UUID().uuidString.lowercased())" | ||
| defaults.set(ownerID, forKey: .authUserId) |
There was a problem hiding this comment.
P3: This helper mutates the process-global UserDefaults.standard .authUserId key to install a test owner, restoring it only via defer. Because currentOwnerId() and many production paths (AuthService, AuthOwnerTransition) read that same shared key, the synthetic owner leaks to any concurrently running test in the same process during the async body, and stays persisted if the process dies mid-test. Use the dedicated test hook RuntimeOwnerIdentity.applyAutomationOwnerOverride instead, which the codebase already documents for injecting a synthetic owner without touching real auth state.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At desktop/macos/Desktop/Tests/RealtimeTurnEvidenceTests.swift, line 653:
<comment>This helper mutates the process-global `UserDefaults.standard` `.authUserId` key to install a test owner, restoring it only via `defer`. Because `currentOwnerId()` and many production paths (`AuthService`, `AuthOwnerTransition`) read that same shared key, the synthetic owner leaks to any concurrently running test in the same process during the async `body`, and stays persisted if the process dies mid-test. Use the dedicated test hook `RuntimeOwnerIdentity.applyAutomationOwnerOverride` instead, which the codebase already documents for injecting a synthetic owner without touching real auth state.</comment>
<file context>
@@ -511,6 +511,157 @@ final class RealtimeTurnEvidenceTests: XCTestCase {
+ let defaults = UserDefaults.standard
+ let previous = defaults.string(forKey: .authUserId)
+ let ownerID = "evidence-attach-\(UUID().uuidString.lowercased())"
+ defaults.set(ownerID, forKey: .authUserId)
+ defer {
+ if let previous {
</file context>
|
Correction on the Test-only fix: #13260. Nothing in this PR touches owner or defaults state; once #13260 lands, a rebase or rerun of this job should be green. |
…-mode test build compiles CI's 'Test UserNotifications callback regression in release mode' step runs swift test -c release, which compiles the whole test target. Three tests here assign RealtimeTurnEvidenceLedger.testingAttachRealtimeUserEvidence, a seam declared under #if DEBUG (since #13235), so the release build fails on any PR that touches desktop tests. Main stayed green only because the step is skipped on pushes that do not touch Desktop/Tests. Wrap the three tests in #if DEBUG to match the seam. Tracked as #13458. Verification: xcrun swift build --build-tests --package-path desktop/macos/Desktop (Build complete); release-mode step run locally before push. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…-mode test build compiles (#13467) CI's 'Test UserNotifications callback regression in release mode' step runs swift test -c release, which compiles the whole test target. Three tests here assign RealtimeTurnEvidenceLedger.testingAttachRealtimeUserEvidence, a seam declared under #if DEBUG (since #13235), so the release build fails on any PR that touches desktop tests. Main stayed green only because the step is skipped on pushes that do not touch Desktop/Tests. Wrap the three tests in #if DEBUG to match the seam. Tracked as #13458. Audit of Desktop/Sources #if DEBUG seams vs Desktop/Tests: 75 DEBUG-only regions in 32 source files. Distinctive DEBUG-only types and testing*/ *ForTesting APIs are already #if DEBUG-guarded in Tests (or are string-literal source-inspection, which still compiles). No further unguarded member accesses. Verification (cwd desktop/macos, Xcode 26.6 / Swift 6.3.3): xcrun swift build --build-tests --package-path Desktop Build complete! (268.91s) xcrun swift test --package-path Desktop --filter RealtimeTurnEvidenceTests/ Executed 26 tests, with 0 failures (including the three DEBUG-guarded attach tests: testOCRBeforeJournalRowDoesNotAttachAndLaterAdmissionCarriesEvidence, testOCRAfterBoundUserRowAndFinalizedStreamingAttachesOnce, testOCRWhileStreamingLedgerContainsKeyWaitsForRecordTask) xcrun swift test -c release --package-path Desktop --filter UserNotificationCallbackBridgeTests/ Build complete! (491.09s) Executed 11 tests, with 0 failures (0 unexpected) Co-authored-by: Claude Fable 5.1 <[email protected]> Co-authored-by: Cursor <[email protected]>
What changed and why
Fixes #13205. Native PTT OCR could finish before the streaming journal inserted the user row;
resolveNativeTurnEvidencethen synthesized a stable turn ID and ran an UPDATE-only attach, which failed with a missing-row RPC, loggedjournal_evidence_append_failed, and marked the ledgerpersistenceFailed. Direct attach now runs only whenjournalUserTurnIDis already bound; otherwise the resolved evidence stays on the ledger for the projection constructor / post-admission persist / producing-row bind. If streaming starts while the deferred attach is in flight, the write joins the ordered tail. Acceptedspawn_agentreceipts now bind the producing user row immediately, because that path creates the kernel row without the streaming constructor.Product invariants affected
How it was verified
Swift unit tests only. Installed binaries were not rebuilt or live-tested (restored authenticated QA sessions took precedence). Pre-fix live evidence is in issue #13205.
93 tests, 0 failures (RealtimeTurnEvidenceTests 26/26 including 3 new admission tests; RealtimeHubBargeInContinuityTests 67/67).
Passed (focused Swift lifecycle/state tests, 33s).
OK.
Tests
RealtimeTurnEvidenceTestsnow drivesresolveNativeTurnEvidencethrough an injectable attach seam:persistenceFailed == false, laterpersistNativeEvidenceAfterJournalAdmissioncarries it.journalUserTurnIDis bound and streaming has finalized → direct attach exactly once.Failure class (fixes)
Failure-Class: none
Scoped cleanups (optional)
Caller audit of
voice:<uuid>user-row creation:attachJournalUserTurn.recordExchangeincludes evidence, binds the row, thenpersistNativeEvidenceAfterJournalAdmission.bindNativeTurnEvidenceToProducingRow.bindNativeTurnEvidenceToProducingRowthere.persistNativeEvidenceAfterJournalAdmissionalso bindsjournalUserTurnIDwhile OCR is still pending so a later resolve can attach.Line-Count-Exception: desktop/macos/Desktop/Sources/FloatingControlBar/RealtimeHubController+SessionDelegate.swift | 1588 -> 1592 | Bind native PTT evidence to the spawn-admitted user row.