Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ every deployment:
meanwhile started a second run on the same conversation. The conversation now picks the Bot's
work up as soon as it continues, so the answer streams in as it is written, and a message typed
while the Bot works waits for it, as it does when you never left.
- **A model nothing answers at says so, and where to change it.** With the AI model's address
pointing at nothing (a local model server that isn't running, a host that doesn't exist, or one
that never answers), the conversation said only "The Bot stopped before it finished answering. Try
again." It now says "OpenBot couldn't reach the AI model at host.docker.internal:48123" and what
to do: on OpenBot Desktop, check it's running or change the address in Settings under AI model;
elsewhere, ask an admin. Only the host and port are shown, never a key or password in the URL.
This covers built-in Bots and Bots on the LangChain and Claude Agent SDK harnesses. A task that
meets it stops rather than trying again. On the Claude Agent SDK harness the sentence can take
about three minutes to appear, because Claude Code retries the connection itself first.

### The server migrates and does the scheduled work itself

Expand Down
63 changes: 63 additions & 0 deletions agent-claude-sdk/src/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import json
import logging
import os
import re
from contextlib import suppress
from dataclasses import dataclass, field, replace
from typing import get_args
Expand Down Expand Up @@ -316,6 +317,58 @@ class ModelRefusedError(Exception):
"""


class ModelUnreachableError(Exception):
"""Nothing answered at the model's address: the connection was refused, the host doesn't
exist, or nothing answered in time. No provider said anything, so it is not a refusal.

FOUND ON OPENBOT DESKTOP: a base URL with nothing listening at it. Claude Code says so in its
own words, which went on as a refusal, and the person read that the provider "turned down the
request". It ends the run coded `OPENBOT_MODEL_UNREACHABLE`, and the server puts the step that
fits its edition in place of the second sentence.
"""


# Claude Code's own account of a request that never reached the API, as 2.1.259 (claude-agent-sdk
# 0.2.152) writes it: "API Error: " and then "Connection refused — a firewall or proxy may be blocking
# it (ConnectionRefused)", "Can't reach the API server — check your internet or DNS (ENOTFOUND)",
# "No internet route — …", "Unable to connect to API (…)" or "Request timed out. …". Not a dropped
# connection ("Connection dropped"), which reached the API, and not a certificate or a proxy the
# person has to fix ("Unable to connect to API: SSL …", "Couldn't connect through your proxy").
UNREACHED = re.compile(
r"^API Error: (?:Connection refused\b|Can't reach the API server\b|No internet route\b"
r"|Unable to connect to API\b(?!:)|Request timed out\b)"
)


def model_address():
"""Where Claude Code reaches the model, as host and port.

