@@ -21,6 +21,7 @@ import { sentryService } from "./sentry.js";
2121import { addCommonOptions , mergeParentOptions } from "./shared-options.js" ;
2222import { posthogService } from "./telemetry/posthogService.js" ;
2323import { post } from "./util/apiClient.js" ;
24+ import { markUnhandledError } from "./util/errorState.js" ;
2425import { gracefulExit } from "./util/exit.js" ;
2526import { logger } from "./util/logger.js" ;
2627import { readStdinSync } from "./util/stdin.js" ;
@@ -35,9 +36,6 @@ let lastCtrlCTime: number;
3536// Agent ID for serve mode - set when serve command is invoked with --id
3637let agentId : string | undefined ;
3738
38- // Track whether any unhandled errors occurred during execution
39- let hasUnhandledError = false ;
40-
4139// Initialize state immediately to avoid temporal dead zone issues with exported functions
4240( function initializeTUIState ( ) {
4341 tuiUnmount = null ;
@@ -51,11 +49,6 @@ export function setAgentId(id: string | undefined) {
5149 agentId = id ;
5250}
5351
54- // Check if any unhandled errors occurred during execution
55- export function hadUnhandledError ( ) : boolean {
56- return hasUnhandledError ;
57- }
58-
5952// Register TUI cleanup function for graceful shutdown
6053export function setTUIUnmount ( unmount : ( ) = > void ) {
6154 tuiUnmount = unmount ;
@@ -130,7 +123,7 @@ async function reportUnhandledErrorToApi(error: Error): Promise<void> {
130123// Add global error handlers to prevent uncaught errors from crashing the process
131124process . on ( "unhandledRejection" , ( reason , promise ) => {
132125 // Mark that an unhandled error occurred - this will cause non-zero exit
133- hasUnhandledError = true ;
126+ markUnhandledError ( ) ;
134127
135128 // Extract useful information from the reason
136129 const errorDetails = {
@@ -165,7 +158,7 @@ process.on("unhandledRejection", (reason, promise) => {
165158
166159process . on ( "uncaughtException" , ( error ) => {
167160 // Mark that an unhandled error occurred - this will cause non-zero exit
168- hasUnhandledError = true ;
161+ markUnhandledError ( ) ;
169162
170163 logger . error ( "Uncaught Exception:" , error ) ;
171164 // Report to API if running in serve mode
0 commit comments