Skip to content

Commit 635aa42

Browse files
committed
fix circular dependency
1 parent d31d6b6 commit 635aa42

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

extensions/cli/src/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { sentryService } from "./sentry.js";
2121
import { addCommonOptions, mergeParentOptions } from "./shared-options.js";
2222
import { posthogService } from "./telemetry/posthogService.js";
2323
import { post } from "./util/apiClient.js";
24+
import { markUnhandledError } from "./util/errorState.js";
2425
import { gracefulExit } from "./util/exit.js";
2526
import { logger } from "./util/logger.js";
2627
import { 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
3637
let 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
6053
export 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
131124
process.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

166159
process.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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Module to track unhandled errors during CLI execution.
3+
* This is in a separate file to avoid circular dependencies between
4+
* index.ts and exit.ts.
5+
*/
6+
7+
// Track whether any unhandled errors occurred during execution
8+
let hasUnhandledError = false;
9+
10+
/**
11+
* Mark that an unhandled error occurred.
12+
* Called by global error handlers in index.ts.
13+
*/
14+
export function markUnhandledError(): void {
15+
hasUnhandledError = true;
16+
}
17+
18+
/**
19+
* Check if any unhandled errors occurred during execution.
20+
* Called by gracefulExit() to determine the exit code.
21+
*/
22+
export function hadUnhandledError(): boolean {
23+
return hasUnhandledError;
24+
}

extensions/cli/src/util/exit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ChatHistoryItem } from "core/index.js";
22

3-
import { hadUnhandledError } from "../index.js";
43
import { sentryService } from "../sentry.js";
54
import { getSessionUsage } from "../session.js";
65
import { telemetryService } from "../telemetry/telemetryService.js";
76

7+
import { hadUnhandledError } from "./errorState.js";
88
import { getGitDiffSnapshot } from "./git.js";
99
import { logger } from "./logger.js";
1010
import {

0 commit comments

Comments
 (0)