Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions loopx/canary/module_metric_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
"loopx/capabilities/native_chat/external_conversations.py": {
"lines": 2111
},
"loopx/capabilities/steward_executor/allocation.py": {
"any_count": 0,
"dict_any_count": 0,
"lines": 225
},
"loopx/capabilities/steward_executor/machine_defaults.py": {
"any_count": 0,
"dict_any_count": 0,
"lines": 379
},
"loopx/chat_actions.py": {
"any_count": 52,
"dict_any_count": 0
Expand Down
35 changes: 23 additions & 12 deletions loopx/capabilities/steward_executor/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from ...reasoning_effort import REASONING_EFFORTS
from .machine_defaults import (
FLEXIBLE_SELECTION_POLICY,
STEWARD_SELECTION_POLICIES,
)

Payload = dict[str, object]
PayloadView = Mapping[str, object]

MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION = "manager_executor_allocation_v0"
MANAGER_ALLOCATION_REASON_USER_EXPLICIT = "user_explicit"
MANAGER_ALLOCATION_REASON_PINNED = "pinned_configuration"
Expand Down Expand Up @@ -66,8 +68,8 @@


def normalize_manager_executor_allocation(
raw: Mapping[str, Any],
) -> dict[str, Any]:
raw: PayloadView,
) -> Payload:
"""Validate the safe, restart-stable allocation stored on a Session."""

unknown = sorted(set(raw) - _FIELDS - _CONFIGURED_FIELDS)
Expand All @@ -86,7 +88,7 @@ def normalize_manager_executor_allocation(
f"{MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION}"
)

