The House and Senate had until midnight Friday to pass a measure to avert a government shutdown.
The catchall spending bill would be the first major piece of bipartisan legislation to advance during President Donald Trump's short tenure in the White House.
It rejects White House budget director Mick Mulvaney's proposals to cut popular programs such as funding medical research and community development grants.
But adds $1.5 billion for border security measures such as additional detention beds.
Most of the core decisions about agency budgets had been worked out, but unrelated policy issues - such as a Democratic request to help the cash-strapped government of Puerto Rico with its Medicaid burden - were among the final holdups.
The aides required anonymity because they were not allowed to speak about the agreement by name.
Democrats have denied Trump a big-picture win on obtaining an initial down payment for his oft-promised border wall with Mexico, while anti-abortion lawmakers didn't even attempt to use the must-pass measure to try to cut off federal money for Planned Parenthood.
Details were being closely held ahead of an agreement, but Trump and Capitol Hill defense hawks procured a $15 billion infusion of cash for Pentagon readiness and won funding for other border security accounts, such as detention beds for people entering the country illegally.
"This agreement is a good agreement for the American people, and takes the threat of a government shutdown off the table," Senate Minority Leader Chuck Schumer, a Democrat from New York, told Politico.
"The bill ensures taxpayer dollars aren't used to fund an ineffective border wall, excludes poison pill riders, and increases investments in programs that the middle-class relies on, like medical research, education, and infrastructure."
Democrats praised a $2 billion funding increase for the National Institutes of Health - rejecting the steep cuts proposed by Trump - as well as additional funding to combat opioid abuse, fund Pell Grants for summer school and additional transit funding.
A provision extending health care for 22,000 retired Appalachian coal miners and their families was on track to provide permanent health benefits, a priority of Senate Majority leader Mitch McConnell, R-Ky., and other Appalachia region lawmakers.
Democratic votes will be needed to pass the measure, so even though Republicans control both the White House and Congress, Democrats have been actively involved in the talks, which appear headed to produce a lowest-common-denominator measure that won't look too much different than the deal that could have been struck on President Barack Obama's watch last year.
Republicans had pressed for policy wins with so-called riders related to other abortion-related issues and blocking environmental regulations such as Obama's sweeping expansion of the Clean Water Act.
They also hoped to chuck new financial rules. But Democrats pushed back and were hopeful that the measure would not contain many items they deemed "poison pills."
The trillion-dollar budget:
* $1.5billion for border security - with strict stipulation that it be used only for technology investments and repairs to existing fencing and infrastructure.
* $61million to reimburse local law enforcement in New York and Florida for the cost of protecting Trump and his family when he's not in Washington.
* Permanent extension of program that offers health benefits to coal miners.
* $407million earmarked for wildfire relief for Western states.