Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(server): parse LoRA request aliases consistently

svonavab1037122d2f3
diff --git a/packages/sie_server/src/sie_server/types/lora.py b/packages/sie_server/src/sie_server/types/lora.py
new file mode 100644
index 000000000..47ae860c3
--- /dev/null
+++ b/packages/sie_server/src/sie_server/types/lora.py
@@ -0,0 +1,49 @@
+"""Dependency-free parsing of public and legacy request LoRA selectors.
+
+Parsing does not authorize an adapter or resolve its source. Callers must check
+the selected profile's declarations before loading or dispatching any work.
+"""
+
+from collections.abc import Mapping
+from dataclasses import dataclass
+from typing import Any
+
+LORA_SELECTION_FIELDS = ("lora_adapter", "lora", "lora_id")
+
+
+class InvalidLoraSelectionError(ValueError):
+ """A request contains an invalid or inconsistent LoRA selector."""
+
+
+@dataclass(frozen=True)
+class LoraSelection:
+ """Preserve absence, explicit base selection, and supplied alias spellings."""
+
+ provided: bool
+ value: str | None
+ fields: tuple[str, ...]
+
+
+def split_lora_selection(options: Mapping[str, Any]) -> tuple[LoraSelection, dict[str, Any]]:
+ """Return the selection and a copy without any request selector fields.
+
+ ``lora_adapter`` is canonical; ``lora`` and ``lora_id`` are compatibility
+ spellings. Multiple supplied fields must contain the same string or all
+ contain null. An absent selector leaves a profile default intact; explicit
+ null requests the base model. Strings are preserved exactly, never trimmed
+ or interpreted as paths. ``fields`` lets admission retain any narrower
+ legacy source-ID rule for ``lora_id``.
+ """
+ fields = tuple(field for field in LORA_SELECTION_FIELDS if field in options)
+ value: str | None = None
+ for index, field in enumerate(fields):
+ candidate = options[field]
+ if candidate is not None and (not isinstance(candidate, str) or not candidate.strip()):
+ raise InvalidLoraSelectionError(f"'{field}' must be a non-empty string or null")
+ if index and candidate != value:
+ raise InvalidLoraSelectionError("LoRA selector fields must agree, including explicit null values")
+ value = candidate
+ return (
+ LoraSelection(provided=bool(fields), value=value, fields=fields),
+ {key: value for key, value in options.items() if key not in LORA_SELECTION_FIELDS},
+ )
diff --git a/packages/sie_server/tests/core/test_lora_selection.py b/packages/sie_server/tests/core/test_lora_selection.py
new file mode 100644
index 000000000..7a31449f7
--- /dev/null
+++ b/packages/sie_server/tests/core/test_lora_selection.py
@@ -0,0 +1,60 @@
+from itertools import combinations
+
+import pytest
+from sie_server.types.lora import (
+ LORA_SELECTION_FIELDS,
+ InvalidLoraSelectionError,
+ split_lora_selection,
+)
+
+
[email protected]("field", LORA_SELECTION_FIELDS)
[email protected]("value", [None, "legal", "org/pinned-adapter"])
+def test_selection_preserves_presence_value_and_caller_options(field: str, value: str | None) -> None:
+ nested = {"schema": ["unchanged"]}
+ options = {field: value, "other": nested}
+ selection, remaining = split_lora_selection(options)
+
+ assert selection.provided
+ assert selection.value == value
+ assert selection.fields == (field,)
+ assert remaining == {"other": nested}
+ assert remaining["other"] is nested
+ assert options == {field: value, "other": nested}
+ remaining["new"] = True
+ assert "new" not in options
+
+
+def test_absent_selection_is_distinct_from_explicit_null() -> None:
+ missing, missing_options = split_lora_selection({"threshold": 0.5})
+ base, base_options = split_lora_selection({"lora_adapter": None, "threshold": 0.5})
+ assert not missing.provided
+ assert missing.fields == ()
+ assert missing.value is None
+ assert base.provided
+ assert base.value is None
+ assert missing_options == base_options == {"threshold": 0.5}
+
+
[email protected]("fields", [*combinations(LORA_SELECTION_FIELDS, 2), LORA_SELECTION_FIELDS])
[email protected]("value", [None, "legal"])
+def test_identical_aliases_are_accepted(fields: tuple[str, ...], value: str | None) -> None:
+ selected, remaining = split_lora_selection(dict.fromkeys(fields, value))
+ assert selected.value == value
+ assert selected.fields == fields
+ assert selected.provided
+ assert remaining == {}
+
+
[email protected]("fields", list(combinations(LORA_SELECTION_FIELDS, 2)))
[email protected]("values", [(None, "legal"), ("legal", None), ("legal", "other")])
+def test_conflicting_aliases_are_rejected(fields: tuple[str, ...], values: tuple[str | None, ...]) -> None:
+ with pytest.raises(InvalidLoraSelectionError, match="must agree"):
+ split_lora_selection(dict(zip(fields, values, strict=True)))
+
+
[email protected]("field", LORA_SELECTION_FIELDS)
[email protected]("value", ["", " \t", 0, False, True, 1.0, [], {}, ["legal"]])
+def test_invalid_selector_types_are_rejected(field: str, value: object) -> None:
+ with pytest.raises(InvalidLoraSelectionError, match="non-empty string or null"):
+ split_lora_selection({field: value})