Skip to content

The agent that grows with you

test: migrate authored bot fixtures to canonical transport

Teknium37d7834e16e2
diff --git a/tests/tools/test_bot_live_owner_delivery.py b/tests/tools/test_bot_live_owner_delivery.py
index e1aea238dcb..4ab4ce85e67 100644
--- a/tests/tools/test_bot_live_owner_delivery.py
+++ b/tests/tools/test_bot_live_owner_delivery.py
@@ -22,10 +22,11 @@ class _FakeAuthority:
record = self.records[params["id"]] = dict(
status="queued", delivery_id=params["id"], profile_home=str(Path(home).resolve()),
session_id="local-bot", message=params["message"], admission_id="adm-" + params["id"][:6],
- reply="")
+ reply="", **({"author": params["author"]} if "author" in params else {}))
with mailbox._locked(home) as root:
mailbox._write(root / f"{params['id']}.json", record)
- elif record["message"] != params["message"]:
+ elif (record["message"] != params["message"]
+ or record.get("author") != params.get("author")):
raise ValueError("admission_conflict")
return {k: record[k] for k in ("status", "delivery_id", "profile_home", "session_id", "reply")}
@@ -118,14 +119,18 @@ def test_canonical_owner_is_the_authority_and_follows_compression(tmp_path, monk
db.close()
-def test_delivery_keeps_the_sender_and_refuses_a_different_one_under_the_same_id(tmp_path):
+def test_delivery_keeps_the_sender_and_refuses_a_different_one_under_the_same_id(tmp_path, monkeypatch):
from tools import bot_live_delivery as mailbox
+ authority = _FakeAuthority()
+ monkeypatch.setattr(mailbox, "authority_delivery", authority)
owner = dict(profile_home=str(tmp_path.resolve()), session_id="chat", lease_id="lease", live_session_id="live")
author = {"id": "bot:coder", "name": "coder", "is_bot": True}
queued = mailbox.deliver_to_live_owner(tmp_path, owner, "hello", delivery_id="b" * 32, author=author)
- assert queued["author"] == author
+ assert authority.calls[-1][1]["author"] == author
+ assert authority.records["b" * 32]["author"] == author
assert mailbox.deliver_to_live_owner(tmp_path, owner, "hello", delivery_id="b" * 32, author=author) == queued
with pytest.raises(ValueError):
mailbox.deliver_to_live_owner(tmp_path, owner, "hello", delivery_id="b" * 32, author={**author, "id": "bot:other"})
- assert "author" not in mailbox.deliver_to_live_owner(tmp_path, owner, "no sender", delivery_id="c" * 32)
+ mailbox.deliver_to_live_owner(tmp_path, owner, "no sender", delivery_id="c" * 32)
+ assert "author" not in authority.calls[-1][1]
diff --git a/tests/tools/test_bot_mode_dm.py b/tests/tools/test_bot_mode_dm.py
index 536c5ff9d90..b3ba43409d9 100644
--- a/tests/tools/test_bot_mode_dm.py
+++ b/tests/tools/test_bot_mode_dm.py
@@ -385,10 +385,11 @@ def test_peer_delivery_author_carries_the_sender_hostname_and_local_stays_bare(t
calls = _capture_spawn(monkeypatch)
monkeypatch.setattr("socket.gethostname", lambda: " eri/mac\x00.local ")
home = _managed_home(tmp_path, teammates=("researcher", "coder"), peers=("spark",))
+ _canonical_target(monkeypatch, home / "profiles" / "researcher")
agent = _FakeAgent(home / "profiles" / "coder", title="Bot Chat")
assert json.loads(bot_mode_dm.message_agent_tool(target="spark", message="ping", agent=agent))["status"] == "sent"
- assert json.loads(bot_mode_dm.message_agent_tool(target="researcher", message="ping", agent=agent))["status"] == "sent"
+ assert json.loads(bot_mode_dm.message_agent_tool(target="researcher", message="ping", agent=agent))["status"] == "queued"
assert _runner_author(calls[0]["command"]) == {"id": "bot:erimac.local/coder", "name": "coder", "is_bot": True}
assert _runner_author(calls[1]["command"]) == {"id": "bot:coder", "name": "coder", "is_bot": True}
@@ -396,7 +397,7 @@ def test_peer_delivery_author_carries_the_sender_hostname_and_local_stays_bare(t
def test_named_profile_sender_prefix(tmp_path, monkeypatch):
"""A named-profile bot signs with its own handle, not @hermes."""
- _capture_spawn(monkeypatch)
+ calls = _capture_spawn(monkeypatch)
home = _managed_home(tmp_path, teammates=("researcher", "coder"))
authority = _canonical_target(monkeypatch, home / "profiles" / "researcher")
monkeypatch.setattr(bot_mode_dm, "_dm_dir", lambda: tmp_path)
@@ -410,8 +411,8 @@ def test_named_profile_sender_prefix(tmp_path, monkeypatch):
assert _runner_author(calls[0]["command"]) == {"id": "bot:coder", "name": "coder", "is_bot": True}
-def test_delivery_command_author_json_survives_quoting_and_windows_slash_rewrite(tmp_path, monkeypatch):
- """The author JSON sits between ``--run-delivery`` and the mode, survives shlex, and the Windows slash rewrite skips it."""
+def test_delivery_command_author_json_survives_quoting(tmp_path):
+ """The optional author JSON survives shell quoting before the delivery mode."""
author = {"id": "bot:default", "name": "hermes", "is_bot": True}
command = bot_mode_dm._delivery_command(["hermes", "-p", "x"], str(tmp_path / "dm.txt"),
stdin_file=False, author=author)
@@ -419,20 +420,22 @@ def test_delivery_command_author_json_survives_quoting_and_windows_slash_rewrite
assert parts[2:4] == ["--run-delivery", "--author"]
assert json.loads(parts[4]) == author
assert parts[5] == "query-file"
- assert _runner_parts(command) == ("query-file", str(tmp_path / "dm.txt"), ["hermes", "-p", "x"])
+ assert _runner_parts(command) == ("query-file", str(tmp_path / "dm.txt"), ["hermes", "-p", "x"], None)
+
+ # Without an author the optional argv pair is absent.
+ command = bot_mode_dm._delivery_command(["hermes"], "dm.txt", stdin_file=True)
+ assert shlex.split(command)[2:4] == ["--run-delivery", "stdin"]
+ assert _runner_author(command) is None
+
- monkeypatch.setattr(sys, "platform", "win32")
+def test_delivery_command_preserves_author_during_windows_slash_rewrite():
command = bot_mode_dm._delivery_command(["hermes", "-p", "x"], "C:\\Users\\me\\dm.txt",
stdin_file=False, author={"id": "bot:default", "name": 'q"q', "is_bot": True})
parts = shlex.split(command)
assert json.loads(parts[4]) == {"id": "bot:default", "name": 'q"q', "is_bot": True}
assert parts[6] == "C:/Users/me/dm.txt"
- # without an author the legacy shape is produced unchanged
- command = bot_mode_dm._delivery_command(["hermes"], "dm.txt", stdin_file=True)
- assert shlex.split(command)[2:4] == ["--run-delivery", "stdin"]
- assert _runner_author(command) is None
-
def test_unavailable_authority_is_reported_never_worked_around(tmp_path, monkeypatch):
"""No canonical target: the sender learns the outcome is unknown and nothing
@@ -605,9 +608,8 @@ def test_local_delivery_runner_surfaces_refusal_and_retains_payload(tmp_path, mo
@pytest.mark.parametrize("mode, author", [
("stdin", {"id": "bot:eri-mac.local/coder", "name": "coder", "is_bot": True}),
- ("query-file", {"id": "bot:coder", "name": "coder", "is_bot": True}),
- ("query-file", None),
-], ids=["stdin", "query-file", "no author"])
+ ("stdin", None),
+], ids=["author", "no author"])
def test_delivery_main_child_env_carries_only_the_argv_author(tmp_path, monkeypatch, mode, author):
"""The ``--author`` payload becomes HERMES_TURN_AUTHOR on the child. Without it the runner drops the
variable it inherited from the sending bot's own turn instead of passing it on as the recipient's author."""
@@ -637,6 +639,32 @@ def test_delivery_main_child_env_carries_only_the_argv_author(tmp_path, monkeypa
assert not dm_file.exists()
[email protected]("author", [{"id": "bot:coder", "name": "coder", "is_bot": True}, None])
+def test_local_runner_sends_only_argv_author_to_authority(tmp_path, monkeypatch, author):
+ from agent.turn_author import TURN_AUTHOR_ENV
+
+ target = tmp_path / "profiles" / "researcher"
+ target.mkdir(parents=True)
+ authority = _canonical_target(monkeypatch, target)
+ dm_file = tmp_path / "message.txt"
+ dm_file.write_text("hello", encoding="utf-8")
+ monkeypatch.setenv(TURN_AUTHOR_ENV, json.dumps({"id": "bot:previous", "name": "previous", "is_bot": True}))
+ monkeypatch.setattr(subprocess, "run", lambda *a, **k: pytest.fail("no local inference child"))
+ waited = []
+ monkeypatch.setattr(bot_mode_dm, "_wait_live_dm", lambda home, key: waited.append((home, key)) or 0)
+ command = bot_mode_dm._delivery_command(
+ ["hermes", "-p", "researcher"], str(dm_file), stdin_file=False,
+ profile_home=target, author=author,
+ )
+ assert bot_mode_dm._delivery_main(shlex.split(command)[2:]) == 0
+ [(home, params)] = authority.calls
+ assert home == target.resolve()
+ assert params.get("author") == author
+ assert params["message"] == "hello"
+ assert waited == [(str(target.resolve()), params["id"])]
+ assert dm_file.exists(), "local admission retains its evidence, not peer-child cleanup"
+
+
def test_real_delivery_command_round_trip_carries_author(tmp_path):
"""Through a real subprocess, the runner argv built by ``_delivery_command`` sets HERMES_TURN_AUTHOR on the child."""
dm_file = tmp_path / "message.txt"
@@ -650,7 +678,7 @@ def test_real_delivery_command_round_trip_carries_author(tmp_path):
)
author = {"id": "bot:default", "name": "hermes", "is_bot": True}
command = bot_mode_dm._delivery_command(
- [sys.executable, str(child), str(observed)], str(dm_file), stdin_file=False, author=author
+ [sys.executable, str(child), str(observed)], str(dm_file), stdin_file=True, author=author
)
result = subprocess.run(shlex.split(command), check=False)
diff --git a/tests/tui_gateway/test_relay_live_author.py b/tests/tui_gateway/test_relay_live_author.py
index 8105f6786d1..e874b941e9d 100644
--- a/tests/tui_gateway/test_relay_live_author.py
+++ b/tests/tui_gateway/test_relay_live_author.py
@@ -1,7 +1,7 @@
"""A relayed dm into a live Bot Chat keeps its sender all the way into ``run_conversation``.
-The relay handler stamps the author as an in-process ``DeliveryAuthor``; ``prompt.submit`` accepts only
-that object, so a dashboard client cannot claim a bot identity through the same RPC.
+The relay bridge forwards authors to canonical authority, never to UI execution.
+The retained prompt path accepts only an in-process ``DeliveryAuthor`` object.
"""
from __future__ import annotations
@@ -57,19 +57,29 @@ def turn_env(monkeypatch, tmp_path):
monkeypatch.setattr(srv, "_get_usage", lambda agent: {})
-def test_live_relay_stamps_the_sender_as_a_delivery_author(tmp_path, monkeypatch):
+def test_live_relay_forwards_sender_to_canonical_authority(tmp_path, monkeypatch):
+ from tools import bot_live_delivery
+
home = tmp_path / ".hermes"
(home / "profiles" / "ops").mkdir(parents=True)
monkeypatch.setenv("HERMES_HOME", str(home))
submitted = []
- monkeypatch.setitem(srv._methods, "prompt.submit", lambda rid, p: submitted.append(p) or srv._ok(rid, {"status": "streaming"}))
- monkeypatch.setattr(srv, "_profile_home", lambda name: home / "profiles" / name)
- monkeypatch.setitem(srv._sessions, "live-ops",
- {"profile_home": str(home / "profiles" / "ops"), "pending_title": "Bot Chat", "history": []})
-
- _result(srv._methods["bot_relay.deliver"](1, {"profile": "ops", "message": "ping", "from_profile": "coder", "from_handle": "coder"}))
-
- assert submitted == [{"session_id": "live-ops", "text": "ping", "queued": True, "_turn_author": DeliveryAuthor(AUTHOR)}]
+ receipt = {"status": "queued", "delivery_id": "a" * 32, "admission_id": "admission"}
+ monkeypatch.setattr(bot_live_delivery, "authority_delivery",
+ lambda target, params: submitted.append((target, params)) or receipt)
+ monkeypatch.setitem(srv._methods, "prompt.submit",
+ lambda *a: pytest.fail("relay must not execute through the UI"))
+ monkeypatch.setattr(srv, "current_transport", lambda: None)
+
+ response = srv._methods["bot_relay.deliver"](1, {
+ "id": "a" * 32, "profile": "ops", "message": "ping",
+ "from_profile": "coder", "from_handle": "coder",
+ })
+
+ assert _result(response) == receipt
+ assert submitted == [(home / "profiles" / "ops", {
+ "id": "a" * 32, "profile": "ops", "message": "ping", "author": AUTHOR,
+ })]
def test_prompt_submit_refuses_a_client_supplied_author(monkeypatch):