Repository navigation
AWS Strands harness as a Bot #2326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2a44222
d027329
824aad8
cda0a76
f328c25
4cfbe89
abd16d4
2eb925d
e085315
95500d9
1a2ea0d
a97deb7
07159b3
6212c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # The AWS Strands harness, as a Bot. The Strands Bot's image with strands-harness added and a | ||
| # different agent mounted; what the two share is copied from shared/. | ||
| FROM python:3.12-slim | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Dependencies before source, so editing the Bot does not re-resolve the framework. | ||
| COPY agent-strands-harness/requirements.txt ./ | ||
| RUN pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| COPY agent-strands-harness/src ./src | ||
| COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py \ | ||
| shared/gemini_signatures.py shared/empty_turns.py \ | ||
| shared/strands_bot.py shared/strands_system_prompt.py /shared/ | ||
|
|
||
| ENV PORT=4217 | ||
| EXPOSE 4217 | ||
| # 0.0.0.0, not `::`. See agent-strands/Dockerfile. | ||
| CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "4217"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| httpx==0.28.1 | ||
| pytest==9.0.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # StrandsHarnessAgent is in the release after ag-ui-strands 1.0.4. Until that is published this | ||
| # line resolves to a release without it and the Bot fails at import; set the floor to the release | ||
| # that carries it when it lands. | ||
| ag-ui-strands | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI installs
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. This stays a draft until |
||
| strands-agents | ||
| strands-harness | ||
| # See agent-strands/requirements.txt: 1.106.0 fails every model call in this harness. | ||
| litellm<1.106.0 | ||
| fastapi | ||
| python-multipart | ||
| uvicorn[standard] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """The AWS Strands harness as a Bot, through `ag_ui_strands`, which AG-UI maintains. | ||
|
|
||
| `agent-strands` is a bare `strands.Agent`: a model and whatever OpenBot grants it. The harness is | ||
| the same class with Amazon's agent assembled on it (context management, its own tools, delegation | ||
| to subagents, approvals), built by `strands_harness.create_harness`. Everything a Bot on Strands | ||
| shares is in `shared/strands_bot.py`; what is here is only what makes this one the harness. | ||
|
|
||
| ONE HARNESS PER CONVERSATION. `StrandsHarnessAgent` calls `create_harness` for each thread, where | ||
| `StrandsAgent` would copy one agent. A harness does not survive being copied: its context manager | ||
| and background tasks stay with the agent they were built on. | ||
|
|
||
| WHAT IS OPENBOT'S STAYS OPENBOT'S. The harness would keep memory, skills and sessions of its own, | ||
| in files beside it. A Bot's memory and skills arrive with every run and its conversations are | ||
| stored by Intelligence, so those three are off. So is the harness's note about the machine it runs | ||
| on, which would describe this container, not the Bot's computer. | ||
|
|
||
| ITS OWN TOOLS ARE OFF. The harness can run commands, edit files and reach the web where it runs. | ||
| Here that is this container, past the server's gateway: no grant, no policy and no audit row. A | ||
| Bot's computer is the one OpenBot gives it, through tools the server checks. There is no switch | ||
| for them: letting a Bot act outside the gateway is a boundary for OpenBot to decide, not a | ||
| deployment's environment. With none of them on there is nothing for the harness's own approval | ||
| gate to ask about, so that is off as well. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # The files every language in the box reads, and what this Bot shares with the Strands Bot: one | ||
| # level above this Bot in the repository, and one level above /app/src in the image the Dockerfile | ||
| # builds. | ||
| sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared")) | ||
| from strands_bot import WithoutForwardedProps, WordsAsSaid, create_app, model_id, serve | ||
| from strands_system_prompt import RunSystemPrompt | ||
|
|
||
| from ag_ui_strands import StrandsAgentConfig, StrandsHarnessAgent | ||
| from strands_harness import HARNESS_CONTRACT | ||
|
|
||
| # The model row this Bot runs on is the Strands Bot's: the same framework, the same clients, the | ||
| # same models verified against them. | ||
| BOT = "agent-strands" | ||
|
|
||
|
|
||
| def _model_id() -> str: | ||
| return model_id(BOT) | ||
|
|
||
|
|
||
| def harness_options() -> dict: | ||
| """What `create_harness` is given for every conversation. | ||
|
|
||
| `STRANDS_HARNESS_MODEL` names a model the harness reaches itself, as `provider/name` or a bare | ||
| Amazon Bedrock model id, with the credentials of this process. That is the AWS route: Claude | ||
| on Bedrock under the task's own role. Without it the model is OpenBot's, through the server's | ||
| gateway on each run's own key, exactly as the Strands Bot's is. | ||
|
|
||
| None of the harness's own tools (`shell`, `read`, `write`, `edit`, `web_fetch`, `web_search`, | ||
| `programmatic_tool_caller`, `subagent`): see the module. What OpenBot grants reaches the agent | ||
| as tools the client runs, which are the server's to check, with its own rules, card and audit | ||
| row. | ||
| """ | ||
| return { | ||
| "model": (os.environ.get("STRANDS_HARNESS_MODEL") or "").strip() | ||
| or WordsAsSaid(model_id=_model_id()), | ||
| "builtin_tools": [], | ||
| "builtin_plugins": [], | ||
| "memory": False, | ||
| "skills": False, | ||
| } | ||
|
|
||
|
|
||
| # How long a conversation's harness is kept with nobody talking in it. A harness is a whole agent | ||
| # per conversation, and the integration keeps each until told otherwise, so without this the | ||
| # process holds every conversation it has ever had. One let go is built again from the messages | ||
| # OpenBot sends with the next run. One still waiting on an approval is kept until it is answered. | ||
| IDLE_SECONDS = 30 * 60 | ||
|
|
||
|
|
||
| class Bot(WithoutForwardedProps, StrandsHarnessAgent): | ||
| """The integration's harness agent, minus a run's forwarded props.""" | ||
|
|
||
|
|
||
| app = create_app("strands-harness") | ||
| serve( | ||
| app, | ||
| Bot( | ||
| name="openbot", | ||
| harness=harness_options(), | ||
| config=StrandsAgentConfig(thread_idle_timeout=IDLE_SECONDS), | ||
| # The harness's own contract first, then the role OpenBot sends with each run. | ||
| hooks=[RunSystemPrompt(HARNESS_CONTRACT)], | ||
| ), | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """The Strands Bot's own suites, run against this Bot. | ||
|
|
||
| The harness Bot is the Strands Bot with a different agent mounted, so what OpenBot asks of one it | ||
| asks of the other: the role in the system prompt, every other row as conversation data, the run's | ||
| own key on its model calls, a model name sent verbatim, forwarded props kept out. Those are written | ||
| once, in `agent-strands/tests`, against whatever `src.main` resolves to. Loading one of them from | ||
| here and then putting this Bot first on the path makes that this Bot's. | ||
| """ | ||
|
|
||
| import importlib.util | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
| BOT = Path(__file__).resolve().parents[1] | ||
|
|
||
|
|
||
| def load(name: str) -> dict: | ||
| """The tests of `agent-strands/tests/<name>.py`, to be run from the module that calls this.""" | ||
| spec = importlib.util.spec_from_file_location( | ||
| f"strands_bot_{name}", ROOT / "agent-strands" / "tests" / f"{name}.py" | ||
| ) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| # The suite put its own Bot first on the path as it loaded. `src.main` is imported when a test | ||
| # runs, so whichever Bot is first then is the one tested. | ||
| sys.path.insert(0, str(BOT)) | ||
| sys.modules.pop("src.main", None) | ||
| sys.modules.pop("src", None) | ||
| return {key: value for key, value in vars(module).items() if not key.startswith("__")} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """`agent-strands/tests/test_conversation_contract.py`, against the harness Bot. See `strands_bot_suite.py`.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | ||
| from strands_bot_suite import load | ||
|
|
||
| globals().update(load("test_conversation_contract")) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """`agent-strands/tests/test_forwarded_props_stay_out.py`, against the harness Bot. See `strands_bot_suite.py`.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | ||
| from strands_bot_suite import load | ||
|
|
||
| globals().update(load("test_forwarded_props_stay_out")) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| """What makes this Bot the harness, and what it leaves to OpenBot.""" | ||
|
|
||
| import importlib | ||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from fastapi.testclient import TestClient | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | ||
| sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared")) | ||
| from harness_contract import conversation, fake_provider, openai_environment | ||
|
|
||
| TOKEN = "contract-token" | ||
| HARNESS_VARIABLES = ("STRANDS_HARNESS_MODEL",) | ||
|
|
||
|
|
||
| def _main(monkeypatch, base, **harness): | ||
| monkeypatch.delenv("OPENAI_API_BASE", raising=False) | ||
| monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN) | ||
| monkeypatch.setenv("OTEL_SDK_DISABLED", "true") | ||
| for key in HARNESS_VARIABLES: | ||
| monkeypatch.delenv(key, raising=False) | ||
| for key, value in {**openai_environment(base), **harness}.items(): | ||
| monkeypatch.setenv(key, value) | ||
| sys.modules.pop("src.main", None) | ||
| sys.modules.pop("src", None) | ||
| return importlib.import_module("src.main") | ||
|
|
||
|
|
||
| def _run(main, thread="harness-thread"): | ||
| response = TestClient(main.app).post( | ||
| "/", json=conversation(thread), headers={"x-openbot-agent-token": TOKEN} | ||
| ) | ||
| assert response.status_code == 200, response.text | ||
| events = [json.loads(line[5:]) for line in response.text.splitlines() if line.startswith("data:")] | ||
| assert not [event for event in events if event["type"] == "RUN_ERROR"], events | ||
| return events | ||
|
|
||
|
|
||
| def _offered(body: dict) -> list[str]: | ||
| return sorted(tool["function"]["name"] for tool in body.get("tools") or []) | ||
|
|
||
|
|
||
| def test_health_names_the_harness(monkeypatch): | ||
| with fake_provider() as (base, _bodies): | ||
| main = _main(monkeypatch, base) | ||
| assert TestClient(main.app).get("/health").json() == {"ok": True, "harness": "strands-harness"} | ||
|
|
||
|
|
||
| def test_each_conversation_gets_a_harness_of_its_own(monkeypatch): | ||
| with fake_provider() as (base, _bodies): | ||
| main = _main(monkeypatch, base) | ||
| _run(main, "one") | ||
| _run(main, "two") | ||
| # The agent is closed over by its route, so it is found by its class. | ||
| agents = [agent for bot in _bots(main) for agent in bot._agents_by_thread.values()] | ||
| assert len(agents) == 2 | ||
| one, two = agents | ||
| assert one is not two | ||
| # What copying one agent would have lost, and what makes it the harness. | ||
| assert one.context_manager is not None | ||
| assert one.context_manager is not two.context_manager | ||
|
|
||
|
|
||
| def _bots(main): | ||
| import gc | ||
|
|
||
| return [obj for obj in gc.get_objects() if type(obj) is main.Bot] | ||
|
|
||
|
|
||
| def test_the_harness_contract_comes_before_the_role_openbot_sends(monkeypatch): | ||
| from strands_harness import HARNESS_CONTRACT | ||
|
|
||
| with fake_provider() as (base, bodies): | ||
| _run(_main(monkeypatch, base)) | ||
| system = "\n".join( | ||
| part.get("text", "") if isinstance(part, dict) else str(part) | ||
| for message in bodies[-1]["messages"] | ||
| if message["role"] == "system" | ||
| for part in (message["content"] if isinstance(message["content"], list) else [message["content"]]) | ||
| ) | ||
| from harness_contract import STANDING_ROLE | ||
|
|
||
| assert HARNESS_CONTRACT.splitlines()[0] in system | ||
| assert system.index(HARNESS_CONTRACT.splitlines()[0]) < system.index(STANDING_ROLE) | ||
|
|
||
|
|
||
| def test_none_of_the_harness_tools_that_act_here_is_offered_and_no_variable_turns_them_on(monkeypatch): | ||
| """They would run in this container, past the server's gateway.""" | ||
| monkeypatch.setenv("STRANDS_HARNESS_TOOLS", "shell, read") | ||
| monkeypatch.setenv("STRANDS_HARNESS_INTERVENTIONS", "ask") | ||
| with fake_provider() as (base, bodies): | ||
| main = _main(monkeypatch, base) | ||
| _run(main) | ||
| assert "interventions" not in main.harness_options() | ||
| acting = {"shell", "read", "write", "edit", "web_fetch", "web_search", "programmatic_tool_caller", "subagent"} | ||
| assert not acting & set(_offered(bodies[-1])) | ||
|
|
||
|
|
||
| def test_what_openbot_keeps_is_not_kept_twice(monkeypatch, tmp_path): | ||
| """No memory, skills or session files beside the process.""" | ||
| monkeypatch.chdir(tmp_path) | ||
| with fake_provider() as (base, bodies): | ||
| _run(_main(monkeypatch, base)) | ||
| assert not (tmp_path / ".agent").exists() | ||
| assert "search_memory" not in _offered(bodies[-1]) | ||
| # Nor the harness's note about the machine it runs on: that is this container. | ||
| assert "Working directory:" not in json.dumps(bodies[-1]) | ||
|
|
||
|
|
||
| def test_a_conversation_nobody_talks_in_is_let_go_and_comes_back_from_its_messages(monkeypatch): | ||
| with fake_provider() as (base, _bodies): | ||
| main = _main(monkeypatch, base) | ||
| _run(main, "quiet") | ||
| (bot,) = [bot for bot in _bots(main) if "quiet" in bot._agents_by_thread] | ||
| assert bot.config.thread_idle_timeout == main.IDLE_SECONDS | ||
| # As if the half hour had passed. | ||
| bot._thread_last_run_ended["quiet"] -= main.IDLE_SECONDS | ||
| _run(main, "busy") | ||
| assert set(bot._agents_by_thread) == {"busy"} | ||
| _run(main, "quiet") | ||
| assert set(bot._agents_by_thread) == {"busy", "quiet"} | ||
|
|
||
|
|
||
| def test_stopping_the_process_shuts_every_conversations_harness_down(monkeypatch): | ||
| with fake_provider() as (base, _bodies): | ||
| main = _main(monkeypatch, base) | ||
| with TestClient(main.app) as client: | ||
| response = client.post("/", json=conversation("closing"), headers={"x-openbot-agent-token": TOKEN}) | ||
| assert response.status_code == 200, response.text | ||
| (bot,) = [bot for bot in _bots(main) if "closing" in bot._agents_by_thread] | ||
| assert bot._agents_by_thread == {} | ||
|
|
||
|
|
||
| def test_a_named_model_is_the_harnesss_to_reach(monkeypatch): | ||
| """The AWS route: Bedrock with this process's credentials, not the server's gateway.""" | ||
| with fake_provider() as (base, _bodies): | ||
| main = _main(monkeypatch, base) | ||
| assert not isinstance(main.harness_options()["model"], str) | ||
| monkeypatch.setenv("STRANDS_HARNESS_MODEL", "bedrock/global.anthropic.claude-opus-5-5") | ||
| assert main.harness_options()["model"] == "bedrock/global.anthropic.claude-opus-5-5" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| """AWS Strands harness receives OpenBot's learned context and read tools.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
| sys.path.insert(0, str(ROOT / "tests")) | ||
| sys.path.insert(0, str(ROOT / "agent-strands-harness")) | ||
|
|
||
| from learning_harness_contract import assert_learning_delivery | ||
|
|
||
|
|
||
| def test_learned_catalog_and_both_read_tools_reach_the_model(monkeypatch): | ||
| assert_learning_delivery(monkeypatch, "agent-strands-harness", "/") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| """`agent-strands/tests/test_main.py`, against the harness Bot. See `strands_bot_suite.py`.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | ||
| from strands_bot_suite import load | ||
|
|
||
| globals().update(load("test_main")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran the merge-queue image check locally. It picks up this Dockerfile and fails because the image is missing from the published list. If we’re holding off on publishing, can we land the Dockerfile with the image entry? Component-list check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it: with the image in
.github/published-images.json,scripts/model-compat.test.ts("every published harness is one the matrix can run") then asks for a row inshared/harnesses.json, which is registering the harness as an engine: the harness enum in the API and the generated clients, the desktop app's list and the model lock. That is more than an image entry, so I left it out of this push. It needs doing before this can merge either way; I'd do the registration as the next step here once the dependency is released, unless you'd rather the Dockerfile moved to that change.