var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
  ErrorCode2["DB_CONNECTION_FAILED"] = "DB_001";
  ErrorCode2["DB_QUERY_FAILED"] = "DB_002";
  ErrorCode2["DB_TRANSACTION_FAILED"] = "DB_003";
  ErrorCode2["DB_MIGRATION_FAILED"] = "DB_004";
  ErrorCode2["DB_CONSTRAINT_VIOLATION"] = "DB_005";
  ErrorCode2["DB_SCHEMA_ERROR"] = "DB_006";
  ErrorCode2["DB_INSERT_FAILED"] = "DB_007";
  ErrorCode2["DB_UPDATE_FAILED"] = "DB_008";
  ErrorCode2["DB_DELETE_FAILED"] = "DB_009";
  ErrorCode2["FRAME_NOT_FOUND"] = "FRAME_001";
  ErrorCode2["FRAME_INVALID_STATE"] = "FRAME_002";
  ErrorCode2["FRAME_PARENT_NOT_FOUND"] = "FRAME_003";
  ErrorCode2["FRAME_CYCLE_DETECTED"] = "FRAME_004";
  ErrorCode2["FRAME_ALREADY_CLOSED"] = "FRAME_005";
  ErrorCode2["FRAME_INIT_FAILED"] = "FRAME_006";
  ErrorCode2["FRAME_INVALID_INPUT"] = "FRAME_007";
  ErrorCode2["FRAME_STACK_OVERFLOW"] = "FRAME_008";
  ErrorCode2["TASK_NOT_FOUND"] = "TASK_001";
  ErrorCode2["TASK_INVALID_STATE"] = "TASK_002";
  ErrorCode2["TASK_DEPENDENCY_CONFLICT"] = "TASK_003";
  ErrorCode2["TASK_CIRCULAR_DEPENDENCY"] = "TASK_004";
  ErrorCode2["LINEAR_AUTH_FAILED"] = "LINEAR_001";
  ErrorCode2["LINEAR_API_ERROR"] = "LINEAR_002";
  ErrorCode2["LINEAR_SYNC_FAILED"] = "LINEAR_003";
  ErrorCode2["LINEAR_WEBHOOK_FAILED"] = "LINEAR_004";
  ErrorCode2["MCP_TOOL_NOT_FOUND"] = "MCP_001";
  ErrorCode2["MCP_INVALID_PARAMS"] = "MCP_002";
  ErrorCode2["MCP_EXECUTION_FAILED"] = "MCP_003";
  ErrorCode2["MCP_RATE_LIMITED"] = "MCP_004";
  ErrorCode2["PROJECT_NOT_FOUND"] = "PROJECT_001";
  ErrorCode2["PROJECT_INVALID_PATH"] = "PROJECT_002";
  ErrorCode2["PROJECT_GIT_ERROR"] = "PROJECT_003";
  ErrorCode2["VALIDATION_FAILED"] = "VAL_001";
  ErrorCode2["INVALID_INPUT"] = "VAL_002";
  ErrorCode2["MISSING_REQUIRED_FIELD"] = "VAL_003";
  ErrorCode2["TYPE_MISMATCH"] = "VAL_004";
  ErrorCode2["INITIALIZATION_ERROR"] = "SYS_001";
  ErrorCode2["NOT_FOUND"] = "SYS_002";
  ErrorCode2["INTERNAL_ERROR"] = "SYS_003";
  ErrorCode2["CONFIGURATION_ERROR"] = "SYS_004";
  ErrorCode2["PERMISSION_DENIED"] = "SYS_005";
  ErrorCode2["RESOURCE_EXHAUSTED"] = "SYS_006";
  ErrorCode2["SERVICE_UNAVAILABLE"] = "SYS_007";
  ErrorCode2["SYSTEM_INIT_FAILED"] = "SYS_008";
  ErrorCode2["STACK_CONTEXT_NOT_FOUND"] = "COLLAB_001";
  ErrorCode2["HANDOFF_REQUEST_EXPIRED"] = "COLLAB_002";
  ErrorCode2["MERGE_CONFLICT_UNRESOLVABLE"] = "COLLAB_003";
  ErrorCode2["PERMISSION_VIOLATION"] = "COLLAB_004";
  ErrorCode2["OPERATION_FAILED"] = "COLLAB_005";
  ErrorCode2["OPERATION_EXPIRED"] = "COLLAB_006";
  ErrorCode2["INVALID_STATE"] = "COLLAB_007";
  ErrorCode2["RESOURCE_NOT_FOUND"] = "COLLAB_008";
  ErrorCode2["HANDOFF_ALREADY_EXISTS"] = "COLLAB_009";
  ErrorCode2["MERGE_SESSION_INVALID"] = "COLLAB_010";
  ErrorCode2["STACK_SWITCH_FAILED"] = "COLLAB_011";
  ErrorCode2["APPROVAL_TIMEOUT"] = "COLLAB_012";
  ErrorCode2["CONFLICT_RESOLUTION_FAILED"] = "COLLAB_013";
  ErrorCode2["TEAM_ACCESS_DENIED"] = "COLLAB_014";
  ErrorCode2["STACK_LIMIT_EXCEEDED"] = "COLLAB_015";
  return ErrorCode2;
})(ErrorCode || {});
class StackMemoryError extends Error {
  code;
  context;
  cause;
  isRetryable;
  httpStatus;
  timestamp;
  constructor(options) {
    super(options.message);
    this.name = this.constructor.name;
    this.code = options.code;
    this.context = options.context;
    this.cause = options.cause;
    this.isRetryable = options.isRetryable ?? false;
    this.httpStatus = options.httpStatus ?? 500;
    this.timestamp = /* @__PURE__ */ new Date();
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, this.constructor);
    }
  }
  toJSON() {
    return {
      name: this.name,
      code: this.code,
      message: this.message,
      context: this.context,
      isRetryable: this.isRetryable,
      httpStatus: this.httpStatus,
      timestamp: this.timestamp.toISOString(),
      stack: this.stack,
      cause: this.cause?.message
    };
  }
}
class DatabaseError extends StackMemoryError {
  constructor(message, code = "DB_002" /* DB_QUERY_FAILED */, context, cause) {
    super({
      code,
      message,
      context,
      cause,
      isRetryable: code === "DB_001" /* DB_CONNECTION_FAILED */,
      httpStatus: 503
    });
  }
}
class FrameError extends StackMemoryError {
  constructor(message, code = "FRAME_002" /* FRAME_INVALID_STATE */, context) {
    super({
      code,
      message,
      context,
      isRetryable: false,
      httpStatus: 400
    });
  }
}
class TaskError extends StackMemoryError {
  constructor(message, code = "TASK_002" /* TASK_INVALID_STATE */, context) {
    super({
      code,
      message,
      context,
      isRetryable: false,
      httpStatus: 400
    });
  }
}
class IntegrationError extends StackMemoryError {
  constructor(message, code = "LINEAR_002" /* LINEAR_API_ERROR */, context, cause) {
    super({
      code,
      message,
      context,
      cause,
      isRetryable: true,
      httpStatus: 502
    });
  }
}
class MCPError extends StackMemoryError {
  constructor(message, code = "MCP_003" /* MCP_EXECUTION_FAILED */, context) {
    super({
      code,
      message,
      context,
      isRetryable: code === "MCP_004" /* MCP_RATE_LIMITED */,
      httpStatus: code === "MCP_004" /* MCP_RATE_LIMITED */ ? 429 : 400
    });
  }
}
class ValidationError extends StackMemoryError {
  constructor(message, code = "VAL_001" /* VALIDATION_FAILED */, context) {
    super({
      code,
      message,
      context,
      isRetryable: false,
      httpStatus: 400
    });
  }
}
class ProjectError extends StackMemoryError {
  constructor(message, code = "PROJECT_001" /* PROJECT_NOT_FOUND */, context) {
    super({
      code,
      message,
      context,
      isRetryable: false,
      httpStatus: 404
    });
  }
}
class SystemError extends StackMemoryError {
  constructor(message, code = "SYS_003" /* INTERNAL_ERROR */, context, cause) {
    super({
      code,
      message,
      context,
      cause,
      isRetryable: code === "SYS_007" /* SERVICE_UNAVAILABLE */,
      httpStatus: 500
    });
  }
}
function isRetryableError(error) {
  if (error instanceof StackMemoryError) {
    return error.isRetryable;
  }
  if (error instanceof Error) {
    const message = error.message.toLowerCase();
    return message.includes("econnrefused") || message.includes("timeout") || message.includes("enotfound") || message.includes("socket hang up");
  }
  return false;
}
function getErrorMessage(error) {
  if (error instanceof Error) {
    return error.message;
  }
  if (typeof error === "string") {
    return error;
  }
  if (error && typeof error === "object" && "message" in error) {
    return String(error.message);
  }
  return "An unknown error occurred";
}
function wrapError(error, defaultMessage, code = "SYS_003" /* INTERNAL_ERROR */, context) {
  if (error instanceof StackMemoryError) {
    return error;
  }
  const cause = error instanceof Error ? error : void 0;
  const message = error instanceof Error ? error.message : defaultMessage;
  return new SystemError(message, code, context, cause);
}
function isStackMemoryError(error) {
  return error instanceof StackMemoryError;
}
function createErrorHandler(defaultContext) {
  return (error, additionalContext) => {
    const context = { ...defaultContext, ...additionalContext };
    if (error instanceof StackMemoryError) {
      return new StackMemoryError({
        code: error.code,
        message: error.message,
        context: { ...error.context, ...context },
        cause: error.cause,
        isRetryable: error.isRetryable,
        httpStatus: error.httpStatus
      });
    }
    return wrapError(
      error,
      getErrorMessage(error),
      "SYS_003" /* INTERNAL_ERROR */,
      context
    );
  };
}
export {
  DatabaseError,
  ErrorCode,
  FrameError,
  IntegrationError,
  MCPError,
  ProjectError,
  StackMemoryError,
  SystemError,
  TaskError,
  ValidationError,
  createErrorHandler,
  getErrorMessage,
  isRetryableError,
  isStackMemoryError,
  wrapError
};
//# sourceMappingURL=index.js.map
