Skip to content

Commit 92b5425

Browse files
committed
fix(mothership): classify a cancel the worker reports as an explicit stop
A turn is cancelled when this node's abort signal fired or when the worker's terminal said cancelled. The worker sends a cancelled terminal only for a Stop, but when that Stop reached it another way (another Sim node, or the worker's own stop handling), live or replayed during recovery, this node's signal never fired. recordCancelled read only the signal, so a user Stop was logged at ERROR as "Stream cancelled with unknown abort reason" with an undefined reason, although finalize correctly reported an explicit stop. recordCancelled now takes AbortReason.WorkerTerminalStop when the signal is still live: that is the only other source of a cancelled verdict. Nothing aborts with it, so the controller, its listeners and the SSE close telemetry are unchanged. The start test's session mock now delegates to the real classifier.
1 parent 695ca94 commit 92b5425

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎apps/sim/lib/mothership/request/lifecycle/start.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
billingAttributionMock,
88
billingAttributionMockFns,
99
} from '@sim/testing/mocks/billing-attribution.mock'
10+
import { getMockLogger } from '@sim/testing/mocks/logger.mock'
1011
import {
1112
mothershipAgentUrlMock,
1213
mothershipAgentUrlMockFns,
@@ -27,6 +28,7 @@ import {
2728
MothershipStreamV1EventType,
2829
} from '@/lib/mothership/generated/mothership-stream-v1'
2930
import { TitleRequest } from '@/lib/mothership/generated/protocol'
31+
import { isExplicitStopReason } from '@/lib/mothership/request/session/abort-reason'
3032
import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
3133

3234
const {
@@ -113,7 +115,7 @@ vi.mock('@/lib/mothership/request/session', () => ({
113115
unregisterActiveStream,
114116
hasStreamViewer,
115117
startAbortPoller: vi.fn().mockReturnValue(setInterval(() => {}, 999999)),
116-
isExplicitStopReason: (reason: unknown) => reason === 'user_stop:abortActiveStream',
118+
isExplicitStopReason: (reason: unknown) => isExplicitStopReason(reason),
117119
SSE_RESPONSE_HEADERS: {},
118120
StreamWriter: class {
119121
attach = vi.fn().mockImplementation((ctrl: ReadableStreamDefaultController) => {
@@ -609,7 +611,9 @@ describe('createSSEStream terminal error handling', () => {
609611
expect(scheduleBufferCleanup).toHaveBeenCalledWith('stream-1')
610612
})
611613

612-
it('publishes a cancelled completion (not an error) when the orchestrator reports cancelled without abortSignal aborted', async () => {
614+
it('publishes a cancelled completion and classifies it as a Stop when the orchestrator reports cancelled without abortSignal aborted', async () => {
615+
// Regression: the worker's cancelled terminal (live or replayed) left this node's signal
616+
// unaborted, so the turn was logged as "Stream cancelled with unknown abort reason".
613617
runCopilotLifecycle.mockResolvedValue({
614618
success: false,
615619
cancelled: true,
@@ -646,6 +650,10 @@ describe('createSSEStream terminal error handling', () => {
646650
}),
647651
})
648652
)
653+
expect(getMockLogger('CopilotChatStreaming').error).not.toHaveBeenCalledWith(
654+
'Stream cancelled with unknown abort reason',
655+
expect.anything()
656+
)
649657
})
650658

651659
it('releases the stream registration and pollers when preview initialization fails before the lifecycle starts', async () => {

‎apps/sim/lib/mothership/request/lifecycle/start.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
186186

187187
// Only explicit cancellation affects the run; browser attachment is independent.
188188
const recordCancelled = (errorMessage?: string): CopilotRequestCancelReasonValue => {
189-
const rawReason = abortController.signal.reason
189+
// A cancelled verdict with this node's signal still live came from the worker's terminal
190+
// (live or replayed in recovery): a Stop that reached the worker before this node saw it.
191+
const rawReason = abortController.signal.aborted
192+
? abortController.signal.reason
193+
: AbortReason.WorkerTerminalStop
190194
let cancelReason: CopilotRequestCancelReasonValue
191195
if (isExplicitStopReason(rawReason)) {
192196
cancelReason = CopilotRequestCancelReason.ExplicitStop

‎apps/sim/lib/mothership/request/otel.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function isGenAIMessageCaptureEnabled(): boolean {
5151
//
5252
// - Plain-string reject value: `controller.abort('user_stop:...')`
5353
// rejects fetch() with the reason STRING directly. Matches
54-
// `isExplicitStopReason()` exactly (UserStop / RedisPoller).
54+
// `isExplicitStopReason()` exactly (any reason it accepts).
5555
// - DOMException / Error object: `controller.abort()` with no arg
5656
// (or older runtimes) rejects with an AbortError whose `.cause`
5757
// or `.message` may carry the reason. We inspect both.

‎apps/sim/lib/mothership/request/session/abort-reason.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export const AbortReason = {
2828
* 250ms poller's next tick) rather than by the polling timer.
2929
*/
3030
MarkerObservedAtBodyClose: 'redis_abort_marker:body_close',
31+
/**
32+
* The turn's cancelled verdict came from the worker's terminal, which it sends only for a
33+
* Stop, while this node's signal never fired: the Stop reached the worker another way.
34+
* Classification only; nothing aborts with it.
35+
*/
36+
WorkerTerminalStop: 'worker_terminal:cancelled',
3137
/** Internal timeout on the outbound explicit-abort fetch to the worker. */
3238
ExplicitAbortFetchTimeout: 'timeout:explicit_abort_fetch',
3339
} as const
@@ -47,6 +53,7 @@ export function isExplicitStopReason(reason: unknown): boolean {
4753
return (
4854
reason === AbortReason.UserStop ||
4955
reason === AbortReason.RedisPoller ||
50-
reason === AbortReason.MarkerObservedAtBodyClose
56+
reason === AbortReason.MarkerObservedAtBodyClose ||
57+
reason === AbortReason.WorkerTerminalStop
5158
)
5259
}

0 commit comments

Comments
 (0)