of civil service affairs (吏部, Lìbu, note different tone than the ministry of rites). Kong later left government service when his mother died, to observe a mourning period. He then returned to the imperial government as Yousi Yuanwailang (), a low-level official under one of the secretaries general of the executive bureau (尚書省, Shangshu Sheng). As the chancellor Zhao Yin was impressed by his writing ability, Zhao recommended him to be an imperial scholar (翰林學士, Hanlin Xueshi), as well as Kaogong Langzhong (), a supervisory official at the minister of civil service affairs; he was also put in charge of drafting edicts. Later, he was made Zhongshu Sheren (), a mid-level official at the legislative bureau (中書省, Zhongshu Sheng), as well as deputy minister of census (戶部侍郎, Hubu Shilang). In the middle of the Qianfu era (874-879) of Emperor Xuānzong's grandson Emperor Xizong, he was relieved of his post as imperial scholar, and made the deputy chief imperial censor (御史中丞, Yushi Zhongcheng). It was said that because Kong had integrity and hated wickedness, during his service at the office of the imperial censors, the office became more cleanly run. Later, when he served successively as the deputy minister of census, then deputy minister of defense (兵部侍郎, Bingbu Shilang), then deputy minister of civil service affairs (吏部侍郎, Lìbu Shilang), he would refuse special requests by powerful individuals. This offended those who made requests of him, and he was given the largely powerless post of minister of worship (太常卿, Taichang Qing). In late 880, the major agrarian rebel Huang Chao attacked Chang'an, and Emperor Xizong fled to Chengdu. Kong followed Emperor Xizong there, and was made the minister of justice (刑部尚書, Xingbu Shangshu) and acting director of finances. However, as the chancellor Xiao Gou, who was a colleague of Kong's while both served as imperial scholars, disliked Kong, he accused Kong of mismanagement, and had Kong given the entirely honorary post of advisor to the Crown Prince (as there was no crown prince at the time). In 885, Kong followed Emperor Xizong back to Chang'an after Huang's defeat. Late that year, the powerful eunuch Tian Lingzi provoked the warlord Wang Chongrong the military governor of Hezhong by ordering Wang transferred, and Wang and his ally Li Keyong the military governor of Hedong Circuit (河東, headquartered in modern Taiyuan, Shanxi) reacted by engaging forces under Tian and Tian's allies Zhu Mei the military