From 92b5425be65758c68ea1d69aa608fd5a8fbd2d47 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 23 Nov 2026 02:51:15 -0800 Subject: [PATCH] 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. --- .../lib/mothership/request/lifecycle/start.test.ts | 12 ++++++++++-- apps/sim/lib/mothership/request/lifecycle/start.ts | 6 +++++- apps/sim/lib/mothership/request/otel.ts | 2 +- .../lib/mothership/request/session/abort-reason.ts | 9 ++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/sim/lib/mothership/request/lifecycle/start.test.ts b/apps/sim/lib/mothership/request/lifecycle/start.test.ts index e3a41c071f8..e1746d03ea4 100644 --- a/apps/sim/lib/mothership/request/lifecycle/start.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/start.test.ts @@ -7,6 +7,7 @@ import { billingAttributionMock, billingAttributionMockFns, } from '@sim/testing/mocks/billing-attribution.mock' +import { getMockLogger } from '@sim/testing/mocks/logger.mock' import { mothershipAgentUrlMock, mothershipAgentUrlMockFns, @@ -27,6 +28,7 @@ import { MothershipStreamV1EventType, } from '@/lib/mothership/generated/mothership-stream-v1' import { TitleRequest } from '@/lib/mothership/generated/protocol' +import { isExplicitStopReason } from '@/lib/mothership/request/session/abort-reason' import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' const { @@ -113,7 +115,7 @@ vi.mock('@/lib/mothership/request/session', () => ({ unregisterActiveStream, hasStreamViewer, startAbortPoller: vi.fn().mockReturnValue(setInterval(() => {}, 999999)), - isExplicitStopReason: (reason: unknown) => reason === 'user_stop:abortActiveStream', + isExplicitStopReason: (reason: unknown) => isExplicitStopReason(reason), SSE_RESPONSE_HEADERS: {}, StreamWriter: class { attach = vi.fn().mockImplementation((ctrl: ReadableStreamDefaultController) => { @@ -609,7 +611,9 @@ describe('createSSEStream terminal error handling', () => { expect(scheduleBufferCleanup).toHaveBeenCalledWith('stream-1') }) - it('publishes a cancelled completion (not an error) when the orchestrator reports cancelled without abortSignal aborted', async () => { + it('publishes a cancelled completion and classifies it as a Stop when the orchestrator reports cancelled without abortSignal aborted', async () => { + // Regression: the worker's cancelled terminal (live or replayed) left this node's signal + // unaborted, so the turn was logged as "Stream cancelled with unknown abort reason". runCopilotLifecycle.mockResolvedValue({ success: false, cancelled: true, @@ -646,6 +650,10 @@ describe('createSSEStream terminal error handling', () => { }), }) ) + expect(getMockLogger('CopilotChatStreaming').error).not.toHaveBeenCalledWith( + 'Stream cancelled with unknown abort reason', + expect.anything() + ) }) it('releases the stream registration and pollers when preview initialization fails before the lifecycle starts', async () => { diff --git a/apps/sim/lib/mothership/request/lifecycle/start.ts b/apps/sim/lib/mothership/request/lifecycle/start.ts index 23e86ebbbc3..cdbb9184670 100644 --- a/apps/sim/lib/mothership/request/lifecycle/start.ts +++ b/apps/sim/lib/mothership/request/lifecycle/start.ts @@ -186,7 +186,11 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS // Only explicit cancellation affects the run; browser attachment is independent. const recordCancelled = (errorMessage?: string): CopilotRequestCancelReasonValue => { - const rawReason = abortController.signal.reason + // A cancelled verdict with this node's signal still live came from the worker's terminal + // (live or replayed in recovery): a Stop that reached the worker before this node saw it. + const rawReason = abortController.signal.aborted + ? abortController.signal.reason + : AbortReason.WorkerTerminalStop let cancelReason: CopilotRequestCancelReasonValue if (isExplicitStopReason(rawReason)) { cancelReason = CopilotRequestCancelReason.ExplicitStop diff --git a/apps/sim/lib/mothership/request/otel.ts b/apps/sim/lib/mothership/request/otel.ts index 711de10f36d..c79036288b3 100644 --- a/apps/sim/lib/mothership/request/otel.ts +++ b/apps/sim/lib/mothership/request/otel.ts @@ -51,7 +51,7 @@ function isGenAIMessageCaptureEnabled(): boolean { // // - Plain-string reject value: `controller.abort('user_stop:...')` // rejects fetch() with the reason STRING directly. Matches -// `isExplicitStopReason()` exactly (UserStop / RedisPoller). +// `isExplicitStopReason()` exactly (any reason it accepts). // - DOMException / Error object: `controller.abort()` with no arg // (or older runtimes) rejects with an AbortError whose `.cause` // or `.message` may carry the reason. We inspect both. diff --git a/apps/sim/lib/mothership/request/session/abort-reason.ts b/apps/sim/lib/mothership/request/session/abort-reason.ts index 25e6a952af6..d761051e7c5 100644 --- a/apps/sim/lib/mothership/request/session/abort-reason.ts +++ b/apps/sim/lib/mothership/request/session/abort-reason.ts @@ -28,6 +28,12 @@ export const AbortReason = { * 250ms poller's next tick) rather than by the polling timer. */ MarkerObservedAtBodyClose: 'redis_abort_marker:body_close', + /** + * The turn's cancelled verdict came from the worker's terminal, which it sends only for a + * Stop, while this node's signal never fired: the Stop reached the worker another way. + * Classification only; nothing aborts with it. + */ + WorkerTerminalStop: 'worker_terminal:cancelled', /** Internal timeout on the outbound explicit-abort fetch to the worker. */ ExplicitAbortFetchTimeout: 'timeout:explicit_abort_fetch', } as const @@ -47,6 +53,7 @@ export function isExplicitStopReason(reason: unknown): boolean { return ( reason === AbortReason.UserStop || reason === AbortReason.RedisPoller || - reason === AbortReason.MarkerObservedAtBodyClose + reason === AbortReason.MarkerObservedAtBodyClose || + reason === AbortReason.WorkerTerminalStop ) }