refactor(delivery): use workspace out as artifact root
Some checks failed
Test Suite / test (3.11) (push) Failing after 1m24s
Test Suite / test (3.12) (push) Failing after 1m46s
Test Suite / test (3.13) (push) Failing after 2m1s

This commit is contained in:
Hua
2026-03-20 09:10:33 +08:00
parent 73af8c574e
commit 9ac73f1e26
13 changed files with 272 additions and 344 deletions

View File

@@ -23,7 +23,7 @@ def _strip_ansi(text: str) -> str:
runner = CliRunner()
class _StopGateway(RuntimeError):
class _StopGatewayError(RuntimeError):
pass
@@ -448,12 +448,12 @@ def test_gateway_uses_workspace_from_config_by_default(monkeypatch, tmp_path: Pa
)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
lambda _config: (_ for _ in ()).throw(_StopGateway("stop")),
lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert seen["config_path"] == config_file.resolve()
assert seen["workspace"] == Path(config.agents.defaults.workspace)
@@ -476,7 +476,7 @@ def test_gateway_workspace_option_overrides_config(monkeypatch, tmp_path: Path)
)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
lambda _config: (_ for _ in ()).throw(_StopGateway("stop")),
lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
)
result = runner.invoke(
@@ -484,7 +484,7 @@ def test_gateway_workspace_option_overrides_config(monkeypatch, tmp_path: Path)
["gateway", "--config", str(config_file), "--workspace", str(override)],
)
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert seen["workspace"] == override
assert config.workspace_path == override
@@ -502,12 +502,12 @@ def test_gateway_warns_about_deprecated_memory_window(monkeypatch, tmp_path: Pat
monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
lambda _config: (_ for _ in ()).throw(_StopGateway("stop")),
lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert "memoryWindow" in result.stdout
assert "contextWindowTokens" in result.stdout
@@ -531,13 +531,13 @@ def test_gateway_uses_config_directory_for_cron_store(monkeypatch, tmp_path: Pat
class _StopCron:
def __init__(self, store_path: Path) -> None:
seen["cron_store"] = store_path
raise _StopGateway("stop")
raise _StopGatewayError("stop")
monkeypatch.setattr("nanobot.cron.service.CronService", _StopCron)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert seen["cron_store"] == config_file.parent / "cron" / "jobs.json"
@@ -554,12 +554,12 @@ def test_gateway_uses_configured_port_when_cli_flag_is_missing(monkeypatch, tmp_
monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
lambda _config: (_ for _ in ()).throw(_StopGateway("stop")),
lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert "port 18791" in result.stdout
@@ -576,10 +576,60 @@ def test_gateway_cli_port_overrides_configured_port(monkeypatch, tmp_path: Path)
monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
monkeypatch.setattr(
"nanobot.cli.commands._make_provider",
lambda _config: (_ for _ in ()).throw(_StopGateway("stop")),
lambda _config: (_ for _ in ()).throw(_StopGatewayError("stop")),
)
result = runner.invoke(app, ["gateway", "--config", str(config_file), "--port", "18792"])
assert isinstance(result.exception, _StopGateway)
assert isinstance(result.exception, _StopGatewayError)
assert "port 18792" in result.stdout
def test_gateway_constructs_http_server_without_public_file_options(monkeypatch, tmp_path: Path) -> None:
config_file = tmp_path / "instance" / "config.json"
config_file.parent.mkdir(parents=True)
config_file.write_text("{}")
config = Config()
seen: dict[str, object] = {}
monkeypatch.setattr("nanobot.config.loader.set_config_path", lambda _path: None)
monkeypatch.setattr("nanobot.config.loader.load_config", lambda _path=None: config)
monkeypatch.setattr("nanobot.cli.commands.sync_workspace_templates", lambda _path: None)
monkeypatch.setattr("nanobot.cli.commands._make_provider", lambda _config: object())
monkeypatch.setattr("nanobot.bus.queue.MessageBus", lambda: object())
monkeypatch.setattr("nanobot.session.manager.SessionManager", lambda _workspace: MagicMock())
class _DummyCronService:
def __init__(self, _store_path: Path) -> None:
pass
class _DummyAgentLoop:
def __init__(self, **kwargs) -> None:
self.model = "test-model"
self.tools = {}
seen["agent_kwargs"] = kwargs
class _DummyChannelManager:
def __init__(self, _config, _bus) -> None:
self.enabled_channels = []
class _CaptureGatewayHttpServer:
def __init__(self, host: str, port: int) -> None:
seen["host"] = host
seen["port"] = port
seen["http_server_ctor"] = True
raise _StopGatewayError("stop")
monkeypatch.setattr("nanobot.cron.service.CronService", _DummyCronService)
monkeypatch.setattr("nanobot.agent.loop.AgentLoop", _DummyAgentLoop)
monkeypatch.setattr("nanobot.channels.manager.ChannelManager", _DummyChannelManager)
monkeypatch.setattr("nanobot.gateway.http.GatewayHttpServer", _CaptureGatewayHttpServer)
result = runner.invoke(app, ["gateway", "--config", str(config_file)])
assert isinstance(result.exception, _StopGatewayError)
assert seen["host"] == config.gateway.host
assert seen["port"] == config.gateway.port
assert seen["http_server_ctor"] is True
assert "public_files_enabled" not in seen["agent_kwargs"]

View File

@@ -54,7 +54,8 @@ def test_system_prompt_mentions_workspace_out_for_generated_artifacts(tmp_path)
prompt = builder.build_system_prompt()
assert f"Put generated artifacts meant for delivery to the user under: {workspace}/out" in prompt
assert f"For QQ delivery, local images under `{workspace}/out` can be auto-published" in prompt
assert "Channels that need public URLs for local delivery artifacts expect files under " in prompt
assert "`mediaBaseUrl` at your own static file server for that directory." in prompt
def test_runtime_context_is_separate_untrusted_user_message(tmp_path) -> None:

View File

@@ -1,44 +1,23 @@
import os
from pathlib import Path
import pytest
from aiohttp.test_utils import make_mocked_request
from nanobot.gateway.http import create_http_app, get_public_dir
from nanobot.gateway.http import create_http_app
@pytest.mark.asyncio
async def test_gateway_public_route_maps_requests_into_workspace_public(tmp_path) -> None:
public_dir = get_public_dir(tmp_path)
file_path = public_dir / "hello.txt"
file_path.write_text("hello", encoding="utf-8")
async def test_gateway_health_route_exists() -> None:
app = create_http_app()
request = make_mocked_request("GET", "/healthz", app=app)
match = await app.router.resolve(request)
app = create_http_app(tmp_path)
assert match.route.resource.canonical == "/healthz"
@pytest.mark.asyncio
async def test_gateway_public_route_is_not_registered() -> None:
app = create_http_app()
request = make_mocked_request("GET", "/public/hello.txt", app=app)
match = await app.router.resolve(request)
assert match.route.resource.canonical == "/public"
assert match["filename"] == "hello.txt"
assert Path(getattr(match.route.resource, "_directory")) == public_dir
@pytest.mark.asyncio
async def test_gateway_public_route_disables_symlink_following_and_allows_hard_links(tmp_path) -> None:
out_dir = tmp_path / "out"
out_dir.mkdir()
source = out_dir / "shot.png"
source.write_bytes(b"png")
public_dir = get_public_dir(tmp_path) / "qq"
public_dir.mkdir()
published = public_dir / "shot.png"
os.link(source, published)
app = create_http_app(tmp_path)
request = make_mocked_request("GET", "/public/qq/shot.png", app=app)
match = await app.router.resolve(request)
assert os.stat(source).st_ino == os.stat(published).st_ino
assert match.route.resource.canonical == "/public"
assert match["filename"] == "qq/shot.png"
assert getattr(match.route.resource, "_follow_symlinks") is False
assert match.http_exception.status == 404
assert [resource.canonical for resource in app.router.resources()] == ["/healthz"]

View File

@@ -1,4 +1,3 @@
import os
from base64 import b64encode
from types import SimpleNamespace
@@ -185,7 +184,7 @@ async def test_send_local_media_falls_back_to_text_notice_when_publishing_not_co
{
"openid": "user123",
"msg_type": 0,
"content": "hello\n[Failed to send: demo.png - QQ local media publishing is not configured]",
"content": "hello\n[Failed to send: demo.png - local media publishing is not configured]",
"msg_id": "msg1",
"msg_seq": 2,
}
@@ -193,70 +192,7 @@ async def test_send_local_media_falls_back_to_text_notice_when_publishing_not_co
@pytest.mark.asyncio
async def test_send_local_media_under_public_dir_uses_c2c_file_api(
monkeypatch,
tmp_path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
public_dir = workspace / "public" / "qq"
public_dir.mkdir(parents=True)
source = public_dir / "demo.png"
source.write_bytes(b"fake-png")
channel = QQChannel(
QQConfig(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
),
MessageBus(),
workspace=workspace,
)
channel._client = _FakeClient()
monkeypatch.setattr("nanobot.channels.qq.validate_url_target", lambda url: (True, ""))
await channel.send(
OutboundMessage(
channel="qq",
chat_id="user123",
content="hello",
media=[str(source)],
metadata={"message_id": "msg1"},
)
)
assert channel._client.api.raw_file_upload_calls == [
{
"method": "POST",
"path": "/v2/users/{openid}/files",
"params": {"openid": "user123"},
"json": {
"file_type": 1,
"url": "https://files.example.com/public/qq/demo.png",
"file_data": b64encode(b"fake-png").decode("ascii"),
"srv_send_msg": False,
},
}
]
assert channel._client.api.c2c_file_calls == []
assert channel._client.api.c2c_calls == [
{
"openid": "user123",
"msg_type": 7,
"content": "hello",
"media": {"file_info": "c2c-file-info", "file_uuid": "c2c-file", "ttl": 60},
"msg_id": "msg1",
"msg_seq": 2,
}
]
@pytest.mark.asyncio
async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_file_api(
async def test_send_local_media_under_out_dir_uses_c2c_file_api(
monkeypatch,
tmp_path,
) -> None:
@@ -264,7 +200,7 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
workspace.mkdir()
out_dir = workspace / "out"
out_dir.mkdir()
source = out_dir / "outside.png"
source = out_dir / "demo.png"
source.write_bytes(b"\x89PNG\r\n\x1a\nfake-png")
channel = QQChannel(
@@ -272,9 +208,7 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
media_base_url="https://files.example.com/out",
),
MessageBus(),
workspace=workspace,
@@ -292,11 +226,6 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
)
)
published = list((workspace / "public" / "qq").iterdir())
assert len(published) == 1
assert published[0].name.startswith("outside-")
assert published[0].suffix == ".png"
assert os.stat(source).st_ino == os.stat(published[0]).st_ino
assert channel._client.api.raw_file_upload_calls == [
{
"method": "POST",
@@ -304,7 +233,7 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
"params": {"openid": "user123"},
"json": {
"file_type": 1,
"url": f"https://files.example.com/public/qq/{published[0].name}",
"url": "https://files.example.com/out/demo.png",
"file_data": b64encode(b"\x89PNG\r\n\x1a\nfake-png").decode("ascii"),
"srv_send_msg": False,
},
@@ -324,7 +253,69 @@ async def test_send_local_media_from_out_auto_links_into_public_then_uses_c2c_fi
@pytest.mark.asyncio
async def test_send_local_media_outside_public_and_out_falls_back_to_text_notice(
async def test_send_local_media_in_nested_out_path_uses_relative_url(
monkeypatch,
tmp_path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
out_dir = workspace / "out"
source_dir = out_dir / "shots"
source_dir.mkdir(parents=True)
source = source_dir / "github.png"
source.write_bytes(b"\x89PNG\r\n\x1a\nfake-png")
channel = QQChannel(
QQConfig(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/qq-media",
),
MessageBus(),
workspace=workspace,
)
channel._client = _FakeClient()
monkeypatch.setattr("nanobot.channels.qq.validate_url_target", lambda url: (True, ""))
await channel.send(
OutboundMessage(
channel="qq",
chat_id="user123",
content="hello",
media=[str(source)],
metadata={"message_id": "msg1"},
)
)
assert channel._client.api.raw_file_upload_calls == [
{
"method": "POST",
"path": "/v2/users/{openid}/files",
"params": {"openid": "user123"},
"json": {
"file_type": 1,
"url": "https://files.example.com/qq-media/shots/github.png",
"file_data": b64encode(b"\x89PNG\r\n\x1a\nfake-png").decode("ascii"),
"srv_send_msg": False,
},
}
]
assert channel._client.api.c2c_file_calls == []
assert channel._client.api.c2c_calls == [
{
"openid": "user123",
"msg_type": 7,
"content": "hello",
"media": {"file_info": "c2c-file-info", "file_uuid": "c2c-file", "ttl": 60},
"msg_id": "msg1",
"msg_seq": 2,
}
]
@pytest.mark.asyncio
async def test_send_local_media_outside_out_falls_back_to_text_notice(
monkeypatch,
tmp_path,
) -> None:
@@ -340,9 +331,7 @@ async def test_send_local_media_outside_public_and_out_falls_back_to_text_notice
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
media_base_url="https://files.example.com/out",
),
MessageBus(),
workspace=workspace,
@@ -365,8 +354,10 @@ async def test_send_local_media_outside_public_and_out_falls_back_to_text_notice
{
"openid": "user123",
"msg_type": 0,
"content": "hello\n[Failed to send: outside.png - QQ local media must stay under "
f"{workspace / 'public' / 'qq'} or {workspace / 'out'}]",
"content": (
"hello\n[Failed to send: outside.png - local delivery media must stay under "
f"{workspace / 'out'}]"
),
"msg_id": "msg1",
"msg_seq": 2,
}
@@ -380,19 +371,17 @@ async def test_send_local_media_falls_back_to_url_only_upload_when_file_data_upl
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
public_dir = workspace / "public" / "qq"
public_dir.mkdir(parents=True)
source = public_dir / "demo.png"
source.write_bytes(b"fake-png")
out_dir = workspace / "out"
out_dir.mkdir()
source = out_dir / "demo.png"
source.write_bytes(b"\x89PNG\r\n\x1a\nfake-png")
channel = QQChannel(
QQConfig(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
media_base_url="https://files.example.com/out",
),
MessageBus(),
workspace=workspace,
@@ -415,7 +404,7 @@ async def test_send_local_media_falls_back_to_url_only_upload_when_file_data_upl
{
"openid": "user123",
"file_type": 1,
"url": "https://files.example.com/public/qq/demo.png",
"url": "https://files.example.com/out/demo.png",
"srv_send_msg": False,
}
]
@@ -432,17 +421,17 @@ async def test_send_local_media_falls_back_to_url_only_upload_when_file_data_upl
@pytest.mark.asyncio
async def test_send_local_media_symlink_to_outside_public_dir_is_rejected(
async def test_send_local_media_symlink_to_outside_out_dir_is_rejected(
monkeypatch,
tmp_path,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
public_dir = workspace / "public" / "qq"
public_dir.mkdir(parents=True)
out_dir = workspace / "out"
out_dir.mkdir()
outside = tmp_path / "secret.png"
outside.write_bytes(b"secret")
source = public_dir / "linked.png"
source = out_dir / "linked.png"
source.symlink_to(outside)
channel = QQChannel(
@@ -450,9 +439,7 @@ async def test_send_local_media_symlink_to_outside_public_dir_is_rejected(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
media_base_url="https://files.example.com/out",
),
MessageBus(),
workspace=workspace,
@@ -475,8 +462,10 @@ async def test_send_local_media_symlink_to_outside_public_dir_is_rejected(
{
"openid": "user123",
"msg_type": 0,
"content": "hello\n[Failed to send: linked.png - QQ local media must stay under "
f"{workspace / 'public' / 'qq'} or {workspace / 'out'}]",
"content": (
"hello\n[Failed to send: linked.png - local delivery media must stay under "
f"{workspace / 'out'}]"
),
"msg_id": "msg1",
"msg_seq": 2,
}
@@ -500,9 +489,7 @@ async def test_send_non_image_media_from_out_falls_back_to_text_notice(
app_id="app",
secret="secret",
allow_from=["*"],
media_base_url="https://files.example.com/public/qq",
media_public_dir="public/qq",
media_ttl_seconds=0,
media_base_url="https://files.example.com/out",
),
MessageBus(),
workspace=workspace,
@@ -525,7 +512,7 @@ async def test_send_non_image_media_from_out_falls_back_to_text_notice(
{
"openid": "user123",
"msg_type": 0,
"content": "hello\n[Failed to send: note.txt - QQ local media must be an image]",
"content": "hello\n[Failed to send: note.txt - local delivery media must be an image]",
"msg_id": "msg1",
"msg_seq": 2,
}