From 50fcf5c077a67fd51af7a48bc589296e9d7f1675 Mon Sep 17 00:00:00 2001 From: Fusion Date: Wed, 20 May 2026 18:11:33 +0200 Subject: [PATCH] fix(tunnels): skip tunnel creation for terminal-only tools --- apps/api/src/api/tool_instances.py | 136 ++++++++++++++++------------- 1 file changed, 75 insertions(+), 61 deletions(-) diff --git a/apps/api/src/api/tool_instances.py b/apps/api/src/api/tool_instances.py index d26e6de..eb2b62f 100644 --- a/apps/api/src/api/tool_instances.py +++ b/apps/api/src/api/tool_instances.py @@ -437,44 +437,52 @@ async def start_instance( } instance_port = tool_type.default_port - logger.info("Tool type for instance %s: name=%s, default_port=%s", - instance.id, tool_type.name, instance_port) + logger.info("Tool type for instance %s: name=%s, default_port=%s, interfaces=%s", + instance.id, tool_type.name, instance_port, tool_type.interfaces) - # Create temporary Cloudflare tunnel for public access - try: - logger.info("Creating temporary tunnel for instance %s (container=%s, port=%d)", - instance.id, instance.container_name, instance_port) - tunnel_info = start_cloudflared_tunnel( - container_name=instance.container_name or instance.name, - port=instance_port, - ) - instance.tunnel_id = tunnel_info["pid"] - instance.public_url = tunnel_info["url"] - instance.url = tunnel_info["url"] - await session.commit() - logger.info( - "Created temporary tunnel for instance %s: pid=%s, url=%s", - instance.id, - tunnel_info["pid"], - tunnel_info["url"], - ) - except Exception as exc: - import traceback - error_msg = str(exc) - error_trace = traceback.format_exc() - logger.error( - "Failed to create tunnel for instance %s: %s\nTraceback:\n%s", - instance.id, - error_msg, - error_trace, - ) - instance.status = "error" + # Only create Cloudflare tunnel for web-enabled tools + if "web" in tool_type.interfaces: + # Create temporary Cloudflare tunnel for public access + try: + logger.info("Creating temporary tunnel for instance %s (container=%s, port=%d)", + instance.id, instance.container_name, instance_port) + tunnel_info = start_cloudflared_tunnel( + container_name=instance.container_name or instance.name, + port=instance_port, + ) + instance.tunnel_id = tunnel_info["pid"] + instance.public_url = tunnel_info["url"] + instance.url = tunnel_info["url"] + await session.commit() + logger.info( + "Created temporary tunnel for instance %s: pid=%s, url=%s", + instance.id, + tunnel_info["pid"], + tunnel_info["url"], + ) + except Exception as exc: + import traceback + error_msg = str(exc) + error_trace = traceback.format_exc() + logger.error( + "Failed to create tunnel for instance %s: %s\nTraceback:\n%s", + instance.id, + error_msg, + error_trace, + ) + instance.status = "error" + instance.url = None + await session.commit() + return { + "status": "error", + "error": f"Failed to create tunnel: {error_msg}", + } + else: + # Terminal-only tool - no tunnel needed + logger.info("Instance %s is terminal-only (no web interface), skipping tunnel creation", instance.id) instance.url = None + instance.public_url = None await session.commit() - return { - "status": "error", - "error": f"Failed to create tunnel: {error_msg}", - } return {"status": instance.status, "url": instance.url} @@ -597,33 +605,39 @@ async def restart_instance( instance_port = tool_type.default_port - # Create new temporary tunnel - try: - tunnel_info = start_cloudflared_tunnel( - container_name=instance.container_name or instance.name, - port=instance_port, - ) - instance.tunnel_id = tunnel_info["pid"] - instance.public_url = tunnel_info["url"] - instance.url = tunnel_info["url"] - logger.info( - "Created new tunnel for instance %s: %s", - instance.id, - tunnel_info["url"], - ) - except Exception as exc: - logger.warning( - "Failed to create tunnel for instance %s: %s", - instance.id, - exc, - ) - instance.status = "error" + # Only create tunnel for web-enabled tools + if "web" in tool_type.interfaces: + # Create new temporary tunnel + try: + tunnel_info = start_cloudflared_tunnel( + container_name=instance.container_name or instance.name, + port=instance_port, + ) + instance.tunnel_id = tunnel_info["pid"] + instance.public_url = tunnel_info["url"] + instance.url = tunnel_info["url"] + logger.info( + "Created new tunnel for instance %s: %s", + instance.id, + tunnel_info["url"], + ) + except Exception as exc: + logger.warning( + "Failed to create tunnel for instance %s: %s", + instance.id, + exc, + ) + instance.status = "error" + instance.url = None + await session.commit() + return { + "status": "error", + "error": f"Failed to create tunnel: {exc}", + } + else: + # Terminal-only tool instance.url = None - await session.commit() - return { - "status": "error", - "error": f"Failed to create tunnel: {exc}", - } + instance.public_url = None await session.commit() return {"status": instance.status, "url": instance.url}