Nothing else of the URL: a base URL can carry a user and password, and some gateways put a key
in the path. Claude Code reads `ANTHROPIC_BASE_URL` from this container's environment.
"""
from urllib.parse import urlsplit

base = (os.environ.get("ANTHROPIC_BASE_URL") or "").strip() or "https://api.anthropic.com"
try:
parts = urlsplit(base)
host, port = parts.hostname, parts.port
except ValueError:
return None
if not host:
return None
shown = f"[{host}]" if ":" in host else host
return f"{shown}:{port}" if port else shown


def unreachable_message(address):
"""The same sentence agent-langgraph-agui ends its run with (`unreachable_message`)."""
at = f" at {address}" if address else ""
return (
f"OpenBot couldn't reach the AI model{at}. "
"Check that it's running and the address is right."
)


def refusal_text(message):
"""What Claude Code said about a refusal, or a name for it when it said nothing."""
text = " ".join(
Expand Down Expand Up @@ -907,6 +960,14 @@ async def messages():
raise ModelAuthenticationError(
"Sign in to your model provider again."
)
if (
isinstance(message, AssistantMessage)
and message.error
and UNREACHED.match(refusal_text(message))
):
# Claude Code's own words stay here, in the log: the sentence replaces them.
logger.warning("The model couldn't be reached: %s", refusal_text(message))
raise ModelUnreachableError(unreachable_message(model_address()))
if isinstance(message, AssistantMessage) and message.error:
raise ModelRefusedError(refusal_text(message))
if (
Expand Down Expand Up @@ -1070,6 +1131,8 @@ async def _pump(self, input_data, turn):
if isinstance(error, ModelAuthenticationError)
else "OPENBOT_MODEL_REFUSED"
if isinstance(error, ModelRefusedError)
else "OPENBOT_MODEL_UNREACHABLE"
if isinstance(error, ModelUnreachableError)
else None,
)
)
Expand Down
117 changes: 117 additions & 0 deletions agent-claude-sdk/tests/test_model_unreachable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""A model address with nothing answering at it is told as that, never as a refusal.

FOUND ON OPENBOT DESKTOP, on the LangGraph harness: a base URL at host.docker.internal:48123 with
nothing listening, and the person read only "The Bot stopped before it finished answering. Try
again." Claude Code meets the same address with an error message of its own, "API Error: Connection
refused — a firewall or proxy may be blocking it (ConnectionRefused)", which this harness passed on
as a provider's refusal and the server told as "Anthropic turned down the request". Nobody turned
anything down: nothing answered.

These run the real Claude Code this harness ships, through the harness's own routes, against a
loopback port with nothing listening and a host that never resolves.
"""

import asyncio
import importlib
import json
import socket
import sys
from pathlib import Path

import httpx
import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

TOKEN = "valid-test-token"
NEXT_STEP = "Check that it's running and the address is right."


def closed_port():
"""A loopback port with nothing listening on it."""
with socket.socket() as probe:
probe.bind(("127.0.0.1", 0))
return probe.getsockname()[1]


def app(monkeypatch, tmp_path, base_url):
# Claude Code itself, on a key, against an address nothing answers at, with its own home.
monkeypatch.setenv("ANTHROPIC_API_KEY", "synthetic-test-key")
monkeypatch.setenv("ANTHROPIC_BASE_URL", base_url)
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setenv("HOME", str(tmp_path))
monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / ".claude"))
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
from src import main

return importlib.reload(main).app


def run(app, path, body):
async def send():
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://harness", timeout=120
) as client:
return await client.post(
path,
headers={"x-openbot-agent-token": TOKEN, "accept": "text/event-stream"},
json=body,
)

response = asyncio.run(send())
assert response.status_code == 200
return [
json.loads(line.removeprefix("data: "))
for line in response.text.splitlines()
if line.startswith("data: ")
]


def ask(forwarded_props):
return {
"threadId": "thread-unreachable",
"runId": "run-unreachable",
"state": {},
"messages": [{"id": "user-1", "role": "user", "content": "Hello?"}],
"tools": [],
"context": [],
# Sent once: Claude Code's own ten retries would only make the test wait for the same end.
"forwardedProps": {**forwarded_props, "openbotSendOnce": True},
}


ROUTES = [
# A Claude Agent SDK Bot, on the harness's own route.
("/", {}),
# A built-in Bot on a Claude plan: the server asks the plan's model through here.
("/model", {"openbotModelOnly": True, "openbotBotId": "personal-assistant"}),
]


@pytest.mark.parametrize("path,props", ROUTES)
def test_a_model_address_with_nothing_listening_is_told_as_unreachable(
monkeypatch, tmp_path, path, props
):
port = closed_port()
events = run(app(monkeypatch, tmp_path, f"http://127.0.0.1:{port}"), path, ask(props))
assert events[-1]["type"] == "RUN_ERROR", [event["type"] for event in events]
# Coded, so the server says it with the step that fits its edition.
assert events[-1]["code"] == "OPENBOT_MODEL_UNREACHABLE"
assert events[-1]["message"] == (
f"OpenBot couldn't reach the AI model at 127.0.0.1:{port}. {NEXT_STEP}"
)
# Claude Code's own account is never the Bot's answer.
assert not [event for event in events if event["type"] == "TEXT_MESSAGE_CONTENT"]