normalized = dict(raw)
normalized: Payload = dict(raw)
configured_fields = set(raw) & _CONFIGURED_FIELDS
if configured_fields and configured_fields != _CONFIGURED_FIELDS:
raise ValueError("manager_executor_allocation must capture both configured model fields")
Expand All @@ -95,8 +97,8 @@ def normalize_manager_executor_allocation(
if value is not None and (not isinstance(value, str) or not value.strip()):
raise ValueError(f"manager_executor_allocation.{field} must be null or non-empty")
normalized[field] = value.strip() if isinstance(value, str) else None
if (normalized.get("configured_reasoning_effort") is not None
and normalized["configured_reasoning_effort"] not in REASONING_EFFORTS):
configured_effort = normalized.get("configured_reasoning_effort")
if configured_effort is not None and configured_effort not in REASONING_EFFORTS:
raise ValueError("manager_executor_allocation.configured_reasoning_effort is unsupported")
for field in _NONEMPTY_TEXT_FIELDS:
value = raw.get(field)
Expand Down Expand Up @@ -165,7 +167,7 @@ def normalize_manager_executor_allocation(


def manager_executor_model(
raw: Mapping[str, Any] | None,
raw: PayloadView | None,
) -> dict[str, str] | None:
"""Project the provider model fields from one persisted allocation."""

Expand All @@ -179,8 +181,8 @@ def manager_executor_model(


def manager_executor_session_fields(
raw: Mapping[str, Any] | None,
) -> dict[str, Any]:
raw: PayloadView | None,
) -> Payload:
"""Persist a validated allocation without leaking its shape into runtime code."""

return (
Expand All @@ -190,10 +192,19 @@ def manager_executor_session_fields(
)


def restored_executor_model(session: Mapping[str, Any]) -> Mapping[str, Any] | None:
def restored_executor_model(session: PayloadView) -> PayloadView | None:
"""Restore the model binding for manager and legacy LoopX-tool Sessions."""

allocation = session.get("manager_executor_allocation")
if isinstance(allocation, Mapping):
return manager_executor_model(allocation)
return session.get("loopx_executor") if session.get("loopx_tools") else None
if allocation.get("schema_version") == MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION:
return manager_executor_model(allocation)
return None
if allocation is not None:
return None
legacy_executor = session.get("loopx_executor")
return (
legacy_executor
if session.get("loopx_tools") and isinstance(legacy_executor, Mapping)
else None
)
31 changes: 19 additions & 12 deletions loopx/capabilities/steward_executor/machine_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from collections.abc import Mapping
import importlib
from pathlib import Path
from typing import Any

from ...control_plane.turn_driver.host_binding import (
EXECUTOR_KIND_MANAGED,
Expand All @@ -38,6 +37,9 @@
machine_configuration_revision,
)

Payload = dict[str, object]
PayloadView = Mapping[str, object]

# The endpoint an operator selects to run the steward on the managed executor,
# and the endpoint the channel runs when nothing is selected. Neither name is
# "default" beyond that: the shipped default is stated once, in the channel
Expand Down Expand Up @@ -100,7 +102,7 @@ def steward_reasoning_efforts() -> tuple[str, ...]:
return REASONING_EFFORTS


def _optional_text(value: Any, *, field: str) -> str | None:
def _optional_text(value: object, *, field: str) -> str | None:
"""Normalize one absent-or-selected text field.

A blank value and an absent value mean the same thing -- this namespace
Expand All @@ -120,8 +122,8 @@ def _optional_text(value: Any, *, field: str) -> str | None:


def normalize_steward_executor_machine_defaults(
raw: Mapping[str, Any],
) -> dict[str, Any]:
raw: PayloadView,
) -> Payload:
"""Normalize one typed steward-executor machine default, fail closed."""

schema_version = str(raw.get("schema_version") or "").strip()
Expand Down Expand Up @@ -161,7 +163,7 @@ def normalize_steward_executor_machine_defaults(
"steward_executor.reasoning_effort must be one of "
+ ", ".join(supported_efforts)
)
normalized = {
normalized: Payload = {
"schema_version": schema_version,
EXECUTOR_ENDPOINT_FIELD: endpoint,
MODEL_FIELD: _optional_text(raw.get(MODEL_FIELD), field=MODEL_FIELD),
Expand Down Expand Up @@ -259,7 +261,7 @@ def _projection(
selection_policy: str = PREFERRED_SELECTION_POLICY,
eligible_endpoints: list[str] | None = None,
repair: str = "",
) -> dict[str, Any]:
) -> Payload:
return {
"schema_version": STEWARD_EXECUTOR_EFFECTIVE_SCHEMA,
"status": status,
Expand All @@ -275,8 +277,8 @@ def _projection(


def effective_steward_executor_defaults(
machine_configuration: Mapping[str, Any] | None,
) -> dict[str, Any]:
machine_configuration: PayloadView | None,
) -> Payload:
"""Resolve the steward executor defaults from one machine document.

``None``, a document that carries no steward namespace, and a malformed
Expand Down Expand Up @@ -311,21 +313,26 @@ def effective_steward_executor_defaults(
"machine_configuration.namespaces.steward_executor must be an object"
)
normalized = normalize_steward_executor_machine_defaults(raw)
raw_model = normalized[MODEL_FIELD]
raw_effort = normalized[REASONING_EFFORT_FIELD]
raw_eligible = normalized.get(ELIGIBLE_ENDPOINTS_FIELD)
return _projection(
status="ready",
source="machine_configuration",
revision=machine_configuration_revision(normalized),
executor_endpoint=str(normalized[EXECUTOR_ENDPOINT_FIELD]),
model=normalized[MODEL_FIELD],
reasoning_effort=normalized[REASONING_EFFORT_FIELD],
model=raw_model if isinstance(raw_model, str) else None,
reasoning_effort=raw_effort if isinstance(raw_effort, str) else None,
selection_policy=str(
normalized.get(SELECTION_POLICY_FIELD) or PREFERRED_SELECTION_POLICY
),
eligible_endpoints=list(normalized.get(ELIGIBLE_ENDPOINTS_FIELD) or []),
eligible_endpoints=[str(item) for item in raw_eligible]
if isinstance(raw_eligible, list)
else [],
)


def load_effective_steward_executor_defaults(runtime_root: Path) -> dict[str, Any]:
def load_effective_steward_executor_defaults(runtime_root: Path) -> Payload:
"""Read this machine's steward executor default from the live store.

Each namespace owns its runtime effect, so a malformed sibling cannot
Expand Down
5 changes: 4 additions & 1 deletion loopx/chat_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,10 @@ def manager_session_model_allocation(
return None
proposed["reasoning_effort"] = session["agent_reasoning_effort"]
return proposed
if not isinstance(previous, Mapping):
if (
not isinstance(previous, Mapping)
or previous.get("schema_version") != MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION
):
return None
if defaults is None or defaults.get("status") != "ready":
return None
Expand Down
56 changes: 56 additions & 0 deletions tests/capabilities/test_steward_executor_machine_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
configure_machine_configuration,
machine_configuration_store_path,
)
from loopx.capabilities.steward_executor.allocation import restored_executor_model
from loopx.capabilities.steward_executor import (
MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION,
STEWARD_EXECUTOR_NAMESPACE,
STEWARD_EXECUTOR_SCHEMA,
effective_steward_executor_defaults,
Expand Down Expand Up @@ -295,3 +297,57 @@ def test_a_malformed_sibling_does_not_rewrite_a_valid_steward_selection(

assert effective["status"] == "ready"
assert effective["executor_endpoint"] == "dsh"


def test_restored_executor_model_ignores_partial_pre_v0_and_validates_v0() -> None:
valid_v0 = {
"schema_version": MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION,
"selection_policy": "preferred",
"allocation_reason": "product_default",
"executor_endpoint": "codex",
"executor_endpoint_source": "product_default",
"executor_endpoint_default_reason": "steward_channel_default",
"configured_endpoint": None,
"eligible_endpoints": [],
"configuration_revision": "",
"available": None,
"model": "gpt-6-astra",
"model_source": "vendor_default",
"reasoning_effort": "high",
}
assert restored_executor_model({"manager_executor_allocation": valid_v0}) == {
"model": "gpt-6-astra",
"reasoning_effort": "high",
}
with pytest.raises(ValueError, match="missing manager executor allocation fields"):
restored_executor_model(
{
"manager_executor_allocation": {
"schema_version": MANAGER_EXECUTOR_ALLOCATION_SCHEMA_VERSION,
"selection_policy": "preferred",
}
}
)

partial = {
"schema_version": 1,
"selection_policy": "preferred",
}
assert (
restored_executor_model(
{
"manager_executor_allocation": partial,
"loopx_tools": True,
"loopx_executor": {"model": "legacy-model"},
}
)
is None
)
assert restored_executor_model(
{
"manager_executor_allocation": None,
"loopx_tools": True,
"loopx_executor": {"model": "legacy-model"},
}
) == {"model": "legacy-model"}
assert restored_executor_model({"loopx_tools": False, "loopx_executor": {"model": "x"}}) is None
14 changes: 13 additions & 1 deletion tests/test_chat_manager_model_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def test_model_edit_keeps_early_context_through_public_resume(upstream_boundary,


@pytest.mark.parametrize("source", ["working", "stale_context", "missing_rollout", "wrong_provider"])
def test_bound_steward_model_edit_uses_original_native_checkpoint(steward, monkeypatch, source):
def test_bound_steward_model_edit_uses_original_native_checkpoint(steward, monkeypatch, source): # noqa: F811
from loopx.capabilities.native_chat import codex_auth

store, runtime, provider, transport, binding, capture, workspace = steward
Expand Down Expand Up @@ -736,3 +736,15 @@ def conversation_epoch(self, *, codex_home, consumer="worker", model=None):
assert preparation["planned_method"] == "thread/resume" and preparation["history_message_count"] == 0
else:
assert preparation is None


def test_manager_session_model_allocation_ignores_partial_pre_v0_record(conversation) -> None:
root, _store, runtime, session, _starts = conversation
configure(root, model="gpt-6.1-sol", effort="high", endpoint="codex", policy="preferred")
partial_allocation = {
"schema_version": 1,
"selection_policy": "preferred",
}
partial_session = {**session, "manager_executor_allocation": partial_allocation}
assert manager_session_model_allocation(runtime, partial_session) is None
assert partial_session["manager_executor_allocation"] == partial_allocation
Loading