Skip to content

Commit 3403b8b

Browse files
committed
better abort handling
1 parent ae54594 commit 3403b8b

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/lib/agent/BrowserAgent.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,13 +779,22 @@ export class BrowserAgent {
779779
*/
780780
private _handleExecutionError(error: unknown, task: string): void {
781781
// Check if this is a user cancellation - handle silently
782+
const isAbortError = error instanceof Error && error.name === 'AbortError';
783+
const abortReason = this.executionContext.abortSignal.reason as any;
784+
const isUserInitiated = abortReason?.userInitiated === true;
785+
782786
const isUserCancellation = error instanceof AbortError ||
783787
this.executionContext.isUserCancellation() ||
784-
(error instanceof Error && error.name === "AbortError");
788+
(isAbortError && isUserInitiated);
785789

786790
if (isUserCancellation) {
787-
// Don't publish message here - already handled in _subscribeToExecutionStatus
788-
// when the cancelled status event is received
791+
// User-initiated cancellation - don't rethrow, let execution end gracefully
792+
Logging.log('BrowserAgent', 'Execution cancelled by user');
793+
return; // Don't rethrow
794+
} else if (isAbortError) {
795+
// System abort (not user-initiated) - still throw
796+
Logging.log('BrowserAgent', 'Execution aborted by system');
797+
throw error;
789798
} else {
790799
// Log error metric with details
791800
const errorMessage = error instanceof Error ? error.message : String(error);

src/lib/agent/ChatAgent.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,23 @@ export class ChatAgent {
132132
Logging.log('ChatAgent', 'Q&A response completed')
133133

134134
} catch (error) {
135-
if (error instanceof AbortError) {
136-
Logging.log('ChatAgent', 'Execution aborted by user')
137-
// Don't publish message here - already handled in _subscribeToExecutionStatus
135+
// Check if this is a user cancellation - handle silently
136+
const isAbortError = error instanceof Error && error.name === 'AbortError'
137+
const abortReason = this.executionContext.abortSignal.reason as any
138+
const isUserInitiated = abortReason?.userInitiated === true
139+
140+
const isUserCancellation = error instanceof AbortError ||
141+
this.executionContext.isUserCancellation() ||
142+
(isAbortError && isUserInitiated)
143+
144+
if (isUserCancellation) {
145+
// User-initiated cancellation - don't rethrow, let execution end gracefully
146+
Logging.log('ChatAgent', 'Execution cancelled by user')
147+
return // Don't rethrow
148+
} else if (isAbortError) {
149+
// System abort (not user-initiated) - still throw
150+
Logging.log('ChatAgent', 'Execution aborted by system')
151+
throw error
138152
} else {
139153
const errorMessage = error instanceof Error ? error.message : String(error)
140154
const errorType = error instanceof Error ? error.name : 'UnknownError'
@@ -150,8 +164,8 @@ export class ChatAgent {
150164

151165
Logging.log('ChatAgent', `Execution failed: ${errorMessage}`, 'error')
152166
this.pubsub.publishMessage(PubSub.createMessage(`Error: ${errorMessage}`, 'error'))
167+
throw error
153168
}
154-
throw error
155169
}
156170
}
157171

src/lib/execution/Execution.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class Execution {
143143
return
144144
}
145145

146+
146147
// Send pause message to the user
147148
if (this.pubsub) {
148149
this.pubsub.publishMessage({
@@ -153,8 +154,9 @@ export class Execution {
153154
})
154155
}
155156

156-
// Abort the current execution
157-
this.currentAbortController.abort()
157+
// Abort the current execution with reason
158+
const abortReason = { userInitiated: true, message: 'User cancelled execution' }
159+
this.currentAbortController.abort(abortReason)
158160
this.currentAbortController = null
159161

160162
Logging.log('Execution', `Cancelled execution ${this.id}`)
@@ -168,7 +170,8 @@ export class Execution {
168170
reset(): void {
169171
// Cancel current execution if running
170172
if (this.currentAbortController) {
171-
this.currentAbortController.abort()
173+
const abortReason = { userInitiated: true, message: 'User cancelled execution' }
174+
this.currentAbortController.abort(abortReason)
172175
this.currentAbortController = null
173176
}
174177

0 commit comments

Comments
 (0)