fix(rpc): harden Python protocol compatibility

This commit is contained in:
Wolfgang Schoenberger
2026-07-29 05:44:49 -07:00
parent 4df68d6043
commit 0f033ed706
6 changed files with 208 additions and 15 deletions
+74 -1
View File
@@ -12,7 +12,14 @@ import threading
import time
import unittest
from omp_rpc import RpcClient, RpcCommandError, RpcConcurrencyError, RpcError, host_tool
from omp_rpc import (
AgentEndEvent,
RpcClient,
RpcCommandError,
RpcConcurrencyError,
RpcError,
host_tool,
)
from omp_rpc.client import _RpcFrameDecoder
@@ -806,6 +813,54 @@ BROKEN_STARTUP_SERVER = textwrap.dedent(
"""
)
FORWARD_COMPAT_SERVER = textwrap.dedent(
"""
import json
import sys
import time
print(json.dumps({"type": "ready"}), flush=True)
for raw_line in sys.stdin:
command = json.loads(raw_line)
print(
json.dumps(
{
"id": command.get("id"),
"type": "response",
"command": command["type"],
"success": True,
}
),
flush=True,
)
if command["type"] != "prompt":
continue
print(
json.dumps(
{
"type": "auto_compaction_start",
"reason": "future_reason",
"action": "future_action",
}
),
flush=True,
)
print(
json.dumps(
{"type": "agent_end", "messages": [], "isTerminal": False}
),
flush=True,
)
time.sleep(0.15)
print(
json.dumps(
{"type": "agent_end", "messages": [], "isTerminal": True}
),
flush=True,
)
"""
)
class RpcClientTests(unittest.TestCase):
def make_client(self, server: str = FAKE_SERVER, **kwargs: object) -> RpcClient:
@@ -1260,6 +1315,24 @@ class RpcClientTests(unittest.TestCase):
self.assertEqual(seen_extension_errors, ["boom"])
self.assertEqual(seen_unknown, ["unknown_future_event"])
def test_additive_notification_values_do_not_stop_the_reader(self) -> None:
unknown_errors: list[str | None] = []
with self.make_client(server=FORWARD_COMPAT_SERVER) as client:
client.on_unknown_notification(
lambda event: unknown_errors.append(event.parse_error)
)
turn = client.prompt_and_wait("forward compatible", timeout=2.0)
terminal_events = [
event for event in turn.events if isinstance(event, AgentEndEvent)
]
self.assertEqual(
[event.is_terminal for event in terminal_events], [False, True]
)
self.assertEqual(len(unknown_errors), 1)
self.assertIn("auto_compaction_start.reason", unknown_errors[0] or "")
def test_ui_confirmation_and_cancel_round_trip(self) -> None:
with self.make_client() as client:
client.prompt("needs confirm")
+56
View File
@@ -4,6 +4,8 @@ import unittest
from omp_rpc import (
AgentEndEvent,
AutoCompactionEndEvent,
AutoCompactionStartEvent,
ExtensionUiRequest,
SessionState,
TodoReminderEvent,
@@ -79,6 +81,11 @@ class ProtocolParsingTests(unittest.TestCase):
"parameters": {"type": "object"},
}
],
"contextUsage": {
"tokens": 12345,
"contextWindow": 200000,
"percent": 6.1725,
},
}
)
@@ -90,6 +97,10 @@ class ProtocolParsingTests(unittest.TestCase):
# Legacy bare-string systemPrompt is accepted and wrapped to a tuple.
self.assertEqual(state.system_prompt, ("You are useful.",))
self.assertEqual(state.dump_tools[0].name, "read")
assert state.context_usage is not None
self.assertEqual(state.context_usage.tokens, 12345)
self.assertEqual(state.context_usage.context_window, 200000)
self.assertEqual(state.context_usage.percent, 6.1725)
assert state.model is not None and state.model.thinking is not None
self.assertEqual(
state.model.thinking.efforts, ("minimal", "low", "medium", "high")
@@ -161,16 +172,43 @@ class ProtocolParsingTests(unittest.TestCase):
}
],
"messageCount": 1,
"isTerminal": False,
}
)
self.assertIsInstance(notification, AgentEndEvent)
self.assertEqual(assistant_text(notification.messages[0]), "hello")
self.assertEqual(notification.message_count, 1)
self.assertFalse(notification.is_terminal)
legacy = AgentEndEvent(notification.messages, "agent_end")
self.assertEqual(legacy.type, "agent_end")
self.assertIsNone(legacy.message_count)
self.assertIsNone(legacy.is_terminal)
def test_parse_current_compaction_variants(self) -> None:
start = parse_notification(
{
"type": "auto_compaction_start",
"reason": "incomplete",
"action": "snapcompact",
}
)
end = parse_notification(
{
"type": "auto_compaction_end",
"action": "shake",
"result": None,
"aborted": False,
"willRetry": False,
}
)
self.assertIsInstance(start, AutoCompactionStartEvent)
self.assertEqual(start.reason, "incomplete")
self.assertEqual(start.action, "snapcompact")
self.assertIsInstance(end, AutoCompactionEndEvent)
self.assertEqual(end.action, "shake")
def test_parse_extension_ui_request(self) -> None:
notification = parse_notification(
@@ -191,6 +229,24 @@ class ProtocolParsingTests(unittest.TestCase):
self.assertTrue(notification.requires_response())
self.assertFalse(notification.is_passive())
def test_parse_open_url_request(self) -> None:
notification = parse_notification(
{
"type": "extension_ui_request",
"id": "ui-oauth",
"method": "open_url",
"url": "https://example.com/oauth",
"launchUrl": "http://127.0.0.1:8123/redirect",
"instructions": "Open this URL to continue.",
}
)
self.assertIsInstance(notification, ExtensionUiRequest)
self.assertEqual(notification.method, "open_url")
self.assertEqual(notification.url, "https://example.com/oauth")
self.assertEqual(notification.launch_url, "http://127.0.0.1:8123/redirect")
self.assertTrue(notification.is_passive())
def test_parse_todo_reminder_notification(self) -> None:
notification = parse_notification(
{