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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions loopx/agent_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,13 @@ def agent_profile_for_goal(goal: dict[str, Any] | None, agent_id: str | None) ->


def load_goal_from_registry(registry_path: Path, goal_id: str) -> dict[str, Any] | None:
from .control_plane.hook_runtime.request_scope import current_hook_request_scope
from .history import load_registry
from .registry import registry_goals

scope = current_hook_request_scope()
if scope is not None:
return scope.load_goal(registry_path, str(goal_id), loader=load_registry)
if not registry_path.exists():
return None
registry = load_registry(registry_path)
Expand Down
17 changes: 8 additions & 9 deletions loopx/capabilities/explore/turn_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TurnStartHookRegistration,
TurnStartContextProjector,
dispatch_turn_start_hooks,
merge_turn_start_dispatch,
)
from ...control_plane.effect_runtime import effect_runtime_request_scope, effect_runtime_result
from ...explore_graph import compact_explore_graph_policy
Expand Down Expand Up @@ -296,14 +297,7 @@ def produce() -> dict[str, Any]:
},
)
extra = dispatch_turn_start_hooks((hook,))
result = dict(dispatch or {})
for key in ("results", "required_reads", "failures", "contexts"):
if key == "contexts" and not extra.get(key):
continue
result[key] = list(result.get(key) or []) + list(extra.get(key) or [])
for key in ("registered_count", "invoked_count"):
result[key] = int(result.get(key) or 0) + int(extra.get(key) or 0)
return result
return merge_turn_start_dispatch(dispatch, extra, include_contexts=True)


def project_settlement_attachment(plan: dict[str, Any], *, registry_path: Path, runtime_root: Path | None = None) -> dict[str, Any]:
Expand Down Expand Up @@ -333,7 +327,12 @@ def produce() -> dict[str, Any]:
projection_slots=("explore_result_attachment",),
requested_read_scope=("goal.explore_policy",), producer=produce,
),))
attachment = dispatch["projections"].get("explore_result_attachment")
projections = dispatch.get("projections")
attachment = (
projections.get("explore_result_attachment")
if isinstance(projections, Mapping)
else None
)
if attachment is None:
return ({**plan, "capability_hook_failures": dispatch["failures"]}
if dispatch["failures"] else plan)
Expand Down
19 changes: 11 additions & 8 deletions loopx/capabilities/periodic_report/cadence_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

from ...agent_registry import registered_agent_ids_for_goal
from ...control_plane.capability_hooks import (
TURN_START_HOOK_RESULT_SCHEMA_VERSION, TurnStartHookRegistration, dispatch_turn_start_hooks,
TURN_START_HOOK_RESULT_SCHEMA_VERSION,
TurnStartHookRegistration,
dispatch_turn_start_hooks,
load_scoped_registry,
merge_turn_start_dispatch,
)
from ...history import load_registry
from ...registry import find_registry_goal
Expand All @@ -26,7 +30,10 @@ def periodic_report_cadence_hooks(
) -> tuple[TurnStartHookRegistration, ...]:
if not agent_id:
return ()
goal = find_registry_goal(load_registry(registry_path), goal_id)
goal = find_registry_goal(
load_scoped_registry(registry_path, loader=load_registry),
goal_id,
)
subscription = resolve_active_goal_periodic_report_subscription(
goal, read_periodic_report_machine_defaults(runtime_root))
if subscription is None or subscription.get("schedule") is None:
Expand Down Expand Up @@ -116,14 +123,10 @@ def resolve_current_subscription():

def extend_cadence_turn_start_dispatch(dispatch: dict, **kwargs) -> dict:
"""Same optional composition for quota and the managed turn driver."""
extra: dict[str, object]
try:
extra = dispatch_turn_start_hooks(periodic_report_cadence_hooks(**kwargs))
except (OSError, TypeError, ValueError):
extra = {"failures": [{"hook_id": "periodic_report.cadence",
"capability_id": "periodic-report", "error_code": "cadence_configuration_unavailable"}]}
result = dict(dispatch)
for key in ("results", "required_reads", "failures"):
result[key] = list(dispatch.get(key) or []) + list(extra.get(key) or [])
for key in ("registered_count", "invoked_count"):
result[key] = int(dispatch.get(key) or 0) + int(extra.get(key) or 0)
return result
return merge_turn_start_dispatch(dispatch, extra, include_contexts=False)
39 changes: 21 additions & 18 deletions loopx/capabilities/turn_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from pathlib import Path
from typing import Any