def test_a_model_host_that_does_not_exist_is_told_the_same_way(monkeypatch, tmp_path):
# `.invalid` never resolves (RFC 2606), so this is a DNS failure and never leaves the machine.
# The address is host and port only: never the credentials a URL can carry.
base = "http://synthetic-user:synthetic-secret@openbot-model.invalid:48123"
events = run(app(monkeypatch, tmp_path, base), "/", ask({}))
assert events[-1]["type"] == "RUN_ERROR", [event["type"] for event in events]
assert events[-1]["code"] == "OPENBOT_MODEL_UNREACHABLE"
assert events[-1]["message"] == (
f"OpenBot couldn't reach the AI model at openbot-model.invalid:48123. {NEXT_STEP}"
)
assert "synthetic-secret" not in json.dumps(events)
84 changes: 84 additions & 0 deletions agent-langgraph-agui/src/tool_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,80 @@ def provider_message(error: BaseException) -> str:
return message


def _links(error: BaseException):
"""The error and every error it was raised from."""
seen = set()
while error is not None and id(error) not in seen:
seen.add(id(error))
yield error
error = error.__cause__


def model_unreachable(error: BaseException) -> bool:
"""Whether the run failed because nothing answered at the model's address.

The connection was refused, the host doesn't exist, or nothing answered in time: in each the
request never reached a model, so no provider said anything, and asking again a moment later
gets the same. Seen on OpenBot Desktop with an OpenAI-compatible base URL at
host.docker.internal:48123 and nothing listening there: `openai.APIConnectionError`, "All
connection attempts failed", and the person read only `RUN_FAILED`.

The SDKs' own classes, and httpx's for an SDK that lets them through (Google's). A tool's call
never raises one here: `_call_tool` answers its own transport errors.
"""
unreached = []
for module, names in (
("openai", ("APIConnectionError",)), # `APITimeoutError` is one.
("anthropic", ("APIConnectionError",)),
("httpx", ("ConnectError", "TimeoutException")),
("httpx2", ("ConnectError", "TimeoutException")),
):
try:
loaded = __import__(module)
except ImportError:
continue
unreached.extend(getattr(loaded, name) for name in names if hasattr(loaded, name))
return any(isinstance(link, tuple(unreached)) for link in _links(error))


def model_address(error: BaseException) -> str | None:
"""Where the run tried to reach the model, as host and port.

Nothing else of the URL: a base URL can carry a user and password, and some gateways put a key
in the path. None when the request isn't known, or when it went to OpenBot's own sign-in proxy,
whose address is OpenBot's and not the model's.
"""
for link in _links(error):
try:
request = getattr(link, "request", None)
except RuntimeError: # httpx raises it for an error that has no request.
request = None
url = getattr(request, "url", None)
host = getattr(url, "host", None)
if not isinstance(host, str) or not host:
continue
if "/api/model-provider/" in str(getattr(url, "path", "")):
return None
shown = f"[{host}]" if ":" in host else host
port = getattr(url, "port", None)
return f"{shown}:{port}" if port else shown
return None


# What the run ends with when nothing answered at the model's address, coded so the server can put
# the step that fits its edition in place of the second sentence (it reads the address back out of
# the first). The step here is right wherever the model is set.
MODEL_UNREACHABLE = "OPENBOT_MODEL_UNREACHABLE"


def unreachable_message(address: str | None) -> str:
at = f" at {address}" if address else ""
return (
f"OpenBot couldn't reach the AI model{at}. "
"Check that it's running and the address is right."
)


# What a person reads when a run fails for a reason that is not the provider's.
RUN_FAILED = "The Bot stopped before it finished answering. Try again."

