There's several factors to consider.
The Analects is a compiled text. This means that it's been edited and rearranged several times and contains several layers of authorship. Not every editor necessarily agreed with every other editor about how things should go. Books XIV and XV are not in the innermost layer, but they are not in the outermost layer either.
One key concept for understanding Confucian philosophy is Ren (仁) [pronounced "Jen"]. This is sometimes translated as "benevolence" and sometimes as "virtue". The meaning seems to vary somewhat depending on context for the Analects, but in many places it indicates a type of compassionate character towards others and is a political virtue.
This stands in contrast to Li (禮) which many understand to represent cultural practices and customs. (See "Li as Cultural Grammar: On the Relation between Li and Ren in Confucius' "Analects"" by Chengyang Li). In some passages, Li seems to be the dominant consideration of right and wrong and in others Ren seems to have overtaken it with the capacity to interpret when cultural practices should themselves be transformed.
A third key concept is the idea of Ming (命) which refers to the mandate that a rule has from heaven to rule. On this analysis, some rulers have a just right to rule such as sage king shun (舜) but others are usurpers with no legitimate right to rule. (see https://en.wikipedia.org/wiki/Three_Sovereigns_and_Five_Emperors). Moreover, such rulers organize the state in such a way that the common people become virtuous (this sentence is one that the Daoists would not accept).
A related issue is history (and mistaken history). The writers of the Analects or some of its parts were not contemporary with the issues they are describing. As such, they have opinions on historical figures that are not always well-informed, but there were certain rulers they revered and others who they thought lacked a mandate to rule.
Finally, all of ancient Chinese philosophy is concerned with the way (Dao, 道). It's impossible to give a sufficient definition here, but let's use "correct philosophy that accords with the social order and its needs" for our purposes.
Given these five things, we can look at the two passages in question. Let's start with XV.7:
子曰：「直哉史魚！邦有道，如矢；邦無道，如矢。」君子哉蘧伯玉！邦有道，則仕；邦無道，則可卷而懷之。」"The Master said, "Truly straightforward was the historiographer Yu. When good government prevailed in his state, he was like an arrow. When bad government prevailed, he was like an arrow. A superior man indeed is Qu Bo Yu! When good government prevails in his state, he is to be found in office. When bad government prevails, he can roll his principles up, and keep them in his breast."" (Chinese and Legge translation from ctext).
What Legge translates as "good government" and "bad government" is literally when "the country that has the way" and "the country that lacks the way." To serve in such a state virtuously would be a waste of time (due to 4 above). But it's not entirely clear at least from Legge and the Chinese text itself that this means he quits his job (though other passages suggest one should not work for an unjust ruler.
For your other passage, I suggest we look at XIV.15-17 rather than just 16-17:
子曰：「晉文公譎而不正，齊桓公正而不譎。」子路曰：「桓公殺公子糾，召忽死之，管仲不死。」曰：「未仁乎？」子曰：「桓公九合諸侯，不以兵車，管仲之力也。如其仁！如其仁！」子貢曰：「管仲非仁者與？桓公殺公子糾，不能死，又相之。」子曰：「管仲相桓公，霸諸侯，一匡天下，民到于今受其賜。微管仲，吾其被髮左衽矣。豈若匹夫匹婦之為諒也，自經於溝瀆，而莫之知也。」 The Master said, "The duke Wen of Jin was crafty and not upright. The duke Huan of Qi was upright and not crafty." Zi Lu said, "The duke Huan caused his brother Jiu to be killed, when Shao Hu died with his master, but Guan Zhong did not die. May not I say that he was wanting in Ren?" The Master said, "The Duke Huan assembled all the princes together, and that not with weapons of war and chariots - it was all through the influence of Guan Zhong. Whose Ren was like his? Whose Ren was like his?" Zi Gong said, "Guan Zhong, I apprehend, was wanting in Ren. When the Duke Huan caused his brother Jiu to be killed, Guan Zhong was not able to die with him. Moreover, he became prime minister to Huan." The Master said, "Guan Zhong acted as prime minister to the duke Huan, made him leader of all the princes, and united and rectified the whole kingdom. Down to the present day, the people enjoy the gifts which he conferred. But for Guan Zhong, we should now be wearing our hair unbound, and the lappets of our coats buttoning on the left side. Will you require from him the small fidelity of common men and common women, who would commit suicide in a stream or ditch, no one knowing anything about them?" (ctext)
It's a lengthy passage, but I think it shows the following:
First, Confucius believes Duke Huan to be a just ruler. Similarly, Confucius believes Duke Huan has something close to Ming. Thus it would be a duty and pleasure to serve him as a minister.
Thus, it follows in the Confucian understanding that Guan Zhong should serve him. The problem is an issue of the interaction between Ren and Li. Li dictated that he should kill himself because his earlier master was killed (There's a passage in Analects 12 saying you should value Ren more than your life). This particular passage suggests that Ren mediates Li in both his choice to stay alive and serve a just new master and in the innovations of custom he dictated.
From the literature, regarding 14.16, Slingerland comments (Analects, Hackett Press: Indianapolis, p. 160-161) that:
1. there's an issue with translation of Ruqiruen (如其仁) because it's curious to praise Guan Zhong's goodness, so Slingerland translates it as "As for his goodness!"
2. Zhu Xi and Kong Anguo see this as somewhat contradictory with Analects 3.22 and explain that while Guan Zhong himself was not good Ren that his goodness Ren extends to the people so we could call him good.
For 15.7, Slingerland's sources are more sparse (p. 171) and basically only look at whether this is historically accurate for Qu Boyu.