Indian Prime Minister Narendra Modi. (Mikhail Metzel/TASS/AP)

How did India and China, nuclear-armed nations collectively home to 36 percent of the world’s people, go from conflict to camaraderie in less than a week? At the end of August, two months into the worst border standoff between the two countries in decades, there were whispers of war. But this week, Indian Prime Minister Narendra Modi and Chinese President Xi Jinping joined Russia, South Africa and Brazil in declaring war on Pakistan-based terror groups.

What just happened? Can it last? And should we trust it?

For the moment, it appears Beijing’s aspirations to be a global leader in a multipolar world have prevailed over bilateral tensions and the competing ambitions of Asia’s two biggest powers — but not before India outstared China in the Doklam border dispute, compelling an interim cessation of hostilities.

The epicenter of the worst seismic shock to India-China relations since the 1980s was the reclusive and gentle mountain kingdom of Bhutan. Bordering China to the north and India to the south, Bhutan — where growth is measured in terms of national happiness, the population is under 800,000 and democracy is less than a decade old — was at the heart of an unlikely dispute that triggered high-octane threats from Beijing as well as lowbrow, racist videos about India. The dispute erupted after the expansion of a road by China in Doklam, in territory claimed by Bhutan. Indian troops swiftly moved into the area to assist its ally, which still does not have diplomatic relations with Beijing. The Modi government showed its nerve by refusing to withdraw its soldiers unless China agreed to pull back it bulldozers.

Eventually, a diplomatic occasion proved to be the ace in India’s hand. Modi indicated he might skip a summit of the BRICS countries — China, Brazil, Russia, India and South Africa — that China hosted last weekend if tensions did not subside. India was able to restore the status quo in Bhutan. Both governments issued differently worded statements, but the road-building party was pulled back by the Chinese, and the soldiers were withdrawn by the Indians.

“Mature diplomatic endeavors together with a firm ground posture have resulted in this outcome,” Ram Madhav, a key foreign-policy strategist for the Modi government, told me. Madhav emphasized how India “did not get provoked and showed great restraint,” despite the aggression of spokespersons in China.

The finesse and firmness of Indian diplomacy has changed some of the rules of the game in the relations between the two powers. India simply did not blink in the way the Chinese expected it to.

Economics also played its part. China is India’s largest trade partner, and despite the Doklam crisis, Indian imports from China were up 33 percent in the April-June quarter. “India has learned the fine art of staring down the dragon as it protects its political space, while embracing China for pragmatic opportunities,” says Samir Saran of the Observer Research Foundation, a leading Indian think tank. “Doklam is where muscular politics played out; BRICS was the vehicle for cooperation in other areas.” With groups considered close to the government even calling for a boycott of Chinese goods, India was able to leverage its otherwise worrying trade deficit into a de facto negotiating tool, with an implicit threat of restricting its markets.

Yet, the Modi government has been wise to not frame the truce in terms of a “win” — even though it is one — because of China’s well-chronicled capacity to let India down on another issue that counts: Pakistan and terrorism.

Yes, the BRICS resolution, which named terrorist groups such as the Jaish-e-Muhammad and the Lashkar-e-Taiba, both headquartered in Pakistan, was a breakthrough moment from an Indian perspective. That it was made at a forum chaired by China on Chinese soil meant heartburn in Pakistan. Clubbed with the Trump administration’s sharp comments at Fort Myer in Arlington, Va., on Pakistan’s offering sanctuary to terrorists, it could appear that the big powers of the West and the East have acknowledged India’s concerns.

Yet serious tensions remain. At the United Nations, China has routinely used its veto to block India’s attempts to get Jaish’s chief, Masood Azhar, designated a global terrorist. The “roads to nowhere” — as one Indian official calls them — being built by the Chinese in areas that India considers sovereign territory in Pakistan-occupied Kashmir will remain a flash point. Just this week, India’s army chief has underlined China’s strategy of “salami slicing and taking over territory gradually” and reiterated the possibility of a “two-front war” — a simultaneous conflict with Pakistan and China.

The proxy battle between India and China will continue to be fought by courting influence in third countries. Hence Modi’s outreach with Japan and Vietnam and, closer to home, with Nepal, Myanmar and Bhutan. China’s Comrade Mao always spoke of “strategic retreat” in the context of “protracted war.” India expects no different.