{
  "pr": 917,
  "title": "Waiting for results held scarce database connections",
  "run": {
    "pr": 917,
    "title": "Spec: mcp-server-core",
    "run_id": "mcp-server-core-20260623T174318286059",
    "mode": "spec",
    "created": "2026-06-23T17:43:18.340714+00:00",
    "merged": "2026-06-23T19:55:11+00:00",
    "agent": "codex",
    "chain_runs": 1,
    "has_implement_event": true,
    "elapsed_hours": 2.197960912777778,
    "implementation_to_merge_hours": 2.192662828611111,
    "recorded_phase_hours": 2.1979127069444444,
    "churn": 2052,
    "files": 15,
    "direct_signals": 0,
    "broad_signals": 0,
    "review_rejected": true,
    "review_rejections": 4,
    "gates_rejected": false
  },
  "merge_sha": "06b8f934bb0b12f9fc8551c794ebe291a1845033",
  "selected_review": {
    "attempt_number": 3,
    "findings": [
      {
        "body": "`get_mcp_actor` receives `conn=Depends(get_db)`, so FastAPI keeps that yielded connection open for the whole `/api/mcp` request. For the `wait_for_run` tool, the handler then loops and sleeps up to 30 seconds before returning, which violates the spec requirement that wait_for_run not hold a database connection while waiting and can exhaust DB connections under concurrent MCP clients. Authenticate with a short-lived connection that is closed before dispatching the tool, or otherwise avoid a yield-scoped DB dependency around the polling loop.",
        "confidence": 0.92,
        "end_line": 98,
        "file": "api/mcp/router.py",
        "id": "F1",
        "severity": "P1",
        "start_line": 95,
        "title": "wait_for_run holds a DB connection while polling"
      }
    ],
    "reviewed_at": "2026-06-23T12:02:00-07:00",
    "reviewed_base_sha": "6f4e96b72108ed11ec0980227c71a2e2a4989e7f",
    "reviewed_head_sha": "bd7aed5f83f90885d9ea35cbab55f85a091810c1",
    "reviewer_agent": "codex",
    "reviewer_role": "independent_pr_reviewer",
    "source_check_name": "review-decision-gate",
    "source_check_url": "private repository PR #917",
    "status": "request_changes",
    "summary": "Found a blocking spec mismatch in wait_for_run: the MCP request keeps the authentication DB connection open while the bounded poll sleeps."
  },
  "final_approval": {
    "attempt_number": 5,
    "findings": [],
    "reviewed_at": "2026-06-23T19:43:00Z",
    "reviewed_base_sha": "6f4e96b72108ed11ec0980227c71a2e2a4989e7f",
    "reviewed_head_sha": "7b8d9711dbf16fc2928d1da9657987c429985209",
    "reviewer_agent": "codex",
    "reviewer_role": "independent_reviewer",
    "source_check_name": "review-decision-gate",
    "source_check_url": "private repository PR #917",
    "status": "approved",
    "summary": "No blocking correctness, security, acceptance-criteria, or test issues found in the PR diff."
  },
  "merged_excerpts": [
    {
      "file": "api/mcp/router.py",
      "start_line": 102,
      "end_line": 108,
      "language": "python",
      "text": "    conn = repository.get_connection(str(settings.database_url))\n    try:\n        user, service_key_id, kind = authenticate_user_from_service_key(token, conn=conn)\n    except Exception as exc:\n        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=\"Authentication required.\") from exc\n    finally:\n        conn.close()"
    },
    {
      "file": "tests/test_mcp_core.py",
      "start_line": 388,
      "end_line": 438,
      "language": "python",
      "text": "async def test_wait_for_run_does_not_hold_get_db_connection_while_sleeping(monkeypatch, mcp_client):\n    client, database_url, app = mcp_client\n    _owner_user_id, _service_key_id, token = _seed_user_and_service_key(database_url)\n    active_get_db_connections = 0\n    rest_calls = 0\n\n    def counting_get_db():\n        nonlocal active_get_db_connections\n        conn = repository.get_connection(database_url)\n        active_get_db_connections += 1\n        try:\n            yield conn\n        finally:\n            active_get_db_connections -= 1\n            conn.close()\n\n    async def fake_rest_call(request, actor, method, path, *, json_body=None):\n        nonlocal rest_calls\n        rest_calls += 1\n        status = \"queued\" if rest_calls == 1 else \"succeeded\"\n        return httpx.Response(\n            200,\n            json={\n                \"id\": 55,\n                \"status\": status,\n                \"run_kind\": \"simulation\",\n                \"stats\": {},\n            },\n        )\n\n    async def fake_sleep(_seconds):\n        assert active_get_db_connections == 0\n        await real_sleep(0)\n\n    mcp_router = importlib.import_module(\"api.mcp.router\")\n\n    app.dependency_overrides[get_db] = counting_get_db\n    real_sleep = mcp_router.asyncio.sleep\n    monkeypatch.setattr(mcp_router, \"_rest_call\", fake_rest_call)\n    monkeypatch.setattr(mcp_router.asyncio, \"sleep\", fake_sleep)\n\n    resp = await _rpc(\n        client,\n        token,\n        \"tools/call\",\n        {\"name\": \"wait_for_run\", \"arguments\": {\"run_id\": 55, \"max_wait_seconds\": 1}},\n    )\n\n    assert resp.status_code == 200, resp.text\n    assert resp.json()[\"result\"][\"structuredContent\"][\"run_id\"] == 55\n    assert rest_calls == 2"
    }
  ],
  "verification": "Inspected recorded finding, final approval, gate histories and merged implementation/test. Historical tests were not rerun.",
  "gates": {
    "spec_id": "mcp-server-core",
    "gates": {
      "e2e": {
        "attempts": 5,
        "history": [
          {
            "attempt": 1,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:20:21.790686+00:00"
          },
          {
            "attempt": 2,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:40:55.571050+00:00"
          },
          {
            "attempt": 3,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:59:43.760567+00:00"
          },
          {
            "attempt": 4,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T19:20:58.536084+00:00"
          },
          {
            "attempt": 5,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T19:40:49.445235+00:00"
          }
        ],
        "last_command": "make e2e",
        "last_status": "passed"
      },
      "lint": {
        "attempts": 5,
        "history": [
          {
            "attempt": 1,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:18:12.730594+00:00"
          },
          {
            "attempt": 2,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:38:50.658113+00:00"
          },
          {
            "attempt": 3,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T18:57:38.134293+00:00"
          },
          {
            "attempt": 4,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T19:18:50.803313+00:00"
          },
          {
            "attempt": 5,
            "error_count": null,
            "failed_count": null,
            "failed_tests": null,
            "failure_fingerprint": "",
            "passed_count": null,
            "status": "passed",
            "timestamp": "2026-06-23T19:38:42.985216+00:00"
          }
        ],
        "last_command": "make lint",
        "last_status": "passed"
      },
      "test": {
        "attempts": 5,
        "history": [
          {
            "attempt": 1,
            "error_count": 0,
            "failed_count": 0,
            "failed_tests": [],
            "failure_fingerprint": "",
            "passed_count": 3686,
            "status": "passed",
            "timestamp": "2026-06-23T18:18:12.727675+00:00"
          },
          {
            "attempt": 2,
            "error_count": 0,
            "failed_count": 0,
            "failed_tests": [],
            "failure_fingerprint": "",
            "passed_count": 3689,
            "status": "passed",
            "timestamp": "2026-06-23T18:38:50.655384+00:00"
          },
          {
            "attempt": 3,
            "error_count": 0,
            "failed_count": 0,
            "failed_tests": [],
            "failure_fingerprint": "",
            "passed_count": 3691,
            "status": "passed",
            "timestamp": "2026-06-23T18:57:38.131449+00:00"
          },
          {
            "attempt": 4,
            "error_count": 0,
            "failed_count": 0,
            "failed_tests": [],
            "failure_fingerprint": "",
            "passed_count": 3693,
            "status": "passed",
            "timestamp": "2026-06-23T19:18:50.800519+00:00"
          },
          {
            "attempt": 5,
            "error_count": 0,
            "failed_count": 0,
            "failed_tests": [],
            "failure_fingerprint": "",
            "passed_count": 3696,
            "status": "passed",
            "timestamp": "2026-06-23T19:38:42.982271+00:00"
          }
        ],
        "last_command": "make test",
        "last_status": "passed"
      }
    }
  }
}
