diff --git a/loopx/canary/module_metric_baseline.json b/loopx/canary/module_metric_baseline.json index e6a6fe67b9..9616e4d26f 100644 --- a/loopx/canary/module_metric_baseline.json +++ b/loopx/canary/module_metric_baseline.json @@ -66,8 +66,9 @@ "dict_any_count": 0 }, "loopx/extensions/lark/goal_topic_runtime.py": { - "any_count": 56, - "dict_any_count": 0 + "any_count": 0, + "dict_any_count": 0, + "lines": 1617 }, "loopx/extensions/lark/presentation/explore_results.py": { "any_count": 78, @@ -88,8 +89,9 @@ "lines": 1199 }, "loopx/history.py": { - "any_count": 51, - "dict_any_count": 0 + "any_count": 0, + "dict_any_count": 0, + "lines": 1035 }, "loopx/pr_review.py": { "any_count": 49, diff --git a/loopx/control_plane/runtime/runtime_projection_route.py b/loopx/control_plane/runtime/runtime_projection_route.py index e0fe8eefe9..f3f75bcd8b 100644 --- a/loopx/control_plane/runtime/runtime_projection_route.py +++ b/loopx/control_plane/runtime/runtime_projection_route.py @@ -478,15 +478,17 @@ def _route_projection_is_current( rows, _ = load_index( target_runtime_root / "goals" / goal_id / "runs" / "index.jsonl" ) - return any( - isinstance(row.get(marker_field), dict) - and row[marker_field].get("runtime_projection_route_id") - == route_id - and row[marker_field].get("source_generated_at") == source_generated_at - and row.get("json_exists") is True - and row.get("markdown_exists") is True - for row in rows - ) + for row in rows: + marker = row.get(marker_field) + if ( + isinstance(marker, dict) + and marker.get("runtime_projection_route_id") == route_id + and marker.get("source_generated_at") == source_generated_at + and row.get("json_exists") is True + and row.get("markdown_exists") is True + ): + return True + return False def _source_routes_for_registry( diff --git a/loopx/extensions/lark/goal_topic_runtime.py b/loopx/extensions/lark/goal_topic_runtime.py index 00fc0b74e9..8b6ffdf842 100644 --- a/loopx/extensions/lark/goal_topic_runtime.py +++ b/loopx/extensions/lark/goal_topic_runtime.py @@ -9,10 +9,11 @@ import re import subprocess import threading -from collections.abc import Callable, Mapping +from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence +from contextlib import AbstractContextManager, nullcontext from datetime import datetime, timezone from pathlib import Path -from typing import TYPE_CHECKING, Any, Literal, overload +from typing import TYPE_CHECKING, Literal, Protocol, overload from ...chat_manager import MANAGER_AGENT_OBJECTIVE from ...control_plane.collaboration import conversation_reply_context @@ -35,11 +36,11 @@ from .event_collector_runtime import EVENT_DIAGNOSTIC_PREFIX, EVENT_READY_PREFIX from .event_inbox import ( LarkTopicInput, - MESSAGE_ID_PATTERN, acknowledge_lark_event_inbox, ingest_lark_event_inbox, inspect_lark_event_inbox, ) +from .identity_shapes import LARK_MESSAGE_ID_PATTERN as MESSAGE_ID_PATTERN from .goal_channel_contracts import ( LarkTopicEventDecisionReason, bindings_for_goal, @@ -99,15 +100,23 @@ from ...chat_runtime import ChatRuntimeController -Answer = Callable[[Mapping[str, Any], str], str | Mapping[str, Any]] -SnapshotProvider = Callable[[], Mapping[str, Any]] -SimpleRunner = Callable[[list[str]], Mapping[str, Any]] -ProcessFactory = Callable[[list[str]], Any] -HealthSink = Callable[[Mapping[str, Any]], None] -ProposalDeliverer = Callable[ - [Mapping[str, Any], list[str]], Mapping[str, Any] -] -ReviewCallbackHandler = Callable[[Mapping[str, Any]], Mapping[str, Any]] +class _StreamProcess(Protocol): + @property + def stdout(self) -> Iterable[str] | None: ... + def poll(self) -> int | None: ... + def terminate(self) -> None: ... + def kill(self) -> None: ... + def wait(self, timeout: float | None = None) -> int: ... + + +Answer = Callable[[Mapping[str, object], str], str | Mapping[str, object]] +SnapshotProvider = Callable[[], Mapping[str, object]] +SimpleRunner = Callable[[Sequence[str]], Mapping[str, object]] +ProcessFactory = Callable[[list[str]], _StreamProcess] +HealthSink = Callable[[Mapping[str, object]], None] +ProposalDeliverer = Callable[[Mapping[str, object], list[str]], Mapping[str, object]] +ReviewCallbackHandler = Callable[[Mapping[str, object]], Mapping[str, object]] +ProviderRunner = Callable[..., object] EventLane = tuple[str, ...] CapturedEventLane = tuple[EventLane, dict[str, object] | None, TopicIntakeRoute | None] @@ -115,7 +124,7 @@ class LarkGoalTopicTurnFailed(RuntimeError): """A terminal runtime receipt, without copying arbitrary upstream details.""" - def __init__(self, error_code: str, effect_receipt: Mapping[str, Any]) -> None: + def __init__(self, error_code: str, effect_receipt: Mapping[str, object]) -> None: super().__init__("Lark Goal Topic turn did not complete") self.error_code = error_code self.effect_receipt = effect_receipt @@ -142,34 +151,98 @@ def __init__(self, error_code: str, effect_receipt: Mapping[str, Any]) -> None: _EVENT_EXIT_REASON = re.compile(r"\(reason: (limit|timeout|signal)\)$") -def _active_group_profile_configs(snapshot: Mapping[str, object]) -> dict[str, dict[str, str]]: - target_payload = snapshot.get("target_payload") - target_payload = target_payload if isinstance(target_payload, Mapping) else {} - binding_payloads = snapshot.get("binding_payloads") - binding_payloads = binding_payloads if isinstance(binding_payloads, Mapping) else {} - profiles: dict[str, dict[str, str]] = {} +def _as_mapping(value: object) -> Mapping[str, object]: + return value if isinstance(value, Mapping) else {} + + +def _as_dict(value: object) -> dict[str, object]: + return {str(k): v for k, v in value.items()} if isinstance(value, Mapping) else {} + + +def _str_field(mapping: Mapping[str, object], key: str, default: str = "") -> str: + return str(mapping.get(key) or default) + + +def _int_field(mapping: Mapping[str, object], key: str, default: int = 0) -> int: + value = mapping.get(key) + if isinstance(value, bool): + return int(value) if value else default + return int(value) if isinstance(value, (int, float, str)) and value != "" else default + + +def _str_list(value: object, *, skip_empty: bool = False) -> list[str]: + if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)): + return [] + items = [str(item) for item in value] + return [item for item in items if item] if skip_empty else items + + +def _dict_list(value: object) -> list[dict[str, object]]: + if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)): + return [] + return [_as_dict(item) for item in value if isinstance(item, Mapping)] + + +def _iter_enabled_bindings( + binding_payloads: Mapping[str, object], +) -> Iterator[tuple[str, Mapping[str, object], Mapping[str, object]]]: for goal_id, payload in binding_payloads.items(): if not isinstance(payload, Mapping): continue for binding in bindings_for_goal(payload, str(goal_id)): - if binding.get("enabled") is not True: - continue - target = goal_channel_target_for_name( - target_payload, - str(binding.get("target_ref") or ""), - ) - if target is None or target.get("enabled") is not True: - continue - identity = target.get("identity") - identity = identity if isinstance(identity, Mapping) else {} - profile = str(identity.get("sender_profile") or "").strip() - if not profile: - continue + if binding.get("enabled") is True: + yield str(goal_id), payload, binding + + +def _event_consume_args( + cli_bin: str, profile: str, *, timeout: str, max_events: str, quiet: bool = False, +) -> list[str]: + args = [ + cli_bin, "--profile", profile, "event", "consume", "im.message.receive_v1", + "--as", "bot", "--timeout", timeout, "--max-events", max_events, "--jq", _EVENT_PROJECTION, + ] + return [*args, "--quiet"] if quiet else args + + +def _empty_poll_result(status: str, *, ok: bool = True) -> dict[str, object]: + return {"ok": ok, "status": status, "event_count": 0, "replied_count": 0} + + +def _topic_error( + status: str, goal_id: object, *, inbox_config_ref: str | None = None, + source_acknowledged: bool | None = None, **extra: object, +) -> dict[str, object]: + payload: dict[str, object] = {"ok": False, "status": status, "goal_id": goal_id, **extra} + if inbox_config_ref is not None: + payload["inbox_config_ref"] = inbox_config_ref + if source_acknowledged is not None: + payload["source_acknowledged"] = source_acknowledged + return payload + + +def _persist_delivery(path: Path, state: dict[str, object], **updates: object) -> None: + if updates: + state.update(updates) + state["updated_at"] = datetime.now(timezone.utc).isoformat() + _write_manager_delivery(path, state) + + +def _active_group_profile_configs(snapshot: Mapping[str, object]) -> dict[str, dict[str, str]]: + target_payload = _as_mapping(snapshot.get("target_payload")) + binding_payloads = _as_mapping(snapshot.get("binding_payloads")) + profiles: dict[str, dict[str, str]] = {} + for _goal_id, _payload, binding in _iter_enabled_bindings(binding_payloads): + target = goal_channel_target_for_name(target_payload, _str_field(binding, "target_ref")) + if target is None or target.get("enabled") is not True: + continue + identity = _as_mapping(target.get("identity")) + profile = _str_field(identity, "sender_profile").strip() + if profile: profiles.setdefault( profile, { - "cli_bin": str(identity.get("cli_bin") or "lark-cli"), - "bot_app_id": str(identity.get("bot_app_id") or ""), + "cli_bin": _str_field(identity, "cli_bin", "lark-cli"), + "bot_app_id": _str_field(identity, "bot_app_id"), }, ) return profiles @@ -179,69 +252,58 @@ def _active_profile_configs(snapshot: Mapping[str, object]) -> dict[str, dict[st from .conversation_identity import identity_ref profiles = _active_group_profile_configs(snapshot) - private = snapshot.get("private_profiles") - for profile, config in (private.items() if isinstance(private, Mapping) else []): + for profile, raw_config in _as_mapping(snapshot.get("private_profiles")).items(): + config = dict(raw_config) if isinstance(raw_config, Mapping) else {} if profile in profiles: stored_app_id = profiles[profile].get("bot_app_id") app_id = stored_app_id or config.get("bot_app_id") if (config.get("audience", "owner") != "owner" or not app_id - or identity_ref(app_id) != config.get("provider_ref")): + or identity_ref(str(app_id)) != config.get("provider_ref")): raise ValueError("shared listener requires the same verified App and separate audiences") - profiles[profile] = {**profiles[profile], **dict(config)} + profiles[str(profile)] = {**profiles[profile], **config} if not stored_app_id: # Keep the pre-upgrade group lease until its original owner # exits. Verified private discovery supplies identity, not a # reason to start a second consumer under a new App key. - profiles[profile]["consumer_ref"] = hashlib.sha256(profile.encode("utf-8")).hexdigest()[:32] + profiles[str(profile)]["consumer_ref"] = hashlib.sha256(str(profile).encode("utf-8")).hexdigest()[:32] else: - profiles[profile] = dict(config) + profiles[str(profile)] = config return profiles -def _native_profile_event(snapshot: Mapping[str, Any], profile: str, event: Mapping[str, object]) -> bool: +def _native_profile_event(snapshot: Mapping[str, object], profile: str, event: Mapping[str, object]) -> bool: """Select a transport lane only; its original owner still admits the input. An owner-private binding may share one consumer with legacy group routes. A private rejection never falls through to a group authority or vice versa. """ - config = dict(snapshot.get("private_profiles") or {}).get(profile) + config = _as_mapping(snapshot.get("private_profiles")).get(profile) return isinstance(config, Mapping) and not ( config.get("audience", "owner") == "owner" and event.get("chat_type") == "group" and profile in _active_group_profile_configs(snapshot) ) -def _default_simple_runner(args: list[str]) -> Mapping[str, Any]: +def _default_simple_runner(args: Sequence[str]) -> Mapping[str, object]: try: completed = subprocess.run( - args, - capture_output=True, - text=True, encoding="utf-8", errors="replace", - check=False, - timeout=15, + args, capture_output=True, text=True, encoding="utf-8", errors="replace", + check=False, timeout=15, ) except (OSError, subprocess.SubprocessError): return {"returncode": 1, "stdout": "", "stderr": ""} - return { - "returncode": completed.returncode, - "stdout": completed.stdout, - "stderr": completed.stderr, - } + return {"returncode": completed.returncode, "stdout": completed.stdout, "stderr": completed.stderr} def _default_process_factory(args: list[str]) -> subprocess.Popen[str]: return subprocess.Popen( - args, - stdin=subprocess.DEVNULL, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - text=True, encoding="utf-8", errors="replace", - bufsize=1, + args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + text=True, encoding="utf-8", errors="replace", bufsize=1, ) def _target_for_profile_chat( - target_payload: Mapping[str, Any], + target_payload: Mapping[str, object], *, profile: str, chat_id: str, @@ -249,74 +311,53 @@ def _target_for_profile_chat( active_target_refs: set[str] | None = None, root_id: str = "", binding_payloads: Mapping[str, object] | None = None, -) -> tuple[str, Mapping[str, Any]] | None: - targets = target_payload.get("targets") - targets = targets if isinstance(targets, Mapping) else {} - candidates: list[tuple[str, Mapping[str, Any]]] = [] +) -> tuple[str, Mapping[str, object]] | None: + targets = _as_mapping(target_payload.get("targets")) + candidates: list[tuple[str, Mapping[str, object]]] = [] for target_ref, target in targets.items(): if active_target_refs is not None and str(target_ref) not in active_target_refs: continue if not isinstance(target, Mapping) or target.get("enabled") is not True: continue - channel = target.get("channel") - channel = channel if isinstance(channel, Mapping) else {} - identity = target.get("identity") - identity = identity if isinstance(identity, Mapping) else {} - same_app = ( - bool(bot_app_id) and str(identity.get("bot_app_id") or "") == bot_app_id - ) - if str(channel.get("chat_id") or "") == chat_id and ( - same_app or str(identity.get("sender_profile") or "") == profile + channel = _as_mapping(target.get("channel")) + identity = _as_mapping(target.get("identity")) + same_app = bool(bot_app_id) and _str_field(identity, "bot_app_id") == bot_app_id + if _str_field(channel, "chat_id") == chat_id and ( + same_app or _str_field(identity, "sender_profile") == profile ): candidates.append((str(target_ref), target)) if len(candidates) == 1: return candidates[0] - bindings = binding_payloads if isinstance(binding_payloads, Mapping) else {} + bindings = _as_mapping(binding_payloads) if root_id: - rooted = [ - candidate - for candidate in candidates - if root_id in _topic_roots_for_target(bindings, target_ref=candidate[0]) - ] + rooted = [c for c in candidates if root_id in _topic_roots_for_target(bindings, target_ref=c[0])] if len(rooted) == 1: return rooted[0] if rooted: return None - managers = [ - candidate - for candidate in candidates - if has_manager_binding(bindings, candidate[0]) - ] + managers = [c for c in candidates if has_manager_binding(bindings, c[0])] return managers[0] if len(managers) == 1 else None -def _topic_roots_for_target( - binding_payloads: Mapping[str, Any], *, target_ref: str -) -> list[str]: +def _topic_roots_for_target(binding_payloads: Mapping[str, object], *, target_ref: str) -> list[str]: roots: list[str] = [] for goal_id, payload in binding_payloads.items(): - if not isinstance(payload, Mapping): - continue - bindings = bindings_for_goal(payload, str(goal_id)) - roots.extend(_topic_roots_for_bindings(bindings, target_ref=target_ref)) + if isinstance(payload, Mapping): + roots.extend(_topic_roots_for_bindings(bindings_for_goal(payload, str(goal_id)), target_ref=target_ref)) return roots def _topic_roots_for_bindings( - bindings: list[Mapping[str, Any]], *, target_ref: str + bindings: Sequence[Mapping[str, object]], *, target_ref: str ) -> list[str]: roots: list[str] = [] for binding in bindings: - if binding.get("enabled") is not True: + if binding.get("enabled") is not True or _str_field(binding, "target_ref") != target_ref: continue - if str(binding.get("target_ref") or "") != target_ref: - continue - topic = binding.get("topic") - topic = topic if isinstance(topic, Mapping) else {} - channel = binding.get("channel") - channel = channel if isinstance(channel, Mapping) else {} root_id = str( - topic.get("root_message_id") or channel.get("pinned_message_id") or "" + _as_mapping(binding.get("topic")).get("root_message_id") + or _as_mapping(binding.get("channel")).get("pinned_message_id") + or "" ) if MESSAGE_ID_PATTERN.fullmatch(root_id): roots.append(root_id) @@ -324,23 +365,17 @@ def _topic_roots_for_bindings( def _binding_payloads_for_target( - binding_payloads: Mapping[str, Any], *, target_ref: str -) -> dict[str, Mapping[str, Any]]: - selected: dict[str, Mapping[str, Any]] = {} - for goal_id, payload in binding_payloads.items(): - if not isinstance(payload, Mapping): - continue - if any( - binding.get("enabled") is True - and str(binding.get("target_ref") or "") == target_ref - for binding in bindings_for_goal(payload, str(goal_id)) - ): - selected[str(goal_id)] = payload - return selected + binding_payloads: Mapping[str, object], *, target_ref: str +) -> dict[str, Mapping[str, object]]: + return { + goal_id: payload + for goal_id, payload, binding in _iter_enabled_bindings(binding_payloads) + if _str_field(binding, "target_ref") == target_ref + } -def _event_payloads(stdout: Any) -> list[Mapping[str, Any]]: - events: list[Mapping[str, Any]] = [] +def _event_payloads(stdout: object) -> list[Mapping[str, object]]: + events: list[Mapping[str, object]] = [] for line in str(stdout or "").splitlines(): try: payload = json.loads(line) @@ -356,59 +391,40 @@ def _event_payloads(stdout: Any) -> list[Mapping[str, Any]]: def _profile_event_route( *, profile: str, - snapshot: Mapping[str, Any], - event: Mapping[str, Any], + snapshot: Mapping[str, object], + event: Mapping[str, object], ) -> tuple[ - tuple[Mapping[str, Any], dict[str, Mapping[str, Any]], dict[str, Any]] | None, str + tuple[Mapping[str, object], dict[str, Mapping[str, object]], dict[str, object]] | None, str ]: """Select the existing profile/target/Topic scope; never grant turn authority.""" profile_config = _active_profile_configs(snapshot).get(profile) if profile_config is None: return None, "inactive" - target_payload = snapshot.get("target_payload") - target_payload = target_payload if isinstance(target_payload, Mapping) else {} - binding_payloads = snapshot.get("binding_payloads") - binding_payloads = binding_payloads if isinstance(binding_payloads, Mapping) else {} + target_payload = _as_mapping(snapshot.get("target_payload")) + binding_payloads = _as_mapping(snapshot.get("binding_payloads")) active_target_refs = { - str(binding.get("target_ref") or "") - for goal_id, payload in binding_payloads.items() - if isinstance(payload, Mapping) - for binding in bindings_for_goal(payload, str(goal_id)) - if binding.get("enabled") is True + _str_field(binding, "target_ref") for _, _, binding in _iter_enabled_bindings(binding_payloads) } - chat_id = str(event.get("chat_id") or "") target_match = _target_for_profile_chat( - target_payload, - profile=profile, - chat_id=chat_id, + target_payload, profile=profile, chat_id=_str_field(event, "chat_id"), bot_app_id=str(profile_config.get("bot_app_id") or ""), - active_target_refs=active_target_refs, - root_id=str(event.get("root_id") or ""), + active_target_refs=active_target_refs, root_id=_str_field(event, "root_id"), binding_payloads=binding_payloads, ) if target_match is None: return None, "target_unmatched" target_ref, _target = target_match routed_event = dict(event) - root_id = str(routed_event.get("root_id") or "") - if not MESSAGE_ID_PATTERN.fullmatch(root_id) and not has_manager_binding( - binding_payloads, target_ref - ): - candidate_roots = _topic_roots_for_target( - binding_payloads, - target_ref=target_ref, - ) + root_id = _str_field(routed_event, "root_id") + if not MESSAGE_ID_PATTERN.fullmatch(root_id) and not has_manager_binding(binding_payloads, target_ref): + candidate_roots = _topic_roots_for_target(binding_payloads, target_ref=target_ref) if len(candidate_roots) > 1: return None, "topic_context_ambiguous" if not candidate_roots: return None, "topic_context_missing" routed_event["root_id"] = candidate_roots[0] - return ( - target_payload, - _binding_payloads_for_target(binding_payloads, target_ref=target_ref), - routed_event, - ), "matched" + return (target_payload, _binding_payloads_for_target(binding_payloads, target_ref=target_ref), routed_event), "matched" @overload @@ -430,7 +446,7 @@ def _profile_event_lane( def _profile_event_lane( *, profile: str, - snapshot: Mapping[str, Any], + snapshot: Mapping[str, object], event: Mapping[str, object], runtime_root: str | Path, capture_mention: MentionCapture | None = None, @@ -445,14 +461,15 @@ def result( ) -> EventLane | CapturedEventLane: return (lane, capture, intake) if include_capture else lane - private_config = dict(snapshot.get("private_profiles") or {}).get(profile) + private_config = _as_mapping(snapshot.get("private_profiles")).get(profile) if isinstance(private_config, Mapping) and _native_profile_event(snapshot, profile, event): from .conversation_identity import lark_private_source try: - private_source = lark_private_source( - provider_ref=private_config["provider_ref"], event=event - ) + raw_provider_ref = private_config["provider_ref"] + if not isinstance(raw_provider_ref, str): + raise TypeError("provider_ref must be a string") + private_source = lark_private_source(provider_ref=raw_provider_ref, event=event) except (KeyError, TypeError, ValueError): pass # Malformed hints retain the conservative unmatched lane. else: @@ -460,20 +477,16 @@ def result( # Use the same App/chat/topic (or private sender) source shape for # scheduling, never a grant or a persisted Session selection. Core # still verifies the provider and acquires its fresh source fence. - return result(("private." + private_source["source_ref"],)) + return result(("private." + str(private_source["source_ref"]),)) - scoped, _status = _profile_event_route( - profile=profile, snapshot=snapshot, event=event - ) + scoped, _status = _profile_event_route(profile=profile, snapshot=snapshot, event=event) if scoped is not None: targets, bindings, routed_event = scoped decision = decide_lark_topic_event( - target_payload=targets, - binding_payloads=bindings, - event=routed_event, - runtime_root=runtime_root, + target_payload=targets, binding_payloads=bindings, + event=routed_event, runtime_root=runtime_root, ) - route = decision.get("route") or {} + route = _as_mapping(decision.get("route")) if route: if route.get("conversation_kind") == "manager": source = "manager." + _opaque_digest(route.get("app_ref"), event.get("chat_id")) @@ -501,66 +514,48 @@ def result( def poll_lark_goal_topic_profile_once( *, profile: str, - snapshot: Mapping[str, Any], + snapshot: Mapping[str, object], runtime_root: str | Path, answer: Answer, consume_runner: SimpleRunner = _default_simple_runner, - provider_runner: Any = subprocess.run, + provider_runner: ProviderRunner = subprocess.run, reply_runner: CommandRunner = _default_simple_runner, proposal_deliverer: ProposalDeliverer | None = None, private_admitter: Callable[[str, dict[str, object]], Mapping[str, object]] | None = None, mention_capture: Mapping[str, object] | None = None, live_turn_capture: Mapping[str, object] | None = None, -) -> dict[str, Any]: +) -> dict[str, object]: """Consume one bounded event batch for an App and reuse Inbox reply/ACK.""" - profile_configs = _active_profile_configs(snapshot) - profile_config = profile_configs.get(profile) + profile_config = _active_profile_configs(snapshot).get(profile) if profile_config is None: - return {"ok": True, "status": "inactive", "event_count": 0, "replied_count": 0} - cli_bin = str(profile_config["cli_bin"]) + return _empty_poll_result("inactive") result = consume_runner( - [ - cli_bin, - "--profile", - profile, - "event", - "consume", - "im.message.receive_v1", - "--as", - "bot", - "--timeout", - "5s", - "--max-events", - "50", - "--jq", - _EVENT_PROJECTION, - "--quiet", - ] + _event_consume_args(str(profile_config["cli_bin"]), profile, timeout="5s", max_events="50", quiet=True) ) - if int(result.get("returncode") or 0) != 0: - return { - "ok": False, - "status": "consume_failed", - "event_count": 0, - "replied_count": 0, - } + if _int_field(result, "returncode") != 0: + return _empty_poll_result("consume_failed", ok=False) events = _event_payloads(result.get("stdout")) + raw_contexts = snapshot.get("goal_contexts") + goal_contexts = ( + {str(k): v for k, v in raw_contexts.items() if isinstance(v, Mapping)} + if isinstance(raw_contexts, Mapping) else {} + ) replied_count = 0 event_statuses: list[str] = [] event_reasons: list[str | None] = [] for event in events: if _native_profile_event(snapshot, profile, event): - private_result = private_admitter(profile, dict(event)) if private_admitter else {"status": "private_admission_unavailable"} - event_statuses.append(str(private_result.get("status") or "unknown")) + private_result = ( + private_admitter(profile, dict(event)) + if private_admitter + else {"status": "private_admission_unavailable"} + ) + event_statuses.append(_str_field(private_result, "status", "unknown")) event_reasons.append(None) continue - scoped, rejection = _profile_event_route( - profile=profile, - snapshot=snapshot, - event=event, - ) + scoped, rejection = _profile_event_route(profile=profile, snapshot=snapshot, event=event) if scoped is None: event_statuses.append(rejection) event_reasons.append(None) @@ -568,36 +563,22 @@ def poll_lark_goal_topic_profile_once( target_payload, binding_payloads, routed_event = scoped try: event_result = process_lark_goal_topic_event( - target_payload=target_payload, - binding_payloads=binding_payloads, - event=routed_event, - runtime_root=runtime_root, - goal_contexts=( - snapshot.get("goal_contexts") - if isinstance(snapshot.get("goal_contexts"), Mapping) - else {} - ), - answer=answer, - reply_runner=reply_runner, - provider_runner=provider_runner, - proposal_deliverer=proposal_deliverer, - mention_capture=mention_capture, + target_payload=target_payload, binding_payloads=binding_payloads, event=routed_event, + runtime_root=runtime_root, goal_contexts=goal_contexts, answer=answer, + reply_runner=reply_runner, provider_runner=provider_runner, + proposal_deliverer=proposal_deliverer, mention_capture=mention_capture, live_turn_capture=live_turn_capture, ) except Exception: event_statuses.append("processing_failed") event_reasons.append(None) continue - event_statuses.append(str(event_result.get("status") or "unknown")) - event_reasons.append(str(event_result.get("reason") or "") or None) + event_statuses.append(_str_field(event_result, "status", "unknown")) + event_reasons.append(_str_field(event_result, "reason") or None) replied_count += int(event_result.get("status") == "replied_and_acknowledged") return { - "ok": True, - "status": "polled", - "event_count": len(events), - "replied_count": replied_count, - "event_statuses": event_statuses, - "event_reasons": event_reasons, + "ok": True, "status": "polled", "event_count": len(events), "replied_count": replied_count, + "event_statuses": event_statuses, "event_reasons": event_reasons, } @@ -609,7 +590,7 @@ def stream_lark_goal_topic_profile( runtime_root: str | Path, answer: Answer, process_factory: ProcessFactory = _default_process_factory, - provider_runner: Any = subprocess.run, + provider_runner: ProviderRunner = subprocess.run, reply_runner: CommandRunner = _default_simple_runner, health_sink: HealthSink | None = None, proposal_deliverer: ProposalDeliverer | None = None, @@ -617,50 +598,22 @@ def stream_lark_goal_topic_profile( capture_mention: MentionCapture | None = None, review_callback_handler: ReviewCallbackHandler | None = None, callback_availability_sink: Callable[[TeamPlanReviewCallbackStream], None] | None = None, -) -> dict[str, Any]: +) -> dict[str, object]: """Keep one bounded long-lived CLI consumer attached between messages.""" snapshot = snapshot_provider() profile_config = _active_profile_configs(snapshot).get(profile) if profile_config is None: - return { - "ok": True, - "status": "inactive", - "event_count": 0, - "replied_count": 0, - } + return _empty_poll_result("inactive") cli_bin = str(profile_config["cli_bin"]) - process = process_factory( - [ - cli_bin, - "--profile", - profile, - "event", - "consume", - "im.message.receive_v1", - "--as", - "bot", - "--timeout", - "30m", - "--max-events", - "0", - "--jq", - _EVENT_PROJECTION, - ] - ) + process = process_factory(_event_consume_args(cli_bin, profile, timeout="30m", max_events="0")) callback_disconnected = threading.Event() callback_stream = ( start_team_plan_review_callback_stream( - snapshot=snapshot, - profile=profile, - cli_bin=cli_bin, - process_factory=process_factory, - parent_stop=stop, - handler=review_callback_handler, - availability_sink=callback_availability_sink, + snapshot=snapshot, profile=profile, cli_bin=cli_bin, process_factory=process_factory, + parent_stop=stop, handler=review_callback_handler, availability_sink=callback_availability_sink, ) - if review_callback_handler is not None - else None + if review_callback_handler is not None else None ) if health_sink is not None: # A live child process is not proof that lark-cli registered a consumer @@ -670,13 +623,16 @@ def stream_lark_goal_topic_profile( watcher_done = threading.Event() configuration_removed = threading.Event() + def terminate_streams(*, include_callback: bool = True) -> None: + if process.poll() is None: + process.terminate() + if include_callback and callback_stream is not None: + callback_stream.terminate() + def stop_consumer() -> None: while not watcher_done.wait(1.0): if stop.is_set(): - if process.poll() is None: - process.terminate() - if callback_stream is not None: - callback_stream.terminate() + terminate_streams() return if dispatch.failed: # A worker can fail while stdout waits indefinitely for the @@ -686,19 +642,13 @@ def stop_consumer() -> None: try: with result_lock: if health_sink is not None: - health_sink( - {"status": "failed", "error_code": "lark_event_listener_failed"} - ) + health_sink({"status": "failed", "error_code": "lark_event_listener_failed"}) finally: - if process.poll() is None: - process.terminate() - if callback_stream is not None: - callback_stream.terminate() + terminate_streams() return if callback_stream is not None and callback_stream.disconnected(): callback_disconnected.set() - if process.poll() is None: - process.terminate() + terminate_streams(include_callback=False) return try: configured = profile in _active_profile_configs(snapshot_provider()) @@ -708,17 +658,10 @@ def stop_consumer() -> None: if not configured: configuration_removed.set() stop.set() - if process.poll() is None: - process.terminate() - if callback_stream is not None: - callback_stream.terminate() + terminate_streams() return - watcher = threading.Thread( - target=stop_consumer, - name=f"loopx-lark-stop-{profile}", - daemon=True, - ) + watcher = threading.Thread(target=stop_consumer, name=f"loopx-lark-stop-{profile}", daemon=True) event_count = 0 replied_count = 0 provider_ready = False @@ -727,7 +670,6 @@ def stop_consumer() -> None: def note_provider_ready() -> None: nonlocal provider_ready - with result_lock: if provider_ready: return @@ -735,15 +677,19 @@ def note_provider_ready() -> None: if health_sink is not None and not dispatch.failed: health_sink({"status": "listening", "error_code": None}) - def report_result(result: Mapping[str, Any]) -> None: + def report_result(result: Mapping[str, object]) -> None: nonlocal event_count, replied_count - - events = int(result.get("event_count") or 0) + events = _int_field(result, "event_count") if not events: return - replies = int(result.get("replied_count") or 0) - statuses = list(result.get("event_statuses") or []) - reasons = list(result.get("event_reasons") or []) + replies = _int_field(result, "replied_count") + statuses = _str_list(result.get("event_statuses")) + raw_reasons = result.get("event_reasons") + reasons = ( + list(raw_reasons) + if isinstance(raw_reasons, Sequence) and not isinstance(raw_reasons, (str, bytes)) + else [] + ) with result_lock: event_count += events replied_count += replies @@ -752,9 +698,8 @@ def report_result(result: Mapping[str, Any]) -> None: health_sink({ "status": "failed" if failed else "listening" if provider_ready else "starting", "error_code": "lark_event_listener_failed" if failed else None, - "event_count": events, - "replied_count": replies, - "last_event_status": str(statuses[-1]) if statuses else None, + "event_count": events, "replied_count": replies, + "last_event_status": statuses[-1] if statuses else None, "last_event_reason": str(reasons[-1]) if reasons and reasons[-1] else None, }) print(json.dumps({ @@ -765,28 +710,17 @@ def report_result(result: Mapping[str, Any]) -> None: def dispatch_event( event: Mapping[str, object], mention: Mapping[str, object] | None, - current_snapshot: Mapping[str, object], - *, live_turn_capture: Mapping[str, object] | None = None, + current_snapshot: Mapping[str, object], *, live_turn_capture: Mapping[str, object] | None = None, ) -> None: - message_id = str(event.get("message_id") or "") + message_id = _str_field(event, "message_id") record_event_timing(profile, message_id, "dispatch_started") record_event_timing(profile, message_id, "dispatch_snapshot_ready") result = poll_lark_goal_topic_profile_once( - profile=profile, - snapshot=current_snapshot, - runtime_root=runtime_root, - answer=answer, - consume_runner=lambda _args: { - "returncode": 0, - "stdout": json.dumps(event, ensure_ascii=False), - "stderr": "", - }, - provider_runner=provider_runner, - reply_runner=reply_runner, - proposal_deliverer=proposal_deliverer, - private_admitter=private_admitter, - mention_capture=mention, - live_turn_capture=live_turn_capture, + profile=profile, snapshot=current_snapshot, runtime_root=runtime_root, answer=answer, + consume_runner=lambda _args: {"returncode": 0, "stdout": json.dumps(event, ensure_ascii=False), "stderr": ""}, + provider_runner=provider_runner, reply_runner=reply_runner, + proposal_deliverer=proposal_deliverer, private_admitter=private_admitter, + mention_capture=mention, live_turn_capture=live_turn_capture, ) record_event_timing(profile, message_id, "dispatch_returned") report_result(result) @@ -801,18 +735,14 @@ def can_steer(state: Mapping[str, object]) -> bool: reserve_priority = can_steer(snapshot) root = Path(runtime_root).expanduser().resolve() - provider_ref = _opaque_digest(profile_config.get("bot_app_id") or profile) - intake = ProfileTopicIntake(root, provider_ref) + intake = ProfileTopicIntake(root, _opaque_digest(profile_config.get("bot_app_id") or profile)) def report_retained_input() -> None: - report_result({ - "event_count": 1, "replied_count": 0, - "event_statuses": ["already_captured"], "event_reasons": [None], - }) + report_result({"event_count": 1, "replied_count": 0, "event_statuses": ["already_captured"], "event_reasons": [None]}) - def matches_input(inbox_key: str, packet: LarkTopicInput, snapshot: Mapping[str, object]) -> bool: + def matches_input(inbox_key: str, packet: LarkTopicInput, current: Mapping[str, object]) -> bool: lane, _, route = _profile_event_lane( - profile=profile, snapshot=snapshot, event=packet["event"], + profile=profile, snapshot=current, event=packet["event"], runtime_root=root, include_capture=True, prepare_intake=True, ) if route is None: @@ -821,7 +751,7 @@ def matches_input(inbox_key: str, packet: LarkTopicInput, snapshot: Mapping[str, # Previously captured compatibility input still belongs to its Topic # even if another connection no longer reserves a steering worker. return ( - (can_steer(snapshot) or ingress_mode == IngressMode.DIRECT_SESSION.value) + (can_steer(current) or ingress_mode == IngressMode.DIRECT_SESSION.value) and route.inbox_key == inbox_key and route.digest == packet["route_ref"] and lane == packet["lane"] and ( @@ -851,22 +781,13 @@ def handle_event(line: str) -> None: dispatch_event(envelope["event"], envelope["mention_capture"], snapshot_provider()) dispatch = ProfileEventDispatch(handle_event, stop, reserve_priority=reserve_priority) - watcher.start() stream_failed = True try: - intake.restore( - dispatch, - lambda key, packet: matches_input(key, packet, snapshot), - ) + intake.restore(dispatch, lambda key, packet: matches_input(key, packet, snapshot)) stdout = process.stdout if stdout is None: - return { - "ok": False, - "status": "stream_failed", - "event_count": 0, - "replied_count": 0, - } + return _empty_poll_result("stream_failed", ok=False) for line in stdout: if stop.is_set(): break @@ -885,7 +806,7 @@ def handle_event(line: str) -> None: for event in _event_payloads(line): # Recovered local input alone does not attest provider readiness. note_provider_ready() - message_id = str(event.get("message_id") or "") + message_id = _str_field(event, "message_id") record_event_timing(profile, message_id, "received") if intake.retains(message_id): # A provider retry is the same source input even if the @@ -899,16 +820,12 @@ def handle_event(line: str) -> None: reserve_priority = can_steer(current_snapshot) dispatch.set_priority_reservation(reserve_priority) lane, mention_capture, intake_route = _profile_event_lane( - profile=profile, - snapshot=current_snapshot, - event=event, - runtime_root=runtime_root, + profile=profile, snapshot=current_snapshot, event=event, runtime_root=runtime_root, capture_mention=capture_mention if reserve_priority else None, - include_capture=True, - prepare_intake=reserve_priority, + include_capture=True, prepare_intake=reserve_priority, ) record_event_timing(profile, message_id, "lane_ready") - priority = bool((mention_capture or {}).get("target_turn_id")) + priority = bool(_as_mapping(mention_capture).get("target_turn_id")) if reserve_priority and intake_route is not None and not priority: live_turn_capture = None if (intake_route.route.get("ingress_mode") == "live_steering" @@ -918,8 +835,7 @@ def handle_event(line: str) -> None: if intake.capture( dispatch, intake_route, lane, event, mention_capture, lambda: _inbox_config( - runtime_root=root, route=intake_route.route, - target_payload=intake_route.targets, + runtime_root=root, route=intake_route.route, target_payload=intake_route.targets, )[0], live_turn_capture=live_turn_capture, ): @@ -951,38 +867,20 @@ def handle_event(line: str) -> None: # successfully with reason=signal (e.g. a Feishu/Lark domain mismatch). # Only our own stop or the requested bound is a planned stream ending. unexpected_exit = ( - provider_ready - and not stopped - and ( - callback_disconnected.is_set() - or returncode != 0 - or exit_reason not in {"limit", "timeout"} - ) + provider_ready and not stopped + and (callback_disconnected.is_set() or returncode != 0 or exit_reason not in {"limit", "timeout"}) ) return { - "ok": configuration_removed.is_set() - or stopped - or (returncode == 0 and provider_ready and not unexpected_exit), + "ok": configuration_removed.is_set() or stopped or (returncode == 0 and provider_ready and not unexpected_exit), **( - { - "error_code": ( - "lark_review_callback_source_disconnected" - if callback_disconnected.is_set() - else "lark_event_source_disconnected" - ) - } - if unexpected_exit - else {} + {"error_code": "lark_review_callback_source_disconnected" if callback_disconnected.is_set() else "lark_event_source_disconnected"} + if unexpected_exit else {} ), "status": ( - "configuration_removed" - if configuration_removed.is_set() - else "stopped" - if stopped - else "source_disconnected" - if unexpected_exit - else "stream_ended" - if provider_ready + "configuration_removed" if configuration_removed.is_set() + else "stopped" if stopped + else "source_disconnected" if unexpected_exit + else "stream_ended" if provider_ready else "stream_not_ready" ), "event_count": event_count, @@ -1065,53 +963,42 @@ def _steering_reply(route: Mapping[str, object], decision: Mapping[str, object]) def answer_lark_goal_topic( *, - route: Mapping[str, Any], + route: Mapping[str, object], text: str, work_dir: str | Path, objective: str, - runtime_controller: Any, -) -> str | Mapping[str, Any]: + runtime_controller: ChatRuntimeController, +) -> str | Mapping[str, object]: """Deliver one Topic message using its exact Agent ingress contract.""" - goal_id = str(route.get("goal_id") or "") - ingress_mode = str(route.get("ingress_mode") or "direct_session") - session_id = str(route.get("session_id") or "") + goal_id = _str_field(route, "goal_id") + ingress_mode = _str_field(route, "ingress_mode", "direct_session") + session_id = _str_field(route, "session_id") manager = route.get("conversation_kind") == "manager" - agent_id = manager_turn_executor(runtime_controller) if manager else str( - route.get("agent_id") or "codex" - ) - expected_channel = ( - str(route.get("manager_channel_id") or "") if manager else f"goal.{goal_id}" - ) + agent_id = manager_turn_executor(runtime_controller) if manager else _str_field(route, "agent_id", "codex") + expected_channel = _str_field(route, "manager_channel_id") if manager else f"goal.{goal_id}" if manager: objective = MANAGER_AGENT_OBJECTIVE resolved_work_dir = Path(work_dir).expanduser().resolve() reply_context = ( str(conversation_reply_context(dict(route))["context_text"]) - if route.get("parent_id") or route.get("thread_context") - else "" + if route.get("parent_id") or route.get("thread_context") else "" ) if manager: - message = manager_message( - text, route.get("context_materials"), reply_context=reply_context - ) + message = manager_message(text, route.get("context_materials"), reply_context=reply_context) else: message = ( "这是来自已绑定 Lark Goal Topic 的用户消息。请直接回答当前问题;" "任何 Goal、Todo 或其他持久状态修改只生成预览,等待用户在 LoopX 明确确认后应用。" + ("\n\n" + reply_context if reply_context else "") - + "\n\n用户消息:" - + str(text or "").strip() + + "\n\n用户消息:" + str(text or "").strip() ) - client_turn_id = "lark." + _opaque_digest( - route.get("message_id"), - route.get("topic_root_message_id"), - ) + client_turn_id = "lark." + _opaque_digest(route.get("message_id"), route.get("topic_root_message_id")) native_mention = route.get("native_mention") is True if native_mention: from ...capabilities.native_chat.queued_admission import deliver_native_mention - capture = route.get("mention_capture") - capture = capture if isinstance(capture, Mapping) else capture_topic_mention(route, runtime_controller) + raw_capture = route.get("mention_capture") + capture = raw_capture if isinstance(raw_capture, Mapping) else capture_topic_mention(route, runtime_controller) current = _bound_topic_session(route, runtime_controller) binding_matches = ( current is not None and capture.get("binding_matches") is True @@ -1123,7 +1010,7 @@ def answer_lark_goal_topic( from ...chat_manager_context import manager_authorization_scope_is_current binding_matches = manager_authorization_scope_is_current( - runtime_controller.registry_path, runtime_controller.manager_scope_resolver, current, + runtime_controller.registry_path, runtime_controller.manager_scope_resolver, dict(current), current.get("manager_authorization_scope_id"), runtime_root=runtime_controller.coordination_runtime_root, ) @@ -1152,22 +1039,18 @@ def answer_lark_goal_topic( if _bound_topic_session(route, runtime_controller) is None: if manager and manager_session_requires_executor_rebind( runtime_controller.store.load_session(session_id), - expected_channel=expected_channel, - agent_id=agent_id, + expected_channel=expected_channel, agent_id=agent_id, ): - raise LarkGoalTopicTurnFailed( - "manager_channel_executor_rebind_required", - _session_turn_effect(route), - ) + raise LarkGoalTopicTurnFailed("manager_channel_executor_rebind_required", _session_turn_effect(route)) raise RuntimeError("bound Agent session is unavailable or no longer matches") if ingress_mode == "live_steering": - capture = route.get("live_turn_capture") - if isinstance(capture, Mapping): - if (capture.get("binding_matches") is not True - or capture.get("session_id") != session_id - or capture.get("connection_id") != route.get("connection_id")): + live_capture = route.get("live_turn_capture") + if isinstance(live_capture, Mapping): + if (live_capture.get("binding_matches") is not True + or live_capture.get("session_id") != session_id + or live_capture.get("connection_id") != route.get("connection_id")): return _steering_reply(route, {"action": "reject", "error_code": "live_steering_session_not_attached"}) - target_turn_id = capture.get("target_turn_id") + target_turn_id = live_capture.get("target_turn_id") if not isinstance(target_turn_id, str) or not target_turn_id: return _steering_reply(route, {"action": "reject", "error_code": "live_steering_requires_active_turn"}) try: @@ -1193,114 +1076,66 @@ def answer_lark_goal_topic( session_id=session_id, client_ingress_id=client_turn_id, message=message, ) else: - if ( - manager - and route.get("source_sender_id") - and hasattr(runtime_controller.store, "root") - ): + if manager and route.get("source_sender_id") and hasattr(runtime_controller.store, "root"): from ...capabilities.manager_context import register_ingress register_ingress( - runtime_controller.store.root.parent, - session_id=session_id, - client_turn_id=client_turn_id, - channel=expected_channel, - sender_id=str(route["source_sender_id"]), - message=message, + runtime_controller.store.root.parent, session_id=session_id, + client_turn_id=client_turn_id, channel=expected_channel, + sender_id=str(route["source_sender_id"]), message=message, source_id="lark:" + str(route["message_id"]), source_message=( - (reply_context + "\n\nUser message:\n" if reply_context else "") - + str(text or "").strip() + (reply_context + "\n\nUser message:\n" if reply_context else "") + str(text or "").strip() ), ) turn, _created = runtime_controller.enqueue_turn( - session_id=session_id, - client_turn_id=client_turn_id, - message=message, - work_dir=resolved_work_dir, - objective=str(objective or goal_id), - origin="lark", + session_id=session_id, client_turn_id=client_turn_id, message=message, + work_dir=resolved_work_dir, objective=str(objective or goal_id), origin="lark", ) if native_mention: - runtime_controller.store.update_ingress_receipt( - session_id, client_turn_id, status="delivered", - ) + runtime_controller.store.update_ingress_receipt(session_id, client_turn_id, status="delivered") else: # Compatibility path for bindings created before the three ingress modes. channel_id = "lark." + _opaque_digest( - route.get("app_ref"), - route.get("target_ref"), - route.get("topic_root_message_id"), + route.get("app_ref"), route.get("target_ref"), route.get("topic_root_message_id"), ) session, _resumed = runtime_controller.open_session( - goal_id=goal_id, - agent_id=agent_id, - work_dir=resolved_work_dir, - objective=str(objective or goal_id), - mode="resume_latest", - channel_id=channel_id, - agent_goal_id=goal_id, + goal_id=goal_id, agent_id=agent_id, work_dir=resolved_work_dir, + objective=str(objective or goal_id), mode="resume_latest", + channel_id=channel_id, agent_goal_id=goal_id, ) session_id = str(session["session_id"]) turn, _created = runtime_controller.submit_turn( - session_id=session_id, - client_turn_id=client_turn_id, - message=message, - work_dir=resolved_work_dir, - objective=str(objective or goal_id), + session_id=session_id, client_turn_id=client_turn_id, message=message, + work_dir=resolved_work_dir, objective=str(objective or goal_id), ) - completed = runtime_controller.wait_for_turn( - session_id=session_id, - turn_id=str(turn["turn_id"]), - ) + completed = runtime_controller.wait_for_turn(session_id=session_id, turn_id=str(turn["turn_id"])) if completed.get("status") != "completed": if completed.get("status") not in {"failed", "timed_out", "interrupted"}: raise RuntimeError("Lark Goal Topic turn has no terminal receipt") - raise LarkGoalTopicTurnFailed( - str(completed.get("error_code") or ""), - _session_turn_effect(route), - ) + raise LarkGoalTopicTurnFailed(str(completed.get("error_code") or ""), _session_turn_effect(route)) response = completed.get("response") - reply_text = ( - str(response.get("message") or "") if isinstance(response, Mapping) else "" - ) + reply_text = _str_field(response, "message") if isinstance(response, Mapping) else "" if not reply_text.strip(): raise RuntimeError("Lark Goal Topic turn returned no message") if not manager: return reply_text - proposal_ids = proposal_ids_after_turn( - runtime_controller, - session_id=session_id, - turn_id=str(turn["turn_id"]), - ) - if proposal_ids: - return { - "response_text": reply_text, - "proposal_ids": proposal_ids, - } - return reply_text + proposal_ids = proposal_ids_after_turn(runtime_controller, session_id=session_id, turn_id=str(turn["turn_id"])) + return {"response_text": reply_text, "proposal_ids": proposal_ids} if proposal_ids else reply_text def _inbox_config( - *, - runtime_root: Path, - route: Mapping[str, Any], - target_payload: Mapping[str, Any], + *, runtime_root: Path, route: Mapping[str, object], target_payload: Mapping[str, object], ) -> tuple[Path, str]: - target_ref = str(route.get("target_ref") or "") - target = goal_channel_target_for_name(target_payload, target_ref) + target = goal_channel_target_for_name(target_payload, _str_field(route, "target_ref")) if target is None: raise ValueError("the routed Lark target is unavailable") - channel = ( - target.get("channel") if isinstance(target.get("channel"), Mapping) else {} - ) - identity = ( - target.get("identity") if isinstance(target.get("identity"), Mapping) else {} - ) - chat_id = str(channel.get("chat_id") or "") - profile = str(route.get("app_ref") or "") - bot_display_name = str(identity.get("bot_display_name") or profile) - digest = topic_inbox_key(profile, chat_id, str(route.get("topic_root_message_id") or "")) + channel = _as_mapping(target.get("channel")) + identity = _as_mapping(target.get("identity")) + chat_id = _str_field(channel, "chat_id") + profile = _str_field(route, "app_ref") + topic_root = _str_field(route, "topic_root_message_id") + digest = topic_inbox_key(profile, chat_id, topic_root) config_ref = f".loopx/config/lark-goal-topics/{digest}.json" config_path = runtime_root / config_ref payload = { @@ -1308,7 +1143,7 @@ def _inbox_config( "enabled": True, "inbox_dir": f".loopx/inbox/lark-goal-topics/{digest}", "capture_scope": "configured_chat_all", - "topic_root_message_id": str(route.get("topic_root_message_id") or ""), + "topic_root_message_id": topic_root, "material_review": { "enabled": route.get("conversation_kind") == "manager", "drain_limit": MANAGER_CONTEXT_ITEM_LIMIT, @@ -1317,15 +1152,13 @@ def _inbox_config( "enabled": True, "sender_profile": profile, "sender_identity": "bot", - "bot_display_name": bot_display_name, - "bot_app_id": str(identity.get("bot_app_id") or ""), - "bot_open_id": str(identity.get("bot_open_id") or ""), + "bot_display_name": _str_field(identity, "bot_display_name", profile), + "bot_app_id": _str_field(identity, "bot_app_id"), + "bot_open_id": _str_field(identity, "bot_open_id"), "chat_id": chat_id, "placement_policy": "source_context", "editorial_style": "bullet_points_preferred", - "received_reaction_policy": ( - "retain" if route.get("conversation_kind") == "manager" else "transient" - ), + "received_reaction_policy": "retain" if route.get("conversation_kind") == "manager" else "transient", }, } config_path.parent.mkdir(parents=True, exist_ok=True, mode=0o700) @@ -1335,18 +1168,18 @@ def _inbox_config( def process_lark_goal_topic_event( *, - target_payload: Mapping[str, Any], - binding_payloads: Mapping[str, Mapping[str, Any]], - event: Mapping[str, Any], + target_payload: Mapping[str, object], + binding_payloads: Mapping[str, Mapping[str, object]], + event: Mapping[str, object], runtime_root: str | Path, - goal_contexts: Mapping[str, Mapping[str, Any]] | None = None, + goal_contexts: Mapping[str, Mapping[str, object]] | None = None, answer: Answer, reply_runner: CommandRunner, - provider_runner: Any | None = None, + provider_runner: ProviderRunner | None = None, proposal_deliverer: ProposalDeliverer | None = None, mention_capture: Mapping[str, object] | None = None, live_turn_capture: Mapping[str, object] | None = None, -) -> dict[str, Any]: +) -> dict[str, object]: """Single-flight a manager or live mention through ingress, reply and ACK. The inbox and delivery receipt make sequential retries safe, but two @@ -1356,21 +1189,20 @@ def process_lark_goal_topic_event( """ decision = decide_lark_topic_event( - target_payload=target_payload, - binding_payloads=binding_payloads, - event=event, - runtime_root=runtime_root, + target_payload=target_payload, binding_payloads=binding_payloads, + event=event, runtime_root=runtime_root, ) - route = decision.get("route") - native_mention = isinstance(route, Mapping) and _route_native_mention(event, route, target_payload) + raw_route = decision.get("route") + route = dict(raw_route) if isinstance(raw_route, Mapping) else None + native_mention = route is not None and _route_native_mention(event, route, target_payload) if mention_capture is not None and not native_mention: return {"ok": True, "status": "ignored", "reason": "mention_binding_changed"} if live_turn_capture is not None: - if not isinstance(route, Mapping) or route.get("ingress_mode") != "live_steering" or native_mention: + if route is None or route.get("ingress_mode") != "live_steering" or native_mention: return {"ok": True, "status": "ignored", "reason": LarkTopicEventDecisionReason.BINDING_UNAVAILABLE.value} route = {**route, "live_turn_capture": live_turn_capture} decision = {**decision, "route": route} - if native_mention: + if route is not None and native_mention: route = { **route, "native_mention": True, @@ -1378,186 +1210,121 @@ def process_lark_goal_topic_event( "mention_capture": mention_capture, } decision = {**decision, "route": route} - if isinstance(route, Mapping) and ( - native_mention or (route.get("conversation_kind") == "manager" - and route.get("authority_mode") == ManagerAuthorityMode.TURN_AUTHORIZED.value) + lock: AbstractContextManager[object] = nullcontext() + if route is not None and ( + native_mention or ( + route.get("conversation_kind") == "manager" + and route.get("authority_mode") == ManagerAuthorityMode.TURN_AUTHORIZED.value + ) ): - message_id = str(route.get("message_id") or "") + message_id = _str_field(route, "message_id") if not MESSAGE_ID_PATTERN.fullmatch(message_id): raise ValueError("manager route has an invalid message id") lock_dir = Path(runtime_root).expanduser().resolve() / ".loopx/locks/lark-manager" lock_dir.mkdir(parents=True, exist_ok=True, mode=0o700) os.chmod(lock_dir, 0o700) - with exclusive_file_lock( - lock_dir / message_id, - timeout_seconds=960, - operation="lark_manager_message", - ): - return _process_lark_goal_topic_event( - target_payload=target_payload, - event=event, - runtime_root=runtime_root, - goal_contexts=goal_contexts, - answer=answer, - reply_runner=reply_runner, - provider_runner=provider_runner, - proposal_deliverer=proposal_deliverer, - decision=decision, - ) - return _process_lark_goal_topic_event( - target_payload=target_payload, - event=event, - runtime_root=runtime_root, - goal_contexts=goal_contexts, - answer=answer, - reply_runner=reply_runner, - provider_runner=provider_runner, - proposal_deliverer=proposal_deliverer, - decision=decision, - ) + lock = exclusive_file_lock(lock_dir / message_id, timeout_seconds=960, operation="lark_manager_message") + with lock: + return _process_lark_goal_topic_event( + target_payload=target_payload, event=event, runtime_root=runtime_root, + goal_contexts=goal_contexts, answer=answer, reply_runner=reply_runner, + provider_runner=provider_runner, proposal_deliverer=proposal_deliverer, decision=decision, + ) def _process_lark_goal_topic_event( *, - target_payload: Mapping[str, Any], - event: Mapping[str, Any], + target_payload: Mapping[str, object], + event: Mapping[str, object], runtime_root: str | Path, - goal_contexts: Mapping[str, Mapping[str, Any]] | None, + goal_contexts: Mapping[str, Mapping[str, object]] | None, answer: Answer, reply_runner: CommandRunner, provider_runner: object | None, proposal_deliverer: ProposalDeliverer | None, - decision: Mapping[str, Any], + decision: Mapping[str, object], ) -> dict[str, object]: """Persist, answer, reply, and ACK the already-routed Topic event.""" - route = decision.get("route") - if route is None: + raw_route = decision.get("route") + if not isinstance(raw_route, Mapping): return { "ok": True, "status": "ignored", - "reason": str( - decision.get("reason") - or LarkTopicEventDecisionReason.BINDING_UNAVAILABLE.value - ), + "reason": _str_field(decision, "reason", LarkTopicEventDecisionReason.BINDING_UNAVAILABLE.value), } - ingress_mode = str(route.get("ingress_mode") or "direct_session") + route: dict[str, object] = dict(raw_route) + goal_id = route["goal_id"] + ingress_mode = _str_field(route, "ingress_mode", "direct_session") if ingress_mode == "async_inbox": - contexts = goal_contexts if isinstance(goal_contexts, Mapping) else {} - context = contexts.get(str(route.get("goal_id") or "")) - context = context if isinstance(context, Mapping) else {} - work_dir = str(context.get("work_dir") or "").strip() - config_ref = str(route.get("inbox_config_ref") or "").strip() + context = _as_mapping(_as_mapping(goal_contexts).get(str(goal_id or ""))) + work_dir = _str_field(context, "work_dir").strip() + config_ref = _str_field(route, "inbox_config_ref").strip() if not work_dir or not config_ref: - return { - "ok": False, - "status": "agent_inbox_unavailable", - "goal_id": route["goal_id"], - } + return _topic_error("agent_inbox_unavailable", goal_id) root = Path(work_dir).expanduser().resolve() config_path = Path(config_ref) else: root = Path(runtime_root).expanduser().resolve() - config_path, config_ref = _inbox_config( - runtime_root=root, - route=route, - target_payload=target_payload, - ) - canonical: dict[str, Any] = { + config_path, config_ref = _inbox_config(runtime_root=root, route=route, target_payload=target_payload) + canonical: dict[str, object] = { "schema_version": "lark_event_inbox_event_v0", "event_id": str(event.get("event_id") or event.get("message_id") or ""), - "message_id": str(event.get("message_id") or ""), - "create_time": str(event.get("create_time") or ""), - "content": str(event.get("content") or ""), - "sender_type": str(event.get("sender_type") or ""), - "sender_id": str(event.get("sender_id") or ""), - "root_id": str(event.get("root_id") or ""), - "thread_id": str(event.get("thread_id") or ""), + "message_id": _str_field(event, "message_id"), + "create_time": _str_field(event, "create_time"), + "content": _str_field(event, "content"), + "sender_type": _str_field(event, "sender_type"), + "sender_id": _str_field(event, "sender_id"), + "root_id": _str_field(event, "root_id"), + "thread_id": _str_field(event, "thread_id"), "thread_context": event.get("thread_context"), - "parent_id": str(event.get("parent_id") or ""), - "mentions": event.get("mentions") - if isinstance(event.get("mentions"), list) - else [], + "parent_id": _str_field(event, "parent_id"), + "mentions": event.get("mentions") if isinstance(event.get("mentions"), list) else [], "reply_context_verified": event.get("reply_context_verified") is True, "reply_to_bot": event.get("reply_to_bot") is True, "reply_context": event.get("reply_context"), } - ingest = ingest_lark_event_inbox( - project=root, - config_path=config_path, - events=[canonical], - execute=True, - ) + ingest = ingest_lark_event_inbox(project=root, config_path=config_path, events=[canonical], execute=True) manager = route.get("conversation_kind") == "manager" - authority_mode = ( - parse_manager_authority_mode(route.get("authority_mode")) if manager else None - ) + authority_mode = parse_manager_authority_mode(route.get("authority_mode")) if manager else None if manager and authority_mode is None: return invalid_manager_authority_result(route, inbox_config_ref=config_ref) retention = {"discarded_count": 0, "expired_count": 0, "overflow_count": 0} if manager: _, retention = manager_context_projection( - project=root, - config_path=config_path, - current_message_id=str(canonical.get("message_id") or ""), + project=root, config_path=config_path, current_message_id=_str_field(canonical, "message_id"), ) if authority_mode is ManagerAuthorityMode.CONTEXT_ONLY: return { "ok": True, - "status": ( - "context_only_captured" - if int(ingest.get("accepted_count") or 0) - else "context_only_already_captured" - ), - "reason": ( - normalize_lark_topic_event_rejection_reason(route.get("trigger_reason")) - or LarkTopicEventDecisionReason.NOT_ADDRESSED.value - ), - "goal_id": route["goal_id"], + "status": "context_only_captured" if _int_field(ingest, "accepted_count") else "context_only_already_captured", + "reason": normalize_lark_topic_event_rejection_reason(route.get("trigger_reason")) or LarkTopicEventDecisionReason.NOT_ADDRESSED.value, + "goal_id": goal_id, "inbox_config_ref": config_ref, "turn_authorized": False, "model_invoked": False, "external_write_performed": False, "context_retention_discarded_count": retention["discarded_count"], } - context_sync: Mapping[str, Any] | None = None - if route.get("conversation_kind") == "manager" and provider_runner is not None: + context_sync: Mapping[str, object] | None = None + if manager and provider_runner is not None: context_sync = sync_manager_context( - project=root, - config_path=config_path, - target_payload=target_payload, - target_ref=str(route.get("target_ref") or ""), - provider_runner=provider_runner, + project=root, config_path=config_path, target_payload=target_payload, + target_ref=_str_field(route, "target_ref"), provider_runner=provider_runner, ) message_id = str(route["message_id"]) - projection = inspect_lark_event_inbox( - project=root, config_path=config_path, limit=0 - ) + projection = inspect_lark_event_inbox(project=root, config_path=config_path, limit=0) if manager: projection, retention = manager_context_projection( - project=root, - config_path=config_path, - current_message_id=message_id, + project=root, config_path=config_path, current_message_id=message_id, ) - pending_ids = { - str(item.get("message_id") or "") - for item in projection.get("items", []) - if isinstance(item, Mapping) - } - if message_id not in pending_ids: - return { - "ok": True, - "status": "already_acknowledged", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - } + projection_items = _dict_list(projection.get("items")) + if message_id not in {_str_field(item, "message_id") for item in projection_items}: + return {"ok": True, "status": "already_acknowledged", "goal_id": goal_id, "inbox_config_ref": config_ref} if ingress_mode == "async_inbox": return { - "ok": True, - "status": "queued_for_agent", - "goal_id": route["goal_id"], - "agent_id": route.get("agent_id"), - "inbox_config_ref": config_ref, + "ok": True, "status": "queued_for_agent", "goal_id": goal_id, + "agent_id": route.get("agent_id"), "inbox_config_ref": config_ref, **({"reason": "async_inbox_requires_bound_session"} if route.get("native_mention") else {}), } # Receipt ACK is visible while the synchronous manager is reasoning. The @@ -1566,100 +1333,62 @@ def _process_lark_goal_topic_event( # A cosmetic reaction failure must not suppress the actual answer. received_reaction = None if manager: - profile = str(route.get("app_ref") or "") + profile = _str_field(route, "app_ref") try: received_reaction = ensure_lark_event_inbox_received_reaction( - project=root, - config_path=config_path, - event=canonical, - create_reaction=lambda mid, emoji: _create_reaction( - runner=reply_runner, - profile=profile, - message_id=mid, - emoji_type=emoji, - ), - delete_reaction=lambda mid, rid: _delete_reaction( - runner=reply_runner, - profile=profile, - message_id=mid, - reaction_id=rid, - ), + project=root, config_path=config_path, event=canonical, + create_reaction=lambda mid, emoji: _create_reaction(runner=reply_runner, profile=profile, message_id=mid, emoji_type=emoji), + delete_reaction=lambda mid, rid: _delete_reaction(runner=reply_runner, profile=profile, message_id=mid, reaction_id=rid), ) except (OSError, ValueError): received_reaction = {"ok": False, "status": "reaction_state_unavailable"} if not received_reaction.get("ok"): - logging.getLogger(__name__).warning( - "Lark manager received reaction was not verified" - ) + logging.getLogger(__name__).warning("Lark manager received reaction was not verified") # Sender provenance comes from the provider event, never the model response. # Context-only messages stay visibly non-authoritative inside the next # addressed manager Turn and never call the model on their own. - context_materials = ( - manager_context_materials(projection, current_message_id=message_id) - if manager - else [] - ) - captured_event = next( - item for item in projection["items"] if item.get("message_id") == message_id - ) + context_materials = manager_context_materials(projection, current_message_id=message_id) if manager else [] + captured_event = next(item for item in projection_items if item.get("message_id") == message_id) if route.get("native_mention") and ( not captured_event.get("target_mention_count") or captured_event.get("historical_context_only") or captured_event.get("sender_id") != canonical["sender_id"] ): - return {"ok": False, "status": "native_mention_proof_changed", "goal_id": route["goal_id"]} + return _topic_error("native_mention_proof_changed", goal_id) route = { **route, - "source_sender_id": str(canonical.get("sender_id") or ""), - "source_conversation_id": str(event.get("chat_id") or ""), + "source_sender_id": _str_field(canonical, "sender_id"), + "source_conversation_id": _str_field(event, "chat_id"), # The immutable captured event owns reply ancestry on delivery retries. "parent_id": captured_event.get("parent_id", ""), "reply_context": captured_event.get("reply_context"), "root_id": captured_event.get("root_id", ""), "thread_id": captured_event.get("thread_id", ""), "thread_context": captured_event.get("thread_context"), - **({"native_mention_fresh": int(ingest.get("accepted_count") or 0) > 0} if route.get("native_mention") else {}), + **({"native_mention_fresh": _int_field(ingest, "accepted_count") > 0} if route.get("native_mention") else {}), **({"context_materials": context_materials} if context_materials else {}), } delivery_path: Path | None = None - delivery_state: dict[str, Any] | None = None + delivery_state: dict[str, object] | None = None saved_response_reused = False if manager: try: - delivery_path, delivery_state = _load_manager_delivery( - project=root, config_path=config_path, event=canonical - ) + delivery_path, delivery_state = _load_manager_delivery(project=root, config_path=config_path, event=canonical) except (OSError, ValueError): - return { - "ok": False, - "status": "reply_delivery_state_invalid", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "source_acknowledged": False, - } - if delivery_state is not None and delivery_state["status"] == "acknowledged": - return { - "ok": False, - "status": "reply_delivery_state_invalid", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "source_acknowledged": False, - } + return _topic_error("reply_delivery_state_invalid", goal_id, inbox_config_ref=config_ref, source_acknowledged=False) + if delivery_state is not None and delivery_state.get("status") == "acknowledged": + return _topic_error("reply_delivery_state_invalid", goal_id, inbox_config_ref=config_ref, source_acknowledged=False) try: - route = restore_manager_context_route( - route, - projection, - current_message_id=message_id, - delivery_state=delivery_state, - ) + route = restore_manager_context_route(route, projection, current_message_id=message_id, delivery_state=delivery_state) except ValueError: - return unavailable_manager_context_result( - route, inbox_config_ref=config_ref - ) - if "context_materials" in route: - context_materials = route["context_materials"] + return unavailable_manager_context_result(route, inbox_config_ref=config_ref) + raw_materials = route.get("context_materials") + if isinstance(raw_materials, list): + context_materials = [ + {str(k): str(v) for k, v in item.items()} for item in raw_materials if isinstance(item, Mapping) + ] failure_code: str | None = None - effect_receipt: Mapping[str, Any] | None = None + effect_receipt: Mapping[str, object] | None = None proposal_ids: list[str] = [] answer_completed = False if delivery_state is not None: @@ -1671,7 +1400,7 @@ def _process_lark_goal_topic_event( effect_receipt = saved_effect if isinstance(saved_effect, Mapping) else None saved_failure = delivery_state.get("failure_code") failure_code = str(saved_failure) if saved_failure else None - proposal_ids = [str(value) for value in delivery_state.get("proposal_ids") or []] + proposal_ids = _str_list(delivery_state.get("proposal_ids")) else: try: answer_result = answer(route, str(canonical["content"])) @@ -1680,10 +1409,7 @@ def _process_lark_goal_topic_event( if not manager: raise failure_code, failure_text = _manager_failure_reply(exc) - answer_result = { - "response_text": failure_text, - "effect_receipt": exc.effect_receipt, - } + answer_result = {"response_text": failure_text, "effect_receipt": exc.effect_receipt} except Exception as exc: # A synchronous manager route must leave a user-visible, bounded # receipt even when the worker raises an untyped exception. The @@ -1691,70 +1417,49 @@ def _process_lark_goal_topic_event( # type is logged and the public reply uses the generic failure label. if not manager: raise - logging.getLogger(__name__).warning( - "Lark manager answer failed with %s", type(exc).__name__ - ) + logging.getLogger(__name__).warning("Lark manager answer failed with %s", type(exc).__name__) failure_code, failure_text = _manager_failure_reply(exc) - answer_result = { - "response_text": failure_text, - "effect_receipt": _session_turn_effect(route), - } + answer_result = {"response_text": failure_text, "effect_receipt": _session_turn_effect(route)} if isinstance(answer_result, Mapping): - reply_text = str(answer_result.get("response_text") or "").strip() + reply_text = _str_field(answer_result, "response_text").strip() candidate_receipt = answer_result.get("effect_receipt") - effect_receipt = ( - candidate_receipt if isinstance(candidate_receipt, Mapping) else None - ) - proposal_ids = [ - str(value) - for value in answer_result.get("proposal_ids") or [] - if str(value) - ] + effect_receipt = candidate_receipt if isinstance(candidate_receipt, Mapping) else None + proposal_ids = _str_list(answer_result.get("proposal_ids"), skip_empty=True) else: reply_text = str(answer_result or "").strip() content_format = "markdown" if manager else "text" - connector = route.get("connector") - connector = connector if isinstance(connector, Mapping) else None + raw_connector = route.get("connector") + connector = raw_connector if isinstance(raw_connector, Mapping) else None if not reply_text: answer_completed = False - if manager: - # Empty model output is a terminal, non-replayable failure for a - # manager request. Reply through the same inbox path so the - # source is ACKed only after provider verification. - failure_code = "answer_empty" - reply_text = ( - "已收到你的消息,但本次没有生成可发送的完整答复。" - "请求不会自动重放;请在 LoopX 管家会话查看状态或重新发起。" - ) - effect_receipt = effect_receipt or _session_turn_effect(route) - else: - return { - "ok": False, - "status": "answer_empty", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - } - rich_text_repairs: list[dict[str, Any]] = [] + if not manager: + return _topic_error("answer_empty", goal_id, inbox_config_ref=config_ref) + # Empty model output is a terminal, non-replayable failure for a + # manager request. Reply through the same inbox path so the + # source is ACKed only after provider verification. + failure_code = "answer_empty" + reply_text = ( + "已收到你的消息,但本次没有生成可发送的完整答复。" + "请求不会自动重放;请在 LoopX 管家会话查看状态或重新发起。" + ) + effect_receipt = effect_receipt or _session_turn_effect(route) + rich_text_repairs: list[dict[str, object]] = [] if manager: # The reader must not receive a one-line answer whose bullet list is # still escaped, nor raw braces left by an unresolved template. Repair # those defects before the first send: the strict validator keeps # owning unsafe markup, and a repaired markdown answer stays markdown # instead of degrading to plain text. - reply_text, rich_text_repairs = repair_manager_reply_text(reply_text) + reply_text, repairs = repair_manager_reply_text(reply_text) + rich_text_repairs = [dict(item) for item in repairs] reply_text = present_lark_conversation_text(reply_text) if manager and delivery_state is None: assert delivery_path is not None delivery_state = _pending_manager_delivery( - event=canonical, - text=reply_text, - content_format=content_format, - effect_receipt=effect_receipt, - failure_code=failure_code, - context_material_ids=[ - item["message_id"] for item in context_materials - ], + event=canonical, text=reply_text, content_format=content_format, + effect_receipt=effect_receipt, failure_code=failure_code, + context_material_ids=[item["message_id"] for item in context_materials], proposal_ids=proposal_ids, ) if rich_text_repairs: @@ -1762,55 +1467,32 @@ def _process_lark_goal_topic_event( try: _write_manager_delivery(delivery_path, delivery_state) except OSError: - return { - "ok": False, - "status": "reply_delivery_state_unavailable", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "source_acknowledged": False, - } + return _topic_error("reply_delivery_state_unavailable", goal_id, inbox_config_ref=config_ref, source_acknowledged=False) if connector is not None: effect_decision = decide_external_event_ack( - event_id=canonical["event_id"], - effect_receipt=effect_receipt, + event_id=str(canonical["event_id"]), effect_receipt=effect_receipt, response_policy=ExternalResponsePolicy.NO_RESPONSE.value, ) if not effect_decision["effect_ready"]: - return { - "ok": False, - "status": "durable_effect_required", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "ack_decision": effect_decision, - } + return _topic_error("durable_effect_required", goal_id, inbox_config_ref=config_ref, ack_decision=effect_decision) - if delivery_state is not None and delivery_state["status"] == "sent_verified": - reply = { + if delivery_state is not None and delivery_state.get("status") == "sent_verified": + reply: dict[str, object] = { "ok": True, "status": "sent_verified", "idempotency_key": delivery_state.get("reply_idempotency_key"), - "external_write_performed": delivery_state.get("external_write_performed") - is True, + "external_write_performed": delivery_state.get("external_write_performed") is True, "verification_performed": True, "reply_verified": True, } else: if delivery_state is not None: - delivery_state["attempt_count"] = ( - int(delivery_state.get("attempt_count") or 0) + 1 - ) - delivery_state["updated_at"] = datetime.now(timezone.utc).isoformat() assert delivery_path is not None - _write_manager_delivery(delivery_path, delivery_state) + _persist_delivery(delivery_path, delivery_state, attempt_count=_int_field(delivery_state, "attempt_count") + 1) try: reply = reply_lark_event_inbox( - project=root, - config_path=config_path, - message_id=message_id, - text=reply_text, - content_format=content_format, - execute=True, - runner=reply_runner, + project=root, config_path=config_path, message_id=message_id, text=reply_text, + content_format=content_format, execute=True, runner=reply_runner, # Ordinary answers use the provider capacity. The shared Inbox # sender splits oversized rich replies without a text downgrade. short_message_limit=None, @@ -1821,174 +1503,109 @@ def _process_lark_goal_topic_event( # Length is handled by the shared rich-post splitter. Invalid # presentation remains an explicit failure, never a text downgrade. assert delivery_state is not None and delivery_path is not None - delivery_state.update(last_delivery_status="format_unrepresentable", - updated_at=datetime.now(timezone.utc).isoformat()) - _write_manager_delivery(delivery_path, delivery_state) - return {"ok": False, "status": "reply_delivery_pending", - "reason": "reply_format_invalid", "format_degraded": False, - "goal_id": route["goal_id"], "inbox_config_ref": config_ref, - "source_acknowledged": False} + _persist_delivery(delivery_path, delivery_state, last_delivery_status="format_unrepresentable") + return _topic_error( + "reply_delivery_pending", goal_id, inbox_config_ref=config_ref, + source_acknowledged=False, reason="reply_format_invalid", format_degraded=False, + ) if manager and reply.get("content_format") in {"markdown", "text"}: content_format = str(reply["content_format"]) if manager and delivery_state is not None: delivery_state.update(manager_part_delivery_readback(reply)) if not reply.get("ok"): + reply_status = _str_field(reply, "status", "reply_failed") if manager: assert delivery_state is not None and delivery_path is not None - delivery_state.update( - last_delivery_status=str(reply.get("status") or "reply_failed"), - last_blocker=str(reply.get("blocker") or "reply_delivery_unverified"), - updated_at=datetime.now(timezone.utc).isoformat(), + _persist_delivery( + delivery_path, delivery_state, last_delivery_status=reply_status, + last_blocker=_str_field(reply, "blocker", "reply_delivery_unverified"), ) - _write_manager_delivery(delivery_path, delivery_state) - return { - "ok": False, - "status": ( - "reply_delivery_pending" - if manager - else str(reply.get("status") or "reply_failed") - ), + return _topic_error( + "reply_delivery_pending" if manager else reply_status, + goal_id, + inbox_config_ref=config_ref, + source_acknowledged=False if manager else None, + blocker=reply.get("blocker"), **( { - "delivery_status": str(reply.get("status") or "reply_failed"), - "format_degraded": bool( - delivery_state and delivery_state.get("format_degraded") - ), - "rich_text_repairs": list( - (delivery_state or {}).get("rich_text_repairs") or [] - ), + "delivery_status": reply_status, + "format_degraded": bool(delivery_state and delivery_state.get("format_degraded")), + "rich_text_repairs": _dict_list((delivery_state or {}).get("rich_text_repairs")), **manager_part_delivery_readback(reply), "delivery_notice_sent": reply.get("delivery_notice_sent") is True, } - if manager - else {} + if manager else {} ), - "goal_id": route["goal_id"], - "blocker": reply.get("blocker"), - "inbox_config_ref": config_ref, - **({"source_acknowledged": False} if manager else {}), - } + ) if manager: assert delivery_state is not None and delivery_path is not None - delivery_state.update( - status="sent_verified", - delivery_text=reply_text, - delivery_digest=_manager_delivery_text_digest(reply_text), - content_format=content_format, - reply_idempotency_key=reply.get("idempotency_key"), - external_write_performed=(reply.get("external_write_performed") is True), - verification_performed=(reply.get("verification_performed") is True), - reply_verified=(reply.get("reply_verified") is True), - last_delivery_status=str(reply.get("status") or "sent_verified"), - updated_at=datetime.now(timezone.utc).isoformat(), - ) try: - _write_manager_delivery(delivery_path, delivery_state) + _persist_delivery( + delivery_path, delivery_state, + status="sent_verified", delivery_text=reply_text, + delivery_digest=_manager_delivery_text_digest(reply_text), + content_format=content_format, + reply_idempotency_key=reply.get("idempotency_key"), + external_write_performed=(reply.get("external_write_performed") is True), + verification_performed=(reply.get("verification_performed") is True), + reply_verified=(reply.get("reply_verified") is True), + last_delivery_status=_str_field(reply, "status", "sent_verified"), + ) except OSError: - return { - "ok": False, - "status": "reply_delivery_receipt_unavailable", - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "source_acknowledged": False, - } + return _topic_error("reply_delivery_receipt_unavailable", goal_id, inbox_config_ref=config_ref, source_acknowledged=False) proposal_delivery_status = settle_team_plan_proposal_delivery( - delivery_state=delivery_state, - delivery_path=delivery_path, - route=route, - proposal_ids=proposal_ids, - proposal_deliverer=proposal_deliverer, + delivery_state=delivery_state, delivery_path=delivery_path, route=route, + proposal_ids=proposal_ids, proposal_deliverer=proposal_deliverer, ) if proposal_delivery_status is not None: - return { - "ok": False, - "status": proposal_delivery_status, - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "source_acknowledged": False, - } + return _topic_error(proposal_delivery_status, goal_id, inbox_config_ref=config_ref, source_acknowledged=False) if connector is not None: ack_decision = decide_external_event_ack( - event_id=canonical["event_id"], + event_id=str(canonical["event_id"]), effect_receipt=effect_receipt, - response_policy=str(connector.get("response_policy") or ""), + response_policy=_str_field(connector, "response_policy"), response_receipt=build_external_event_response_receipt( - event_id=canonical["event_id"], - external_write_performed=( - reply.get("external_write_performed") is True - ), + event_id=str(canonical["event_id"]), + external_write_performed=(reply.get("external_write_performed") is True), verification_performed=(reply.get("verification_performed") is True), response_verified=reply.get("reply_verified") is True, ), ) if not ack_decision["ack_allowed"]: - return { - "ok": False, - "status": str(ack_decision["reason"]), - "goal_id": route["goal_id"], - "inbox_config_ref": config_ref, - "ack_decision": ack_decision, - } + return _topic_error(str(ack_decision["reason"]), goal_id, inbox_config_ref=config_ref, ack_decision=ack_decision) context_settled_count = settle_manager_context( - project=root, - config_path=config_path, + project=root, config_path=config_path, materials=context_materials if answer_completed else [], ) - acknowledge_lark_event_inbox( - project=root, - config_path=config_path, - message_ids=[message_id], - execute=True, - ) + acknowledge_lark_event_inbox(project=root, config_path=config_path, message_ids=[message_id], execute=True) if manager: assert delivery_state is not None and delivery_path is not None - delivery_state.update( - status="acknowledged", - updated_at=datetime.now(timezone.utc).isoformat(), - ) delivery_state.pop("delivery_text", None) delivery_state.pop("effect_receipt", None) try: - _write_manager_delivery(delivery_path, delivery_state) + _persist_delivery(delivery_path, delivery_state, status="acknowledged") except OSError: - logging.getLogger(__name__).warning( - "Lark manager delivery receipt cleanup could not be persisted" - ) + logging.getLogger(__name__).warning("Lark manager delivery receipt cleanup could not be persisted") return { "ok": failure_code is None, "status": "processing_failed" if failure_code else "replied_and_acknowledged", - **( - { - "reason": failure_code, - "failure_reply_verified": True, - "source_acknowledged": True, - } - if failure_code - else {} - ), + **({"reason": failure_code, "failure_reply_verified": True, "source_acknowledged": True} if failure_code else {}), "received_reaction_status": (received_reaction or {}).get("status"), **( { "saved_response_reused": saved_response_reused, "format_degraded": bool(delivery_state.get("format_degraded")), - "rich_text_repairs": list( - delivery_state.get("rich_text_repairs") or [] - ), + "rich_text_repairs": _dict_list(delivery_state.get("rich_text_repairs")), **manager_part_delivery_readback(delivery_state), } - if manager and delivery_state is not None - else {} + if manager and delivery_state is not None else {} ), "context_material_count": len(context_materials), "context_settled_count": context_settled_count, "context_retention_discarded_count": retention["discarded_count"], - "context_sync_status": ( - str(context_sync.get("status") or "unknown") - if context_sync is not None - else "not_attempted" - ), - "goal_id": route["goal_id"], + "context_sync_status": _str_field(context_sync, "status", "unknown") if context_sync is not None else "not_attempted", + "goal_id": goal_id, "inbox_config_ref": config_ref, } diff --git a/loopx/history.py b/loopx/history.py index e863b93230..11bbc3fd85 100644 --- a/loopx/history.py +++ b/loopx/history.py @@ -1,11 +1,9 @@ from __future__ import annotations -from typing import Any, cast - import hashlib import json from contextlib import nullcontext -from collections.abc import Callable, Iterable, Mapping +from collections.abc import Callable, Iterable, Mapping, Sequence from dataclasses import dataclass from heapq import merge from itertools import chain, islice @@ -87,9 +85,86 @@ ) -def goal_registry_digest(goal: Mapping[str, Any]) -> str: +def _as_dict(value: object) -> dict[str, object]: + return {str(k): v for k, v in value.items()} if isinstance(value, Mapping) else {} + + +def _as_mapping(value: object) -> Mapping[str, object]: + return value if isinstance(value, Mapping) else {} + + +def _mapping_list(value: object) -> list[Mapping[str, object]]: + if not isinstance(value, Sequence) or isinstance(value, (str, bytes, bytearray)): + return [] + return [item for item in value if isinstance(item, Mapping)] + + +def _int_field(mapping: Mapping[str, object], key: str, default: int = 0) -> int: + value = mapping.get(key) + if isinstance(value, bool): + return int(value) if value else default + return int(value) if isinstance(value, (int, float, str)) and value != "" else default + + +def _normalized_activation(state: GoalActivationState | str | None) -> str | None: + return normalize_goal_activation_state(state).value if state is not None else None + + +def _goal_index_paths( + registry_path: Path, runtime_root_override: str | None, goal_id: str | None, +) -> tuple[dict[str, object], Path, list[tuple[str, Path]]]: + registry = load_registry(registry_path) + runtime_root = resolve_runtime_root(registry, runtime_root_override, registry_path=registry_path) + goal_ids = discover_goal_ids(runtime_root, registry, goal_id) + return registry, runtime_root, [ + (gid, runtime_root / "goals" / gid / "runs" / "index.jsonl") for gid in goal_ids + ] + + +def _index_command_payload( + registry_path: Path, runtime_root: Path, goal_id: str | None, + checked_goal_count: int, **extra: object, +) -> dict[str, object]: + return { + "ok": True, "registry": str(registry_path), "runtime_root": str(runtime_root), + "goal_filter": goal_id, "checked_goal_count": checked_goal_count, **extra, + } + + +def _group_index_lines( + lines: Iterable[str], +) -> tuple[int, dict[tuple[str, str, str], list[tuple[int, dict[str, object]]]]]: + raw_count = 0 + grouped: dict[tuple[str, str, str], list[tuple[int, dict[str, object]]]] = {} + for line_number, line in enumerate(lines, start=1): + if not line.strip(): + continue + raw_count += 1 + try: + item = json.loads(line) + except json.JSONDecodeError: + continue + if isinstance(item, dict): + grouped.setdefault(index_identity(item), []).append((line_number, item)) + return raw_count, grouped + + +def _markdown_report_lines( + title: str, payload: Mapping[str, object], header_fields: Sequence[tuple[str, object]], + *, columns: Sequence[str] = (), rows: Iterable[Sequence[object]] = (), +) -> list[str] | str: + if not payload.get("ok"): + return f"# {title}\n\n- ok: `False`\n- error: {payload.get('error')}" + lines = [f"# {title}", "", *(f"- {label}: `{value}`" for label, value in header_fields)] + if columns: + lines.extend(["", markdown_table_row(list(columns)), markdown_table_separator(len(columns))]) + lines.extend(markdown_table_row(list(row)) for row in rows) + return lines + + +def goal_registry_digest(goal: Mapping[str, object]) -> str: encoded = json.dumps( - goal, + goal if isinstance(goal, dict) else dict(goal), ensure_ascii=True, sort_keys=True, separators=(",", ":"), @@ -97,7 +172,7 @@ def goal_registry_digest(goal: Mapping[str, Any]) -> str: return f"sha256:{hashlib.sha256(encoded).hexdigest()}" -def host_worker_goal_observation_digest(goal: dict[str, Any], *, registry_path: Path) -> str | None: +def host_worker_goal_observation_digest(goal: Mapping[str, object], *, registry_path: Path) -> str | None: """Bind a driver observation to its actual source Goal, not sync metadata. A global row must still match the existing sanitizer/merge projection of @@ -110,7 +185,8 @@ def host_worker_goal_observation_digest(goal: dict[str, Any], *, registry_path: try: selected_registry = registry_path.expanduser().resolve() source_route = goal.get("source_registry") - source_registry, source_goal = selected_registry, goal + source_registry = selected_registry + source_goal: Mapping[str, object] = goal if source_route: if not isinstance(source_route, str) or not Path(source_route).is_absolute(): return None @@ -120,15 +196,16 @@ def host_worker_goal_observation_digest(goal: dict[str, Any], *, registry_path: if row.get("id") == goal.get("id")] if len(candidates) != 1: return None - source_goal = candidates[0] + candidate = candidates[0] # A global row has one owning source. Do not accept an # unverified chain of projections as that source. - if source_goal.get("source_registry"): + if candidate.get("source_registry"): return None expected = preserve_attention_override({}, sanitize_goal_for_global( - source_goal, source_registry=source_registry, synced_at="")) + candidate, source_registry=source_registry, synced_at="")) if preserve_attention_override({}, {**goal, "synced_at": ""}) != expected: return None + source_goal = candidate return goal_registry_digest({"source_registry": str(source_registry), "goal": source_goal}) except (OSError, RuntimeError, TypeError, ValueError): return None @@ -155,37 +232,27 @@ class RunHistoryAudit: goals: tuple[RunIndexAudit, ...] def matches( - self, - *, - registry_path: Path, - runtime_root: Path, - goal_id: str | None, - activation_state_filter: GoalActivationState | str | None, - include_runtime_goals: bool, + self, *, registry_path: Path, runtime_root: Path, goal_id: str | None, + activation_state_filter: GoalActivationState | str | None, include_runtime_goals: bool, ) -> bool: - normalized_activation = ( - normalize_goal_activation_state(activation_state_filter).value - if activation_state_filter is not None - else None - ) return ( self.registry_path == registry_path.expanduser().resolve() and self.runtime_root == runtime_root.expanduser().resolve() and self.goal_id == (str(goal_id or "").strip() or None) - and self.activation_state_filter == normalized_activation + and self.activation_state_filter == _normalized_activation(activation_state_filter) and self.include_runtime_goals is include_runtime_goals ) @dataclass(frozen=True, slots=True) class StatusHistoryCollection: - status_history: dict[str, Any] + status_history: dict[str, object] contract_audit: RunHistoryAudit @dataclass(frozen=True, slots=True) class RunIndexSnapshot: - records: list[dict[str, Any]] + records: list[dict[str, object]] raw_count: int digest: str | None invalid_count: int = 0 @@ -211,14 +278,18 @@ def reserve_unique_run_paths(runs_dir: Path, generated_at: str) -> tuple[Path, P def append_run_index_record( index_path: Path, - record: dict[str, Any], + record: Mapping[str, object], *, allow_nan: bool = True, ) -> None: """Append one readable JSONL record after any unterminated final row.""" encoded = ( - json.dumps(record, ensure_ascii=False, allow_nan=allow_nan).encode("utf-8") + json.dumps( + record if isinstance(record, dict) else dict(record), + ensure_ascii=False, + allow_nan=allow_nan, + ).encode("utf-8") + b"\n" ) with index_path.open("a+b") as handle: @@ -234,10 +305,10 @@ def write_reserved_run_artifacts( *, runs_dir: Path, generated_at: str, - record: dict[str, Any], - index_record: dict[str, Any], - payload: dict[str, Any], - render_markdown: Callable[[dict[str, Any]], str], + record: dict[str, object], + index_record: dict[str, object], + payload: dict[str, object], + render_markdown: Callable[[dict[str, object]], str], ) -> None: from .control_plane.quota.usage_collector import ingest_usage_into_run_record from .control_plane.work_items.delivery_history import ( @@ -260,8 +331,9 @@ def write_reserved_run_artifacts( payload["json_path"] = str(json_path) payload["markdown_path"] = str(markdown_path) payload["index_path"] = str(index_path) - if isinstance(record.get("usage"), dict): - payload["usage"] = dict(record["usage"]) + usage = record.get("usage") + if isinstance(usage, dict): + payload["usage"] = dict(usage) json_path.write_text( json.dumps(record, ensure_ascii=False, indent=2, allow_nan=False) + "\n", encoding="utf-8", @@ -272,7 +344,7 @@ def write_reserved_run_artifacts( def discover_goal_ids( runtime_root: Path, - registry: dict[str, Any], + registry: Mapping[str, object], requested_goal: str | None, *, include_runtime_goals: bool = True, @@ -280,14 +352,15 @@ def discover_goal_ids( if requested_goal: return [requested_goal] - ids = {str(goal.get("id")) for goal in registry_goals(registry)} + raw_registry = registry if isinstance(registry, dict) else dict(registry) + ids = {str(goal.get("id")) for goal in registry_goals(raw_registry)} goals_dir = runtime_root / "goals" if include_runtime_goals and goals_dir.exists(): ids.update(path.name for path in goals_dir.iterdir() if path.is_dir()) return sorted(ids) -def _indexed_artifact_exists(value: Any, *, artifact_root: Path | None) -> bool: +def _indexed_artifact_exists(value: object, *, artifact_root: Path | None) -> bool: text = str(value or "").strip() if not text: return False @@ -304,7 +377,7 @@ def _indexed_artifact_exists(value: Any, *, artifact_root: Path | None) -> bool: def _observe_run_artifacts( - records: Iterable[dict[str, Any]], *, artifact_root: Path | None, + records: Iterable[dict[str, object]], *, artifact_root: Path | None, ) -> None: """Fresh request-local observations, shared by full and selected reads.""" observed: dict[str, bool] = {} @@ -342,7 +415,7 @@ def load_index_snapshot( except FileNotFoundError: return RunIndexSnapshot(records=[], raw_count=0, digest=None) - records: list[dict[str, Any]] = [] + records: list[dict[str, object]] = [] positions: dict[tuple[str, str, str], int] = {} raw_count = 0 invalid_count = 0 @@ -376,11 +449,7 @@ def load_index_snapshot( invalid_count += 1 continue - key = ( - str(item.get("generated_at") or ""), - str(item.get("json_path") or ""), - str(item.get("markdown_path") or ""), - ) + key = index_identity(item) item = dict(item) # These are observations, never persisted index authority. item.pop("json_exists", None) @@ -405,12 +474,12 @@ def load_index( path: Path, *, artifact_root: Path | None = None, -) -> tuple[list[dict[str, Any]], int]: +) -> tuple[list[dict[str, object]], int]: snapshot = load_index_snapshot(path, artifact_root=artifact_root) return snapshot.records, snapshot.raw_count -def latest_status_run(runs: list[dict[str, Any]]) -> dict[str, Any] | None: +def latest_status_run(runs: Sequence[dict[str, object]]) -> dict[str, object] | None: for run in runs: if str(run.get("classification") or "") in STATUS_NEUTRAL_CLASSIFICATIONS: continue @@ -430,74 +499,53 @@ def collect_history( activation_state_filter: GoalActivationState | str | None = None, agent_lane_id: str | None = None, scoped_agent_id: str | None = None, - registry: dict[str, Any] | None = None, + registry: Mapping[str, object] | None = None, tolerate_undecodable: bool = False, -) -> dict[str, Any]: +) -> dict[str, object]: from .capabilities.machine_configuration.builtins import ( read_builtin_goal_machine_configuration, project_goal_with_builtin_machine_configuration, ) from .quota import goal_quota_with_spend_ledger, quota_with_unreadable_index_rows - if registry is None: - registry = load_registry(registry_path) + resolved_registry = ( + load_registry(registry_path) if registry is None + else (registry if isinstance(registry, dict) else dict(registry)) + ) machine_configuration = read_builtin_goal_machine_configuration(runtime_root) - registry_goal_rows = registry_goals(registry) - registry_goal_digests = { - str(goal.get("id")): goal_registry_digest(goal) - for goal in registry_goal_rows - } - registry_goal_meta = { - str(goal.get("id")): goal - for goal in registry_goal_rows - } + registry_goal_meta = {str(goal.get("id")): goal for goal in registry_goals(resolved_registry)} + registry_goal_digests = {gid: goal_registry_digest(goal) for gid, goal in registry_goal_meta.items()} goal_meta = { - str(goal.get("id")): project_goal_with_builtin_machine_configuration( - goal, machine_configuration - ) - for goal in registry_goal_rows + gid: project_goal_with_builtin_machine_configuration(goal, machine_configuration) + for gid, goal in registry_goal_meta.items() } activation_filter = ( normalize_goal_activation_state(activation_state_filter) - if activation_state_filter is not None - else None + if activation_state_filter is not None else None ) - goals: list[dict[str, Any]] = [] - recent_runs: list[dict[str, Any]] = [] + goals: list[dict[str, object]] = [] + recent_runs: list[dict[str, object]] = [] run_count = 0 recent_limit = max(0, limit) for current_goal_id in discover_goal_ids( - runtime_root, - registry, - goal_id, + runtime_root, resolved_registry, goal_id, include_runtime_goals=include_runtime_goals and activation_filter is None, ): registry_member = current_goal_id in goal_meta meta = goal_meta.get(current_goal_id) or {} - activation_state = ( - goal_activation_state(meta) - if registry_member - else GoalActivationState.ACTIVE - ) + activation_state = goal_activation_state(meta) if registry_member else GoalActivationState.ACTIVE if activation_filter is not None and activation_state is not activation_filter: continue index_path = runtime_root / "goals" / current_goal_id / "runs" / "index.jsonl" index_snapshot = load_index_snapshot( - index_path, - include_artifact_status=False, - tolerate_undecodable=tolerate_undecodable, + index_path, include_artifact_status=False, tolerate_undecodable=tolerate_undecodable, ) - runs = index_snapshot.records raw_count = index_snapshot.raw_count runs = [ - run - for _, run in sorted( - enumerate(runs), - key=lambda item: ( - *_chronology_key(item[1].get("generated_at")), - item[0], - ), + run for _, run in sorted( + enumerate(index_snapshot.records), + key=lambda item: (*_chronology_key(item[1].get("generated_at")), item[0]), reverse=True, ) ] @@ -510,25 +558,29 @@ def collect_history( if scoped_agent_id: runs = [run for run in goal_runs if run.get("agent_id") == scoped_agent_id] run_count += len(runs) - recent_runs = list( - islice( - merge( - recent_runs, - runs, - key=lambda item: _chronology_key(item.get("generated_at")), - reverse=True, - ), - recent_limit, - ) - ) + recent_runs = list(islice( + merge(recent_runs, runs, key=lambda item: _chronology_key(item.get("generated_at")), reverse=True), + recent_limit, + )) - adapter = cast(dict[str, Any], meta.get("adapter")) if isinstance(meta.get("adapter"), dict) else {} + adapter = _as_mapping(meta.get("adapter")) + raw_spawn_policy = meta.get("spawn_policy") + spawn_policy = _as_dict(raw_spawn_policy) if isinstance(raw_spawn_policy, dict) else None + explore_harness = (spawn_policy or {}).get("explore_harness") + raw_guards = meta.get("guards") quota = goal_quota_with_spend_ledger(meta, goal_runs) if registry_member else None if quota is not None and index_snapshot.undecodable_count: # The undecodable rows may be the spend or void events the projection # just summed without them; report that instead of an exact count. quota = quota_with_unreadable_index_rows(quota, index_snapshot.undecodable_count) - goal_record: dict[str, Any] = { + status_run = latest_status_run(runs) + latest_runs = latest_runs_with_agent_context( + runs, + limit=limit, + agent_lane_id=None if scoped_agent_id else agent_lane_id, + ) + semantic_history = goal_semantic_history_from_runs(runs) + goal_record: dict[str, object] = { "id": current_goal_id, **goal_parent_fields(meta), "activation_state": activation_state.value, @@ -548,12 +600,16 @@ def collect_history( "legacy_runtime_goal": not registry_member, "adapter_kind": adapter.get("kind"), "adapter_status": adapter.get("status"), - "coordination": cast(dict[str, Any], meta.get("coordination")) if isinstance(meta.get("coordination"), dict) else None, - "explore_graph": compact_explore_graph_policy(meta.get("explore_graph"), (meta.get("spawn_policy") or {}).get("explore_harness")) if meta.get("explore_graph") is not None or (meta.get("spawn_policy") or {}).get("explore_harness") else None, - "spawn_policy": cast(dict[str, Any], meta.get("spawn_policy")) if isinstance(meta.get("spawn_policy"), dict) else None, + "coordination": _as_dict(meta.get("coordination")) if isinstance(meta.get("coordination"), dict) else None, + "explore_graph": ( + compact_explore_graph_policy(meta.get("explore_graph"), explore_harness) + if meta.get("explore_graph") is not None or explore_harness + else None + ), + "spawn_policy": spawn_policy, "execution_profile": compact_execution_profile(meta.get("execution_profile")) if registry_member else None, "control_plane": compact_control_plane_policy(meta.get("control_plane")) if registry_member else None, - "guards": cast(list[Any], meta.get("guards")) if isinstance(meta.get("guards"), list) else [], + "guards": list(raw_guards) if isinstance(raw_guards, list) else [], "next_probe": meta.get("next_probe") if registry_member else None, "authority_registry": goal_authority_registry_summary(meta) if registry_member else None, "quota": quota, @@ -569,23 +625,18 @@ def collect_history( else {} ), "unique_runs": len(runs), - "latest_status_run": latest_status_run(runs), - "latest_runs": latest_runs_with_agent_context( - runs, - limit=limit, - agent_lane_id=None if scoped_agent_id else agent_lane_id, - ), - "semantic_history": goal_semantic_history_from_runs(runs), + "latest_status_run": status_run, + "latest_runs": latest_runs, + "semantic_history": semantic_history, } # Reduce the full index first. Old semantic evidence and lane windows # may survive outside --limit; every returned Run still needs fresh # flags. These references also cover the global recent_runs window. - status_run = goal_record["latest_status_run"] _observe_run_artifacts( chain( - goal_record["latest_runs"], + latest_runs, [status_run] if status_run is not None else [], - iter_goal_semantic_history_runs(goal_record["semantic_history"]), + iter_goal_semantic_history_runs(semantic_history), ), artifact_root=registry_project_root(registry_path), ) @@ -608,115 +659,65 @@ def collect_history( def build_run_history_audit( - history: dict[str, Any], - *, - registry_path: Path, - runtime_root: Path, - goal_id: str | None, - activation_state_filter: GoalActivationState | str | None, + history: Mapping[str, object], *, registry_path: Path, runtime_root: Path, + goal_id: str | None, activation_state_filter: GoalActivationState | str | None, include_runtime_goals: bool, ) -> RunHistoryAudit: - normalized_activation = ( - normalize_goal_activation_state(activation_state_filter).value - if activation_state_filter is not None - else None - ) return RunHistoryAudit( registry_path=registry_path.expanduser().resolve(), runtime_root=runtime_root.expanduser().resolve(), goal_id=str(goal_id or "").strip() or None, - activation_state_filter=normalized_activation, + activation_state_filter=_normalized_activation(activation_state_filter), include_runtime_goals=include_runtime_goals, - goal_count=int(history.get("goal_count") or 0), - run_count=int(history.get("run_count") or 0), + goal_count=_int_field(history, "goal_count"), + run_count=_int_field(history, "run_count"), goals=tuple( RunIndexAudit( goal_id=str(item.get("id") or ""), index_path=Path(str(item.get("index_path") or "")), - raw_index_records=int(item.get("raw_index_records") or 0), - unique_runs=int(item.get("unique_runs") or 0), + raw_index_records=_int_field(item, "raw_index_records"), + unique_runs=_int_field(item, "unique_runs"), legacy_runtime_goal=bool(item.get("legacy_runtime_goal")), ) - for item in history.get("goals") or [] - if isinstance(item, dict) + for item in _mapping_list(history.get("goals")) ), ) -def history_for_registry_members( - history: dict[str, Any], - *, - limit: int, -) -> dict[str, Any]: - goals = [ - goal - for goal in history.get("goals") or [] - if isinstance(goal, dict) and goal.get("registry_member") is True - ] +def history_for_registry_members(history: Mapping[str, object], *, limit: int) -> dict[str, object]: + goals = [goal for goal in _mapping_list(history.get("goals")) if goal.get("registry_member") is True] recent_limit = max(0, limit) - recent_runs = list( - islice( - merge( - *( - goal.get("latest_runs", [])[:recent_limit] - for goal in goals - if isinstance(goal.get("latest_runs"), list) - ), - key=lambda item: _chronology_key(item.get("generated_at")), - reverse=True, - ), - recent_limit, - ) - ) + recent_runs = list(islice( + merge(*(_mapping_list(goal.get("latest_runs"))[:recent_limit] for goal in goals), + key=lambda item: _chronology_key(item.get("generated_at")), reverse=True), + recent_limit, + )) return { - **history, - "goal_count": len(goals), - "run_count": sum(int(goal.get("unique_runs") or 0) for goal in goals), - "goals": goals, - "runs": recent_runs, + **history, "goal_count": len(goals), + "run_count": sum(_int_field(goal, "unique_runs") for goal in goals), + "goals": goals, "runs": recent_runs, } def collect_status_history( - *, - registry_path: Path, - runtime_root: Path, - goal_id: str | None, - limit: int, + *, registry_path: Path, runtime_root: Path, goal_id: str | None, limit: int, status_include_runtime_goals: bool, activation_state_filter: GoalActivationState | str | None = None, - agent_lane_id: str | None = None, - registry: dict[str, Any] | None = None, + agent_lane_id: str | None = None, registry: Mapping[str, object] | None = None, ) -> StatusHistoryCollection: history = collect_history( - registry_path=registry_path, - runtime_root=runtime_root, - goal_id=goal_id, - limit=limit, - include_runtime_goals=True, - activation_state_filter=activation_state_filter, - agent_lane_id=agent_lane_id, - registry=registry, + registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, limit=limit, + include_runtime_goals=True, activation_state_filter=activation_state_filter, + agent_lane_id=agent_lane_id, registry=registry, ) audit = build_run_history_audit( - history, - registry_path=registry_path, - runtime_root=runtime_root, - goal_id=goal_id, - activation_state_filter=activation_state_filter, - include_runtime_goals=True, + history, registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id, + activation_state_filter=activation_state_filter, include_runtime_goals=True, ) status_history = history - if ( - not status_include_runtime_goals - and not str(goal_id or "").strip() - and activation_state_filter is None - ): + if not status_include_runtime_goals and not str(goal_id or "").strip() and activation_state_filter is None: status_history = history_for_registry_members(history, limit=limit) - return StatusHistoryCollection( - status_history=status_history, - contract_audit=audit, - ) + return StatusHistoryCollection(status_history=status_history, contract_audit=audit) def inspect_index_duplicates( @@ -725,101 +726,61 @@ def inspect_index_duplicates( runtime_root_override: str | None, goal_id: str | None, limit: int, -) -> dict[str, Any]: - registry = load_registry(registry_path) - runtime_root = resolve_runtime_root( - registry, - runtime_root_override, - registry_path=registry_path, +) -> dict[str, object]: + _registry, runtime_root, goal_indexes = _goal_index_paths( + registry_path, runtime_root_override, goal_id ) - groups: list[dict[str, Any]] = [] - checked_goal_count = 0 + artifact_root = registry_project_root(registry_path) + groups: list[dict[str, object]] = [] raw_index_records = 0 duplicate_row_count = 0 - for current_goal_id in discover_goal_ids(runtime_root, registry, goal_id): - checked_goal_count += 1 - index_path = runtime_root / "goals" / current_goal_id / "runs" / "index.jsonl" + for current_goal_id, index_path in goal_indexes: if not index_path.exists(): continue - - grouped: dict[tuple[str, str, str], list[tuple[int, dict[str, Any]]]] = {} with index_path.open(encoding="utf-8") as f: - for line_number, line in enumerate(f, start=1): - if not line.strip(): - continue - raw_index_records += 1 - try: - item = json.loads(line) - except json.JSONDecodeError: - continue - if not isinstance(item, dict): - continue - key = ( - str(item.get("generated_at") or ""), - str(item.get("json_path") or ""), - str(item.get("markdown_path") or ""), - ) - grouped.setdefault(key, []).append((line_number, item)) + file_raw_count, grouped = _group_index_lines(f) + raw_index_records += file_raw_count for key, records in grouped.items(): if len(records) <= 1: continue duplicate_row_count += len(records) - 1 generated_at, json_path, markdown_path = key - reward_records = sum(1 for _, record in records if isinstance(record.get("human_reward"), dict)) - classifications = sorted({str(record.get("classification") or "") for _, record in records}) - health_checks = sorted({str(record.get("health_check") or "") for _, record in records if record.get("health_check")}) - duplicate_classification = classify_index_duplicate_records([record for _, record in records]) - groups.append( - { - "goal_id": current_goal_id, - "index_path": str(index_path), - "generated_at": generated_at, - "json_path": json_path, - "markdown_path": markdown_path, - "json_exists": _indexed_artifact_exists( - json_path, - artifact_root=registry_project_root(registry_path), - ), - "markdown_exists": _indexed_artifact_exists( - markdown_path, - artifact_root=registry_project_root(registry_path), - ), - "line_numbers": [line_number for line_number, _ in records], - "raw_rows": len(records), - "duplicate_rows": len(records) - 1, - "duplicate_kind": duplicate_classification.get("duplicate_kind"), - "severity": duplicate_classification.get("severity"), - "classifications": classifications, - "health_checks": health_checks, - "reward_overlay_rows": reward_records, - "repair_hint": duplicate_classification.get("repair_hint"), - } - ) + raw_records = [record for _, record in records] + duplicate_classification = classify_index_duplicate_records(raw_records) + groups.append({ + "goal_id": current_goal_id, + "index_path": str(index_path), + "generated_at": generated_at, + "json_path": json_path, + "markdown_path": markdown_path, + "json_exists": _indexed_artifact_exists(json_path, artifact_root=artifact_root), + "markdown_exists": _indexed_artifact_exists(markdown_path, artifact_root=artifact_root), + "line_numbers": [line_number for line_number, _ in records], + "raw_rows": len(records), + "duplicate_rows": len(records) - 1, + "duplicate_kind": duplicate_classification.get("duplicate_kind"), + "severity": duplicate_classification.get("severity"), + "classifications": sorted({str(record.get("classification") or "") for record in raw_records}), + "health_checks": sorted({str(record.get("health_check") or "") for record in raw_records if record.get("health_check")}), + "reward_overlay_rows": sum(1 for record in raw_records if isinstance(record.get("human_reward"), dict)), + "repair_hint": duplicate_classification.get("repair_hint"), + }) severity_priority = {"warning": 0, "info": 1} - groups.sort( - key=lambda item: ( - severity_priority.get(str(item.get("severity")), 99), - str(item.get("goal_id")), - str(item.get("generated_at")), - ) - ) + groups.sort(key=lambda item: ( + severity_priority.get(str(item.get("severity")), 99), + str(item.get("goal_id")), + str(item.get("generated_at")), + )) limited_groups = groups[: max(0, limit)] - return { - "ok": True, - "registry": str(registry_path), - "runtime_root": str(runtime_root), - "goal_filter": goal_id, - "checked_goal_count": checked_goal_count, - "raw_index_records": raw_index_records, - "duplicate_group_count": len(groups), - "duplicate_row_count": duplicate_row_count, - "groups": limited_groups, - "truncated": len(groups) > len(limited_groups), - "limit": limit, - } + return _index_command_payload( + registry_path, runtime_root, goal_id, len(goal_indexes), + raw_index_records=raw_index_records, duplicate_group_count=len(groups), + duplicate_row_count=duplicate_row_count, groups=limited_groups, + truncated=len(groups) > len(limited_groups), limit=limit, + ) def repair_index_duplicates( @@ -829,106 +790,74 @@ def repair_index_duplicates( goal_id: str | None, limit: int, execute: bool, -) -> dict[str, Any]: - registry = load_registry(registry_path) - runtime_root = resolve_runtime_root( - registry, - runtime_root_override, - registry_path=registry_path, +) -> dict[str, object]: + _registry, runtime_root, goal_indexes = _goal_index_paths( + registry_path, runtime_root_override, goal_id ) - checked_goal_count = 0 raw_index_records = 0 removed_row_count = 0 preserved_reward_overlay_rows = 0 unrepaired_group_count = 0 - groups: list[dict[str, Any]] = [] + groups: list[dict[str, object]] = [] - for current_goal_id in discover_goal_ids(runtime_root, registry, goal_id): - checked_goal_count += 1 - index_path = runtime_root / "goals" / current_goal_id / "runs" / "index.jsonl" + for current_goal_id, index_path in goal_indexes: if not index_path.exists(): continue - # GH-C07: read and rewrite the index under the same lock the append # path takes. A dry run only reports, so it must not block writers. lock = ( exclusive_run_index_lock(index_path, operation="history_index_repair") - if execute - else nullcontext() + if execute else nullcontext() ) with lock: raw_lines = split_index_lines(index_path.read_text(encoding="utf-8")) - grouped: dict[tuple[str, str, str], list[tuple[int, dict[str, Any]]]] = {} - for line_number, line in enumerate(raw_lines, start=1): - if not line.strip(): - continue - raw_index_records += 1 - try: - item = json.loads(line) - except json.JSONDecodeError: - continue - if not isinstance(item, dict): - continue - grouped.setdefault(index_identity(item), []).append((line_number, item)) + file_raw_count, grouped = _group_index_lines(raw_lines) + raw_index_records += file_raw_count remove_lines: set[int] = set() for records in grouped.values(): if len(records) <= 1: continue decision = duplicate_repair_decision(records) - removed_lines = list(decision.get("removed_line_numbers") or []) + removed_lines = [int(ln) for ln in (decision.get("removed_line_numbers") or [])] if decision.get("action") == "preserve_reward_overlay": preserved_reward_overlay_rows += len(records) - 1 elif decision.get("repairable"): - remove_lines.update(int(line_number) for line_number in removed_lines) + remove_lines.update(removed_lines) removed_row_count += len(removed_lines) else: unrepaired_group_count += 1 first_record = records[0][1] - groups.append( - { - "goal_id": current_goal_id, - "index_path": str(index_path), - "generated_at": first_record.get("generated_at"), - "json_path": first_record.get("json_path"), - "markdown_path": first_record.get("markdown_path"), - "action": decision.get("action"), - "repairable": decision.get("repairable"), - "line_numbers": decision.get("line_numbers"), - "kept_line_numbers": decision.get("kept_line_numbers"), - "removed_line_numbers": removed_lines, - "reason": decision.get("reason"), - } - ) + groups.append({ + "goal_id": current_goal_id, + "index_path": str(index_path), + "generated_at": first_record.get("generated_at"), + "json_path": first_record.get("json_path"), + "markdown_path": first_record.get("markdown_path"), + "action": decision.get("action"), + "repairable": decision.get("repairable"), + "line_numbers": decision.get("line_numbers"), + "kept_line_numbers": decision.get("kept_line_numbers"), + "removed_line_numbers": removed_lines, + "reason": decision.get("reason"), + }) if execute and remove_lines: - rewritten = [ - line - for line_number, line in enumerate(raw_lines, start=1) - if line_number not in remove_lines - ] + rewritten = [line for ln, line in enumerate(raw_lines, start=1) if ln not in remove_lines] tmp_path = index_path.with_suffix(index_path.suffix + ".tmp") tmp_path.write_text("".join(line + "\n" for line in rewritten), encoding="utf-8") tmp_path.replace(index_path) limited_groups = groups[: max(0, limit)] - return { - "ok": True, - "dry_run": not execute, - "repaired": bool(execute and removed_row_count), - "registry": str(registry_path), - "runtime_root": str(runtime_root), - "goal_filter": goal_id, - "checked_goal_count": checked_goal_count, - "raw_index_records": raw_index_records, - "removed_row_count": removed_row_count, - "preserved_reward_overlay_rows": preserved_reward_overlay_rows, - "unrepaired_group_count": unrepaired_group_count, - "groups": limited_groups, - "truncated": len(groups) > len(limited_groups), - "limit": limit, - } + return _index_command_payload( + registry_path, runtime_root, goal_id, len(goal_indexes), + dry_run=not execute, repaired=bool(execute and removed_row_count), + raw_index_records=raw_index_records, removed_row_count=removed_row_count, + preserved_reward_overlay_rows=preserved_reward_overlay_rows, + unrepaired_group_count=unrepaired_group_count, groups=limited_groups, + truncated=len(groups) > len(limited_groups), limit=limit, + ) def rebuild_index_artifact_collisions( @@ -937,216 +866,170 @@ def rebuild_index_artifact_collisions( runtime_root_override: str | None, goal_id: str | None, limit: int, - reviewed_plan: dict[str, Any] | None, + reviewed_plan: Mapping[str, object] | None, execute: bool, -) -> dict[str, Any]: - registry = load_registry(registry_path) - runtime_root = resolve_runtime_root( - registry, - runtime_root_override, - registry_path=registry_path, +) -> dict[str, object]: + _registry, runtime_root, goal_indexes = _goal_index_paths( + registry_path, runtime_root_override, goal_id ) - all_groups: list[dict[str, Any]] = [] - checked_goal_count = 0 - for current_goal_id in discover_goal_ids(runtime_root, registry, goal_id): - checked_goal_count += 1 - index_path = runtime_root / "goals" / current_goal_id / "runs" / "index.jsonl" + all_groups: list[dict[str, object]] = [] + for current_goal_id, index_path in goal_indexes: if index_path.exists(): all_groups.extend(collision_review_groups(index_path, current_goal_id)) groups = all_groups[: max(0, limit)] truncated = len(groups) < len(all_groups) current_plan = build_collision_rebuild_plan( - groups, - goal_filter=goal_id, - total_collision_group_count=len(all_groups), - truncated=truncated, + groups, goal_filter=goal_id, total_collision_group_count=len(all_groups), truncated=truncated, ) if execute: if reviewed_plan is None: raise ValueError( "rebuild-index-collisions --execute requires --review-plan-json from a reviewed dry-run" ) - plan_sha256 = validate_reviewed_collision_plan(reviewed_plan, current_plan) - rebuilt_indexes = apply_reviewed_collision_rebuild( + plan_sha256 = validate_reviewed_collision_plan( + reviewed_plan if isinstance(reviewed_plan, dict) else dict(reviewed_plan), current_plan, - plan_sha256=plan_sha256, ) + rebuilt_indexes = apply_reviewed_collision_rebuild(current_plan, plan_sha256=plan_sha256) else: rebuilt_indexes = [] - return { - "ok": True, - "dry_run": not execute, - "rebuilt": bool(execute and rebuilt_indexes), - "registry": str(registry_path), - "runtime_root": str(runtime_root), - "goal_filter": goal_id, - "checked_goal_count": checked_goal_count, - "collision_group_count": len(groups), - "total_collision_group_count": len(all_groups), - "truncated": truncated, - "review_required": not execute and bool(groups), - "review_plan": current_plan, - "rebuilt_indexes": rebuilt_indexes, - "destructive_row_deletion": False, - } + return _index_command_payload( + registry_path, runtime_root, goal_id, len(goal_indexes), + dry_run=not execute, rebuilt=bool(execute and rebuilt_indexes), + collision_group_count=len(groups), total_collision_group_count=len(all_groups), + truncated=truncated, review_required=not execute and bool(groups), + review_plan=current_plan, rebuilt_indexes=rebuilt_indexes, + destructive_row_deletion=False, + ) -def render_index_collision_rebuild_markdown(payload: dict[str, Any]) -> str: - if not payload.get("ok"): - return "# LoopX Artifact Collision Rebuild\n\n- ok: `False`\n- error: " + str(payload.get("error")) - plan = payload.get("review_plan") or {} - return "\n".join( +def render_index_collision_rebuild_markdown(payload: Mapping[str, object]) -> str: + plan = _as_mapping(payload.get("review_plan")) + lines = _markdown_report_lines( + "LoopX Artifact Collision Rebuild", + payload, [ - "# LoopX Artifact Collision Rebuild", - "", - f"- dry_run: `{payload.get('dry_run')}`", - f"- rebuilt: `{payload.get('rebuilt')}`", - f"- collision_groups: `{payload.get('collision_group_count')}`", - f"- total_collision_groups: `{payload.get('total_collision_group_count')}`", - f"- truncated: `{payload.get('truncated')}`", - f"- review_required: `{payload.get('review_required')}`", - f"- plan_sha256: `{plan.get('plan_sha256')}`", - "- destructive_row_deletion: `False`", - "- contract: `review the exact plan, then execute with --review-plan-json`", - ] + ("dry_run", payload.get("dry_run")), + ("rebuilt", payload.get("rebuilt")), + ("collision_groups", payload.get("collision_group_count")), + ("total_collision_groups", payload.get("total_collision_group_count")), + ("truncated", payload.get("truncated")), + ("review_required", payload.get("review_required")), + ("plan_sha256", plan.get("plan_sha256")), + ("destructive_row_deletion", False), + ("contract", "review the exact plan, then execute with --review-plan-json"), + ], ) + return lines if isinstance(lines, str) else "\n".join(lines) -def render_index_duplicate_repair_markdown(payload: dict[str, Any]) -> str: - if not payload.get("ok"): - return "# LoopX Index Duplicate Repair\n\n- ok: `False`\n- error: " + str(payload.get("error")) - - lines = [ - "# LoopX Index Duplicate Repair", - "", - f"- dry_run: `{payload.get('dry_run')}`", - f"- repaired: `{payload.get('repaired')}`", - f"- runtime_root: `{payload.get('runtime_root')}`", - f"- registry: `{payload.get('registry')}`", - f"- goal_filter: `{payload.get('goal_filter')}`", - f"- checked_goals: `{payload.get('checked_goal_count')}`", - f"- raw_index_records: `{payload.get('raw_index_records')}`", - f"- removed_rows: `{payload.get('removed_row_count')}`", - f"- preserved_reward_overlay_rows: `{payload.get('preserved_reward_overlay_rows')}`", - f"- unrepaired_groups: `{payload.get('unrepaired_group_count')}`", - f"- truncated: `{payload.get('truncated')}`", - "", - markdown_table_row( - ["goal", "generated_at", "action", "repairable", "kept", "removed", "reason"] - ), - markdown_table_separator(7), - ] - for group in payload.get("groups") or []: - lines.append( - markdown_table_row( - [ - markdown_code(group.get("goal_id")), - markdown_code(group.get("generated_at")), - markdown_code(group.get("action")), - markdown_code(group.get("repairable")), - markdown_code(group.get("kept_line_numbers")), - markdown_code(group.get("removed_line_numbers")), - group.get("reason"), - ] +def render_index_duplicate_repair_markdown(payload: Mapping[str, object]) -> str: + rendered = _markdown_report_lines( + "LoopX Index Duplicate Repair", + payload, + [ + (label, payload.get(key)) + for label, key in ( + ("dry_run", "dry_run"), ("repaired", "repaired"), ("runtime_root", "runtime_root"), + ("registry", "registry"), ("goal_filter", "goal_filter"), ("checked_goals", "checked_goal_count"), + ("raw_index_records", "raw_index_records"), ("removed_rows", "removed_row_count"), + ("preserved_reward_overlay_rows", "preserved_reward_overlay_rows"), + ("unrepaired_groups", "unrepaired_group_count"), ("truncated", "truncated"), ) - ) - return "\n".join(lines) + ], + columns=("goal", "generated_at", "action", "repairable", "kept", "removed", "reason"), + rows=( + ( + markdown_code(group.get("goal_id")), markdown_code(group.get("generated_at")), + markdown_code(group.get("action")), markdown_code(group.get("repairable")), + markdown_code(group.get("kept_line_numbers")), markdown_code(group.get("removed_line_numbers")), + group.get("reason"), + ) + for group in _mapping_list(payload.get("groups")) + ), + ) + return rendered if isinstance(rendered, str) else "\n".join(rendered) -def render_history_markdown(payload: dict[str, Any]) -> str: - if not payload.get("ok"): - return "# LoopX Run History\n\n- ok: `False`\n- error: " + str(payload.get("error")) - - lines = [ - "# LoopX Run History", - "", - f"- runtime_root: `{payload.get('runtime_root')}`", - f"- registry: `{payload.get('registry')}`", - f"- goal_filter: `{payload.get('goal_filter')}`", - f"- goals: `{payload.get('goal_count')}`", - f"- unique_runs: `{payload.get('run_count')}`", - ] - unreadable = sum( - int(goal.get("unreadable_index_rows") or 0) for goal in payload.get("goals") or [] +def render_history_markdown(payload: Mapping[str, object]) -> str: + goals = _mapping_list(payload.get("goals")) + unreadable = sum(_int_field(goal, "unreadable_index_rows") for goal in goals) + lines = _markdown_report_lines( + "LoopX Run History", + payload, + [ + (label, payload.get(key)) + for label, key in ( + ("runtime_root", "runtime_root"), ("registry", "registry"), + ("goal_filter", "goal_filter"), ("goals", "goal_count"), ("unique_runs", "run_count"), + ) + ], + columns=("generated_at", "goal", "classification", "files", "action"), + rows=( + ( + markdown_code(run.get("generated_at")), markdown_code(run.get("goal_id")), + markdown_code(run.get("classification")), + f"{run.get('json_exists')}/{run.get('markdown_exists')}", run.get("recommended_action"), + ) + for run in _mapping_list(payload.get("runs")) + ), ) + if isinstance(lines, str): + return lines if unreadable: - lines.append( + lines.insert( + 7, f"- WARNING unreadable_index_rows: `{unreadable}` " - "(rows that could not be decoded were skipped; the listing below is incomplete)" - ) - lines += [ - "", - markdown_table_row(["generated_at", "goal", "classification", "files", "action"]), - markdown_table_separator(5), - ] - for run in payload.get("runs") or []: - lines.append( - markdown_table_row( - [ - markdown_code(run.get("generated_at")), - markdown_code(run.get("goal_id")), - markdown_code(run.get("classification")), - f"{run.get('json_exists')}/{run.get('markdown_exists')}", - run.get("recommended_action"), - ] - ) + "(rows that could not be decoded were skipped; the listing below is incomplete)", ) - lines.extend(["", "## Goals"]) - for goal in payload.get("goals") or []: + for goal in goals: + unreadable_suffix = ( + f", unreadable_index_rows=`{goal.get('unreadable_index_rows')}`" + if goal.get("unreadable_index_rows") else "" + ) lines.append( - "- " - f"`{goal.get('id')}`: status=`{goal.get('status')}`, " + f"- `{goal.get('id')}`: status=`{goal.get('status')}`, " f"registry_member=`{goal.get('registry_member')}`, " f"adapter=`{goal.get('adapter_kind')}:{goal.get('adapter_status')}`, " f"index_exists=`{goal.get('index_exists')}`, " f"records=`{goal.get('raw_index_records')}`, " - f"unique_runs=`{goal.get('unique_runs')}`" - + ( - f", unreadable_index_rows=`{goal.get('unreadable_index_rows')}`" - if goal.get("unreadable_index_rows") - else "" - ) + f"unique_runs=`{goal.get('unique_runs')}`{unreadable_suffix}" ) return "\n".join(lines) -def render_index_duplicate_inspection_markdown(payload: dict[str, Any]) -> str: - if not payload.get("ok"): - return "# LoopX Index Duplicate Inspection\n\n- ok: `False`\n- error: " + str(payload.get("error")) - - lines = [ - "# LoopX Index Duplicate Inspection", - "", - f"- runtime_root: `{payload.get('runtime_root')}`", - f"- registry: `{payload.get('registry')}`", - f"- goal_filter: `{payload.get('goal_filter')}`", - f"- checked_goals: `{payload.get('checked_goal_count')}`", - f"- raw_index_records: `{payload.get('raw_index_records')}`", - f"- duplicate_groups: `{payload.get('duplicate_group_count')}`", - f"- duplicate_rows: `{payload.get('duplicate_row_count')}`", - f"- truncated: `{payload.get('truncated')}`", - "", - markdown_table_row( - ["goal", "generated_at", "kind", "severity", "rows", "classifications", "repair_hint"] - ), - markdown_table_separator(7), - ] - for group in payload.get("groups") or []: - classifications = ", ".join(str(item) for item in group.get("classifications") or []) - lines.append( - markdown_table_row( - [ - markdown_code(group.get("goal_id")), - markdown_code(group.get("generated_at")), - markdown_code(group.get("duplicate_kind")), - markdown_code(group.get("severity")), - markdown_code(group.get("line_numbers")), - markdown_code(classifications), - group.get("repair_hint"), - ] - ) +def render_index_duplicate_inspection_markdown(payload: Mapping[str, object]) -> str: + def row(group: Mapping[str, object]) -> tuple[object, ...]: + raw_classes = group.get("classifications") + class_items = ( + raw_classes + if isinstance(raw_classes, Sequence) and not isinstance(raw_classes, (str, bytes)) + else [] ) - return "\n".join(lines) + return ( + markdown_code(group.get("goal_id")), markdown_code(group.get("generated_at")), + markdown_code(group.get("duplicate_kind")), markdown_code(group.get("severity")), + markdown_code(group.get("line_numbers")), + markdown_code(", ".join(str(item) for item in class_items)), + group.get("repair_hint"), + ) + + rendered = _markdown_report_lines( + "LoopX Index Duplicate Inspection", + payload, + [ + (label, payload.get(key)) + for label, key in ( + ("runtime_root", "runtime_root"), ("registry", "registry"), ("goal_filter", "goal_filter"), + ("checked_goals", "checked_goal_count"), ("raw_index_records", "raw_index_records"), + ("duplicate_groups", "duplicate_group_count"), ("duplicate_rows", "duplicate_row_count"), + ("truncated", "truncated"), + ) + ], + columns=("goal", "generated_at", "kind", "severity", "rows", "classifications", "repair_hint"), + rows=(row(group) for group in _mapping_list(payload.get("groups"))), + ) + return rendered if isinstance(rendered, str) else "\n".join(rendered) diff --git a/tests/canary/test_maintainability_ratchet.py b/tests/canary/test_maintainability_ratchet.py index 6e8f1a6a56..af9c89d6e6 100644 --- a/tests/canary/test_maintainability_ratchet.py +++ b/tests/canary/test_maintainability_ratchet.py @@ -46,7 +46,7 @@ def test_current_repository_debt_is_reviewed_without_line_count_pins() -> None: baseline = module_metric_baseline( REPOSITORY_ROOT / "loopx" / "canary" / "module_metric_baseline.json" ) - assert "lines" not in baseline["loopx/extensions/lark/goal_topic_runtime.py"] + assert "lines" not in baseline["loopx/canary/planner.py"] assert baseline["loopx/heartbeat_prompt.py"]["lines"] == 1199 for relative_path, ceilings in baseline.items(): if ceilings.get("lines", MODULE_LINE_LIMIT) > MODULE_LINE_LIMIT: diff --git a/tests/control_plane/test_contract_health.py b/tests/control_plane/test_contract_health.py index 6cfe65c3de..533cef355a 100644 --- a/tests/control_plane/test_contract_health.py +++ b/tests/control_plane/test_contract_health.py @@ -7,7 +7,6 @@ from canonical_authority_fixture import initialize_canonical_source_fixture from loopx.cli_runtime import report_json_text -from loopx.paths import display_report_text from loopx.contract import check_contract, display_report_value from loopx.control_plane.goals.contract_health import ( contract_error_diagnostic,