Skip to content

Commit d975738

Browse files
committed
fix(talk): preserve replacement speech when an old utterance is canceled
Revalidate the speech operation token before delayed cancellation cleanup stops the shared Apple synthesizer. Preserve active cancellation and explicit stop, and retain an opt-in real-audio regression for the replacement race. Fixes #130674.
1 parent 73b98c4 commit d975738

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎apps/shared/OpenClawKit/Sources/OpenClawKit/TalkSystemSpeechSynthesizer.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public final class TalkSystemSpeechSynthesizer: NSObject {
7474
}
7575
}, onCancel: {
7676
Task { @MainActor in
77+
// A replacement may start before this actor hop runs. Cancellation
78+
// must stop only the utterance owned by the canceled call.
79+
guard self.currentToken == token else { return }
7780
self.stop()
7881
}
7982
})

‎apps/shared/OpenClawKit/Tests/OpenClawKitTests/TalkSystemSpeechSynthesizerTests.swift‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,44 @@ import XCTest
33

44
@MainActor
55
final class TalkSystemSpeechSynthesizerTests: XCTestCase {
6+
func testLiveOldCancellationDoesNotStopReplacement() async throws {
7+
try XCTSkipUnless(
8+
ProcessInfo.processInfo.environment["OPENCLAW_LIVE_TEST"] == "1",
9+
"Requires working native speech/audio services; run with OPENCLAW_LIVE_TEST=1.")
10+
let speaker = TalkSystemSpeechSynthesizer.shared
11+
defer { speaker.stop() }
12+
let first = await self.startSpeech()
13+
defer { first.cancel() }
14+
15+
first.cancel()
16+
var replacementStarts = 0
17+
try await speaker.speak(
18+
text: "Replacement speech.", language: "en-US",
19+
onStart: { replacementStarts += 1 })
20+
21+
XCTAssertEqual(replacementStarts, 1)
22+
try await self.assertCanceled(first)
23+
}
24+
25+
private func startSpeech() async -> Task<Void, Error> {
26+
let started = self.expectation(description: "utterance started")
27+
let speech = Task { @MainActor in
28+
try await TalkSystemSpeechSynthesizer.shared.speak(
29+
text: String(repeating: "This utterance will be interrupted. ", count: 20),
30+
language: "en-US",
31+
onStart: { started.fulfill() })
32+
}
33+
await self.fulfillment(of: [started], timeout: 10)
34+
return speech
35+
}
36+
37+
private func assertCanceled(_ speech: Task<Void, Error>) async throws {
38+
do {
39+
try await speech.value
40+
XCTFail("Interrupted speech completed successfully")
41+
} catch TalkSystemSpeechSynthesizer.SpeakError.canceled {}
42+
}
43+
644
func testWatchdogTimeoutDefaultsToLatinProfile() {
745
let timeout = TalkSystemSpeechSynthesizer.watchdogTimeoutSeconds(
846
text: String(repeating: "a", count: 100),
@@ -37,7 +75,7 @@ final class TalkSystemSpeechSynthesizerTests: XCTestCase {
3775

3876
func testWatchdogTimeoutClampsVeryLongUtterances() {
3977
let timeout = TalkSystemSpeechSynthesizer.watchdogTimeoutSeconds(
40-
text: String(repeating: "a", count: 10_000),
78+
text: String(repeating: "a", count: 10000),
4179
language: "en-US")
4280

4381
XCTAssertEqual(timeout, 900.0, accuracy: 0.001)

0 commit comments

Comments
 (0)