Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/sim/lib/mothership/request/lifecycle/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down
6 changes: 5 additions & 1 deletion apps/sim/lib/mothership/request/lifecycle/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/lib/mothership/request/otel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion apps/sim/lib/mothership/request/session/abort-reason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
}
Loading