forked from retoor/devplacepy
Keep composers usable, group actions and notifications, and drop the tool-count cap
Comment and post layouts stay full width on small screens. Secondary actions live in the More menu. Notifications can be grouped and throttled, and Enter can submit when that profile setting is on. Emulated-tool models accept large catalogs, with reserved names aliased on the wire and the catalog cached per owner. A dropped service-worker navigation is retried once. The test server waits 120s to finish importing before it is stopped.
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from devplacepy.database import (
|
||||
NOTIFICATION_GROUP_KEYS,
|
||||
count_unread_groups,
|
||||
get_notification_grouping_enabled,
|
||||
get_notification_throttle_enabled,
|
||||
notification_group_key,
|
||||
set_notification_grouping_enabled,
|
||||
set_notification_throttle_enabled,
|
||||
)
|
||||
from devplacepy.utils import generate_uid
|
||||
|
||||
|
||||
def _row(ntype, related="actor-1", target="/posts/x", uid=None):
|
||||
return {
|
||||
"type": ntype,
|
||||
"related_uid": related,
|
||||
"target_url": target,
|
||||
"uid": uid or generate_uid(),
|
||||
}
|
||||
|
||||
|
||||
def test_group_key_actor_target_single():
|
||||
assert notification_group_key(_row("message")) == ("message", "actor-1", "/posts/x")
|
||||
assert notification_group_key(_row("vote", related="a")) == notification_group_key(
|
||||
_row("vote", related="b")
|
||||
)
|
||||
assert notification_group_key(_row("follow", related="a", target="/profile/a")) == ("follow",)
|
||||
assert notification_group_key(_row("follow", related="b", target="/profile/b")) == ("follow",)
|
||||
single = _row("badge")
|
||||
assert notification_group_key(single) == ("badge", single["uid"])
|
||||
|
||||
|
||||
def test_group_key_unlisted_type_is_single():
|
||||
row = _row("some_future_type")
|
||||
assert NOTIFICATION_GROUP_KEYS.get("some_future_type", "single") == "single"
|
||||
assert notification_group_key(row) == ("some_future_type", row["uid"])
|
||||
|
||||
|
||||
def test_all_registered_types_have_a_group_shape():
|
||||
from devplacepy.database import NOTIFICATION_TYPES
|
||||
|
||||
for entry in NOTIFICATION_TYPES:
|
||||
assert NOTIFICATION_GROUP_KEYS[entry["key"]] in ("actor", "target", "single")
|
||||
|
||||
|
||||
def _seed_user(local_db, suffix):
|
||||
uid = f"notif-{suffix}-{generate_uid()[-8:]}"
|
||||
local_db["users"].insert(
|
||||
{
|
||||
"uid": uid,
|
||||
"username": uid,
|
||||
"terms_version": "1",
|
||||
"role": "Member",
|
||||
"created_at": "1990-01-01T00:00:00",
|
||||
"deleted_at": None,
|
||||
}
|
||||
)
|
||||
return uid
|
||||
|
||||
|
||||
def _seed_notifications(local_db, user_uid, count, ntype="vote"):
|
||||
table = local_db["notifications"]
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
for i in range(count):
|
||||
table.insert(
|
||||
{
|
||||
"uid": generate_uid(),
|
||||
"user_uid": user_uid,
|
||||
"type": ntype,
|
||||
"message": f"seeded {i}",
|
||||
"related_uid": f"actor-{i}",
|
||||
"target_url": "/posts/seeded",
|
||||
"read": False,
|
||||
"created_at": now,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_count_unread_groups_respects_setting(local_db):
|
||||
uid = _seed_user(local_db, "groups")
|
||||
try:
|
||||
_seed_notifications(local_db, uid, 5)
|
||||
assert get_notification_grouping_enabled(uid) is True
|
||||
assert count_unread_groups(uid) == 1
|
||||
set_notification_grouping_enabled(uid, False)
|
||||
assert get_notification_grouping_enabled(uid) is False
|
||||
assert count_unread_groups(uid) == 5
|
||||
set_notification_grouping_enabled(uid, True)
|
||||
assert count_unread_groups(uid) == 1
|
||||
finally:
|
||||
local_db["notifications"].delete(user_uid=uid)
|
||||
local_db["users"].delete(uid=uid)
|
||||
|
||||
|
||||
def test_throttle_setting_defaults_on_and_toggles(local_db):
|
||||
uid = _seed_user(local_db, "throttle")
|
||||
try:
|
||||
assert get_notification_throttle_enabled(uid) is True
|
||||
set_notification_throttle_enabled(uid, False)
|
||||
assert get_notification_throttle_enabled(uid) is False
|
||||
set_notification_throttle_enabled(uid, True)
|
||||
assert get_notification_throttle_enabled(uid) is True
|
||||
finally:
|
||||
local_db["users"].delete(uid=uid)
|
||||
|
||||
|
||||
def test_missing_user_defaults_on():
|
||||
assert get_notification_grouping_enabled("no-such-user") is True
|
||||
assert get_notification_throttle_enabled("no-such-user") is True
|
||||
@@ -2757,6 +2757,225 @@ def test_meta_tool_names_restored_on_stream_chunks(local_db, monkeypatch):
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def _setup_chat_meta_route_openai_gateway():
|
||||
from devplacepy.services.openai_gateway import routing
|
||||
|
||||
routing.provider_store.set(
|
||||
routing.ProviderIn(
|
||||
name="gwmetacc",
|
||||
base_url="https://api.meta.ai/v1/chat/completions",
|
||||
api_key="sk-test",
|
||||
)
|
||||
)
|
||||
routing.model_store.set(
|
||||
routing.ModelRouteIn(
|
||||
source_model="meta-chat-cc",
|
||||
provider="gwmetacc",
|
||||
target_model="muse-spark",
|
||||
kind="chat",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _teardown_chat_meta_route_openai_gateway():
|
||||
from devplacepy.services.openai_gateway import routing
|
||||
|
||||
routing.model_store.remove("meta-chat-cc")
|
||||
routing.provider_store.remove("gwmetacc")
|
||||
|
||||
|
||||
def _tai_tool_body_openai_gateway(**over):
|
||||
# Mirrors a real tai/detz turn: core tools are always sent, so "shell"
|
||||
# (a Pi-verified gate trigger missing from the denylist until
|
||||
# 2026-11-10) rides every request alongside the lazy "read_file".
|
||||
body = _chat_body_openai_gateway(**over)
|
||||
body["model"] = "meta-chat-cc"
|
||||
body["tools"] = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "shell",
|
||||
"description": "run a shell command",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "read_file",
|
||||
"description": "read a file",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "web_fetch",
|
||||
"description": "fetch a URL",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
},
|
||||
},
|
||||
]
|
||||
return body
|
||||
|
||||
|
||||
class FakeChatMetaStreamResp_openai_gateway(FakeResp_openai_gateway):
|
||||
def __init__(self, payload):
|
||||
super().__init__(status=200, payload=payload)
|
||||
self._payload = payload
|
||||
|
||||
async def aiter_lines(self):
|
||||
calls = self._payload["choices"][0]["message"]["tool_calls"]
|
||||
yield (
|
||||
'data: {"id":"chatcmpl-x","object":"chat.completion.chunk",'
|
||||
'"model":"m","choices":[{"index":0,"delta":{"role":"assistant"}}]}'
|
||||
)
|
||||
for pos, call in enumerate(calls):
|
||||
name = call["function"]["name"]
|
||||
yield (
|
||||
'data: {"id":"chatcmpl-x","object":"chat.completion.chunk",'
|
||||
'"model":"m","choices":[{"index":0,"delta":{"tool_calls":'
|
||||
'[{"index":%d,"id":"call_%d","function":{"name":"%s",'
|
||||
'"arguments":"{}"}}]}}]}' % (pos, pos, name)
|
||||
)
|
||||
yield (
|
||||
'data: {"id":"chatcmpl-x","object":"chat.completion.chunk",'
|
||||
'"model":"m","choices":[{"index":0,"delta":{},'
|
||||
'"finish_reason":"tool_calls"}]}'
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
|
||||
class FakeChatMetaClient_openai_gateway:
|
||||
def __init__(self, *a, **k):
|
||||
self.calls = []
|
||||
|
||||
def build_request(self, method, url, headers=None, json=None, content=None):
|
||||
return FakeRequest(method, url, json, headers)
|
||||
|
||||
async def send(self, request, stream=False):
|
||||
self.calls.append((request.url, request.json_body))
|
||||
body = request.json_body or {}
|
||||
wire_names = [
|
||||
(tool.get("function") or {}).get("name")
|
||||
for tool in body.get("tools") or []
|
||||
]
|
||||
payload = {
|
||||
"id": "chatcmpl-x",
|
||||
"object": "chat.completion",
|
||||
"model": body.get("model"),
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "call_7",
|
||||
"type": "function",
|
||||
"function": {"name": name, "arguments": "{}"},
|
||||
}
|
||||
for name in wire_names
|
||||
],
|
||||
},
|
||||
"finish_reason": "tool_calls",
|
||||
}
|
||||
],
|
||||
"usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7},
|
||||
}
|
||||
if stream:
|
||||
return FakeChatMetaStreamResp_openai_gateway(payload)
|
||||
return FakeResp_openai_gateway(
|
||||
payload=payload, content=json.dumps(payload).encode()
|
||||
)
|
||||
|
||||
async def post(self, url, headers=None, json=None, timeout=None):
|
||||
return await self.send(FakeRequest("POST", url, json), stream=False)
|
||||
|
||||
async def aclose(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_meta_tool_names_aliased_on_chat_completions_wire(local_db, monkeypatch):
|
||||
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeChatMetaClient_openai_gateway)
|
||||
_setup_chat_meta_route_openai_gateway()
|
||||
try:
|
||||
svc = GatewayService()
|
||||
cfg = svc.effective_config()
|
||||
cfg["gateway_vision_enabled"] = False
|
||||
rt = svc.runtime()
|
||||
logged = []
|
||||
resp = run_async(
|
||||
rt.handle_chat(
|
||||
_tai_tool_body_openai_gateway(),
|
||||
cfg,
|
||||
("guest", "test"),
|
||||
"test",
|
||||
"default",
|
||||
log=logged.append,
|
||||
)
|
||||
)
|
||||
_, sent = rt._client.calls[-1]
|
||||
wire = [tool["function"]["name"] for tool in sent["tools"]]
|
||||
assert wire == ["ext_shell", "ext_read_file", "ext_web_fetch"]
|
||||
assert any("shell->ext_shell" in line for line in logged)
|
||||
assert any("read_file->ext_read_file" in line for line in logged)
|
||||
assert any("web_fetch->ext_web_fetch" in line for line in logged)
|
||||
assert resp.status_code == 200
|
||||
payload = json.loads(bytes(resp.body).decode())
|
||||
calls = payload["choices"][0]["message"]["tool_calls"]
|
||||
assert [call["function"]["name"] for call in calls] == [
|
||||
"shell",
|
||||
"read_file",
|
||||
"web_fetch",
|
||||
]
|
||||
assert payload["choices"][0]["finish_reason"] == "tool_calls"
|
||||
finally:
|
||||
_teardown_chat_meta_route_openai_gateway()
|
||||
|
||||
|
||||
def test_meta_tool_names_restored_on_chat_completions_stream(
|
||||
local_db, monkeypatch
|
||||
):
|
||||
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeChatMetaClient_openai_gateway)
|
||||
_setup_chat_meta_route_openai_gateway()
|
||||
try:
|
||||
svc = GatewayService()
|
||||
cfg = svc.effective_config()
|
||||
cfg["gateway_vision_enabled"] = False
|
||||
rt = svc.runtime()
|
||||
resp = run_async(
|
||||
rt.handle_chat(
|
||||
_tai_tool_body_openai_gateway(stream=True),
|
||||
cfg,
|
||||
("guest", "test"),
|
||||
"test",
|
||||
"default",
|
||||
)
|
||||
)
|
||||
|
||||
async def drain():
|
||||
out = []
|
||||
async for chunk in resp.body_iterator:
|
||||
out.append(chunk if isinstance(chunk, str) else chunk.decode())
|
||||
return "".join(out)
|
||||
|
||||
body = run_async(drain())
|
||||
_, sent = rt._client.calls[-1]
|
||||
wire = [tool["function"]["name"] for tool in sent["tools"]]
|
||||
assert wire == ["ext_shell", "ext_read_file", "ext_web_fetch"]
|
||||
assert '"name": "shell"' in body
|
||||
assert '"name": "read_file"' in body
|
||||
assert '"name": "web_fetch"' in body
|
||||
assert "ext_shell" not in body
|
||||
assert "ext_read_file" not in body
|
||||
assert "ext_web_fetch" not in body
|
||||
assert "[DONE]" in body
|
||||
finally:
|
||||
_teardown_chat_meta_route_openai_gateway()
|
||||
|
||||
|
||||
def test_responses_protocol_stream_relays_chat_chunks(local_db, monkeypatch):
|
||||
monkeypatch.setattr(gwmod.httpx, "AsyncClient", FakeResponsesClient_openai_gateway)
|
||||
FakeResponsesClient_openai_gateway.behavior = "ok"
|
||||
@@ -3173,16 +3392,28 @@ def test_emulated_tools_convert_history_for_the_meta_transpiler(local_db, monkey
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
def test_emulated_tools_over_the_cap_is_a_400_with_no_upstream_call(local_db, monkeypatch):
|
||||
from devplacepy.services.openai_gateway import tool_emulation
|
||||
|
||||
body = _chat_body_openai_gateway(
|
||||
tools=[BASH_TOOL_openai_gateway] * (tool_emulation.MAX_TOOLS + 1)
|
||||
)
|
||||
def test_emulated_tools_serve_large_catalogs_with_no_cap(local_db, monkeypatch):
|
||||
tools = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": f"tool_{index}",
|
||||
"description": f"tool number {index}",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
}
|
||||
for index in range(200)
|
||||
]
|
||||
body = _chat_body_openai_gateway(tools=tools, tool_choice="auto")
|
||||
try:
|
||||
rt, resp = _emulated_chat_openai_gateway(monkeypatch, body)
|
||||
assert resp.status_code == 400
|
||||
assert rt._client.calls == []
|
||||
assert resp.status_code == 200
|
||||
assert len(rt._client.calls) == 1
|
||||
_, sent = rt._client.calls[-1]
|
||||
assert "tools" not in sent and "tool_choice" not in sent
|
||||
assert '"name": "tool_0"' in sent["instructions"]
|
||||
assert '"name": "tool_199"' in sent["instructions"]
|
||||
assert len(rt._catalog_cache._entries) == 1
|
||||
finally:
|
||||
_teardown_responses_route_openai_gateway()
|
||||
|
||||
|
||||
@@ -43,6 +43,8 @@ def test_reserved_tool_names_cover_the_measured_denylist():
|
||||
"update_goal",
|
||||
"report_progress",
|
||||
"read_skill",
|
||||
"shell",
|
||||
"web_fetch",
|
||||
"write_todos",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -110,15 +110,81 @@ def test_history_roundtrips_into_the_same_format_the_model_emits():
|
||||
assert '"name": "bash"' in results and "a.txt" in results
|
||||
|
||||
|
||||
def test_caps_reject_loudly():
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[BASH] * (te.MAX_TOOLS + 1)))
|
||||
huge = json.loads(json.dumps(BASH))
|
||||
huge["function"]["description"] = "x" * (te.MAX_CATALOG_CHARS + 1)
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[huge]))
|
||||
def _many_tools(count):
|
||||
return [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": f"tool_{index}",
|
||||
"description": f"tool number {index}",
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
},
|
||||
}
|
||||
for index in range(count)
|
||||
]
|
||||
|
||||
|
||||
def test_large_catalogs_are_served_with_no_cap():
|
||||
out = te.emulate_request(_payload(tools=_many_tools(424)))
|
||||
system = out["messages"][0]["content"]
|
||||
assert '"name": "tool_0"' in system
|
||||
assert '"name": "tool_423"' in system
|
||||
assert "tools" not in out and "tool_choice" not in out
|
||||
|
||||
|
||||
def test_malformed_tools_still_reject_loudly():
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[{"type": "retrieval"}]))
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[{"type": "function", "function": {}}]))
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools="bash"))
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.cached_emulated_tools(_payload(tools="bash"))
|
||||
|
||||
|
||||
def test_catalog_cache_reuses_the_built_instruction_per_owner():
|
||||
cache = te.CatalogCache()
|
||||
owner = ("user", "uid-1")
|
||||
first = te.emulate_request(_payload(tools=_many_tools(424)), cache, owner)
|
||||
assert len(cache._entries) == 1
|
||||
second = te.emulate_request(_payload(tools=_many_tools(424)), cache, owner)
|
||||
assert len(cache._entries) == 1
|
||||
assert second["messages"][0]["content"] == first["messages"][0]["content"]
|
||||
emulated = te.cached_emulated_tools(_payload(tools=_many_tools(424)), cache, owner)
|
||||
assert emulated is not None and len(emulated[0]) == 424
|
||||
|
||||
|
||||
def test_catalog_cache_is_namespaced_per_owner_and_toolset():
|
||||
cache = te.CatalogCache()
|
||||
payload = _payload(tools=_many_tools(10))
|
||||
te.emulate_request(payload, cache, ("user", "uid-1"))
|
||||
te.emulate_request(payload, cache, ("user", "uid-2"))
|
||||
assert len(cache._entries) == 2
|
||||
assert cache.lookup(payload, ("user", "uid-3")) is None
|
||||
changed = _payload(tools=_many_tools(11))
|
||||
assert cache.lookup(changed, ("user", "uid-1")) is None
|
||||
renamed_choice = _payload(tools=_many_tools(10), tool_choice="required")
|
||||
assert cache.lookup(renamed_choice, ("user", "uid-1")) is None
|
||||
|
||||
|
||||
def test_catalog_cache_evicts_oldest_first_within_bounds():
|
||||
cache = te.CatalogCache()
|
||||
cache.MAX_ENTRIES = 3
|
||||
for index in range(5):
|
||||
te.emulate_request(
|
||||
_payload(tools=_many_tools(index + 1)), cache, ("user", "uid-1")
|
||||
)
|
||||
assert len(cache._entries) == 3
|
||||
assert cache.lookup(_payload(tools=_many_tools(1)), ("user", "uid-1")) is None
|
||||
assert cache.lookup(_payload(tools=_many_tools(5)), ("user", "uid-1")) is not None
|
||||
|
||||
|
||||
def test_catalog_cache_never_caches_a_shape_error():
|
||||
cache = te.CatalogCache()
|
||||
with pytest.raises(te.ToolEmulationError):
|
||||
te.emulate_request(_payload(tools=[{"type": "retrieval"}]), cache, ("user", "uid-1"))
|
||||
assert len(cache._entries) == 0
|
||||
|
||||
|
||||
def test_existing_stop_sequences_are_kept_and_capped():
|
||||
|
||||
Reference in New Issue
Block a user