fix circular dependency

This commit is contained in:
Nate 2025-12-23 13:19:42 -05:00
parent d31d6b6fc4
commit 635aa42811
3 changed files with 28 additions and 11 deletions

View file

@ -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<void> {
// 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

View file

@ -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;
}

View file

@ -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 {