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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
backed up (`backup.kind`), the agent refuses every upgrade that changes the database, before
touching anything.

### Bots and models

- **Every harness OpenBot ships calls the model gateway with its run's own token, where the server
sends one.** One harness serves every organization of an OpenBot, and the key in its environment
names one organization. A run that brings its own token (`forwardedProps.openbotModelRoute`; on
Mastra the `OpenBot model route` context entry) calls the gateway with it, so the call is answered
on that run's organization's AI connection. CrewAI, LlamaIndex, Agno, LangChain, Google ADK,
Pydantic AI, Microsoft Agent Framework, Strands, AG2, Langroid, the Claude Agent SDK on a key and
Mastra all do. Only calls to the gateway carry it, and the token never reaches the model or a
framework's own state. A run that brings none keeps the environment's key, so OpenBot Desktop
behaves as before.
- **A Claude plan's query is kept per organization on the LangGraph (TypeScript) engine.** The same
conversation id for a Bot of the same id in two organizations is two Claude queries now, not one
shared transcript. A run with no organization keeps the query name it always had.

## 1.1.2

**Before upgrading.** Nothing in a Helm values file, a Compose override or a script has to change,
Expand Down
2 changes: 1 addition & 1 deletion agent-adk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-adk/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-adk/src ./src
COPY shared/model_providers.py shared/model-providers.json /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py /shared/

ENV PORT=4208
EXPOSE 4208
Expand Down
3 changes: 3 additions & 0 deletions agent-adk/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# one level above /app/src in the image the Dockerfile builds.
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import bot_settings
from run_model_key import RunModelKey