from ..control_plane.capability_hooks import dispatch_turn_start_hooks
from ..control_plane.capability_hooks import (
capability_hook_request_scope,
dispatch_turn_start_hooks,
merge_turn_start_dispatch,
)
from ..control_plane.todos.contract import normalize_required_capabilities


Expand All @@ -21,23 +25,22 @@ def extend_turn_start_dispatch(dispatch: dict[str, Any], *, registry_path: Path,
factories = [("issue-fix", dependencies)]
if normalize_required_capabilities(available):
factories.append(("agent-capability-memory", capability_memory))
registrations = [hook for capability, factory in factories if (hook := factory(
registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
agent_id=agent_id, available=available,
**(provider_options or {}).get(capability, {}),
)) is not None]
if not registrations:
return dispatch
extra = dispatch_turn_start_hooks(registrations)
result = dict(dispatch)
for key in ("results", "required_reads", "failures", "contexts"):
if extra.get(key):
result[key] = list(dispatch.get(key) or []) + list(extra[key])
for key in ("registered_count", "invoked_count"):
result[key] = int(dispatch.get(key) or 0) + int(extra.get(key) or 0)
if extra.get("uncertain_local_effects"):
result["uncertain_local_effects"] = True
return result
with capability_hook_request_scope():
registrations = [hook for capability, factory in factories if (hook := factory(
registry_path=registry_path, runtime_root=runtime_root, goal_id=goal_id,
agent_id=agent_id, available=available,
**(provider_options or {}).get(capability, {}),
)) is not None]
if not registrations:
return dispatch
extra = dispatch_turn_start_hooks(registrations)
return merge_turn_start_dispatch(
dispatch,
extra,
include_contexts=True,
only_nonempty_extra=True,
include_uncertain=True,
)


def attach_turn_start_effects(payload: dict[str, Any], dispatch: dict[str, Any]) -> None:
Expand Down
140 changes: 73 additions & 67 deletions loopx/cli_commands/quota_should_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,85 +71,91 @@ def dispatch_quota_turn_start_hooks(
preview = bool(getattr(args, "dry_run", False))
if preview and not args.agent_id:
return None, False
dispatch: dict[str, object] = {}
if not preview and lark_turn_start_hook_dispatcher is not None:
dispatch = lark_turn_start_hook_dispatcher(
registry_path=registry_path,
runtime_root_arg=runtime_root_arg,
goal_id=args.goal_id,
agent_id=args.agent_id,
)
if args.agent_id:
from ..capabilities.manager_context import turn_start_hook
from ..capabilities.semantic_preference.agent_preferences import (
extend_turn_start_dispatch as extend_preferences,
)
from ..control_plane.capability_hooks import dispatch_turn_start_hooks
from ..history import load_registry
from ..paths import resolve_runtime_root

root = resolve_runtime_root(
load_registry(registry_path),
runtime_root_arg,
registry_path=registry_path,
)
if not preview:
from ..capabilities.turn_start import (
extend_turn_start_dispatch as extend_registered_capabilities,
)
from ..control_plane.capability_hooks import (
capability_hook_request_scope,
dispatch_turn_start_hooks,
load_scoped_registry,
)

dispatch = extend_registered_capabilities(
dispatch,
dispatch: dict[str, object] = {}
with capability_hook_request_scope():
if not preview and lark_turn_start_hook_dispatcher is not None:
dispatch = lark_turn_start_hook_dispatcher(
registry_path=registry_path,
runtime_root=root,
runtime_root_arg=runtime_root_arg,
goal_id=args.goal_id,
agent_id=args.agent_id,
available=args.available_capabilities,
)
# A preview still needs the current local Inbox and preferences. These
# hooks observe existing context without recording reads or delivery.
model_read = not getattr(args, "scheduler_observation", False)
context_dispatch = dispatch_turn_start_hooks(
(
turn_start_hook(
root,
registry_path,
args.goal_id,
args.agent_id,
model_read=model_read,
),
)
)
context_dispatch = extend_preferences(
context_dispatch,
runtime_root=root,
registry_path=registry_path,
goal_id=args.goal_id,
agent_id=args.agent_id,
)
dispatch = dict(dispatch)
for key in ("results", "required_reads", "failures", "contexts"):
if key == "contexts" and not context_dispatch.get(key):
continue
dispatch[key] = list(dispatch.get(key) or []) + list(
context_dispatch.get(key) or []
)
for key in ("registered_count", "invoked_count"):
dispatch[key] = int(dispatch.get(key) or 0) + int(
context_dispatch.get(key) or 0
)
if not preview:
from ..capabilities.periodic_report.cadence_runtime import (
extend_cadence_turn_start_dispatch,
if args.agent_id:
from ..capabilities.manager_context import turn_start_hook
from ..capabilities.semantic_preference.agent_preferences import (
extend_turn_start_dispatch as extend_preferences,
)
from ..history import load_registry
from ..paths import resolve_runtime_root

dispatch = extend_cadence_turn_start_dispatch(
dispatch,
root = resolve_runtime_root(
load_scoped_registry(registry_path, loader=load_registry),
runtime_root_arg,
registry_path=registry_path,
)
if not preview:
from ..capabilities.turn_start import (
extend_turn_start_dispatch as extend_registered_capabilities,
)

dispatch = extend_registered_capabilities(
dispatch,
registry_path=registry_path,
runtime_root=root,
goal_id=args.goal_id,
agent_id=args.agent_id,
available=args.available_capabilities,
)
# A preview still needs the current local Inbox and preferences. These
# hooks observe existing context without recording reads or delivery.
model_read = not getattr(args, "scheduler_observation", False)
context_dispatch = dispatch_turn_start_hooks(
(
turn_start_hook(
root,
registry_path,
args.goal_id,
args.agent_id,
model_read=model_read,
),
)
)
context_dispatch = extend_preferences(
context_dispatch,
runtime_root=root,
registry_path=registry_path,
goal_id=args.goal_id,
agent_id=args.agent_id,
)
dispatch = dict(dispatch)
for key in ("results", "required_reads", "failures", "contexts"):
if key == "contexts" and not context_dispatch.get(key):
continue
dispatch[key] = list(dispatch.get(key) or []) + list(
context_dispatch.get(key) or []
)
for key in ("registered_count", "invoked_count"):
dispatch[key] = int(dispatch.get(key) or 0) + int(
context_dispatch.get(key) or 0
)
if not preview:
from ..capabilities.periodic_report.cadence_runtime import (
extend_cadence_turn_start_dispatch,
)

dispatch = extend_cadence_turn_start_dispatch(
dispatch,
registry_path=registry_path,
runtime_root=root,
goal_id=args.goal_id,
agent_id=args.agent_id,
)
local_private_state_mutated = bool(dispatch.get("uncertain_local_effects")) or any(
isinstance(result, Mapping)
and result.get("local_private_state_mutated") is not False
Expand Down
9 changes: 2 additions & 7 deletions loopx/control_plane/agents/capability_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def extend_turn_start_dispatch(
normalized = normalize_required_capabilities(available)
if not agent_id or not normalized:
return dispatch
from ..capability_hooks import dispatch_turn_start_hooks
from ..capability_hooks import dispatch_turn_start_hooks, merge_turn_start_dispatch

extra = dispatch_turn_start_hooks(
(
Expand All @@ -235,12 +235,7 @@ def extend_turn_start_dispatch(
),
)
)
result = dict(dispatch)
for key in ("results", "required_reads", "failures"):
result[key] = list(dispatch.get(key) or []) + list(extra.get(key) or [])
for key in ("registered_count", "invoked_count"):
result[key] = int(dispatch.get(key) or 0) + int(extra.get(key) or 0)
return result
return merge_turn_start_dispatch(dispatch, extra, include_contexts=False)


def heartbeat_evidence_poll_possible(
Expand Down
Loading
Loading