sync update from LAN repo (source master @ ca48c4ec)

This commit is contained in:
gozeros committed 2026-11-05 10:53:54 +08:00
1 parent 8bc8b0e082
commit ea9dcff9c0
873 files changed
+107866 -14011

No files matched your search

@@ -2,6 +2,8 @@
from __future__ import annotations
import errno
from pathlib import Path
from types import SimpleNamespace
import pytest
@@ -26,6 +28,80 @@ from deerflow.persistence.model_capabilities import (
)
def test_config_atomic_replace_retries_transient_busy_filesystem_error(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
original = "models:\n - name: old\n"
updated = "models:\n - name: new\n"
config_path.write_text(original, encoding="utf-8")
real_replace = router.os.replace
attempts = 0
def replace_with_transient_busy(source: str, target: Path) -> None:
nonlocal attempts
attempts += 1
if attempts < 3:
raise OSError(errno.EBUSY, "resource busy", str(target))
real_replace(source, target)
monkeypatch.setattr(router.os, "replace", replace_with_transient_busy)
monkeypatch.setattr(router, "sleep", lambda _delay: None)
backup = router._backup_and_write_config(config_path, original, updated)
assert attempts == 3
assert config_path.read_text(encoding="utf-8") == updated
assert backup.read_text(encoding="utf-8") == original
def test_config_atomic_replace_does_not_retry_permanent_error(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
original = "models:\n - name: old\n"
config_path.write_text(original, encoding="utf-8")
attempts = 0
def fail_replace(_source: str, _target: Path) -> None:
nonlocal attempts
attempts += 1
raise OSError(errno.EACCES, "permission denied")
monkeypatch.setattr(router.os, "replace", fail_replace)
with pytest.raises(OSError, match="permission denied"):
router._backup_and_write_config(config_path, original, "models:\n - name: new\n")
assert attempts == 1
assert config_path.read_text(encoding="utf-8") == original
assert not list(tmp_path.glob(".config.yaml.*.tmp"))
def test_config_single_file_mount_is_updated_without_atomic_rename(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
original = "models:\n - name: old\n"
updated = "models:\n - name: mounted-new\n"
config_path.write_text(original, encoding="utf-8")
monkeypatch.setattr(router, "_is_file_mountpoint", lambda _path: True)
def unexpected_replace(_source: str, _target: Path) -> None:
pytest.fail("single-file bind mount must not be replaced by rename")
monkeypatch.setattr(router.os, "replace", unexpected_replace)
backup = router._backup_and_write_config(config_path, original, updated)
assert config_path.read_text(encoding="utf-8") == updated
assert backup.read_text(encoding="utf-8") == original
assert not list(tmp_path.glob(".config.yaml.*.tmp"))
def test_replace_models_block_keeps_other_sections_and_comments() -> None:
raw = """config_version: 1
models:
@@ -139,6 +215,260 @@ def test_reasoning_capability_route_precedes_generic_model_update_and_accepts_sl
]
def test_write_file_transport_route_precedes_generic_model_update() -> None:
scope = {
"type": "http",
"method": "PUT",
"path": "/api/admin/models/write-file-transport/zai-org/GLM-5-FP8",
}
matching_paths = [route.path for route in router.router.routes if route.matches(scope)[0] is Match.FULL]
assert matching_paths[:2] == [
"/api/admin/models/write-file-transport/{model_name:path}",
"/api/admin/models/{model_name:path}",
]
def _admin_request():
return SimpleNamespace(
state=SimpleNamespace(user=SimpleNamespace(system_role="admin")),
app=SimpleNamespace(state=SimpleNamespace()),
)
@pytest.mark.asyncio
async def test_write_file_transport_toggle_persists_native_and_buffered_gateway(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
config_path.write_text(
"""models:
- name: flash
use: langchain_openai:ChatOpenAI
model: flash
""",
encoding="utf-8",
)
saved: dict[str, object] = {}
async def save_models(_request, raw_before: str, models: list[dict[str, object]]) -> list[dict[str, object]]:
saved["raw_before"] = raw_before
saved["models"] = models
return models
monkeypatch.setattr(router, "_resolve_config_path", lambda: config_path)
monkeypatch.setattr(router, "_save_models", save_models)
enabled = await router.update_write_file_transport(
"flash",
router.WriteFileTransportToggleRequest(write_file_transport="buffered_gateway"),
_admin_request(),
)
assert enabled.write_file_transport == "buffered_gateway"
assert saved["models"] == [
{
"name": "flash",
"use": "langchain_openai:ChatOpenAI",
"model": "flash",
"write_file_transport": "buffered_gateway",
}
]
disabled = await router.update_write_file_transport(
"flash",
router.WriteFileTransportToggleRequest(write_file_transport="native"),
_admin_request(),
)
assert disabled.write_file_transport == "native"
assert saved["models"][0]["write_file_transport"] == "native"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("use", "use_responses_api"),
[
("langchain_anthropic:ChatAnthropic", None),
("langchain_openai:ChatOpenAI", True),
],
)
async def test_write_file_transport_rejects_incompatible_models(
use: str,
use_responses_api: bool | None,
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
original = f"""models:
- name: incompatible
use: {use}
model: incompatible
"""
if use_responses_api:
original = original.replace(
" model: incompatible\n",
" model: incompatible\n use_responses_api: true\n",
)
config_path.write_text(original, encoding="utf-8")
monkeypatch.setattr(router, "_resolve_config_path", lambda: config_path)
async def fail_save_models(_request, _raw_before, _models):
raise AssertionError("incompatible models must not be persisted")
monkeypatch.setattr(router, "_save_models", fail_save_models)
with pytest.raises(HTTPException) as exc_info:
await router.update_write_file_transport(
"incompatible",
router.WriteFileTransportToggleRequest(write_file_transport="buffered_gateway"),
_admin_request(),
)
assert exc_info.value.status_code == 422
assert config_path.read_text(encoding="utf-8") == original
def test_strict_stream_integrity_route_precedes_generic_model_update() -> None:
scope = {
"type": "http",
"method": "PUT",
"path": "/api/admin/models/strict-stream-integrity/zai-org/GLM-5-FP8",
}
matching_paths = [route.path for route in router.router.routes if route.matches(scope)[0] is Match.FULL]
assert matching_paths[:2] == [
"/api/admin/models/strict-stream-integrity/{model_name:path}",
"/api/admin/models/{model_name:path}",
]
@pytest.mark.asyncio
async def test_strict_stream_integrity_toggle_persists_explicit_bool(
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
config_path.write_text(
"""models:
- name: flash
use: langchain_openai:ChatOpenAI
model: flash
""",
encoding="utf-8",
)
saved: dict[str, object] = {}
async def save_models(_request, raw_before: str, models: list[dict[str, object]]) -> list[dict[str, object]]:
saved["models"] = models
return models
monkeypatch.setattr(router, "_resolve_config_path", lambda: config_path)
monkeypatch.setattr(router, "_save_models", save_models)
enabled = await router.update_strict_stream_integrity(
"flash",
router.StrictStreamIntegrityToggleRequest(strict_stream_integrity=True),
_admin_request(),
)
assert enabled.strict_stream_integrity is True
assert saved["models"] == [
{
"name": "flash",
"use": "langchain_openai:ChatOpenAI",
"model": "flash",
"strict_stream_integrity": True,
}
]
disabled = await router.update_strict_stream_integrity(
"flash",
router.StrictStreamIntegrityToggleRequest(strict_stream_integrity=False),
_admin_request(),
)
assert disabled.strict_stream_integrity is False
assert saved["models"][0]["strict_stream_integrity"] is False
@pytest.mark.asyncio
@pytest.mark.parametrize(
("use", "use_responses_api"),
[
("langchain_anthropic:ChatAnthropic", None),
("langchain_openai:ChatOpenAI", True),
],
)
async def test_strict_stream_integrity_rejects_incompatible_models(
use: str,
use_responses_api: bool | None,
tmp_path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
config_path = tmp_path / "config.yaml"
original = f"""models:
- name: incompatible
use: {use}
model: incompatible
"""
if use_responses_api:
original = original.replace(
" model: incompatible\n",
" model: incompatible\n use_responses_api: true\n",
)
config_path.write_text(original, encoding="utf-8")
monkeypatch.setattr(router, "_resolve_config_path", lambda: config_path)
async def fail_save_models(_request, _raw_before, _models):
raise AssertionError("incompatible models must not be persisted")
monkeypatch.setattr(router, "_save_models", fail_save_models)
with pytest.raises(HTTPException) as exc_info:
await router.update_strict_stream_integrity(
"incompatible",
router.StrictStreamIntegrityToggleRequest(strict_stream_integrity=True),
_admin_request(),
)
assert exc_info.value.status_code == 422
assert config_path.read_text(encoding="utf-8") == original
def test_legacy_model_edit_preserves_strict_stream_integrity() -> None:
existing = {
"name": "relay-model",
"use": "langchain_openai:ChatOpenAI",
"model": "relay-model",
"strict_stream_integrity": True,
}
merged = router._request_to_raw_model(
router.ModelUpsertRequest(
name="relay-model",
use="langchain_openai:ChatOpenAI",
model="relay-model-v2",
),
existing,
)
assert merged["model"] == "relay-model-v2"
assert merged["strict_stream_integrity"] is True
changed = router._request_to_raw_model(
router.ModelUpsertRequest(
name="relay-model",
use="langchain_openai:ChatOpenAI",
model="relay-model-v2",
strict_stream_integrity=False,
),
merged,
)
assert changed["strict_stream_integrity"] is False
def _reasoning_request(store, *, config: AppConfig | None = None):
return SimpleNamespace(
state=SimpleNamespace(
@@ -272,15 +602,21 @@ async def test_reasoning_capability_endpoint_and_model_apis_use_database_not_yam
updated = await router.update_reasoning_effort_capability(
"zai-org/GLM-5-FP8",
router.ReasoningEffortCapabilityRequest(supports_reasoning_effort=True),
router.ReasoningEffortCapabilityRequest(
supports_reasoning_effort=True,
default_reasoning_effort="low",
),
request,
)
admin_list = await router.list_model_configurations(request)
public_list = await public_models.list_models(request, config)
assert updated.supports_reasoning_effort is True
assert updated.default_reasoning_effort == "low"
assert admin_list.models[0].supports_reasoning_effort is True
assert public_list.models[0].supports_reasoning_effort is True
assert public_list.models[0].default_reasoning_effort == "low"
assert (await store.get_capability("zai-org/GLM-5-FP8"))["default_reasoning_effort"] == "low"
assert config_path.read_text(encoding="utf-8") == original
@@ -605,6 +941,7 @@ def test_final_reasoning_policy_overrides_both_preseeded_runtime_containers() ->
config,
capability_enabled=True,
model_supports_thinking=True,
default_reasoning_effort="low",
)
assert normalized == "low"
@@ -661,7 +998,7 @@ def test_reasoning_policy_strips_effort_when_selection_is_not_available(
assert "reasoning_effort" not in config["context"]
def test_reasoning_policy_rejects_unknown_effort() -> None:
def test_reasoning_policy_uses_global_default_instead_of_stale_thread_value() -> None:
config = {
"configurable": {},
"context": {
@@ -670,14 +1007,15 @@ def test_reasoning_policy_rejects_unknown_effort() -> None:
},
}
with pytest.raises(HTTPException) as exc_info:
services.enforce_reasoning_effort_run_config(
config,
capability_enabled=True,
model_supports_thinking=True,
)
assert exc_info.value.status_code == 422
normalized = services.enforce_reasoning_effort_run_config(
config,
capability_enabled=True,
model_supports_thinking=True,
default_reasoning_effort="low",
)
assert normalized == "low"
assert config["configurable"]["reasoning_effort"] == "low"
assert config["context"]["reasoning_effort"] == "low"
@pytest.mark.asyncio
@@ -692,7 +1030,7 @@ def test_reasoning_policy_rejects_unknown_effort() -> None:
"reasoning_effort": "minimal",
},
"qwen-a",
"low",
"medium",
),
(
{
@@ -732,6 +1070,7 @@ async def test_start_run_applies_database_reasoning_policy_after_final_context_m
await store.set_reasoning_effort_support(
"qwen-a",
supports_reasoning_effort=True,
default_reasoning_effort="medium",
)
class ThreadStore:
@@ -764,6 +1103,9 @@ async def test_start_run_applies_database_reasoning_policy_after_final_context_m
async def set_status(self, *_args, **_kwargs):
return None
def watch_completion(self, _record):
return None
run_context = SimpleNamespace(
app_config=app_config,
thread_store=ThreadStore(),