After months of fraught negotiations with the United States, Japan clinched a deal just days before punitive tariffs were scheduled to take effect.