Expand Down Expand Up @@ -309,6 +383,16 @@ async def run(self, input: RunAgentInput):
message="Sign in to your model provider again.",
code="OPENBOT_MODEL_AUTH_REQUIRED",
)
elif model_unreachable(error):
# Nothing answered, so there is no provider's account to pass on: the person is
# told where OpenBot looked. The SDK's own words ("Connection error.") are the log's.
address = model_address(error)
logger.warning("The model couldn't be reached at %s: %r", address, error)
yield RunErrorEvent(
type=EventType.RUN_ERROR,
message=unreachable_message(address),
code=MODEL_UNREACHABLE,
)
elif provider_refusal(error):
# Raised, it ended the response stream after RUN_STARTED with no terminal event,
# and the provider's reason never reached anyone. Coded as a refusal, so the server
Expand Down
62 changes: 62 additions & 0 deletions agent-langgraph-agui/tests/test_tool_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,68 @@ async def test_a_provider_out_of_credit_is_told_as_that_never_as_sign_in_again(
assert said in message


def closed_port():
"""A loopback port with nothing listening on it."""
import socket

with socket.socket() as probe:
probe.bind(("127.0.0.1", 0))
return probe.getsockname()[1]


@pytest.mark.asyncio
async def test_a_model_address_with_nothing_listening_is_told_as_unreachable(
boundary, monkeypatch
):
"""Seen on OpenBot Desktop: an OpenAI-compatible base URL at host.docker.internal:48123 with
nothing listening. The harness log said `openai.APIConnectionError` ("All connection attempts
failed") and the person read only "The Bot stopped before it finished answering. Try again.",
which says nothing about the model or where to change it."""
port = closed_port()
monkeypatch.setenv("OPENAI_BASE_URL", f"http://synthetic-user:synthetic-secret@127.0.0.1:{port}/v1")
events = await run_protocol(run_input([]), allow_error=True)
assert events[-1]["type"] == "RUN_ERROR", "every run ends with a terminal event"
# Coded, so the server says it with the step that fits its edition.
assert events[-1]["code"] == "OPENBOT_MODEL_UNREACHABLE"
assert events[-1]["message"] == (
f"OpenBot couldn't reach the AI model at 127.0.0.1:{port}. "
"Check that it's running and the address is right."
)
# The address is host and port only: never the credentials or the path a URL can carry.
assert "synthetic-secret" not in json.dumps(events)
assert not boundary["model"], "nothing answered, so nothing was asked"


@pytest.mark.asyncio
async def test_a_model_host_that_does_not_exist_is_told_the_same_way(boundary, monkeypatch):
# `.invalid` never resolves (RFC 2606), so this is a DNS failure and never leaves the machine.
monkeypatch.setenv("OPENAI_BASE_URL", "http://openbot-model.invalid:48123/v1")
events = await run_protocol(run_input([]), allow_error=True)
assert events[-1]["type"] == "RUN_ERROR"
assert events[-1]["code"] == "OPENBOT_MODEL_UNREACHABLE"
assert events[-1]["message"] == (
"OpenBot couldn't reach the AI model at openbot-model.invalid:48123. "
"Check that it's running and the address is right."
)


def test_a_model_that_never_answers_in_time_is_unreachable_too():
"""A timeout is the third way an address can't be reached, and is told the same way."""
import openai

from src.tool_runtime import model_address, model_unreachable

request = httpx.Request("POST", "http://10.0.0.7:8080/v1/chat/completions")
timeout = openai.APITimeoutError(request=request)
assert model_unreachable(timeout)
assert model_address(timeout) == "10.0.0.7:8080"
# A provider that answered, with whatever status, is not unreachable: it said something.
refusal = openai.APIStatusError(
"Error code: 503", response=httpx.Response(503, request=request), body=None
)
assert not model_unreachable(refusal)


def test_the_provider_is_named_by_who_serves_the_model(monkeypatch):
from src.tool_runtime import provider_name

Expand Down
Loading