The talks got serious immediately after the shutdown when Mr. McConnell approached Mr. Schumer and asked how they should proceed on the spending divide. They ended Tuesday in Mr. McConnell’s office with Mr. Schumer inviting Mr. McConnell, a die-hard University of Louisville basketball fan, to New York to attend a future Louisville game against Syracuse University. (By earlier arrangement, Mr. Schumer will speak Monday at the university’s McConnell Center.)

Both sides were heavily invested in trying to produce a result that would get them out of the stubborn cycle of budget dysfunction and, just as important, get them through the November elections without another politically damaging shutdown or threat to default on the federal debt.

The brief January disruption of the government had been more than enough to remind both sides of the risks of closing the government even to protect a group as sympathetic as undocumented immigrants brought into the country as children. The plight of those immigrants was walled off from the spending agreement and will now be the subject of a coming Senate floor debate, with the outcome highly uncertain.

The final two-year deal that must be approved by the House and Senate delivered tangible benefits to both sides while easing tight spending restrictions instituted when Republicans were insisting on austerity for the Obama administration. Aides said the agreement was $113 billion beyond President Trump’s budget proposal for this fiscal year.

Defense hawks received $165 billion more for the Pentagon, which they said had been hamstrung not only by a shortage of money, but by the stop-and-start process that had made forecasting and planning impossible.