diff --git a/pathwaysutils/experimental/shared_pathways_service/isc_pathways.py b/pathwaysutils/experimental/shared_pathways_service/isc_pathways.py index a628928..e730240 100644 --- a/pathwaysutils/experimental/shared_pathways_service/isc_pathways.py +++ b/pathwaysutils/experimental/shared_pathways_service/isc_pathways.py @@ -380,6 +380,35 @@ def _get_total_chips(self) -> int: total_chips += chips_per_instance * count return total_chips + @property + def proxy_job_name(self) -> str: + """The name of the GKE job that runs this session's Pathways proxy.""" + return self._proxy_job_name + + @property + def proxy_port(self) -> int | None: + """The local port forwarded to the proxy, or None if not connected.""" + return self._proxy_port + + @property + def backend_target(self) -> str | None: + """The JAX backend target (`grpc://127.0.0.1:`), or None.""" + if self._proxy_port is None: + return None + return f"{_JAX_BACKEND_TARGET_HOSTNAME}:{self._proxy_port}" + + def is_alive(self) -> bool: + """Returns True if the session is connected and port forwarding is running. + + Long-lived callers that keep one session open across many workloads can use + this to detect a dropped port-forward (e.g. after the proxy pod restarts) + and reconnect. + """ + return ( + self._port_forward_process is not None + and self._port_forward_process.poll() is None + ) + def __enter__(self): """Enters the context manager, ensuring cluster exists.""" atexit.register(self._cleanup) diff --git a/pathwaysutils/test/experimental/shared_pathways_service/isc_pathways_test.py b/pathwaysutils/test/experimental/shared_pathways_service/isc_pathways_test.py index aabb385..6e16546 100644 --- a/pathwaysutils/test/experimental/shared_pathways_service/isc_pathways_test.py +++ b/pathwaysutils/test/experimental/shared_pathways_service/isc_pathways_test.py @@ -1506,6 +1506,46 @@ def test_cleanup_terminates_port_forward_and_log_processes(self): self.assertIsNone(manager._port_forward_process) self.assertIsNone(manager._log_process) + def test_public_accessors_before_and_after_connect(self): + manager = isc_pathways._ISCPathways( + cluster="test-cluster", + project="test-project", + region="test-region", + gcs_bucket="test-bucket", + pathways_service="test-service:1234", + expected_tpu_instances={"tpuv6e:2x2": 1}, + proxy_job_name="test-proxy", + proxy_server_image="test-image", + ) + self.assertEqual(manager.proxy_job_name, "test-proxy") + self.assertIsNone(manager.proxy_port) + self.assertIsNone(manager.backend_target) + self.assertFalse(manager.is_alive()) + + mock_pf_proc = mock.create_autospec(subprocess.Popen, instance=True) + mock_pf_proc.poll.return_value = None + manager._proxy_port = 29005 + manager._port_forward_process = mock_pf_proc + self.assertEqual(manager.proxy_port, 29005) + self.assertEqual(manager.backend_target, "grpc://127.0.0.1:29005") + self.assertTrue(manager.is_alive()) + + def test_is_alive_false_after_port_forward_exits(self): + manager = isc_pathways._ISCPathways( + cluster="test-cluster", + project="test-project", + region="test-region", + gcs_bucket="test-bucket", + pathways_service="test-service:1234", + expected_tpu_instances={"tpuv6e:2x2": 1}, + proxy_job_name="test-proxy", + proxy_server_image="test-image", + ) + mock_pf_proc = mock.create_autospec(subprocess.Popen, instance=True) + mock_pf_proc.poll.return_value = 1 + manager._port_forward_process = mock_pf_proc + self.assertFalse(manager.is_alive()) + def test_connect_auto_detect_proxy_image_success(self): """Tests that connect auto-detects compatible proxy image when not provided.""" self.enter_context(mock.patch.dict(os.environ, {"USER": "testuser"}))