When Confucius served as an official in the State of Lu, he governed the state in an orderly manner. The people of the neighboring State of Qi, upon hearing this, grew fearful, worried that a powerful Lu would annex them. Duke Jing of Qi regretted that he had not given Confucius an important position when he had visited Qi years earlier. He therefore requested a meeting with the ruler of Lu to express the intention of fostering good relations between the two states.
In the tenth year of Duke Ding of Lu (500 BCE), at the covenant meeting between the states of Lu and Qi at Jiagu, Confucius, serving as the Minister of Crime (Sikou) of Lu, holding the rank of Senior Minister (Shangqing), and being well-versed in Li (Propriety), was appointed as the ceremonial assistant (Xiangli) for the ruler of Lu. Li, a high official of Qi, likely like most people, relied only on hearsay and hastily concluded that Confucius was a man who understood only rituals and not military affairs. So he secretly advised Duke Jing of Qi to send nearby Lai people (a minority group) to pretend to perform Lai music during the covenant, waiting for an opportunity to seize Duke Ding of Lu by force. Before the meeting, Duke Ding of Lu planned to travel by carriage to show peace and goodwill, but Confucius believed that "where there are civil affairs, there must be military preparations; where there are military affairs, there must be civil preparations." To guard against possible surprises at the meeting, he proposed having "both left and right military commanders (Sima)" escort and protect the ruler, a suggestion that Duke Ding adopted. After the two rulers had smeared blood on their lips to seal the covenant, the Qi side sent Lai people holding swords, halberds, and flags to perform a dance, attempting to seize Duke Ding of Lu to coerce the Lu state. Confucius immediately ordered his accompanying soldiers to counter the Lai people, quickly stepped forward to shield Duke Ding of Lu and move him back, and sternly rebuked Duke Jing of Qi, saying: "This is a friendly gathering of two state rulers. Why do you not follow the rituals of the Duke of Zhou and use elegant court music, but instead bring these barbarians here? This is inauspicious before the spirits, harmful to virtue, and extremely discourteous to people. What exactly do you intend?"
Confucius's forceful and principled rebuttal left Duke Jing of Qi speechless, who could only hastily order the people of Lai to withdraw and admit his mistake to Duke Ding of Lu. The next day, Qi and Lu formally concluded a covenant. The initial draft of the covenant was prepared in advance by Qi, and one clause stipulated that when Qi sent troops on an expedition, Lu must dispatch three hundred chariots to accompany them. Seeing this, Confucius realized it was clearly an attempt to make Lu acknowledge itself as a vassal state of Qi, and he demanded an explanation from Duke Jing of Qi. Duke Jing of Qi said, "Since the two states have formed an alliance, they should naturally assist each other." Confucius replied, "Since the two states have established friendly relations and signed a covenant, Qi should return the three territories of Yun, Wenyang, and Guiyin that it previously seized from Lu." The Duke of Qi was forced to agree.
At this Jiagu Alliance, Confucius adapted to the situation with propriety and restraint, thwarting the Qi ruler’s scheme to take the Lu ruler hostage by force. Through diplomatic negotiation, he also recovered the territory that Yang Hu (a rebellious Lu official) had secretly ceded to Qi, thus safeguarding national dignity and achieving a major diplomatic victory.
