diff --git a/extensions/cli/src/index.ts b/extensions/cli/src/index.ts index 113ebdf35..68a6b58cd 100644 --- a/extensions/cli/src/index.ts +++ b/extensions/cli/src/index.ts @@ -21,6 +21,7 @@ import { sentryService } from "./sentry.js"; import { addCommonOptions, mergeParentOptions } from "./shared-options.js"; import { posthogService } from "./telemetry/posthogService.js"; import { post } from "./util/apiClient.js"; +import { markUnhandledError } from "./util/errorState.js"; import { gracefulExit } from "./util/exit.js"; import { logger } from "./util/logger.js"; import { readStdinSync } from "./util/stdin.js"; @@ -35,9 +36,6 @@ let lastCtrlCTime: number; // Agent ID for serve mode - set when serve command is invoked with --id let agentId: string | undefined; -// Track whether any unhandled errors occurred during execution -let hasUnhandledError = false; - // Initialize state immediately to avoid temporal dead zone issues with exported functions (function initializeTUIState() { tuiUnmount = null; @@ -51,11 +49,6 @@ export function setAgentId(id: string | undefined) { agentId = id; } -// Check if any unhandled errors occurred during execution -export function hadUnhandledError(): boolean { - return hasUnhandledError; -} - // Register TUI cleanup function for graceful shutdown export function setTUIUnmount(unmount: () => void) { tuiUnmount = unmount; @@ -130,7 +123,7 @@ async function reportUnhandledErrorToApi(error: Error): Promise { // Add global error handlers to prevent uncaught errors from crashing the process process.on("unhandledRejection", (reason, promise) => { // Mark that an unhandled error occurred - this will cause non-zero exit - hasUnhandledError = true; + markUnhandledError(); // Extract useful information from the reason const errorDetails = { @@ -165,7 +158,7 @@ process.on("unhandledRejection", (reason, promise) => { process.on("uncaughtException", (error) => { // Mark that an unhandled error occurred - this will cause non-zero exit - hasUnhandledError = true; + markUnhandledError(); logger.error("Uncaught Exception:", error); // Report to API if running in serve mode diff --git a/extensions/cli/src/util/errorState.ts b/extensions/cli/src/util/errorState.ts new file mode 100644 index 000000000..de8fd2549 --- /dev/null +++ b/extensions/cli/src/util/errorState.ts @@ -0,0 +1,24 @@ +/** + * Module to track unhandled errors during CLI execution. + * This is in a separate file to avoid circular dependencies between + * index.ts and exit.ts. + */ + +// Track whether any unhandled errors occurred during execution +let hasUnhandledError = false; + +/** + * Mark that an unhandled error occurred. + * Called by global error handlers in index.ts. + */ +export function markUnhandledError(): void { + hasUnhandledError = true; +} + +/** + * Check if any unhandled errors occurred during execution. + * Called by gracefulExit() to determine the exit code. + */ +export function hadUnhandledError(): boolean { + return hasUnhandledError; +} diff --git a/extensions/cli/src/util/exit.ts b/extensions/cli/src/util/exit.ts index 1135ed49b..2d2c052d9 100644 --- a/extensions/cli/src/util/exit.ts +++ b/extensions/cli/src/util/exit.ts @@ -1,10 +1,10 @@ import type { ChatHistoryItem } from "core/index.js"; -import { hadUnhandledError } from "../index.js"; import { sentryService } from "../sentry.js"; import { getSessionUsage } from "../session.js"; import { telemetryService } from "../telemetry/telemetryService.js"; +import { hadUnhandledError } from "./errorState.js"; import { getGitDiffSnapshot } from "./git.js"; import { logger } from "./logger.js"; import {