{
  "filename": "src/main/scala/Reserve.scala",
  "rule": "- name: failed-stock-check-prevents-insert\n  where: \"src/main/scala/Reserve.scala\"\n  ensure: >-\n    The order INSERT must run only if the inventory UPDATE affected one row. A zero-row reservation must raise an error before insertion.",
  "corrected": "import cats.effect.IO\nimport doobie._\nimport doobie.implicits._\nimport cats.syntax.all._\n\nobject Reserve {\n  def apply(xa:Transactor[IO], tenant:String, sku:String, quantity:Int): IO[Int] = {\n    if (quantity <= 0) IO.raiseError(new IllegalArgumentException(\"Positive quantity required\"))\n    else {\n      val action = for {\n        count <- sql\"UPDATE inventory SET available=available-$quantity WHERE tenant_id=$tenant AND sku=$sku AND available >= $quantity\".update.run\n        _ <- if (count == 1) ().pure[ConnectionIO]\n             else new RuntimeException(\"Insufficient stock\").raiseError[ConnectionIO,Unit]\n        created <- sql\"INSERT INTO orders (tenant_id,sku,quantity) VALUES ($tenant,$sku,$quantity)\".update.run\n      } yield created\n      action.transact(xa)\n    }\n  }\n}\n",
  "broken": "import cats.effect.IO\nimport doobie._\nimport doobie.implicits._\nimport cats.syntax.all._\n\nobject Reserve {\n  def apply(xa:Transactor[IO], tenant:String, sku:String, quantity:Int): IO[Int] = {\n    if (quantity <= 0) IO.raiseError(new IllegalArgumentException(\"Positive quantity required\"))\n    else {\n      val action = for {\n        count <- sql\"UPDATE inventory SET available=available-$quantity WHERE tenant_id=$tenant AND sku=$sku AND available >= $quantity\".update.run\n        _ <- if (true) ().pure[ConnectionIO]\n             else new RuntimeException(\"Insufficient stock\").raiseError[ConnectionIO,Unit]\n        created <- sql\"INSERT INTO orders (tenant_id,sku,quantity) VALUES ($tenant,$sku,$quantity)\".update.run\n      } yield created\n      action.transact(xa)\n    }\n  }\n}\n",
  "regression": [
    "if (count == 1)",
    "if (true)"
  ],
  "test_scenarios": [
    [
      "Sufficient stock",
      "Inventory and order commit"
    ],
    [
      "Insufficient stock",
      "No order"
    ],
    [
      "Order INSERT fails",
      "Stock decrement rolls back"
    ],
    [
      "Two requests for final unit",
      "Conditional update permits one reservation"
    ]
  ],
  "test_source": null,
  "test_command": "sbt \"testOnly ReserveIntegrationSuite\""
}