from ag_ui.core import (
AssistantMessage,
Expand Down Expand Up @@ -343,6 +344,8 @@ def _model_id() -> str:


app = FastAPI()
# Each run's model calls carry its own gateway token, in its organization. See run_model_key.py.
app.add_middleware(RunModelKey)


@app.middleware("http")
Expand Down
21 changes: 21 additions & 0 deletions agent-adk/tests/test_conversation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
LATEST_QUESTION,
STANDING_ROLE,
anthropic_environment,
check_gateway_keys,
conversation,
fake_provider,
missing_from,
Expand Down Expand Up @@ -119,6 +120,26 @@ async def turns():
return asyncio.run(asyncio.wait_for(turns(), timeout=60))


@pytest.mark.parametrize("wire", ["openai", "anthropic"])
def test_a_run_calls_the_gateway_with_its_own_key(monkeypatch, wire):
"""The run's own gateway token, in its organization; the environment's without one.

See `shared/run_model_key.py`. Gemini goes to Google with its own key, never the gateway.
"""

def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
monkeypatch.setenv("OTEL_SDK_DISABLED", "true")
for key, value in environment.items():
monkeypatch.setenv(key, value)
from src import main

return importlib.reload(main).app

check_gateway_keys(app_for, wire, token=TOKEN)


@pytest.mark.parametrize("environment", [openai_environment, anthropic_environment])
def test_a_fresh_process_answers_from_the_whole_thread_in_its_role(monkeypatch, environment):
with fake_provider() as (base, bodies):
Expand Down
2 changes: 1 addition & 1 deletion agent-ag2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-ag2/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-ag2/src ./src
COPY shared/model_providers.py shared/model-providers.json /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py /shared/

ENV PORT=4210
EXPOSE 4210
Expand Down
3 changes: 3 additions & 0 deletions agent-ag2/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# one level above /app/src in the image the Dockerfile builds.
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import bot_settings, openai_api
from run_model_key import RunModelKey

from ag2 import Agent
from ag2.ag_ui import AGUIStream
Expand Down Expand Up @@ -55,6 +56,8 @@ def _config() -> AnthropicConfig | OpenAICompletionsConfig | OpenAIResponsesConf
stream = AGUIStream(agent)

app = FastAPI()
# Each run's model calls carry its own gateway token, in its organization. See run_model_key.py.
app.add_middleware(RunModelKey)


@app.middleware("http")
Expand Down
21 changes: 21 additions & 0 deletions agent-ag2/tests/test_conversation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from harness_contract import (
anthropic_environment,
check_gateway_keys,
conversation,
fake_provider,
missing_from,
Expand Down Expand Up @@ -47,6 +48,26 @@ def _run(monkeypatch, environment, body):
return bodies[-1]


@pytest.mark.parametrize("wire", ['openai', 'anthropic'])
def test_a_run_calls_the_gateway_with_its_own_key(monkeypatch, wire):
"""The run's own gateway token, in its organization; the environment's without one.

See `shared/run_model_key.py`.
"""

def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
for key, value in environment.items():
monkeypatch.setenv(key, value)
from src import main

return importlib.reload(main).app

check_gateway_keys(app_for, wire, path="/", token=TOKEN)


@pytest.mark.parametrize("environment", [openai_environment, anthropic_environment])
def test_a_fresh_process_answers_from_the_whole_thread_in_its_role(monkeypatch, environment):
assert missing_from(_run(monkeypatch, environment, conversation())) == []
2 changes: 1 addition & 1 deletion agent-agno/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-agno/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-agno/src ./src
COPY shared/model_providers.py shared/model-providers.json /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py /shared/

ENV PORT=4203
EXPOSE 4203
Expand Down
3 changes: 3 additions & 0 deletions agent-agno/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# one level above /app/src in the image the Dockerfile builds.
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import bot_settings
from run_model_key import RunModelKey

from agno.agent import Agent
from agno.models.litellm import LiteLLM
Expand Down Expand Up @@ -81,6 +82,8 @@ def _sampling() -> dict:
)

app = AgentOS(agents=[agent], interfaces=[AGUI(agent=agent)]).get_app()
# Each run's model calls carry its own gateway token, in its organization. See run_model_key.py.
app.add_middleware(RunModelKey)


@app.middleware("http")
Expand Down
21 changes: 21 additions & 0 deletions agent-agno/tests/test_conversation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from harness_contract import (
anthropic_environment,
check_gateway_keys,
conversation,
fake_provider,
missing_from,
Expand Down Expand Up @@ -48,6 +49,26 @@ def _run(monkeypatch, environment, body):
return bodies[-1]


@pytest.mark.parametrize("wire", ['openai', 'anthropic'])
def test_a_run_calls_the_gateway_with_its_own_key(monkeypatch, wire):
"""The run's own gateway token, in its organization; the environment's without one.

See `shared/run_model_key.py`.
"""

def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
for key, value in environment.items():
monkeypatch.setenv(key, value)
from src import main

return importlib.reload(main).app

check_gateway_keys(app_for, wire, path="/agui", token=TOKEN)


@pytest.mark.parametrize("environment", [openai_environment, anthropic_environment])
def test_a_fresh_process_answers_from_the_whole_thread_in_its_role(monkeypatch, environment):
assert missing_from(_run(monkeypatch, environment, conversation())) == []
Expand Down
2 changes: 1 addition & 1 deletion agent-antigravity/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ COPY agent-antigravity/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-antigravity/src ./src
COPY shared/model_providers.py shared/model-providers.json /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py /shared/

ENV PORT=4214
EXPOSE 4214
Expand Down
4 changes: 4 additions & 0 deletions agent-antigravity/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# one level above /app/src in the image the Dockerfile builds.
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import SPEC, bot_settings
from run_model_key import RunModelKey

from ag_ui_antigravity import create_antigravity_app
from fastapi import Request
Expand Down Expand Up @@ -95,6 +96,9 @@ def _agent() -> OpenBotAntigravityAgent:


app = create_antigravity_app(_agent(), path="/")
# Gemini is reached with Google's own key, never OpenBot's model gateway, so no run's token is ever
# sent anywhere from here; the server's props still never reach the agent. See run_model_key.py.
app.add_middleware(RunModelKey)


@app.middleware("http")
Expand Down
14 changes: 14 additions & 0 deletions agent-antigravity/tests/test_conversation_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
from harness_contract import (
EARLIER_QUESTION,
LATEST_QUESTION,
RUN_KEY,
STANDING_ROLE,
conversation,
missing_from,
run_events,
with_run_key,
)

TOKEN = "contract-token"
Expand Down Expand Up @@ -177,6 +179,18 @@ def test_a_fresh_process_answers_from_the_whole_thread_in_its_role(monkeypatch,
assert STANDING_ROLE in _system_instruction(body), "the standing role is a system instruction"


def test_a_runs_gateway_token_never_reaches_gemini(monkeypatch, tmp_path):
"""Gemini is reached with Google's own key: the run's token for OpenBot's gateway goes nowhere.

See `shared/run_model_key.py`.
"""
with fresh_bot(monkeypatch, tmp_path) as run:
body = run(with_run_key(conversation(), "openai"))
assert missing_from(body) == []
assert RUN_KEY not in json.dumps(run.bodies)
assert "contract-organization" not in json.dumps(run.bodies)


def test_a_live_session_is_not_sent_the_history_twice(monkeypatch, tmp_path):
first = conversation()
with fresh_bot(monkeypatch, tmp_path) as run:
Expand Down
2 changes: 2 additions & 0 deletions agent-claude-sdk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ COPY agent-claude-sdk/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-claude-sdk/src ./src
# A run's own key to OpenBot's model gateway, shared by every Python harness (src/run_model_proxy.py).
COPY shared/run_model_key.py /shared/

ENV PORT=4212
EXPOSE 4212
Expand Down
2 changes: 2 additions & 0 deletions agent-claude-sdk/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ag-ui-claude-sdk==0.1.5
claude-agent-sdk==0.2.182
fastapi
# Passes Claude Code's model calls on to OpenBot's model gateway (src/run_model_proxy.py).
httpx>=0.28.1,<0.29
python-multipart
uvicorn[standard]
11 changes: 10 additions & 1 deletion agent-claude-sdk/src/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from claude_agent_sdk.types import StreamEvent

from .replay import RESULT_CHARS, cut_note, run_replayed_query, system_instructions
from .run_model_proxy import query_environment, refresh

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -827,7 +828,13 @@ def build_options(self, input_data=None, thread_id=None):
if chosen:
# After the upstream merge, so the server's choice wins over a forwarded `model`.
options.model, options.effort = chosen
options.env = {**(options.env or {}), UNKNOWN_MODEL_WINDOW: "1"}
# On the gateway, the query's calls come back through this harness with the newest run
# token of its thread. See run_model_proxy.py.
options.env = {
**(options.env or {}),
UNKNOWN_MODEL_WINDOW: "1",
**query_environment(thread_id),
}
if once:
options.env[MAX_RETRIES] = "0"
options.max_buffer_size = MESSAGE_BUFFER_BYTES
Expand Down Expand Up @@ -1291,6 +1298,8 @@ async def _pump_query(self, input_data, turn, fit_results=False):

async def run(self, input_data):
thread_id, run_id = input_data.thread_id, input_data.run_id
# A query this run carries on sends its calls with this run's token from now.
refresh(thread_id)
lock = self._segment_locks.setdefault(thread_id, asyncio.Lock())
async with lock:
answer_only = (
Expand Down
13 changes: 12 additions & 1 deletion agent-claude-sdk/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from pydantic import BaseModel, Field

from .adapter import OpenBotClaudeAgentAdapter
from .run_model_proxy import PROXY_PATH, forward
from run_model_key import RunModelKey # On the path run_model_proxy puts shared/ on.

TOKEN_HEADER = "x-openbot-agent-token"

Expand Down Expand Up @@ -44,11 +46,15 @@ def _refuse_both_credentials() -> None:
_refuse_both_credentials()

app = FastAPI()
# Each run's model calls carry its own gateway token, in its organization. See run_model_proxy.py.
app.add_middleware(RunModelKey)


@app.middleware("http")
async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
# Claude Code's own model calls come back here on a secret path of their query's, from this
# container's loopback, and carry no server token. See run_model_proxy.py.
if request.url.path != "/health" and not request.url.path.startswith(PROXY_PATH):
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
Expand All @@ -61,6 +67,11 @@ async def health():
return {"ok": True, "harness": "claude-agent-sdk"}


@app.api_route(PROXY_PATH + "{secret}/{path:path}", methods=["GET", "POST"])
async def run_model(request: Request, secret: str, path: str):
return await forward(request, secret, path)


add_claude_fastapi_endpoint(
app=app,
adapter=OpenBotClaudeAgentAdapter(name="openbot"),
Expand Down
Loading
Loading