From fd1d5e7004c91d00c1c7572a060430157d929ef8 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:11:24 +0800 Subject: [PATCH] refactor(native-chat): eliminate Any and slim conversation_bindings Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- loopx/canary/module_metric_baseline.json | 5 + .../native_chat/conversation_bindings.py | 1332 ++++++++--------- 2 files changed, 640 insertions(+), 697 deletions(-) diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json index 9e224aaf1d..8b9a42cbde 100644 --- a/loopx/canary/module_metric_baseline.json +++ b/loopx/canary/module_metric_baseline.json @@ -45,6 +45,11 @@ "loopx/capabilities/issue_fix/pr_lifecycle.py": { "lines": 2056 }, + "loopx/capabilities/native_chat/conversation_bindings.py": { + "any_count": 0, + "dict_any_count": 0, + "lines": 1108 + }, "loopx/capabilities/reward_memory/utility_reducer.py": { "any_count": 0, "dict_any_count": 0, diff --git a/loopx/capabilities/native_chat/conversation_bindings.py b/loopx/capabilities/native_chat/conversation_bindings.py index 76a3e2c34e..4cfd670ee7 100644 --- a/loopx/capabilities/native_chat/conversation_bindings.py +++ b/loopx/capabilities/native_chat/conversation_bindings.py @@ -6,118 +6,229 @@ """ from __future__ import annotations -from collections.abc import Callable, Iterator, Mapping -from contextlib import contextmanager +from collections.abc import Callable, Iterator, Mapping, Sequence +from contextlib import AbstractContextManager, contextmanager +import hashlib +import json import logging from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Protocol, TypeAlias, TypedDict, TypeVar, cast import uuid -import hashlib from ...chat_store import TERMINAL_TURN_STATES, _atomic_write_json, _read_json -from ...control_plane.effect_runtime import ( - EffectRuntimeRejected, effect_runtime_request_scope, effect_runtime_result, -) +from ...control_plane.collaboration import inbox +from ...control_plane.collaboration.goal_instance_scope import collaboration_goal_scope +from ...control_plane.collaboration.source_policy_fence import source_policy_fence +from ...control_plane.effect_runtime import EffectRuntimeRejected, effect_runtime_request_scope, effect_runtime_result from ...file_lock import exclusive_file_lock +JsonDict: TypeAlias = dict[str, object] +JsonMap: TypeAlias = Mapping[str, object] +_T = TypeVar("_T") + if TYPE_CHECKING: from ...chat_store import ChatSessionStore + from ...control_plane.collaboration.goal_instance_scope import CollaborationGoalScope + class _ProjectContexts(Protocol): + workspace_grant: str + conversation_bindings: ChatConversationBindings | None + def available(self) -> Sequence[JsonMap]: ... + def session_context(self, session: JsonDict) -> JsonDict: ... + class _BindingController(Protocol): + store: ChatSessionStore + coordination_runtime_root: Path + registry_path: Path | None + project_contexts: _ProjectContexts + def interrupt_turn(self, *, session_id: str, turn_id: str) -> JsonDict: ... + def resume_session_queue(self, *, session_id: str, work_dir: Path, objective: str) -> None: ... + class _NativePeerSession(Protocol): + def send(self, user_message: str, *, project_collaboration: bool = False, + turn_start_guard: Callable[[], AbstractContextManager[None]] | None = None, + on_event: Callable[[str, JsonDict], None] | None = None) -> JsonDict: ... + class _RequestLockFn(Protocol): + def __call__(self, root: Path, request_id: str, scope: object, legacy_path: Path, *, force_exact: bool = False) -> AbstractContextManager[None]: ... + class _RecordResultFn(Protocol): + def __call__(self, root: Path, row: JsonMap, phase: str, text: str, *, request_revision: int | None = None) -> JsonDict: ... + class _AgentObservation(TypedDict): + session: JsonDict + goal: JsonDict + host_binding_verified: bool + host_ref: str + host_audience_binding_ids: list[str] + _EventSink: TypeAlias = Callable[[str, JsonDict], None] + + +def _need(value: object, kind: type[_T], message: str = "unexpected value type") -> _T: + if not isinstance(value, kind): + raise ValueError(message) + return value + + +def _as_dict(value: object, message: str = "expected JSON object") -> JsonDict: + return cast("JsonDict", _need(value, dict, message)) + + +def _dict_or_empty(value: object) -> JsonDict: + return cast("JsonDict", value) if isinstance(value, dict) else {} + + +def _dict_list(value: object, message: str = "expected list of objects") -> list[JsonDict]: + return [_as_dict(item, message) for item in _need(value, list, message)] + + +def _find_binding(state: JsonMap, key: str, value: object, *, default_empty: bool = False) -> JsonDict | None: + rows = state.get("bindings", []) if default_empty else state["bindings"] + if not isinstance(rows, list): + raise ValueError("invalid conversation bindings state") + return next((cast("JsonDict", row) for row in rows if isinstance(row, dict) and row.get(key) == value), None) + + +def _require_binding(state: JsonMap, key: str, value: object) -> JsonDict: + if (row := _find_binding(state, key, value)) is None: + raise ValueError("conversation binding is no longer authorized") + return row + + +def _session_mentions_binding(session: JsonMap, binding_id: str) -> bool: + return any(isinstance(ctx := session.get(k), dict) and ctx.get("binding_id") == binding_id for k in ("project_context", "steward_context")) + + +def _iter_open_session_turns(store: ChatSessionStore) -> Iterator[tuple[JsonDict, JsonDict]]: + for row in store.list_sessions(): + if (session := store.load_session(_need(row["session_id"], str))) is None: + continue + session_id = _need(session["session_id"], str) + turns = store.queued_turns(session_id) + if isinstance(active_id := session.get("active_turn_id"), str) and active_id and (active := store.load_turn(session_id, active_id)) is not None: + turns.append(active) + for turn in turns: + if turn.get("status") not in TERMINAL_TURN_STATES: + yield session, turn + + +def _host_observation_fields(store: ChatSessionStore, session: JsonMap, goal: JsonMap) -> JsonDict: + return {"goal": {"goal_id": goal["id"], "workspace_path": str(Path(_need(goal["repo"], str)).resolve()), + "registered_agents": _dict_or_empty(goal.get("coordination")).get("registered_agents", [])}, + "host_binding_verified": True, "host_ref": _host_ref(session), "host_audience_binding_ids": _host_audiences(store, session)} class ChatConversationBindings: - def __init__(self, *, root: Path, project_contexts: Any, - observe: Callable[[str], dict[str, Any]], - observe_group: Callable[[str], dict[str, Any]] | None = None) -> None: - self.path = root / "conversation-bindings.json" - self.projects = project_contexts - self._observe_owner = observe - self._observe_group = observe_group - self.controller: Any | None = None - - def observe(self, transport_ref: str, *, audience: str | None = None) -> dict[str, Any]: - row: dict[str, Any] = next((item for item in self.read()["bindings"] if item["transport_ref"] == transport_ref), {}) + def __init__(self, *, root: Path, project_contexts: _ProjectContexts | None, + observe: Callable[[str], JsonDict], observe_group: Callable[[str], JsonDict] | None = None) -> None: + self.path, self.projects = root / "conversation-bindings.json", project_contexts + self._observe_owner, self._observe_group = observe, observe_group + self.controller: _BindingController | None = None + + def _require_projects(self) -> _ProjectContexts: + if self.projects is None: + raise ValueError("workspace grants unavailable") + return self.projects + + def _require_controller(self) -> _BindingController: + if self.controller is None: + raise ValueError("the bound conversation store is unavailable") + return self.controller + + def _publish_state(self, result: JsonMap, *, error_message: str, preserve_mode: bool = False) -> JsonDict: + state = _as_dict(result["state"]) + if result["changed"]: + _atomic_write_json(self.path, state, preserve_mode=True) if preserve_mode else _atomic_write_json(self.path, state) + if (readback := self.read()) != state: + raise OSError(error_message) + return readback + + def _verify_unchanged(self, key: str, value: object, expected: JsonMap, *, + message: str = "conversation binding changed during identity verification", default_empty: bool = False) -> JsonDict: + current = self.read() + if _find_binding(current, key, value, default_empty=default_empty) != expected: + raise ValueError(message) + return current + + def _resolve_source(self, binding_id: str, source: JsonMap, *, include_observation: bool = False, session: JsonMap | None = None) -> JsonDict: + session_context, group_ref, topic_ref, origin = source.get("session_context"), source.get("group_ref"), source.get("topic_ref"), source.get("origin") + return self.resolve( + binding_id=binding_id, source_ref=_need(source["source_ref"], str), sender_ref=_need(source["sender_ref"], str), + private_human_message=_need(source["private_human_message"], bool), + session_context=_as_dict(session_context) if session_context is not None else None, + group_ref=_need(group_ref, str) if group_ref is not None else None, topic_ref=_need(topic_ref, str) if topic_ref is not None else None, + group_human_message=_need(source.get("group_human_message", False), bool), include_observation=include_observation, + origin=_need(origin, str) if origin is not None else None, session=session, + ) + + def observe(self, transport_ref: str, *, audience: str | None = None) -> JsonDict: + row = _find_binding(self.read(), "transport_ref", transport_ref) or {} if audience is None: - audience = row.get("audience", "owner") + audience = raw if isinstance(raw := row.get("audience"), str) else "owner" if audience == "group" and self._observe_group is not None: observed = self._observe_group(transport_ref) # Earlier group grants used the verified user's principal. Keep # that exact identity/home until an explicit disconnect/reconnect; # changing it during a poll would invalidate resumed Sessions. - if (row.get("audience") == "group" and row.get("provider_ref") == observed["provider_ref"] - and row.get("operator_ref") != observed["operator_ref"]): + if row.get("audience") == "group" and row.get("provider_ref") == observed["provider_ref"] and row.get("operator_ref") != observed["operator_ref"]: return self._observe_owner(transport_ref) return observed return self._observe_owner(transport_ref) - def read(self) -> dict[str, Any]: + def read(self) -> JsonDict: if not self.path.exists(): return {"schema_version": "loopx_chat_conversation_bindings_v0", "revision": 0, "bindings": []} # An unreadable existing grant is not an empty configuration to overwrite. - return _read_json(self.path) + return cast("JsonDict", _read_json(self.path)) @staticmethod - def _core(operation: str, params: dict[str, Any], *, large_local_snapshot: bool = False) -> dict[str, Any]: + def _core(operation: str, params: JsonMap, *, large_local_snapshot: bool = False) -> JsonDict: try: result = effect_runtime_result(operation, params, large_local_snapshot=large_local_snapshot) except EffectRuntimeRejected as exc: raise ValueError(str(exc)) from exc if not isinstance(result, dict): raise ValueError("conversation authority returned an invalid result") - return result - - def configure(self, *, transport_ref: str, project_ref: str, - executor_endpoint_id: str, context_kind: str = "project", - project_grant: str | None = None, goal_scope: str | None = None, - audience: str | None = None, group_refs: list[str] | None = None, - available_group_refs: list[str] | None = None, - expected_provider_ref: str | None = None, - expected_operator_ref: str | None = None) -> dict[str, Any]: + return cast("JsonDict", result) + + def configure(self, *, transport_ref: str, project_ref: str, executor_endpoint_id: str, + context_kind: str = "project", project_grant: str | None = None, goal_scope: str | None = None, + audience: str | None = None, group_refs: list[str] | None = None, available_group_refs: list[str] | None = None, + expected_provider_ref: str | None = None, expected_operator_ref: str | None = None) -> JsonDict: if goal_scope is not None and (context_kind != "steward" or goal_scope not in {"selected", "all_registered"}): raise ValueError("select a supported steward Goal scope") + projects = self._require_projects() if project_grant is None: - project_grant = self.projects.workspace_grant if context_kind == "project" and executor_endpoint_id == "codex" else "workspace_read" + project_grant = projects.workspace_grant if context_kind == "project" and executor_endpoint_id == "codex" else "workspace_read" if project_grant not in {"workspace_read", "workspace_write"} or (context_kind != "project" and project_grant != "workspace_read"): raise ValueError("workspace write authorization is only available for project Chat") observation = self.observe(transport_ref, audience=audience or "owner") - if expected_provider_ref is not None and observation["provider_ref"] != expected_provider_ref: - raise ValueError("the selected App changed before binding publication; verify it again") - if expected_operator_ref is not None and observation["operator_ref"] != expected_operator_ref: - raise ValueError("the selected owner changed before binding publication; verify it again") - candidate = { - "schema_version": "loopx_chat_conversation_binding_v0", - "binding_id": uuid.uuid4().hex[:24], "transport_ref": transport_ref, - "provider_ref": observation["provider_ref"], "operator_ref": observation["operator_ref"], - "context_kind": context_kind, "project_ref": project_ref, - "executor_endpoint_id": executor_endpoint_id, + for key, expected, label in (("provider_ref", expected_provider_ref, "App"), ("operator_ref", expected_operator_ref, "owner")): + if expected is not None and observation[key] != expected: + raise ValueError(f"the selected {label} changed before binding publication; verify it again") + candidate: JsonDict = { + "schema_version": "loopx_chat_conversation_binding_v0", "binding_id": uuid.uuid4().hex[:24], + "transport_ref": transport_ref, "provider_ref": observation["provider_ref"], "operator_ref": observation["operator_ref"], + "context_kind": context_kind, "project_ref": project_ref, "executor_endpoint_id": executor_endpoint_id, "grant": project_grant if context_kind == "project" else "portfolio_read", "enabled": True, **({"goal_ids": [], "goal_scope": goal_scope or "all_registered"} if context_kind == "steward" else {}), - **({"audience": audience} if audience is not None else {}), - **({"group_refs": group_refs} if group_refs is not None else {}), + **({"audience": audience} if audience is not None else {}), **({"group_refs": group_refs} if group_refs is not None else {}), } with exclusive_file_lock(self.path, operation="configure_chat_conversation_binding"): current = self.read() - previous = next((row for row in current["bindings"] if row["transport_ref"] == transport_ref), None) - if previous and all(previous.get(key) == candidate.get(key) for key in ["context_kind", "project_ref", "executor_endpoint_id", "provider_ref", "operator_ref", "grant", "audience", "group_refs"]): - if previous.get("agent_targets"): + previous = _find_binding(current, "transport_ref", transport_ref) + if previous is not None and all(previous.get(k) == candidate.get(k) for k in ("context_kind", "project_ref", "executor_endpoint_id", "provider_ref", "operator_ref")): + if all(previous.get(k) == candidate.get(k) for k in ("grant", "audience", "group_refs")) and previous.get("agent_targets"): candidate["agent_targets"] = previous["agent_targets"] - if (context_kind == "steward" and previous and all(previous.get(key) == candidate.get(key) - for key in ["context_kind", "project_ref", "executor_endpoint_id", "provider_ref", "operator_ref"])): - candidate["goal_ids"] = previous["goal_ids"] - # Existing installations retain their exact audience and scope - # until the local operator explicitly changes it. - if goal_scope is None: - candidate.pop("goal_scope", None) - if "goal_scope" in previous: - candidate["goal_scope"] = previous["goal_scope"] - candidate["binding_id"] = previous["binding_id"] - if previous.get("managed_actor") is not None: - candidate["managed_actor"] = previous["managed_actor"] + if context_kind == "steward": + candidate.update(goal_ids=previous["goal_ids"], binding_id=previous["binding_id"]) + # Existing installations retain their exact audience and scope + # until the local operator explicitly changes it. + if goal_scope is None: + candidate.pop("goal_scope", None) + if "goal_scope" in previous: + candidate["goal_scope"] = previous["goal_scope"] + if previous.get("managed_actor") is not None: + candidate["managed_actor"] = previous["managed_actor"] result = self._core("collaboration.conversation.binding", { - "current": current, "expected_revision": current.get("revision"), "operation": "configure", - "binding": candidate, "observation": observation, "available_projects": self.projects.available(), - **({"available_group_refs": available_group_refs} if audience == "group" else {}), + "current": current, "expected_revision": current.get("revision"), "operation": "configure", "binding": candidate, + "observation": observation, "available_projects": projects.available(), **({"available_group_refs": available_group_refs} if audience == "group" else {}), }) - proposed = next(row for row in result["state"]["bindings"] if row["transport_ref"] == transport_ref) + proposed = _require_binding(_as_dict(result["state"]), "transport_ref", transport_ref) channels = self.delivery_channels(proposed) if goal_scope is not None else [] for channel in channels: self._ensure_delivery_scope({"binding": proposed, "channel_id": channel}, execute=False) @@ -128,72 +239,50 @@ def configure(self, *, transport_ref: str, project_ref: str, for channel in channels: self._ensure_delivery_scope({"binding": proposed, "channel_id": channel}, if_absent=False) if previous and previous["binding_id"] != proposed["binding_id"]: - self._revoke_delivery_sources(previous["binding_id"]) - if result["changed"]: - _atomic_write_json(self.path, result["state"]) - readback = self.read() - if readback != result["state"]: - raise OSError("conversation binding publication did not verify") - binding: dict[str, Any] = next(row for row in readback["bindings"] if row["transport_ref"] == transport_ref) + self._revoke_delivery_sources(_need(previous["binding_id"], str)) + binding = _require_binding(self._publish_state(result, error_message="conversation binding publication did not verify"), "transport_ref", transport_ref) if previous and previous["binding_id"] != binding["binding_id"]: - self._stop_revoked_binding(previous["binding_id"]) + self._stop_revoked_binding(_need(previous["binding_id"], str)) return binding - def disconnect(self, binding_id: str, *, expected_revision: int) -> dict[str, Any]: + def disconnect(self, binding_id: str, *, expected_revision: int) -> JsonDict: with exclusive_file_lock(self.path, operation="disconnect_chat_conversation_binding"): - result = self._core("collaboration.conversation.binding", { - "current": self.read(), "expected_revision": expected_revision, - "operation": "disconnect", "binding_id": binding_id, - }) + result = self._core("collaboration.conversation.binding", {"current": self.read(), "expected_revision": expected_revision, "operation": "disconnect", "binding_id": binding_id}) self._revoke_delivery_sources(binding_id) - if result["changed"]: - _atomic_write_json(self.path, result["state"]) - if self.read() != result["state"]: - raise OSError("conversation binding revocation did not verify") + state = self._publish_state(result, error_message="conversation binding revocation did not verify") self._stop_revoked_binding(binding_id) - return dict(result["state"]) + return dict(state) - def select_app_steward(self, binding_id: str | None, *, expected_revision: int) -> dict[str, Any]: + def select_app_steward(self, binding_id: str | None, *, expected_revision: int) -> JsonDict: """Select one existing source for the App entry without changing grants.""" - row = next((item for item in self.read()["bindings"] if item["binding_id"] == binding_id), None) - observation = self.observe(row["transport_ref"]) if row is not None else None + row = _find_binding(self.read(), "binding_id", binding_id) if binding_id is not None else None + observation = self.observe(_need(row["transport_ref"], str)) if row is not None else None with exclusive_file_lock(self.path, operation="select_app_steward"): result = self._core("collaboration.conversation.binding", { - "current": self.read(), "expected_revision": expected_revision, - "operation": "select_app_steward", "binding_id": binding_id, - "observation": observation, "available_projects": self.projects.available() if binding_id is not None else [], + "current": self.read(), "expected_revision": expected_revision, "operation": "select_app_steward", + "binding_id": binding_id, "observation": observation, "available_projects": self._require_projects().available() if binding_id is not None else [], }) - if result["changed"]: - _atomic_write_json(self.path, result["state"]) - if self.read() != result["state"]: - raise OSError("App steward selection did not verify") - return dict(result["state"]) + return dict(self._publish_state(result, error_message="App steward selection did not verify")) def reconcile_steward_executors(self) -> None: """Publish a previously adopted machine executor at Chat startup.""" from ...chat_manager import steward_machine_defaults - controller = self.controller - if controller is None: + if (controller := self.controller) is None: return defaults = steward_machine_defaults(controller) - if (not defaults or defaults.get("status") != "ready" - or defaults.get("source") != "machine_configuration" - or defaults.get("selection_policy") not in {"preferred", "pinned"} - or defaults.get("executor_endpoint") not in {"antigravity", "codex"}): + if (not defaults or defaults.get("status") != "ready" or defaults.get("source") != "machine_configuration" + or defaults.get("selection_policy") not in {"preferred", "pinned"} or defaults.get("executor_endpoint") not in {"antigravity", "codex"}): return try: - rows = self.read()["bindings"] - if not isinstance(rows, list): + if not isinstance(rows := self.read()["bindings"], list): raise ValueError("invalid conversation bindings state") except (OSError, ValueError, KeyError) as exc: logging.getLogger(__name__).warning("Steward bindings unavailable for reconciliation (%s)", type(exc).__name__) return for row in rows: - if (not isinstance(row, dict) or row.get("context_kind") != "steward" - or row.get("audience", "owner") != "owner" - or row.get("enabled") is not True - or row.get("executor_endpoint_id") not in {"antigravity", "codex"} + if (not isinstance(row, dict) or row.get("context_kind") != "steward" or row.get("audience", "owner") != "owner" + or row.get("enabled") is not True or row.get("executor_endpoint_id") not in {"antigravity", "codex"} or row.get("executor_endpoint_id") == defaults["executor_endpoint"]): continue try: @@ -201,103 +290,84 @@ def reconcile_steward_executors(self) -> None: except (OSError, ValueError, TypeError, KeyError, RuntimeError) as exc: logging.getLogger(__name__).warning("Steward executor reconciliation held (%s)", type(exc).__name__) - def _reconcile_steward_executor(self, row: dict[str, Any], defaults: Mapping[str, Any]) -> None: + def _reconcile_steward_executor(self, row: JsonDict, defaults: JsonMap) -> None: from ...chat_manager import manager_executor_allocation, steward_machine_defaults - from ..machine_configuration.builtins import build_builtin_machine_configuration_registry - from ..machine_configuration.store import machine_configuration_revision, read_machine_configuration - from ..steward_executor.allocation import normalize_manager_executor_allocation - from ..steward_executor.machine_defaults import effective_steward_executor_defaults + from ..machine_configuration import builtins as _mc_builtins, contract as _mc_contract, store as _mc_store + from ..steward_executor import allocation as _alloc, machine_defaults as _md - store = self.controller.store - binding_id = row["binding_id"] - endpoint = defaults["executor_endpoint"] - channel_prefix = f"manager.external.native.{binding_id}." - candidates = [s for s in store.list_sessions() - if s.get("channel_id", "").startswith(channel_prefix) and s.get("status") != "closed"] + controller = self._require_controller() + store, binding_id, endpoint = controller.store, _need(row["binding_id"], str), defaults["executor_endpoint"] + prefix = f"manager.external.native.{binding_id}." + candidates = [s for s in store.list_sessions() if str(s.get("channel_id") or "").startswith(prefix) and s.get("status") != "closed"] if len(candidates) != 1 or candidates[0].get("agent_id") != endpoint or candidates[0].get("status") != "ready": return # Defaults alone do not retarget an existing session. - observation = self.observe(row["transport_ref"]) + observation, session_id = self.observe(_need(row["transport_ref"], str)), _need(candidates[0]["session_id"], str) with exclusive_file_lock(self.path, operation="reconcile_steward_executor"): current = self.read() - if row not in current["bindings"] or steward_machine_defaults(self.controller) != defaults: + if row not in _dict_list(current["bindings"]) or steward_machine_defaults(controller) != defaults or not (session := store.load_session(session_id)): return - session = store.load_session(candidates[0]["session_id"]) - saved = (session or {}).get("steward_context") - stored = (session or {}).get("manager_executor_allocation") - if (not session or not isinstance(saved, dict) or not isinstance(stored, dict) - or session.get("status") != "ready" or session.get("active_turn_id") is not None - or session.get("agent_id") != endpoint or session.get("executor_endpoint_id") != endpoint - or stored.get("selection_policy") != defaults["selection_policy"] - or store.queued_turns(session["session_id"], strict=True, include_reserved=True)): + saved, stored = session.get("steward_context"), session.get("manager_executor_allocation") + if (not isinstance(saved, dict) or not isinstance(stored, dict) or session.get("status") != "ready" + or session.get("active_turn_id") is not None or session.get("agent_id") != endpoint or session.get("executor_endpoint_id") != endpoint + or stored.get("selection_policy") != defaults["selection_policy"] or store.queued_turns(session_id, strict=True, include_reserved=True)): return - allocation = None + allocation: JsonDict | None = None if stored.get("configuration_revision") == defaults.get("configuration_revision"): try: - allocation = normalize_manager_executor_allocation(stored) + allocation = _alloc.normalize_manager_executor_allocation(stored) except (TypeError, ValueError): pass else: # A machine configuration transaction records the revision of # the whole document. Match that exact basis before projecting # it to the narrower revision used by steward allocations. - configuration = read_machine_configuration(store.root.parent, - registry=build_builtin_machine_configuration_registry()) - if (not configuration or machine_configuration_revision(configuration) != stored.get("configuration_revision") - or effective_steward_executor_defaults(configuration) != defaults): + configuration = _mc_store.read_machine_configuration(store.root.parent, registry=_mc_builtins.build_builtin_machine_configuration_registry()) + if (not configuration or _mc_contract.machine_configuration_revision(configuration) != stored.get("configuration_revision") + or _md.effective_steward_executor_defaults(configuration) != defaults): return if allocation is None: - allocation = manager_executor_allocation(self.controller, machine_defaults=defaults) - expected = {**self._session_observation(session), "manager_executor_allocation": allocation, - "active_turn_id": session.get("active_turn_id"), "agent_model_id": session.get("agent_model_id"), - "agent_reasoning_effort": session.get("agent_reasoning_effort")} + allocation = manager_executor_allocation(controller, machine_defaults=defaults) + expected: JsonDict = {**self._session_observation(session), "manager_executor_allocation": allocation, + "active_turn_id": session.get("active_turn_id"), "agent_model_id": session.get("agent_model_id"), + "agent_reasoning_effort": session.get("agent_reasoning_effort")} result = self._core("collaboration.conversation.binding", { - "current": current, "expected_revision": current["revision"], "operation": "reconcile_steward_executor", - "binding_id": binding_id, "observation": observation, "available_projects": self.projects.available(), - "available_goal_ids": self.goal_scope_ids(row), "machine_defaults": dict(defaults), - "session": expected, "queue_empty": True, + "current": current, "expected_revision": current["revision"], "operation": "reconcile_steward_executor", "binding_id": binding_id, + "observation": observation, "available_projects": self._require_projects().available(), "available_goal_ids": self.goal_scope_ids(row), + "machine_defaults": dict(defaults), "session": expected, "queue_empty": True, }) if stored != allocation: - store.update_session(session["session_id"], expected_statuses={"ready"}, - expected_upstream_thread_id=session.get("upstream_thread_id"), manager_executor_allocation=allocation) - fresh = store.load_session(session["session_id"]) - if (not fresh or any(fresh.get(key) != value for key, value in expected.items()) - or store.queued_turns(session["session_id"], strict=True, include_reserved=True) - or steward_machine_defaults(self.controller) != defaults): + upstream = session.get("upstream_thread_id") + store.update_session(session_id, expected_statuses={"ready"}, expected_upstream_thread_id=upstream if isinstance(upstream, str) else None, + manager_executor_allocation=allocation) + fresh = store.load_session(session_id) + if (not fresh or any(fresh.get(k) != v for k, v in expected.items()) + or store.queued_turns(session_id, strict=True, include_reserved=True) or steward_machine_defaults(controller) != defaults): return - if result["changed"]: - _atomic_write_json(self.path, result["state"], preserve_mode=True) - if self.read() != result["state"]: - raise OSError("steward executor binding publication did not verify") - - def app_steward_target(self) -> dict[str, Any]: - session = self.app_steward_session() - if session is None: + self._publish_state(result, error_message="steward executor binding publication did not verify", preserve_mode=True) + + def app_steward_target(self) -> dict[str, str | None]: + if (session := self.app_steward_session()) is None: return {"binding_id": None, "session_id": None} - return {"binding_id": session["steward_context"]["binding_id"], "session_id": session["session_id"]} + return {"binding_id": _need(_as_dict(session["steward_context"])["binding_id"], str), "session_id": _need(session["session_id"], str)} - def app_steward_session(self) -> dict[str, Any] | None: + def app_steward_session(self) -> JsonDict | None: """Read the Session within the selected source without starting work.""" current = self.read() - selected = current.get("app_steward_binding_id") - if selected is None: + if (selected := current.get("app_steward_binding_id")) is None: # Validate legacy state through the same pure owner plan. An # unreadable grant must not masquerade as an unselected entry. - self._core("collaboration.conversation.binding", {"current": current, - "expected_revision": current.get("revision"), "operation": "select_app_steward", "binding_id": None}) + self._core("collaboration.conversation.binding", {"current": current, "expected_revision": current.get("revision"), "operation": "select_app_steward", "binding_id": None}) return None - current, sessions, _ = self._observe_local_sessions(selected, app_steward_entry=True) + current, sessions, _ = self._observe_local_sessions(_need(selected, str), app_steward_entry=True) if not sessions: raise ValueError("the original steward Session is unavailable") - session = max(sessions, key=lambda item: (item.get("last_activity_at") or "", item["session_id"])) + session = max(sessions, key=lambda item: (str(item.get("last_activity_at") or ""), _need(item["session_id"], str))) # Close uses the same Session fences. Keep it from committing between # this read and the final grant decision; grant revocation still races # freely and is detected by the version check. All provider/Core IO has # already completed, and background grant inspection remains nonblocking. - with self.controller.store.read_session_fenced(session["session_id"]) as fresh: - if fresh is None or fresh.get("status") == "closed": - raise ValueError("the original steward Session is unavailable") - expected = {**self._session_observation(session), "status": fresh.get("status")} - if self._session_observation(fresh) != expected: + with self._require_controller().store.read_session_fenced(_need(session["session_id"], str)) as fresh: + if fresh is None or fresh.get("status") == "closed" or self._session_observation(fresh) != {**self._session_observation(session), "status": fresh.get("status")}: raise ValueError("the original steward Session is unavailable") self._recheck_local_bindings(current) return fresh @@ -315,129 +385,93 @@ def _stop_revoked_binding(self, binding_id: str) -> None: local Turns in a shared attached Session retain their own authority. Retrying disconnect with the current revision retries an unconfirmed stop. """ - if self.controller is None: + if (controller := self.controller) is None: return - store = self.controller.store - failures = [] - for row in store.list_sessions(): - session = store.load_session(row["session_id"]) - if session is None: + failures: list[Exception] = [] + for session, turn in _iter_open_session_turns(controller.store): + if not _session_mentions_binding(session, binding_id) and _dict_or_empty(_dict_or_empty(turn.get("external_agent_target")).get("context")).get("binding_id") != binding_id: continue - bound = any(isinstance(session.get(key), dict) - and session[key].get("binding_id") == binding_id - for key in ("project_context", "steward_context")) - turns = store.queued_turns(session["session_id"]) - active = store.load_turn(session["session_id"], session["active_turn_id"]) if session.get("active_turn_id") else None - if active: - turns.append(active) - for turn in turns: - target = turn.get("external_agent_target") or {} - if (not bound and (target.get("context") or {}).get("binding_id") != binding_id - or turn.get("status") in TERMINAL_TURN_STATES): - continue - try: - stopped = self.controller.interrupt_turn(session_id=session["session_id"], turn_id=turn["turn_id"]) - if stopped.get("status") not in TERMINAL_TURN_STATES: - raise RuntimeError("Turn stop did not settle") - except Exception as exc: - # Revocation remains published even when a host lacks stop - # control. Continue stopping other exact targets, then report - # failure without exposing private transport diagnostics. - failures.append(exc) + try: + if controller.interrupt_turn(session_id=_need(session["session_id"], str), turn_id=_need(turn["turn_id"], str)).get("status") not in TERMINAL_TURN_STATES: + raise RuntimeError("Turn stop did not settle") + except Exception as exc: + # Revocation remains published even when a host lacks stop + # control. Continue stopping other exact targets, then report + # failure without exposing private transport diagnostics. + failures.append(exc) if failures: raise OSError("Conversation grant revoked; execution stop is not confirmed. Retry stop for this binding.") from failures[0] - def resolve(self, *, binding_id: str, source_ref: str, sender_ref: str, - private_human_message: bool, session_context: dict[str, Any] | None = None, - group_ref: str | None = None, topic_ref: str | None = None, - group_human_message: bool = False, include_observation: bool = False, - origin: str | None = None, session: dict[str, Any] | None = None) -> dict[str, Any]: - current = self.read() - row = next((item for item in current.get("bindings", []) if item.get("binding_id") == binding_id), None) - if row is None: + def resolve(self, *, binding_id: str, source_ref: str, sender_ref: str, private_human_message: bool, + session_context: JsonMap | None = None, group_ref: str | None = None, topic_ref: str | None = None, + group_human_message: bool = False, include_observation: bool = False, origin: str | None = None, session: JsonMap | None = None) -> JsonDict: + if (row := _find_binding(self.read(), "binding_id", binding_id, default_empty=True)) is None: raise ValueError("conversation binding is no longer authorized") - observation = dict(self.observe(row["transport_ref"])) + observation = dict(self.observe(_need(row["transport_ref"], str))) # Provider verification can wait on IO. It cannot authorize a binding # that was revoked or replaced while that observation was in flight. # Unrelated binding changes do not invalidate this exact source. - current = self.read() - if next((item for item in current.get("bindings", []) - if item.get("binding_id") == binding_id), None) != row: - raise ValueError("conversation binding changed during identity verification") + current = self._verify_unchanged("binding_id", binding_id, row, default_empty=True) selected = self._resolve_observed(current, row, observation, { "source_ref": source_ref, "sender_ref": sender_ref, "private_human_message": private_human_message, - **({"origin": origin} if origin is not None else {}), - **({"session_context": session_context} if session_context is not None else {}), + **({"origin": origin} if origin is not None else {}), **({"session_context": session_context} if session_context is not None else {}), **({"session": self._session_observation(session)} if session is not None else {}), - **({"group_ref": group_ref, "topic_ref": topic_ref, - "group_human_message": group_human_message} if group_ref is not None or topic_ref is not None or group_human_message else {}), + **({"group_ref": group_ref, "topic_ref": topic_ref, "group_human_message": group_human_message} if group_ref is not None or topic_ref is not None or group_human_message else {}), }) # Presentation may need the name from this exact verified read. Keep it # outside the persisted context and opt-in; it is never a cached grant # or a substitute for the next admission/delivery owner's fresh check. return {**selected, "observation": observation} if include_observation else selected - def _resolve_observed(self, current: dict[str, Any], row: dict[str, Any], - observation: dict[str, Any], source: dict[str, Any]) -> dict[str, Any]: + def _resolve_observed(self, current: JsonMap, row: JsonMap, observation: JsonMap, source: JsonMap) -> JsonDict: return self._core("collaboration.conversation.bound_context", { - "current": current, "binding_id": row["binding_id"], "observation": observation, - "available_projects": self.projects.available(), **source, + "current": current, "binding_id": row["binding_id"], "observation": observation, "available_projects": self._require_projects().available(), **source, **({"available_goal_ids": self.goal_scope_ids(row)} if row.get("goal_scope") == "all_registered" else {}), }) @contextmanager - def input_context(self, saved: dict[str, Any], *, origin: str, session: dict[str, Any], - store: ChatSessionStore) -> Iterator[dict[str, Any]]: + def input_context(self, saved: JsonMap, *, origin: str, session: JsonMap, store: ChatSessionStore) -> Iterator[JsonDict]: """Observe outside the publication fence, then retain it through enqueue. Saved fields select an identity; fresh Core checks grant it. Local Web continuation keeps its actual origin and never becomes a provider event. """ if origin == "web": - source = {"source_ref": saved["source_ref"], "sender_ref": saved["operator_ref"], - "private_human_message": False, "origin": "web", "session_context": saved} + source: JsonDict = {"source_ref": saved["source_ref"], "sender_ref": saved["operator_ref"], "private_human_message": False, "origin": "web", "session_context": saved} elif origin in {"lark", "external"}: source = self._session_source(saved) else: raise ValueError("unsupported bound conversation input origin") - observed = self.resolve(binding_id=saved["binding_id"], include_observation=True, session=session, **source) + binding_id = _need(saved["binding_id"], str) + observed = self._resolve_source(binding_id, source, include_observation=True, session=session) with exclusive_file_lock(self.path, operation="admit_bound_input"): - current = self.read() - if next((row for row in current.get("bindings", []) - if row.get("binding_id") == saved["binding_id"]), None) != observed["binding"]: - raise ValueError("conversation binding changed before input admission") + current = self._verify_unchanged("binding_id", binding_id, _as_dict(observed["binding"]), message="conversation binding changed before input admission", default_empty=True) # The provider observation may have waited. Re-read canonical # identity under the binding fence before Core authorizes input. - fresh = store.load_session(session["session_id"]) - if fresh is None: + if (fresh := store.load_session(_need(session["session_id"], str))) is None: raise KeyError("chat session was not found") - yield self._resolve_observed(current, observed["binding"], observed["observation"], - {**source, "session": self._session_observation(fresh)}) + yield self._resolve_observed(current, _as_dict(observed["binding"]), _as_dict(observed["observation"]), {**source, "session": self._session_observation(fresh)}) @staticmethod - def _session_observation(session: dict[str, Any]) -> dict[str, Any]: - return {key: session.get(key) for key in ( - "session_id", "goal_id", "agent_id", "executor_endpoint_id", "channel_id", - "session_mode", "status", "project_context", "steward_context", - "manager_executor_allocation", "upstream_context_history")} + def _session_observation(session: JsonMap) -> JsonDict: + return {key: session.get(key) for key in ("session_id", "goal_id", "agent_id", "executor_endpoint_id", "channel_id", + "session_mode", "status", "project_context", "steward_context", "manager_executor_allocation", "upstream_context_history")} - def local_sessions(self, binding_id: str, *, app_steward_entry: bool = False) -> list[dict[str, Any]]: - return self.local_session_snapshot(binding_id, app_steward_entry=app_steward_entry)["sessions"] + def local_sessions(self, binding_id: str, *, app_steward_entry: bool = False) -> list[JsonDict]: + return cast("list[JsonDict]", self.local_session_snapshot(binding_id, app_steward_entry=app_steward_entry)["sessions"]) - def local_session_snapshot(self, binding_id: str, *, app_steward_entry: bool = False) -> dict[str, Any]: + def local_session_snapshot(self, binding_id: str, *, app_steward_entry: bool = False) -> JsonDict: current, sessions, source = self._observe_local_sessions(binding_id, app_steward_entry=app_steward_entry) - payload = {"sessions": [self.controller.store.public_session(session) for session in sessions], - "conversation_source": source} + store = self._require_controller().store + payload: JsonDict = {"sessions": [store.public_session(dict(s)) for s in sessions], "conversation_source": source} self._recheck_local_bindings(current) return payload - def _recheck_local_bindings(self, observed: dict[str, Any]) -> None: + def _recheck_local_bindings(self, observed: JsonMap) -> None: if self.read() != observed: raise ValueError("conversation binding changed during discovery") - def _observe_local_sessions( - self, binding_id: str, *, app_steward_entry: bool, - ) -> tuple[dict[str, Any], list[dict[str, Any]], dict[str, Any]]: + def _observe_local_sessions(self, binding_id: str, *, app_steward_entry: bool) -> tuple[JsonDict, list[JsonDict], JsonDict]: """Read original targets; metadata hints never replace Core's grant. This read-only snapshot does not wait for a mutation fence. Core checks @@ -445,177 +479,139 @@ def _observe_local_sessions( changes. actual submissions retain their fresh identity and effect-time grant fences. """ - if self.controller is None: - raise ValueError("the bound conversation store is unavailable") - row = next((item for item in self.read()["bindings"] if item["binding_id"] == binding_id), None) - if row is None: - raise ValueError("conversation binding is no longer authorized") - observation = dict(self.observe(row["transport_ref"])) - store = self.controller.store - - def mentions_binding(session: dict[str, Any]) -> bool: - return any(isinstance(session.get(key), dict) and session[key].get("binding_id") == binding_id - for key in ("project_context", "steward_context")) - + store = self._require_controller().store + row = _require_binding(self.read(), "binding_id", binding_id) + observation = dict(self.observe(_need(row["transport_ref"], str))) # Keep aliases in the candidate set even when their own metadata is # unrelated. Core must still see duplicate canonical Session identities. - hints = (session for path, session in store.iter_session_metadata() - if path.parent.name != session["session_id"] or mentions_binding(session)) - sessions: list[dict[str, Any]] = [] - for hint in sorted(hints, key=lambda item: str(item.get("updated_at") or ""), reverse=True): - session = store.load_session(hint["session_id"]) - if session and mentions_binding(session): - sessions.append(session) - current = self.read() - if next((item for item in current["bindings"] if item["binding_id"] == binding_id), None) != row: - raise ValueError("conversation binding changed during identity verification") - projects = self.projects.available() + hints = (s for path, s in store.iter_session_metadata() if path.parent.name != s["session_id"] or _session_mentions_binding(s, binding_id)) + sessions = [s for h in sorted(hints, key=lambda item: str(item.get("updated_at") or ""), reverse=True) + if (s := store.load_session(_need(h["session_id"], str))) and _session_mentions_binding(s, binding_id)] + current, projects = self._verify_unchanged("binding_id", binding_id, row), self._require_projects().available() result = self._core("collaboration.conversation.local_sessions", { - "current": current, "binding_id": binding_id, "observation": observation, - "available_projects": projects, - "sessions": [self._session_observation(session) for session in sessions], - **({"app_steward_entry": True} if app_steward_entry else {}), + "current": current, "binding_id": binding_id, "observation": observation, "available_projects": projects, + "sessions": [self._session_observation(s) for s in sessions], **({"app_steward_entry": True} if app_steward_entry else {}), **({"available_goal_ids": self.goal_scope_ids(row)} if row.get("goal_scope") == "all_registered" else {}), }) # Build the lookup only after Core has rejected duplicate identity; # an IO dictionary must not silently turn ambiguity into one target. - by_id = {session["session_id"]: session for session in sessions} - project = next(project for project in projects if project["project_ref"] == row["project_ref"]) - source = {"binding_id": binding_id, "app_ref": row["transport_ref"], - "context_kind": row["context_kind"], "audience": row.get("audience", "owner"), - "project_title": Path(project["workspace_path"]).name, "context_available": True, - "executor_endpoint_id": row["executor_endpoint_id"]} + by_id = {_need(s["session_id"], str): s for s in sessions} + project = next(p for p in projects if p["project_ref"] == row["project_ref"]) + source: JsonDict = {"binding_id": binding_id, "app_ref": row["transport_ref"], "context_kind": row["context_kind"], "audience": row.get("audience", "owner"), + "project_title": Path(_need(project["workspace_path"], str)).name, "context_available": True, "executor_endpoint_id": row["executor_endpoint_id"]} # Bind the projection to the exact atomic configuration Core checked. # This is not an execution grant or a cached provider observation. - return current, [by_id[sid] for sid in result["session_ids"]], source + return current, [by_id[_need(sid, str)] for sid in _need(result["session_ids"], list)], source - def goal_scope_ids(self, binding: dict[str, Any]) -> list[str]: + def goal_scope_ids(self, binding: JsonMap) -> list[str]: if binding.get("goal_scope") != "all_registered": - return list(binding.get("goal_ids", [])) + return [_need(item, str) for item in raw_ids] if isinstance(raw_ids := binding.get("goal_ids", []), list) else [] from ...control_plane.projects.registry_codec import load_registry if self.controller is None or self.controller.registry_path is None: raise ValueError("the steward registry is unavailable") - return [goal["id"] for goal in load_registry(self.controller.registry_path).get("goals", [])] + return [_need(goal["id"], str) for goal in load_registry(self.controller.registry_path).get("goals", []) if isinstance(goal, dict)] - def delivery_channels(self, binding: dict[str, Any]) -> list[str]: + def delivery_channels(self, binding: JsonMap) -> list[str]: if self.controller is None: return [] from ..manager_context import POLICY_SCHEMA, _root - path = _root(self.controller.coordination_runtime_root) / "policy.json" - if not path.exists(): + if not (path := _root(self.controller.coordination_runtime_root) / "policy.json").exists(): return [] policy = _read_json(path) - if policy.get("schema_version") != POLICY_SCHEMA or not isinstance(policy.get("sources"), dict): + if policy.get("schema_version") != POLICY_SCHEMA or not isinstance(sources := policy.get("sources"), dict): raise ValueError("invalid manager policy") prefix = f"manager.external.native.{binding['binding_id']}." - return [channel for channel, source in policy["sources"].items() - if channel.startswith(prefix) and isinstance(source, dict) - and binding["operator_ref"] in source.get("sender_ids", [])] + return [str(ch) for ch, src in sources.items() if str(ch).startswith(prefix) and isinstance(src, dict) and binding["operator_ref"] in (src.get("sender_ids") or [])] - def ensure_delivery_scope(self, selected: dict[str, Any], *, if_absent: bool = True, execute: bool = True) -> None: - if selected["binding"].get("goal_scope") not in {"all_registered", "selected"} or self.controller is None: + def ensure_delivery_scope(self, selected: JsonMap, *, if_absent: bool = True, execute: bool = True) -> None: + if (binding := _as_dict(selected["binding"])).get("goal_scope") not in {"all_registered", "selected"} or self.controller is None: return # Match configuration's binding -> source-policy order. An observation # from before disconnect must never recreate its retired source grant. with exclusive_file_lock(self.path, operation="configure_bound_delivery_scope"): - if selected["binding"] not in self.read()["bindings"]: + if binding not in _dict_list(self.read()["bindings"]): raise ValueError("conversation binding is no longer authorized") self._ensure_delivery_scope(selected, if_absent=if_absent, execute=execute) - def _ensure_delivery_scope(self, selected: dict[str, Any], *, if_absent: bool = True, execute: bool = True) -> None: + def _ensure_delivery_scope(self, selected: JsonMap, *, if_absent: bool = True, execute: bool = True) -> None: """The caller retains the binding publication fence, including configure.""" - binding = selected["binding"] - if binding.get("goal_scope") not in {"all_registered", "selected"} or self.controller is None: + binding = _as_dict(selected["binding"]) + if (goal_scope := binding.get("goal_scope")) not in {"all_registered", "selected"} or self.controller is None: return from ..manager_context import configure_delivery_scope - configure_delivery_scope(self.controller.coordination_runtime_root, - channel=selected["channel_id"], local_delivery_scope=binding["goal_scope"], - sender_id=binding["operator_ref"], execute=execute, if_absent=if_absent) + configure_delivery_scope(self.controller.coordination_runtime_root, channel=_need(selected["channel_id"], str), + local_delivery_scope=_need(goal_scope, str), sender_id=_need(binding["operator_ref"], str), execute=execute, if_absent=if_absent) @staticmethod - def _session_source(saved: dict[str, Any]) -> dict[str, Any]: + def _session_source(saved: JsonMap) -> JsonDict: + base: JsonDict = {"source_ref": saved["source_ref"], "sender_ref": saved["operator_ref"], "private_human_message": True, "session_context": saved} if saved.get("audience") == "bound_group": - return {"source_ref": saved["source_ref"], "sender_ref": saved["operator_ref"], - "private_human_message": False, "group_ref": saved["group_ref"], - "topic_ref": saved["topic_ref"], "group_human_message": True, "session_context": saved} - return {"source_ref": saved["source_ref"], "sender_ref": saved["operator_ref"], - "private_human_message": True, "session_context": saved} + return {**base, "private_human_message": False, "group_ref": saved["group_ref"], "topic_ref": saved["topic_ref"], "group_human_message": True} + return base - def session_context(self, saved: dict[str, Any], *, session: dict[str, Any] | None = None) -> dict[str, Any]: - return self.resolve(binding_id=saved["binding_id"], session=session, **self._session_source(saved)) + def session_context(self, saved: JsonMap, *, session: JsonMap | None = None) -> JsonDict: + return self._resolve_source(_need(saved["binding_id"], str), self._session_source(saved), session=session) - def manager_runtime_owner(self, saved: dict[str, Any], channel_id: str) -> bool: + def manager_runtime_owner(self, saved: JsonMap, channel_id: str) -> bool: """Fresh Core audience proof; saved Session text is never authority.""" - selected = self.session_context(saved) - if selected["channel_id"] != channel_id: + if (selected := self.session_context(saved))["channel_id"] != channel_id: raise ValueError("bound manager audience changed") return selected["owner_manager_audience"] is True - def steward_scope(self, session: dict[str, Any]) -> list[str] | None: - saved = session.get("steward_context") - if not isinstance(saved, dict): + def steward_scope(self, session: JsonMap) -> list[str] | None: + if not isinstance(saved := session.get("steward_context"), dict): return None selected = self.session_context(saved) if session.get("goal_id") != "loopx-manager" or session.get("channel_id") != selected["channel_id"]: raise ValueError("bound steward audience changed") - return list(selected["context"]["goal_ids"]) + goal_ids = _as_dict(selected["context"])["goal_ids"] + return [_need(item, str) for item in goal_ids] if isinstance(goal_ids, list) else [] - def adopt_created_goal(self, *, binding_id: str, source: dict[str, Any], proposal: dict[str, Any], - goal: dict[str, Any]) -> None: - selected = self.resolve(binding_id=binding_id, **source) + def adopt_created_goal(self, *, binding_id: str, source: JsonMap, proposal: JsonMap, goal: JsonMap) -> None: + selected = self._resolve_source(binding_id, source) with exclusive_file_lock(self.path, operation="adopt_steward_created_goal"): current = self.read() result = self._core("collaboration.conversation.binding", { "current": current, "expected_revision": current["revision"], "operation": "adopt_created_goal", "binding_id": binding_id, "context": selected["context"], "proposal": proposal, - "goal": {"goal_id": goal["id"], "workspace_path": str(Path(goal["repo"]).resolve()), - "creation_operation_id": goal.get("creation_operation_id")}, + "goal": {"goal_id": goal["id"], "workspace_path": str(Path(_need(goal["repo"], str)).resolve()), "creation_operation_id": goal.get("creation_operation_id")}, }) - if result["changed"]: - _atomic_write_json(self.path, result["state"]) - if self.read() != result["state"]: - raise OSError("steward scope publication did not verify") + self._publish_state(result, error_message="steward scope publication did not verify") - def agent_observation(self, session_id: str) -> dict[str, Any]: + def agent_observation(self, session_id: str) -> _AgentObservation: """Read only the explicitly selected canonical attached Session/Goal. No host history, remote thread database or prior identity is discovered. """ - from ...attached_session import require_current_attached_session, _require_bound_host + from ... import attached_session as _attached, registry as _registry from ...control_plane.projects.registry_codec import load_registry - from ...registry import find_registry_goal + if self.controller is None or self.controller.registry_path is None: raise ValueError("the attached Session registry is unavailable") - session = require_current_attached_session(store=self.controller.store, - registry_path=self.controller.registry_path, session_id=session_id) - registry = load_registry(self.controller.registry_path) - goal = find_registry_goal(registry, str(session.get("goal_id") or "")) + store = self.controller.store + session = _attached.require_current_attached_session(store=store, registry_path=self.controller.registry_path, session_id=session_id) + goal = _registry.find_registry_goal(load_registry(self.controller.registry_path), str(session.get("goal_id") or "")) if goal is None or not goal.get("repo"): raise ValueError("the exact Agent workspace is unavailable") - _require_bound_host(goal=goal, agent_id=session["agent_id"], host_surface=session["host_surface"], - host_session_id=session["upstream_thread_id"]) - return {"session": {key: session.get(key) for key in ["session_id", "goal_id", "goal_instance_id", - "agent_id", "executor_endpoint_id", "session_mode", "status", "external_conversation_binding_id"]}, - "goal": {"goal_id": goal["id"], "workspace_path": str(Path(goal["repo"]).resolve()), - "registered_agents": (goal.get("coordination") or {}).get("registered_agents", [])}, - "host_binding_verified": True, "host_ref": _host_ref(session), - "host_audience_binding_ids": _host_audiences(self.controller.store, session)} - - def agent_candidates(self, binding_id: str) -> list[dict[str, Any]]: - row = next(item for item in self.read()["bindings"] if item["binding_id"] == binding_id) + _attached._require_bound_host(goal=goal, agent_id=session["agent_id"], host_surface=session["host_surface"], host_session_id=session["upstream_thread_id"]) + host = _host_observation_fields(store, session, goal) + keys = ("session_id", "goal_id", "goal_instance_id", "agent_id", "executor_endpoint_id", "session_mode", "status", "external_conversation_binding_id") + return {"session": {k: session.get(k) for k in keys}, "goal": _as_dict(host["goal"]), "host_binding_verified": True, + "host_ref": _need(host["host_ref"], str), "host_audience_binding_ids": [_need(i, str) for i in cast("list[object]", host["host_audience_binding_ids"])]} + + def agent_candidates(self, binding_id: str) -> list[JsonDict]: + row = _require_binding(self.read(), "binding_id", binding_id) if row["context_kind"] != "project" or row.get("audience") == "group" or self.controller is None: return [] - project = next((item for item in self.projects.available() if item["project_ref"] == row["project_ref"]), None) - if project is None: + if (project := next((item for item in self._require_projects().available() if item["project_ref"] == row["project_ref"]), None)) is None: return [] - candidates = [] + candidates: list[JsonDict] = [] for session in self.controller.store.list_sessions(): if session.get("session_mode") != "attached_host" or session.get("status") == "closed": continue try: - observed = self.agent_observation(session["session_id"]) - if (observed["session"]["status"] in {"ready", "busy"} - and observed["goal"]["workspace_path"] == project["workspace_path"] + observed = self.agent_observation(_need(session["session_id"], str)) + if (observed["session"]["status"] in {"ready", "busy"} and observed["goal"]["workspace_path"] == project["workspace_path"] and observed["session"].get("external_conversation_binding_id") in {None, binding_id} and all(owner == binding_id for owner in observed["host_audience_binding_ids"])): candidates.append(observed["session"]) @@ -623,48 +619,35 @@ def agent_candidates(self, binding_id: str) -> list[dict[str, Any]]: continue return candidates[:16] - def change_agent_target(self, *, binding_id: str, expected_revision: int, - session_id: str | None = None, target_ref: str | None = None) -> dict[str, Any]: - row = next((item for item in self.read()["bindings"] if item["binding_id"] == binding_id), None) - if row is None: - raise ValueError("conversation binding is no longer authorized") + def change_agent_target(self, *, binding_id: str, expected_revision: int, session_id: str | None = None, target_ref: str | None = None) -> JsonDict: + row = _require_binding(self.read(), "binding_id", binding_id) # Provider IO must not prevent disconnect from publishing revocation. # Recheck the exact binding under its fence before using this observation. - observation = self.observe(row["transport_ref"]) + observation = self.observe(_need(row["transport_ref"], str)) with exclusive_file_lock(self.path, operation="configure_conversation_agent_target"): - current = self.read() - if next((item for item in current["bindings"] if item["binding_id"] == binding_id), None) != row: - raise ValueError("conversation binding changed during identity verification") - params = {"current": current, "expected_revision": expected_revision, "binding_id": binding_id, - "observation": observation, "available_projects": self.projects.available()} + params: JsonDict = {"current": self._verify_unchanged("binding_id", binding_id, row), "expected_revision": expected_revision, + "binding_id": binding_id, "observation": observation, "available_projects": self._require_projects().available()} if session_id is not None: observed = self.agent_observation(session_id) - target = {key: observed["session"].get(key) for key in ["session_id", "goal_id", "goal_instance_id", - "agent_id", "executor_endpoint_id"]} - params.update(operation="grant_agent_target", target={"target_ref": uuid.uuid4().hex[:24], "host_ref": observed["host_ref"], **target}, - target_observation=observed) + target = {k: observed["session"].get(k) for k in ("session_id", "goal_id", "goal_instance_id", "agent_id", "executor_endpoint_id")} + params.update(operation="grant_agent_target", target_observation=observed, target={"target_ref": uuid.uuid4().hex[:24], "host_ref": observed["host_ref"], **target}) else: params.update(operation="revoke_agent_target", target_ref=target_ref) result = self._core("collaboration.conversation.binding", params) - if result["changed"]: - if session_id is not None: - # agent_observation already requires this exact local controller. - assert self.controller is not None - self.controller.store.update_session(session_id, external_conversation_binding_id=binding_id) - _atomic_write_json(self.path, result["state"]) - if self.read() != result["state"]: - raise OSError("Agent target grant publication did not verify") - state: dict[str, Any] = result["state"] - return state - - def resolve_agent_target(self, selected: dict[str, Any], target: dict[str, Any]) -> dict[str, Any]: - return self._core("collaboration.conversation.agent_target", {"binding": selected["binding"], - "context": selected["context"], "target": target, - "target_observation": self.agent_observation(target["session_id"])}) - + if result["changed"] and session_id is not None: + # agent_observation already requires this exact local controller. + assert self.controller is not None + self.controller.store.update_session(session_id, external_conversation_binding_id=binding_id) + return self._publish_state(result, error_message="Agent target grant publication did not verify") + + def resolve_agent_target(self, selected: JsonMap, target: JsonMap) -> JsonDict: + return self._core("collaboration.conversation.agent_target", { + "binding": selected["binding"], "context": selected["context"], "target": target, + "target_observation": self.agent_observation(_need(target["session_id"], str)), + }) @effect_runtime_request_scope() - def stop_recoveries(self, current: dict[str, Any]) -> list[dict[str, Any]]: + def stop_recoveries(self, current: JsonMap) -> list[JsonDict]: """Project revoked execution from canonical Turns, never recreate a grant. Session and external-target context retain the exact binding identity @@ -674,75 +657,56 @@ def stop_recoveries(self, current: dict[str, Any]) -> list[dict[str, Any]]: """ if self.controller is None: return [] - authorized = {row["binding_id"] for row in current["bindings"]} - store = self.controller.store - pending: dict[str, dict[str, Any]] = {} - for row in store.list_sessions(): - session = store.load_session(row["session_id"]) - if session is None: + authorized = {row["binding_id"] for row in _dict_list(current["bindings"])} + counts: dict[str, int] = {} + project_refs: dict[str, str] = {} + for session, turn in _iter_open_session_turns(self.controller.store): + context = _dict_or_empty(_dict_or_empty(turn.get("external_agent_target")).get("context")) or _dict_or_empty(session.get("project_context")) or _dict_or_empty(session.get("steward_context")) + if not isinstance(binding_id := context.get("binding_id"), str) or not binding_id or binding_id in authorized: continue - turns = store.queued_turns(session["session_id"]) - active = store.load_turn(session["session_id"], session["active_turn_id"]) if session.get("active_turn_id") else None - if active: - turns.append(active) - for turn in turns: - if turn.get("status") in TERMINAL_TURN_STATES: - continue - target = turn.get("external_agent_target") or {} - context = target.get("context") or session.get("project_context") or session.get("steward_context") or {} - binding_id = context.get("binding_id") - if not binding_id or binding_id in authorized: - continue - recovery = pending.setdefault(binding_id, {"binding_id": binding_id, - "project_ref": context.get("project_ref", ""), "pending_count": 0}) - recovery["pending_count"] += 1 - return [pending[key] for key in sorted(pending)] + project_refs.setdefault(binding_id, str(context.get("project_ref") or "")) + counts[binding_id] = counts.get(binding_id, 0) + 1 + return [{"binding_id": bid, "project_ref": project_refs[bid], "pending_count": counts[bid]} for bid in sorted(counts)] -def validate_external_agent_turn(*, store: Any, session: dict[str, Any], - goal: dict[str, Any] | None, turn: dict[str, Any]) -> None: +def validate_external_agent_turn(*, store: ChatSessionStore, session: JsonMap, goal: JsonMap | None, turn: JsonMap) -> None: """Called under the grant and canonical queue fences before host claim. The owner-local grant persists independently of a live provider connection. Admission and result delivery also verify the App/owner with the provider. """ - saved = turn.get("external_agent_target") - if saved is None: + if (saved := turn.get("external_agent_target")) is None: return from ...attached_session import _require_bound_host if not isinstance(saved, dict) or turn.get("origin") not in {"lark", "external"} or goal is None: raise ValueError("the attached external audience is unavailable") - context, target = saved["context"], saved["target"] - if not Path(goal["repo"]).is_dir(): + context, target = _as_dict(saved["context"]), saved["target"] + if not Path(_need(goal["repo"], str)).is_dir(): raise ValueError("the attached Agent workspace is unavailable") - state = _read_json(store.root / "conversation-bindings.json") - selected = next((row for row in state["bindings"] if row["binding_id"] == context["binding_id"]), None) - if selected is None: + if (selected := _find_binding(_read_json(store.root / "conversation-bindings.json"), "binding_id", context["binding_id"])) is None: raise ValueError("the Agent audience grant was revoked") - _require_bound_host(goal=goal, agent_id=session["agent_id"], host_surface=session["host_surface"], - host_session_id=session["upstream_thread_id"]) - ChatConversationBindings._core("collaboration.conversation.agent_target", {"binding": selected, - "context": context, "target": target, "target_observation": {"session": session, - "goal": {"goal_id": goal["id"], "workspace_path": str(Path(goal["repo"]).resolve()), - "registered_agents": (goal.get("coordination") or {}).get("registered_agents", [])}, - "host_binding_verified": True, "host_ref": _host_ref(session), - "host_audience_binding_ids": _host_audiences(store, session)}}) + _require_bound_host(goal=dict(goal), agent_id=_need(session["agent_id"], str), + host_surface=_need(session["host_surface"], str), host_session_id=_need(session["upstream_thread_id"], str)) + ChatConversationBindings._core("collaboration.conversation.agent_target", { + "binding": selected, "context": context, "target": target, + "target_observation": {"session": session, **_host_observation_fields(store, session, goal)}, + }) -def _host_ref(session: dict[str, Any]) -> str: +def _host_ref(session: JsonMap) -> str: return hashlib.sha256(f"{session['host_surface']}\0{session['upstream_thread_id']}".encode()).hexdigest()[:24] -def _host_audiences(store: Any, session: dict[str, Any]) -> list[str]: +def _host_audiences(store: ChatSessionStore, session: JsonMap) -> list[str]: """Content-free stamps in this canonical store, including closed Sessions.""" - owners = set() - for row in store.list_sessions(): - candidate = store.load_session(row["session_id"]) - if (candidate and candidate.get("host_surface") == session["host_surface"] - and candidate.get("upstream_thread_id") == session["upstream_thread_id"] - and candidate.get("external_conversation_binding_id")): - owners.add(candidate["external_conversation_binding_id"]) - return sorted(owners) + return sorted({ + candidate["external_conversation_binding_id"] + for row in store.list_sessions() + if (candidate := store.load_session(_need(row["session_id"], str))) + and candidate.get("host_surface") == session["host_surface"] + and candidate.get("upstream_thread_id") == session["upstream_thread_id"] + and isinstance(candidate.get("external_conversation_binding_id"), str) and candidate["external_conversation_binding_id"] + }) class OriginalConversationRequests: @@ -752,121 +716,109 @@ class OriginalConversationRequests: Session FIFO remain their respective owners. No shadow queue is created. """ - def __init__(self, controller): - self.controller = controller - self.store = controller.store - self.root = controller.coordination_runtime_root - self.bindings = controller.project_contexts.conversation_bindings - if self.bindings is None: + def __init__(self, controller: _BindingController) -> None: + self.controller, self.store, self.root = controller, controller.store, controller.coordination_runtime_root + if (bindings := controller.project_contexts.conversation_bindings) is None: raise ValueError("original conversation bindings are unavailable") + self.bindings = bindings _core = staticmethod(ChatConversationBindings._core) - def _policy(self): - from ...control_plane.collaboration.inbox import _read, _root - path = _root(self.root) / "policy.json" - return _read(path) if path.exists() else {"schema_version": "loopx_manager_context_policy_v1", "sources": {}} + def _path(self, *parts: str) -> Path: + return inbox._root(self.root).joinpath(*parts) + + def _entry_path(self, target: JsonMap, request_id: str) -> Path: + return self._path("entries", inbox._hash(dict(target)), f"{request_id}.json") + + def _lock_request(self, request_id: str, legacy_path: Path | None = None) -> AbstractContextManager[None]: + return cast("_RequestLockFn", inbox._request_lock)(self.root, request_id, None, legacy_path or Path(), force_exact=True) + + @staticmethod + def _write_verified(path: Path, payload: JsonMap, error_message: str, *, write: bool = True) -> None: + if write: + inbox._write(path, dict(payload)) + if inbox._read(path) != payload: + raise OSError(error_message) - def _observation(self, session_id): - session = self.store.load_session(session_id) - if session is None: + def _policy(self) -> JsonDict: + return cast("JsonDict", inbox._read(path)) if (path := self._path("policy.json")).exists() else {"schema_version": "loopx_manager_context_policy_v1", "sources": {}} + + def _observation(self, session_id: str) -> tuple[JsonDict, JsonDict]: + if (session := self.store.load_session(session_id)) is None: raise ValueError("original project Session is unavailable") - saved = session.get("project_context") or {} - current = self.bindings.read() - binding = next((row for row in current["bindings"] if row["binding_id"] == saved.get("binding_id")), None) - if binding is None: + if (binding := _find_binding(self.bindings.read(), "binding_id", _dict_or_empty(session.get("project_context")).get("binding_id"))) is None: raise ValueError("original conversation grant was revoked") - observed = dict(self.bindings.observe(binding["transport_ref"])) - return binding, observed - - def _recipient(self, session_id, expected_binding, observation, policy): - current = self.bindings.read() - if next((row for row in current["bindings"] if row["binding_id"] == expected_binding["binding_id"]), None) != expected_binding: - raise ValueError("original conversation binding changed during verification") - return self._core("collaboration.project.recipient", {"current": current, - "binding_id": expected_binding["binding_id"], "observation": observation, - "available_projects": self.bindings.projects.available(), - "session": self.store.load_session(session_id), "policy": policy, - "sessions": [self.store.load_session(row["session_id"]) for row in self.store.list_sessions()]}) + return binding, dict(self.bindings.observe(_need(binding["transport_ref"], str))) + + def _recipient(self, session_id: str, expected_binding: JsonMap, observation: JsonMap, policy: JsonMap) -> JsonDict: + current = self.bindings._verify_unchanged("binding_id", expected_binding["binding_id"], expected_binding, message="original conversation binding changed during verification") + return self._core("collaboration.project.recipient", { + "current": current, "binding_id": expected_binding["binding_id"], "observation": observation, + "available_projects": self.bindings._require_projects().available(), "session": self.store.load_session(session_id), "policy": policy, + "sessions": [self.store.load_session(_need(row["session_id"], str)) for row in self.store.list_sessions()], + }) @contextmanager - def _source(self, requester, parent_request_id, *, require_active=True): - from ...control_plane.collaboration.goal_instance_scope import collaboration_goal_scope - from ...control_plane.collaboration.inbox import _entry + def _source(self, requester: JsonMap, parent_request_id: str, *, require_active: bool = True) -> Iterator[tuple[JsonDict, CollaborationGoalScope]]: from ...control_plane.collaboration.peer_context_observation import require_parent_context_access - ref = requester.get("goal_ref") or {} - with collaboration_goal_scope(self.controller.registry_path, goal_id=ref.get("goal_id"), - agents=(requester.get("agent_id"),), caller_goal_ref=ref, require_active=require_active) as scope: - if not scope.exact or requester != {"goal_ref": scope.caller_goal_ref, "agent_id": requester.get("agent_id")}: + + if (registry_path := self.controller.registry_path) is None: + raise ValueError("the steward registry is unavailable") + ref, agent_id = requester.get("goal_ref"), requester.get("agent_id") + if not isinstance(ref, Mapping) or not isinstance(ref.get("goal_id"), str) or not isinstance(agent_id, str) or not all(isinstance(k, str) and isinstance(v, str) for k, v in ref.items()): + raise ValueError("an exact original requester is required") + with collaboration_goal_scope(registry_path, goal_id=ref["goal_id"], agents=(agent_id,), caller_goal_ref={str(k): str(v) for k, v in ref.items()}, require_active=require_active) as scope: + if not scope.exact or requester != {"goal_ref": scope.caller_goal_ref, "agent_id": agent_id}: raise ValueError("an exact original requester is required") - parent = _entry(self.root, scope.goal_id, requester["agent_id"], parent_request_id, scope=scope) + parent = inbox._entry(self.root, scope.goal_id, agent_id, parent_request_id, scope=scope) if "request_control" in parent and self._view(parent)["control_state"] in {"cancelled", "cancellation_pending"}: raise ValueError("the original source request was cancelled") - access = require_parent_context_access(self.root, self.controller.registry_path, parent, - requester["agent_id"], scope=scope) - yield {"requester": requester, "parent": parent, "parent_request_id": parent_request_id, - "original_source_id": access["source_id"], "parent_access_verified": True}, scope + access = require_parent_context_access(self.root, registry_path, parent, agent_id, scope=scope) + yield {"requester": requester, "parent": parent, "parent_request_id": parent_request_id, "original_source_id": access["source_id"], "parent_access_verified": True}, scope @contextmanager - def _fenced(self, session_id, requester, parent_request_id): - from ...control_plane.collaboration.source_policy_fence import source_policy_fence - from ...control_plane.collaboration.inbox import _root + def _fenced(self, session_id: str, requester: JsonMap, parent_request_id: str) -> Iterator[tuple[JsonDict, CollaborationGoalScope]]: binding, observed = self._observation(session_id) # Every writer uses source -> Goal -> binding -> request -> Session. # Provider IO precedes these fences; canonical identity is reread inside. - with source_policy_fence(_root(self.root) / "policy.json"), self._source(requester, parent_request_id) as (source, scope): - with exclusive_file_lock(self.bindings.path, operation="original_conversation_peer"): - policy = self._policy() - selected = self._recipient(session_id, binding, observed, policy) - yield {**source, "policy": policy, "recipient": selected["recipient"], - "current_recipient": selected["recipient"], "policy_sha256": selected["policy_sha256"]}, scope - - def configure(self, session_id, body): - from ...control_plane.collaboration.inbox import _root, _write, _read - from ...control_plane.collaboration.source_policy_fence import source_policy_fence - enable = body.get("grant") - if not isinstance(enable, bool): + with source_policy_fence(self._path("policy.json")), self._source(requester, parent_request_id) as (source, scope), exclusive_file_lock(self.bindings.path, operation="original_conversation_peer"): + policy = self._policy() + selected = self._recipient(session_id, binding, observed, policy) + yield {**source, "policy": policy, "recipient": selected["recipient"], "current_recipient": selected["recipient"], "policy_sha256": selected["policy_sha256"]}, scope + + def configure(self, session_id: str, body: JsonMap) -> JsonDict: + if not isinstance(enable := body.get("grant"), bool): raise ValueError("grant must be an explicit boolean") + execute = body.get("execute") is True - def apply(facts): - plan = self._core("collaboration.source.original_conversation_grant", { - **facts, "grant": enable, "expected_policy_sha256": body.get("expected_policy_sha256")}) - path = _root(self.root) / "policy.json" - if body.get("execute") is True and plan["would_change"]: - _write(path, plan["policy"]) - if _read(path) != plan["policy"]: - raise OSError("original conversation source grant did not verify") - return {"ok": True, "executed": body.get("execute") is True, - **{key: value for key, value in plan.items() if key != "policy"}} + def apply(facts: JsonMap) -> JsonDict: + plan = self._core("collaboration.source.original_conversation_grant", {**facts, "grant": enable, "expected_policy_sha256": body.get("expected_policy_sha256")}) + if execute and plan["would_change"]: + self._write_verified(self._path("policy.json"), _as_dict(plan["policy"]), "original conversation source grant did not verify") + return {"ok": True, "executed": execute, **{k: v for k, v in plan.items() if k != "policy"}} + requester, parent_request_id = _as_dict(body["requester"]), _need(body["parent_request_id"], str) if enable: - with self._fenced(session_id, body["requester"], body["parent_request_id"]) as (facts, _): + with self._fenced(session_id, requester, parent_request_id) as (facts, _): return apply(facts) # An owner may revoke a retained grant even while its provider or source # is unavailable. - with source_policy_fence(_root(self.root) / "policy.json"): - recipient = body["recipient"] - if recipient.get("session_id") != session_id: + with source_policy_fence(self._path("policy.json")): + if (recipient := _as_dict(body["recipient"])).get("session_id") != session_id: raise ValueError("original conversation revocation scope mismatch") - return apply({"policy": self._policy(), "recipient": recipient, - "requester": body["requester"], "parent_request_id": body["parent_request_id"], - "original_source_id": body["source_id"]}) - - def management_view(self, session_id, *, search="", cursor=None): - from ...control_plane.collaboration.inbox import _root, _read - from ...control_plane.collaboration.source_policy_fence import source_policy_fence - with source_policy_fence(_root(self.root) / "policy.json"): + return apply({"policy": self._policy(), "recipient": recipient, "requester": requester, "parent_request_id": parent_request_id, "original_source_id": body["source_id"]}) + + def management_view(self, session_id: str, *, search: str = "", cursor: str | None = None) -> JsonDict: + with source_policy_fence(self._path("policy.json")): policy = self._policy() view = self._core("collaboration.source.original_conversation_grants", {"policy": policy, "session_id": session_id}) - for grant in view["grants"]: - requester = grant["requester"] - address = {"goal_ref": requester["goal_ref"], "agent_id": requester["agent_id"]} + for grant in _dict_list(view["grants"]): + requester = _as_dict(grant["requester"]) try: - from ...control_plane.collaboration.inbox import _hash - parent = _read(_root(self.root) / "entries" / _hash(address) / (grant["parent_request_id"] + ".json")) + parent = inbox._read(self._entry_path({"goal_ref": requester["goal_ref"], "agent_id": requester["agent_id"]}, _need(grant["parent_request_id"], str))) if parent.get("goal_ref") != requester["goal_ref"] or parent.get("agent_id") != requester["agent_id"]: raise ValueError("source grant parent identity changed") - grant["source_purpose"] = (parent.get("brief") or {}).get("purpose", parent.get("message", "")) + grant["source_purpose"] = _dict_or_empty(parent.get("brief")).get("purpose", parent.get("message", "")) except (ValueError, OSError): grant["source_purpose"] = None try: @@ -878,278 +830,264 @@ def management_view(self, session_id, *, search="", cursor=None): candidates, unreadable, overflow = [], 0, False # Content is projected only to the trusted local owner. A later grant # still rechecks the exact registration, parent lineage and source. - for path in sorted((_root(self.root) / "entries").glob("*/*.json")): + for path in sorted(self._path("entries").glob("*/*.json")): try: - row = _read(path) + row = inbox._read(path) if row.get("schema_version") != "loopx_manager_context_entry_v2" or not row.get("goal_ref"): continue - purpose = (row.get("brief") or {}).get("purpose", row.get("message", "")) - if search.strip().casefold() not in " ".join((row["goal_ref"]["goal_id"], row["agent_id"], purpose)).casefold(): + purpose = str(_dict_or_empty(row.get("brief")).get("purpose", row.get("message", ""))) + if search.strip().casefold() not in " ".join((str(row["goal_ref"]["goal_id"]), str(row["agent_id"]), purpose)).casefold(): continue - candidates.append({"requester": {"goal_ref": row["goal_ref"], "agent_id": row["agent_id"]}, - "parent_request_id": row["request_id"], "purpose": (row.get("brief") or {}).get("purpose", row.get("message", ""))}) + candidates.append({"requester": {"goal_ref": row["goal_ref"], "agent_id": row["agent_id"]}, "parent_request_id": row["request_id"], "purpose": purpose}) if len(candidates) == 128: overflow = True break - except (ValueError, OSError, KeyError): + except (ValueError, OSError, KeyError, TypeError): unreadable += 1 view.update(candidates=candidates, coverage={"unreadable": unreadable, "overflow": overflow}) - page = self._inbox(view["recipient"], mark_read=False, cursor=cursor) - view["requests"], view["requests_has_more"] = page["items"], page["has_more"] - view["requests_next_cursor"] = page["next_cursor"] - from ...control_plane.collaboration.inbox import result_paths, inspect_result_identity, reply_status - for item in view["requests"]: - results = [] - statuses = {row.get("result_key", row["phase"]): row["status"] for row in reply_status(self.root, item)} - queue = self._core("collaboration.project.continuation", {"entry": self._entry(item["request_id"], item["recipient"])}) - turn = self.store.turn_for_client(session_id, queue["client_turn_id"]) + page = self._inbox(_as_dict(view["recipient"]), mark_read=False, cursor=cursor) + requests = _dict_list(page["items"]) + view["requests"], view["requests_has_more"], view["requests_next_cursor"] = requests, page["has_more"], page["next_cursor"] + for item in requests: + request_id = _need(item["request_id"], str) + entry = self._entry(request_id, _as_dict(item["recipient"])) + statuses = {str(r.get("result_key", r["phase"])): r["status"] for r in cast("Callable[[Path, JsonMap], list[JsonDict]]", inbox.reply_status)(self.root, item)} + turn = self.store.turn_for_client(session_id, _need(self._core("collaboration.project.continuation", {"entry": entry})["client_turn_id"], str)) item["queue_status"] = turn.get("status") if turn else None - for path in result_paths(_root(self.root) / "replies" / item["request_id"]): - reply = _read(path) - if inspect_result_identity(reply, self._entry(item["request_id"], item["recipient"]), path): - results.append({"result_key": path.stem, "text": reply["text"], "decision": reply["decision"], - "request_revision": reply.get("request_revision", 1), "delivery_status": statuses.get(path.stem, "queued")}) - item["returns"] = results + item["returns"] = [ + {"result_key": path.stem, "text": reply["text"], "decision": reply["decision"], + "request_revision": reply.get("request_revision", 1), "delivery_status": statuses.get(path.stem, "queued")} + for path in cast("Callable[[Path], list[Path]]", inbox.result_paths)(self._path("replies", request_id)) + if cast("Callable[[JsonMap, JsonMap, Path], object]", inbox.inspect_result_identity)(reply := inbox._read(path), entry, path) + ] return {"ok": True, **view} - def discover(self, requester, parent_request_id): - from ...control_plane.collaboration.inbox import _root - from ...control_plane.collaboration.source_policy_fence import source_policy_fence - with source_policy_fence(_root(self.root) / "policy.json"), self._source(requester, parent_request_id) as (source, _): - grants = self._core("collaboration.source.original_conversation_grants", {"policy": self._policy()})["grants"] + def discover(self, requester: JsonMap, parent_request_id: str) -> JsonDict: + with source_policy_fence(self._path("policy.json")), self._source(requester, parent_request_id): + grants = _dict_list(self._core("collaboration.source.original_conversation_grants", {"policy": self._policy()})["grants"]) recipients, unavailable = [], 0 for grant in grants: if grant["requester"] != requester or grant["parent_request_id"] != parent_request_id: continue try: - with self._fenced(grant["recipient"]["session_id"], requester, parent_request_id) as (facts, _): + grant_recipient = _as_dict(grant["recipient"]) + with self._fenced(_need(grant_recipient["session_id"], str), requester, parent_request_id) as (facts, _): self._core("collaboration.source.original_conversation_access", facts) - if facts["recipient"] != grant["recipient"]: + if facts["recipient"] != grant_recipient: raise ValueError("original conversation recipient changed") - session = self.store.load_session(facts["recipient"]["session_id"]) - recipients.append({"recipient": facts["recipient"], "title": session.get("title") or "Project conversation"}) + facts_recipient = _as_dict(facts["recipient"]) + session = self.store.load_session(_need(facts_recipient["session_id"], str)) + recipients.append({"recipient": facts_recipient, "title": (session or {}).get("title") or "Project conversation"}) except (ValueError, KeyError, OSError): unavailable += 1 return {"ok": True, "recipients": recipients, "unavailable": unavailable} - def publish(self, body): - from ...control_plane.collaboration.inbox import _hash, _now, _read, _request_lock, _root, _write + def publish(self, body: JsonMap) -> JsonDict: from ...control_plane.collaboration.peers import _operation_address - recipient = body["recipient"] - session_id = recipient["session_id"] - with self._fenced(session_id, body["requester"], body["parent_request_id"]) as (facts, scope): + + recipient, requester = _as_dict(body["recipient"]), _as_dict(body["requester"]) + session_id = _need(recipient["session_id"], str) + with self._fenced(session_id, requester, _need(body["parent_request_id"], str)) as (facts, scope): if recipient != facts["recipient"]: raise ValueError("original conversation recipient identity changed") - source_id, op_path = _operation_address(self.root, scope, body["requester"]["agent_id"], body["operation_id"]) + source_id, op_path = _operation_address(self.root, scope, _need(requester["agent_id"], str), _need(body["operation_id"], str)) # Native planning owns identity and replay. The old operation is # consulted before any queue input or return route can be written. - prior = _read(op_path) if op_path.exists() else None - preview = self._core("collaboration.project.request", {**facts, "source_id": source_id, - "request": {"recipient": recipient, "brief": body["brief"]}, "observed_at": _now()}) - request_id = preview["entry"]["request_id"] - path = _root(self.root) / "entries" / _hash({"recipient": recipient}) / (request_id + ".json") - with _request_lock(self.root, request_id, None, op_path, force_exact=True): - prior = _read(op_path) if op_path.exists() else prior - plan = self._core("collaboration.project.request", {**facts, "source_id": source_id, - "request": {"recipient": recipient, "brief": body["brief"]}, "observed_at": _now(), - "operation": prior, "entry": _read(path) if path.exists() else None}) - row = plan["entry"] - route = plan["return_route"] - route_path = _root(self.root) / "roundtrips" / (request_id + ".json") - if route_path.exists() and _read(route_path) != route: + prior = inbox._read(op_path) if op_path.exists() else None + req_payload: JsonDict = {**facts, "source_id": source_id, "request": {"recipient": recipient, "brief": body["brief"]}} + request_id = _need(_as_dict(self._core("collaboration.project.request", {**req_payload, "observed_at": inbox._now()})["entry"])["request_id"], str) + path = self._entry_path({"recipient": recipient}, request_id) + with self._lock_request(request_id, op_path): + prior = inbox._read(op_path) if op_path.exists() else prior + plan = self._core("collaboration.project.request", {**req_payload, "observed_at": inbox._now(), "operation": prior, "entry": inbox._read(path) if path.exists() else None}) + row, route, route_path = _as_dict(plan["entry"]), _as_dict(plan["return_route"]), self._path("roundtrips", f"{request_id}.json") + if route_path.exists() and inbox._read(route_path) != route: raise ValueError("original requester return route identity conflict") if prior is None: - _write(op_path, plan["operation"]) - if not route_path.exists(): - _write(route_path, route) - if _read(route_path) != route: - raise OSError("original requester return route did not verify") - if not plan["replayed"]: - _write(path, row) - if _read(path) != row: - raise OSError("original conversation request did not verify") - view = self._view(row) - if view["control_state"] == "cancelled": + inbox._write(op_path, _as_dict(plan["operation"])) + self._write_verified(route_path, route, "original requester return route did not verify", write=not route_path.exists()) + self._write_verified(path, row, "original conversation request did not verify", write=not plan["replayed"]) + if self._view(row)["control_state"] == "cancelled": raise ValueError("original conversation request is cancelled") turn, created = self._enqueue(row) - recovered = self._resume(session_id, turn, created) - return {"ok": True, "request_id": request_id, "recipient": recipient, "turn_id": turn["turn_id"], - "status": turn["status"], "replayed": not created, "goal_created": False, "recovered_result": recovered} + return {"ok": True, "request_id": request_id, "recipient": recipient, "turn_id": turn["turn_id"], "status": turn["status"], + "replayed": not created, "goal_created": False, "recovered_result": self._resume(session_id, turn, created)} - def _enqueue(self, row): + def _enqueue(self, row: JsonMap) -> tuple[JsonDict, bool]: queue = self._core("collaboration.project.continuation", {"entry": row}) - turn, created = self.store.create_queued_turn(row["recipient"]["session_id"], - client_turn_id=queue["client_turn_id"], message=queue["message"], origin="peer", - collaboration_request=queue["collaboration_request"]) - return turn, created + return self.store.create_queued_turn(_need(_as_dict(row["recipient"])["session_id"], str), client_turn_id=_need(queue["client_turn_id"], str), + message=_need(queue["message"], str), origin="peer", collaboration_request=_as_dict(queue["collaboration_request"])) - def _stop_turns(self, session_id, client_ids): - stopped = None + def _stop_turns(self, session_id: str, client_ids: Sequence[str]) -> JsonDict | None: + stopped: JsonDict | None = None for client_id in client_ids: - turn = self.store.turn_for_client(session_id, client_id) - if turn and turn["status"] not in TERMINAL_TURN_STATES: - stopped = self.controller.interrupt_turn(session_id=session_id, turn_id=turn["turn_id"]) + if (turn := self.store.turn_for_client(session_id, client_id)) and turn["status"] not in TERMINAL_TURN_STATES: + stopped = self.controller.interrupt_turn(session_id=session_id, turn_id=_need(turn["turn_id"], str)) return stopped - def _resume(self, session_id, turn, created): - recovered = None - if not created and turn.get("collaboration_answer") is not None: - recovered = self._complete_answer(session_id, turn, turn["collaboration_answer"])["collaboration_result"] + def _resume(self, session_id: str, turn: JsonMap, created: bool) -> JsonDict | None: + recovered = _as_dict(self._complete_answer(session_id, turn, _as_dict(answer))["collaboration_result"]) if not created and (answer := turn.get("collaboration_answer")) is not None else None if turn["status"] not in TERMINAL_TURN_STATES: - context = self.controller.project_contexts.session_context(self.store.load_session(session_id)) - self.controller.resume_session_queue(session_id=session_id, work_dir=Path(context["project"]), objective=str(context.get("objective") or context["title"])) + if (session := self.store.load_session(session_id)) is None: + raise ValueError("original project Session is unavailable") + context = self.controller.project_contexts.session_context(session) + project = context["project"] + self.controller.resume_session_queue(session_id=session_id, work_dir=project if isinstance(project, Path) else Path(_need(project, str)), objective=str(context.get("objective") or context["title"])) return recovered - def _entry(self, request_id, recipient): - from ...control_plane.collaboration.inbox import _entry - return _entry(self.root, None, None, request_id, recipient=recipient) - - def _view(self, row): - return self._core("collaboration.request.control", {"entry": row, "input": {"operation": "inspect"}, "source": {}})["view"] - - def _inbox(self, recipient, *, mark_read, request_id=None, cursor=None): - from ...control_plane.collaboration.inbox import _now - result = self._core("collaboration.inbox.read", {"runtime_root": str(self.root.resolve()), - "scope": {"profile_id": "original_conversation_v0", "recipient": recipient}, - "operation": "receiver_read" if mark_read else "pending", "request_id": request_id, - "include_settled": not mark_read, - "cursor": cursor, - "observed_at": _now(), "read_command": "original project Turn supplied context"}, large_local_snapshot=True) - return result["result"] - - def control(self, body): - from ...control_plane.collaboration.goal_instance_scope import collaboration_goal_scope - from ...control_plane.collaboration.source_policy_fence import source_policy_fence - from ...control_plane.collaboration.inbox import _now, _root, _request_lock, _hash, _write, _read - requester, recipient = body["requester"], body["recipient"] + def _entry(self, request_id: str, recipient: JsonMap) -> JsonDict: + return inbox._entry(self.root, None, None, request_id, recipient=dict(recipient)) + + def _view(self, row: JsonMap) -> JsonDict: + return _as_dict(self._core("collaboration.request.control", {"entry": row, "input": {"operation": "inspect"}, "source": {}})["view"]) + + def _inbox(self, recipient: JsonMap, *, mark_read: bool, request_id: str | None = None, cursor: str | None = None) -> JsonDict: + return _as_dict(self._core("collaboration.inbox.read", { + "runtime_root": str(self.root.resolve()), "scope": {"profile_id": "original_conversation_v0", "recipient": recipient}, + "operation": "receiver_read" if mark_read else "pending", "request_id": request_id, "include_settled": not mark_read, + "cursor": cursor, "observed_at": inbox._now(), "read_command": "original project Turn supplied context", + }, large_local_snapshot=True)["result"]) + + def control(self, body: JsonMap) -> JsonDict: + requester, recipient = _as_dict(body["requester"]), _as_dict(body["recipient"]) operation, parameters = body["control"], body["parameters"] if operation not in {"inspect", "revise", "cancel"} or not isinstance(parameters, dict): raise ValueError("unsupported original conversation source control") - row = self._entry(body["request_id"], recipient) + row = self._entry(_need(body["request_id"], str), recipient) if row["requester"] != requester: raise ValueError("original requester identity changed") - if operation == "revise": - fence = self._fenced(recipient["session_id"], requester, row["parent_request_id"]) - else: - # Revocation or a closed recipient must not remove the original - # source's ability to inspect/cancel its retained request. - @contextmanager - def retained_source(): - with source_policy_fence(_root(self.root) / "policy.json"), collaboration_goal_scope( - self.controller.registry_path, goal_id=requester["goal_ref"]["goal_id"], - agents=(requester["agent_id"],), caller_goal_ref=requester["goal_ref"]) as scope: - if not scope.exact: - raise ValueError("original requester exact instance is unavailable") - yield {}, scope - fence = retained_source() - with fence as (facts, _), _request_lock(self.root, row["request_id"], None, Path(), force_exact=True): - current = self._entry(row["request_id"], recipient) - route = _read(_root(self.root) / "roundtrips" / (row["request_id"] + ".json")) - self._core("collaboration.project.return_route", {"entry": current, "route": route}) + request_id, session_id = _need(row["request_id"], str), _need(recipient["session_id"], str) + + @contextmanager + def retained_source() -> Iterator[tuple[JsonDict, CollaborationGoalScope]]: + if (registry_path := self.controller.registry_path) is None: + raise ValueError("the steward registry is unavailable") + goal_ref = _as_dict(requester["goal_ref"]) + with source_policy_fence(self._path("policy.json")), collaboration_goal_scope( + registry_path, goal_id=_need(goal_ref["goal_id"], str), agents=(_need(requester["agent_id"], str),), caller_goal_ref={k: _need(v, str) for k, v in goal_ref.items()}, + ) as scope: + if not scope.exact: + raise ValueError("original requester exact instance is unavailable") + yield {}, scope + + # Revocation or a closed recipient must not remove the original + # source's ability to inspect/cancel its retained request. + fence = self._fenced(session_id, requester, _need(row["parent_request_id"], str)) if operation == "revise" else retained_source() + with fence as (facts, _), self._lock_request(request_id): + current = self._entry(request_id, recipient) + self._core("collaboration.project.return_route", {"entry": current, "route": inbox._read(self._path("roundtrips", f"{request_id}.json"))}) if operation == "revise": if recipient != facts["recipient"]: raise ValueError("original conversation recipient changed") self._core("collaboration.source.original_conversation_access", facts) - plan = self._core("collaboration.request.control", {"entry": current, - "input": {**parameters, "operation": operation}, - "source": {"actor": "source", "actor_agent_id": requester["agent_id"], - "actor_requester": requester, - "recipient_active": operation == "revise", "observed_at": _now()}}) + plan = self._core("collaboration.request.control", { + "entry": current, "input": {**_as_dict(parameters), "operation": operation}, + "source": {"actor": "source", "actor_agent_id": requester["agent_id"], "actor_requester": requester, "recipient_active": operation == "revise", "observed_at": inbox._now()}, + }) if plan["changed"]: - _write(_root(self.root) / "entries" / _hash({"recipient": recipient}) / (row["request_id"] + ".json"), plan["entry"]) + inbox._write(self._entry_path({"recipient": recipient}, request_id), _as_dict(plan["entry"])) if operation == "revise": - turn, created = self._enqueue(plan["entry"]) - stop = None + turn, created = self._enqueue(_as_dict(plan["entry"])) + stop: JsonDict | None = None if operation == "revise": - self._resume(recipient["session_id"], turn, created) + self._resume(session_id, turn, created) elif operation == "cancel": queue = self._core("collaboration.project.continuation", {"entry": plan["entry"]}) - stop = self._stop_turns(recipient["session_id"], [*queue["superseded_client_turn_ids"], queue["client_turn_id"]]) + superseded = queue["superseded_client_turn_ids"] + client_ids = [*(_need(cid, str) for cid in superseded)] if isinstance(superseded, list) else [] + stop = self._stop_turns(session_id, [*client_ids, _need(queue["client_turn_id"], str)]) return {"ok": True, "request_control": plan["view"], "replayed": plan.get("replayed", False), "turn_stop": stop} - def validate_turn(self, session_id, turn): + def validate_turn(self, session_id: str, turn: JsonMap) -> None: if turn.get("origin") != "peer" and "collaboration_request" not in turn: return - reference = turn.get("collaboration_request") - if turn.get("origin") != "peer" or not isinstance(reference, dict): + if turn.get("origin") != "peer" or not isinstance(reference := turn.get("collaboration_request"), dict): raise ValueError("peer Turn provenance is unavailable") - row = self._entry(reference["request_id"], reference["recipient"]) - if row["recipient"]["session_id"] != session_id: + row = self._entry(_need(reference["request_id"], str), _as_dict(reference["recipient"])) + if _as_dict(row["recipient"])["session_id"] != session_id: raise ValueError("peer Turn does not belong to this original Session") self._core("collaboration.project.continuation", {"entry": row, "turn": turn}) - with self._fenced(session_id, row["requester"], row["parent_request_id"]) as (facts, _): + with self._fenced(session_id, _as_dict(row["requester"]), _need(row["parent_request_id"], str)) as (facts, _): self._validate_current(row, facts) if self._view(row)["control_state"] == "cancelled": raise ValueError("original conversation request was cancelled") - def _validate_current(self, row, facts): - from ...control_plane.collaboration.inbox import _read, _root - self._core("collaboration.project.request", {**facts, "source_id": row["source_id"], - "request": {"recipient": row["recipient"], "brief": row["brief"]}, "entry": row, - "operation": None, "observed_at": row["delivered_at"], - "route": _read(_root(self.root) / "roundtrips" / (row["request_id"] + ".json"))}) + def _validate_current(self, row: JsonMap, facts: JsonMap, *, check_access: bool = False) -> None: + self._core("collaboration.project.request", { + **facts, "source_id": row["source_id"], "request": {"recipient": row["recipient"], "brief": row["brief"]}, + "entry": row, "operation": None, "observed_at": row["delivered_at"], "route": inbox._read(self._path("roundtrips", f"{_need(row['request_id'], str)}.json")), + }) + if check_access: + self._core("collaboration.source.original_conversation_access", facts) - def _complete_answer(self, session_id, turn, answer): - from ...control_plane.collaboration.inbox import _now, _request_lock, _root, _hash, _write, record_result + def _complete_answer(self, session_id: str, turn: JsonMap, answer: JsonMap) -> JsonDict: from ...chat import CHAT_AGENT_RESPONSE_SCHEMA_VERSION - if set(answer) != set(self._core("collaboration.project.response_schema", {})["required"]): + + if not isinstance(required := self._core("collaboration.project.response_schema", {})["required"], list) or set(answer) != set(required): raise ValueError("original conversation answer has unsupported fields") - ref = turn["collaboration_request"] - row = self._entry(ref["request_id"], ref["recipient"]) - with self._fenced(session_id, row["requester"], row["parent_request_id"]) as (facts, _): - self._validate_current(row, facts) - self._core("collaboration.source.original_conversation_access", facts) + ref = _as_dict(turn["collaboration_request"]) + request_id = _need(ref["request_id"], str) + row = self._entry(request_id, _as_dict(ref["recipient"])) + with self._fenced(session_id, _as_dict(row["requester"]), _need(row["parent_request_id"], str)) as (facts, _): + self._validate_current(row, facts, check_access=True) if facts["recipient"] != row["recipient"]: raise ValueError("original conversation recipient changed") - with _request_lock(self.root, row["request_id"], None, Path(), force_exact=True): - row = self._entry(row["request_id"], row["recipient"]) + with self._lock_request(request_id): + row = self._entry(request_id, _as_dict(row["recipient"])) self._core("collaboration.project.continuation", {"entry": row, "turn": turn}) view = self._view(row) - if (view["control_state"] == "cancelled" or answer["request_revision"] != view["revision"] - or answer["request_digest"] != view["revision_digest"]): + if view["control_state"] == "cancelled" or answer["request_revision"] != view["revision"] or answer["request_digest"] != view["revision_digest"]: raise ValueError("original conversation answer belongs to a stale or cancelled request") - plan = self._core("collaboration.request.assess", {"entry": row, - "actor_recipient": row["recipient"], "recipient_active": True, "observed_at": _now(), - "revision": answer["request_revision"], "revision_digest": answer["request_digest"], - "decision": answer["decision"], "reason": answer["reason"]}) + plan = self._core("collaboration.request.assess", { + "entry": row, "actor_recipient": row["recipient"], "recipient_active": True, "observed_at": inbox._now(), + "revision": answer["request_revision"], "revision_digest": answer["request_digest"], "decision": answer["decision"], "reason": answer["reason"], + }) if plan["changed"]: - _write(_root(self.root) / "entries" / _hash({"recipient": row["recipient"]}) / (row["request_id"] + ".json"), plan["entry"]) - receipt = record_result(self.root, row, "conclusion", answer["text"], request_revision=answer["request_revision"]) - return {"schema_version": CHAT_AGENT_RESPONSE_SCHEMA_VERSION, "message": answer["text"], - "proposals": [], "protected_action": None, "goal_draft": None, "context_handoff": None, - "gate": None, "unfinished_work": None, "collaboration_result": receipt} - - def execute(self, session_id, turn_id, native_session, event_sink): - import json - turn = self.store.load_turn(session_id, turn_id) + inbox._write(self._entry_path({"recipient": row["recipient"]}, request_id), _as_dict(plan["entry"])) + receipt = cast("_RecordResultFn", inbox.record_result)( + self.root, row, "conclusion", _need(answer["text"], str), + request_revision=raw if isinstance(raw := answer["request_revision"], int) and not isinstance(raw, bool) else None, + ) + return {"schema_version": CHAT_AGENT_RESPONSE_SCHEMA_VERSION, "message": answer["text"], "proposals": [], + "protected_action": None, "goal_draft": None, "context_handoff": None, "gate": None, "unfinished_work": None, "collaboration_result": receipt} + + def execute(self, session_id: str, turn_id: str, native_session: _NativePeerSession, event_sink: _EventSink) -> JsonDict: + if (turn := self.store.load_turn(session_id, turn_id)) is None: + raise KeyError("chat turn was not found") self.validate_turn(session_id, turn) - ref = turn["collaboration_request"] - row = self._entry(ref["request_id"], ref["recipient"]) - answer = turn.get("collaboration_answer") - if answer is None: - with self._fenced(session_id, row["requester"], row["parent_request_id"]) as (facts, _): + ref = _as_dict(turn["collaboration_request"]) + request_id, ref_recipient = _need(ref["request_id"], str), _as_dict(ref["recipient"]) + row = self._entry(request_id, ref_recipient) + requester, parent_request_id = _as_dict(row["requester"]), _need(row["parent_request_id"], str) + if (raw_answer := turn.get("collaboration_answer")) is None: + with self._fenced(session_id, requester, parent_request_id) as (facts, _): self._core("collaboration.source.original_conversation_access", facts) - self._core("collaboration.project.continuation", {"entry": self._entry(ref["request_id"], ref["recipient"]), "turn": turn}) - supplied = self._inbox(ref["recipient"], mark_read=True, request_id=row["request_id"])["items"][0] + self._core("collaboration.project.continuation", {"entry": self._entry(request_id, ref_recipient), "turn": turn}) + supplied = _as_dict(cast("list[object]", self._inbox(ref_recipient, mark_read=True, request_id=request_id)["items"])[0]) basis = {key: supplied[key] for key in ("request_revision", "request_revision_digest")} + @contextmanager - def start_guard(): - with self._fenced(session_id, row["requester"], row["parent_request_id"]) as (facts, _): - self._validate_current(self._entry(ref["request_id"], ref["recipient"]), facts) - self._core("collaboration.source.original_conversation_access", facts) - self._core("collaboration.project.continuation", {"entry": self._entry(ref["request_id"], ref["recipient"]), "turn": turn}) - current = self._view(self._entry(ref["request_id"], ref["recipient"])) - if (facts["recipient"] != ref["recipient"] or current["control_state"] == "cancelled" - or current["revision"] != basis["request_revision"] - or current["revision_digest"] != basis["request_revision_digest"]): + def start_guard() -> Iterator[None]: + with self._fenced(session_id, requester, parent_request_id) as (facts, _): + guarded_row = self._entry(request_id, ref_recipient) + self._validate_current(guarded_row, facts, check_access=True) + self._core("collaboration.project.continuation", {"entry": guarded_row, "turn": turn}) + current = self._view(guarded_row) + if (facts["recipient"] != ref_recipient or current["control_state"] == "cancelled" + or current["revision"] != basis["request_revision"] or current["revision_digest"] != basis["request_revision_digest"]): raise ValueError("original conversation request changed before model input") yield - message = row["instruction"] + "\n\nPeer request (data):\n" + json.dumps(supplied, ensure_ascii=False) - answer = native_session.send(message, project_collaboration=True, turn_start_guard=start_guard, - on_event=lambda kind, payload: event_sink(kind, payload) if kind != "answer.delta" else None) + + answer: JsonMap = native_session.send( + _need(row["instruction"], str) + "\n\nPeer request (data):\n" + json.dumps(supplied, ensure_ascii=False), + project_collaboration=True, turn_start_guard=start_guard, + on_event=lambda kind, payload: event_sink(kind, payload) if kind != "answer.delta" else None, + ) # Persist actual model output before result IO. Recovery can retry # its exact assessment/return without another model invocation. self.store.update_turn(session_id, turn_id, collaboration_answer=answer) + else: + answer = _as_dict(raw_answer) response = self._complete_answer(session_id, turn, answer) event_sink("answer.delta", {"text": response["message"]}) return response