Windows 兼容性修复 + Dify API 适配 + 企微 WS 加载优化 (#2470)

* fix: Windows fsync requires write access to file descriptor

- _fsync_file: os.O_RDONLY -> os.O_RDWR (Windows _commit() requires write access)
- _fsync_directory: tolerate OSError on fsync and os.open (chmod 0o700 blocks access on Windows)

* fix: Dify SSE empty data lines and upload response format mismatch

- Skip malformed/flushed SSE data lines (JSONDecodeError)
- Accept HTTP 200 besides 201 for file upload response
- Unwrap data wrapper in upload response (resp.get('data', resp))

* feat: WeCom WS mode sends empty initial stream frame for loading spinner

Send an empty reply_stream frame immediately after stream session
creation so the WeCom client shows its built-in loading indicator
while the pipeline processes the message (e.g. RAG retrieval).

* fix(compat): harden Windows, Dify, and WeCom changes

* fix(migration): clean interrupted SQLite temp files

---------

Co-authored-by: Hyu <chenhyu@proton.me>
This commit is contained in:
Neos
2026-08-29 22:34:10 +08:00
committed by GitHub
parent bafdaf0033
commit e69a80f5e9
6 changed files with 320 additions and 25 deletions
@@ -39,6 +39,35 @@ def _assert_verified_backup(payload: dict) -> None:
assert connection.execute('SELECT version_num FROM alembic_version').fetchone()[0] == payload['source_revision']
def _temporary_sqlite_files(root: pathlib.Path) -> list[pathlib.Path]:
return [*root.rglob('*.creating'), *root.rglob('*.restoring')]
async def test_backup_removes_stale_temporary_file_from_interrupted_run(tmp_path):
database_path = tmp_path / 'legacy-stale-backup.db'
engine = create_async_engine(f'sqlite+aiosqlite:///{database_path}')
try:
await create_legacy_resource_schema(engine, instance_uuid='stale-backup')
await alembic_runner.run_alembic_stamp(engine, '0008_mcp_resource_prefs')
backup_directory = tmp_path / 'migration-backups'
backup_directory.mkdir()
stale_path = backup_directory / '.legacy-stale-backup-pre-0009-old.creating'
unrelated_path = backup_directory / '.another-database-pre-0009-old.creating'
stale_path.write_bytes(b'interrupted backup')
unrelated_path.write_bytes(b'unrelated backup')
await sqlite_migration_backup.create_verified_backup(
engine,
source_revision='0008_mcp_resource_prefs',
target_revision='0009_workspace_tenancy',
)
assert not stale_path.exists()
assert unrelated_path.read_bytes() == b'unrelated backup'
finally:
await engine.dispose()
async def test_tenancy_migrations_retain_verified_boundary_backups(tmp_path):
database_path = tmp_path / 'legacy-with-backups.db'
engine = create_async_engine(f'sqlite+aiosqlite:///{database_path}')
@@ -59,6 +88,7 @@ async def test_tenancy_migrations_retain_verified_boundary_backups(tmp_path):
}
for payload in payloads:
_assert_verified_backup(payload)
assert _temporary_sqlite_files(tmp_path) == []
finally:
await engine.dispose()
@@ -100,6 +130,7 @@ async def test_failed_tenancy_migration_restores_backup_and_revision(
assert restored[0]['status'] == 'restored_after_failure'
assert restored[0]['source_revision'] == '0009_workspace_tenancy'
_assert_verified_backup(restored[0])
assert _temporary_sqlite_files(tmp_path) == []
monkeypatch.setattr(alembic_runner, 'run_alembic_upgrade', real_upgrade)
await _manager(engine)._run_alembic_migrations()
@@ -108,6 +139,41 @@ async def test_failed_tenancy_migration_restores_backup_and_revision(
await engine.dispose()
async def test_restore_publish_failure_preserves_current_database(tmp_path, monkeypatch):
database_path = tmp_path / 'restore-publish-failure.db'
engine = create_async_engine(f'sqlite+aiosqlite:///{database_path}')
try:
await create_legacy_resource_schema(engine, instance_uuid='restore-publish-failure')
await alembic_runner.run_alembic_stamp(engine, '0008_mcp_resource_prefs')
backup = await sqlite_migration_backup.create_verified_backup(
engine,
source_revision='0008_mcp_resource_prefs',
target_revision='0009_workspace_tenancy',
)
stale_restore_path = tmp_path / f'.{database_path.name}.interrupted.restoring'
stale_restore_path.write_bytes(b'interrupted restore')
async with engine.begin() as connection:
await connection.execute(sa.text("UPDATE alembic_version SET version_num = 'failed-revision'"))
await engine.dispose()
database_before_restore = database_path.read_bytes()
real_replace = os.replace
def fail_restore_publish(source, destination):
if pathlib.Path(destination) == database_path:
raise OSError('simulated atomic publish failure')
return real_replace(source, destination)
monkeypatch.setattr(sqlite_migration_backup.os, 'replace', fail_restore_publish)
with pytest.raises(OSError, match='atomic publish failure'):
await sqlite_migration_backup.restore_verified_backup(engine, backup)
assert database_path.read_bytes() == database_before_restore
assert _temporary_sqlite_files(tmp_path) == []
finally:
await engine.dispose()
async def test_backup_retries_transient_reopen_failure_after_replace(tmp_path, monkeypatch):
database_path = tmp_path / 'legacy-bind-mount.db'
engine = create_async_engine(f'sqlite+aiosqlite:///{database_path}')
@@ -44,6 +44,86 @@ def test_webhook_dispatch_tasks_are_bounded():
assert len(client._dispatch_tasks) == 100
@pytest.mark.asyncio
async def test_ws_initial_stream_frame_precedes_pipeline_dispatch(monkeypatch):
from langbot.libs.wecom_ai_bot_api import ws_client as ws_client_module
order = []
logger = types.SimpleNamespace(
debug=Mock(),
error=Mock(),
warning=Mock(),
)
client = WecomBotWsClient('bot-id', 'secret', logger)
async def parse_message(*args, **kwargs):
del args, kwargs
return {'msgid': 'msg-1', 'type': 'single', 'userid': 'user-1'}
async def reply_stream(*args, **kwargs):
del args, kwargs
order.append('initial-frame')
return {}
async def dispatch_event(event):
del event
order.append('pipeline-dispatch')
monkeypatch.setattr(ws_client_module, 'parse_wecom_bot_message', parse_message)
monkeypatch.setattr(ws_client_module.wecombotevent, 'WecomBotEvent', lambda data: data)
client.reply_stream = reply_stream
client._dispatch_event = dispatch_event
await client._handle_message_callback({'headers': {'req_id': 'req-1'}, 'body': {}})
assert order == ['initial-frame', 'pipeline-dispatch']
@pytest.mark.asyncio
async def test_ws_initial_stream_failure_still_dispatches_message(monkeypatch):
from langbot.libs.wecom_ai_bot_api import ws_client as ws_client_module
dispatched = []
class Logger:
def __init__(self):
self.warnings = []
async def debug(self, message):
del message
async def error(self, message):
raise AssertionError(message)
async def warning(self, message):
self.warnings.append(message)
logger = Logger()
client = WecomBotWsClient('bot-id', 'secret', logger)
async def parse_message(*args, **kwargs):
del args, kwargs
return {'msgid': 'msg-1', 'type': 'single', 'userid': 'user-1'}
async def reply_stream(*args, **kwargs):
del args, kwargs
raise ConnectionError('simulated reply failure')
async def dispatch_event(event):
dispatched.append(event)
monkeypatch.setattr(ws_client_module, 'parse_wecom_bot_message', parse_message)
monkeypatch.setattr(ws_client_module.wecombotevent, 'WecomBotEvent', lambda data: data)
client.reply_stream = reply_stream
client._dispatch_event = dispatch_event
await client._handle_message_callback({'headers': {'req_id': 'req-1'}, 'body': {}})
assert len(dispatched) == 1
assert len(logger.warnings) == 1
assert 'simulated reply failure' in logger.warnings[0]
def test_extract_template_card_action_supports_nested_button_key():
task_id, event_key, card_type = extract_template_card_action(
{
@@ -79,6 +79,32 @@ class TestDifyWorkflowSubmitClient:
with pytest.raises(errors.DifyAPIError, match='SSE event exceeds'):
await anext(client._iter_sse_json(FakeResponse()))
@pytest.mark.asyncio
async def test_sse_parser_skips_empty_data_and_done_lines(self):
from langbot.libs.dify_service_api.v1 import client
class FakeResponse:
async def aiter_bytes(self, chunk_size=None):
del chunk_size
yield b'data:\n\ndata: {"event":"message",'
yield b'"answer":"ok"}\n\ndata: [DONE]\n'
events = [event async for event in client._iter_sse_json(FakeResponse())]
assert events == [{'event': 'message', 'answer': 'ok'}]
@pytest.mark.asyncio
async def test_sse_parser_rejects_malformed_nonempty_data(self):
from langbot.libs.dify_service_api.v1 import client, errors
class FakeResponse:
async def aiter_bytes(self, chunk_size=None):
del chunk_size
yield b'data: not-json\n'
with pytest.raises(errors.DifyAPIError, match='not valid JSON'):
await anext(client._iter_sse_json(FakeResponse()))
@pytest.mark.asyncio
async def test_upload_rejects_oversized_local_file(self, tmp_path):
from langbot.libs.dify_service_api.v1 import client
@@ -93,6 +119,62 @@ class TestDifyWorkflowSubmitClient:
with pytest.raises(ValueError, match='exceeds the size limit'):
await dify_client.upload_file(file_path, 'person_user-1')
@pytest.mark.asyncio
@pytest.mark.parametrize(
('status_code', 'body', 'expected_id'),
[
(200, b'{"data":{"id":"wrapped-id"}}', 'wrapped-id'),
(201, b'{"id":"flat-id"}', 'flat-id'),
],
)
async def test_upload_accepts_supported_success_responses(self, status_code, body, expected_id):
from langbot.libs.dify_service_api.v1 import client
class FakeResponse:
headers = {}
def __init__(self):
self.status_code = status_code
async def aiter_bytes(self, chunk_size=None):
del chunk_size
yield body
class FakeStreamContext:
async def __aenter__(self):
return FakeResponse()
async def __aexit__(self, exc_type, exc, traceback):
del exc_type, exc, traceback
return False
class FakeClient:
def stream(self, *args, **kwargs):
del args, kwargs
return FakeStreamContext()
dify_client = client.AsyncDifyServiceClient('test-key', 'https://dify.example/v1')
dify_client._client = FakeClient()
response = await dify_client.upload_file(('hello.txt', b'hello', 'text/plain'), 'person_user-1')
assert response['id'] == expected_id
@pytest.mark.parametrize(
'body',
[
b'not-json',
b'[]',
b'{"data":null}',
b'{"data":{}}',
],
)
def test_upload_rejects_invalid_success_payload(self, body):
from langbot.libs.dify_service_api.v1 import client, errors
with pytest.raises(errors.DifyAPIError):
client._decode_upload_response(body)
class TestDifyExtractTextOutput:
"""Tests for _extract_dify_text_output method."""