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
21 changes: 20 additions & 1 deletion loopx/capabilities/issue_fix/pr_gate_reconcile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from collections.abc import Callable, Mapping, Sequence
from pathlib import Path
from typing import Any, Mapping
from typing import Any

from ...history import load_registry
from ...paths import resolve_runtime_root
Expand Down Expand Up @@ -193,6 +194,8 @@ def reconcile_issue_fix_pr_review(
fetch_timeout_seconds: int = 10,
execute: bool = False,
generated_at: str | None = None,
prefetched_todos: Sequence[Mapping[str, Any]] | None = None,
on_todo_mutation: Callable[[], None] | None = None,
) -> dict[str, Any]:
context = resolve_issue_fix_pr_review_context(
registry_path=registry_path,
Expand All @@ -202,6 +205,7 @@ def reconcile_issue_fix_pr_review(
project=project,
url=url,
ack_receipt=ack_receipt,
prefetched_todos=prefetched_todos,
)
context_status = str(context.get("status") or "ack_receipt_invalid")
if context_status == "current" and ack_receipt is None:
Expand Down Expand Up @@ -256,6 +260,7 @@ def reconcile_issue_fix_pr_review(
project=project,
url=url,
ack_receipt=ack_receipt,
prefetched_todos=None if fetch_metadata else prefetched_todos,
)
ack_status = str(current.get("status") or "ack_receipt_invalid")
ack_valid = ack_status == "matched"
Expand Down Expand Up @@ -335,6 +340,8 @@ def reconcile_issue_fix_pr_review(
project=project,
)
receipt["write_performed"] = bool(completion.get("changed"))
if receipt["write_performed"] and on_todo_mutation is not None:
on_todo_mutation()
receipt["todo_completion"] = {
"completed": completion.get("completed"),
"status": completion.get("status"),
Expand Down Expand Up @@ -364,6 +371,8 @@ def reconcile_acknowledged_issue_fix_pr_reviews(
fetch_timeout_seconds: int = 10,
execute: bool = False,
generated_at: str | None = None,
todo_loader: Callable[[], Sequence[Mapping[str, Any]]] | None = None,
on_todo_mutation: Callable[[], None] | None = None,
) -> dict[str, Any]:
"""Reconcile current review todos from their persisted acknowledgement bindings."""

Expand All @@ -377,6 +386,14 @@ def reconcile_acknowledged_issue_fix_pr_reviews(
goal_id=goal_id,
agent_id=agent_id,
)
prefetched_todos: Sequence[Mapping[str, Any]] | None = None
if receipts and todo_loader is not None and any(
isinstance(receipt.get("binding"), Mapping)
and str(receipt["binding"].get("todo_id") or "").strip()
and str(receipt["binding"].get("permalink") or "").strip()
for receipt in receipts
):
prefetched_todos = todo_loader()
results: list[dict[str, Any]] = []
for ack_receipt in receipts:
binding = ack_receipt.get("binding")
Expand Down Expand Up @@ -419,6 +436,8 @@ def reconcile_acknowledged_issue_fix_pr_reviews(
fetch_timeout_seconds=fetch_timeout_seconds,
execute=execute,
generated_at=generated_at,
prefetched_todos=prefetched_todos,
on_todo_mutation=on_todo_mutation,
)
except ValueError:
result = {
Expand Down
44 changes: 29 additions & 15 deletions loopx/capabilities/issue_fix/pr_review_ack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import hashlib
import json
from collections.abc import Mapping
from collections.abc import Mapping, Sequence
from pathlib import Path
from typing import Any

Expand All @@ -16,6 +16,7 @@
from ...rollout_event_log import (
append_rollout_event_once,
build_rollout_event,
iter_reverse_rollout_events_by_kind,
load_rollout_events,
rollout_event_log_path,
)
Expand Down Expand Up @@ -66,15 +67,27 @@ def _require_exact_review_todo(
todo_id: str,
agent_id: str,
project: Path | None,
prefetched_todos: Sequence[Mapping[str, Any]] | None = None,
) -> dict[str, Any]:
todo_payload = list_goal_todos(
registry_path=registry_path,
goal_id=goal_id,
role="user",
todo_id=todo_id,
project=project,
)
todo = todo_payload.get("todo")
todo: dict[str, Any] | None = None
if prefetched_todos is not None:
for candidate in prefetched_todos:
if (
candidate.get("todo_id") == todo_id
and candidate.get("role") in (None, "user")
):
todo = dict(candidate)
break
if todo is None:
todo_payload = list_goal_todos(
registry_path=registry_path,
goal_id=goal_id,
role="user",
todo_id=todo_id,
project=project,
)
raw_todo = todo_payload.get("todo")
todo = raw_todo if isinstance(raw_todo, dict) else None
if not isinstance(todo, dict):
raise ValueError(f"user todo {todo_id!r} was not found")
if todo_item_task_class(todo) != TODO_TASK_CLASS_USER_ACTION:
Expand Down Expand Up @@ -115,6 +128,7 @@ def resolve_issue_fix_pr_review_context(
project: Path | None,
url: str,
ack_receipt: Mapping[str, Any] | None = None,
prefetched_todos: Sequence[Mapping[str, Any]] | None = None,
) -> dict[str, Any]:
try:
todo = _require_exact_review_todo(
Expand All @@ -123,6 +137,7 @@ def resolve_issue_fix_pr_review_context(
todo_id=todo_id,
agent_id=agent_id,
project=project,
prefetched_todos=prefetched_todos,
)
except ValueError as exc:
return {
Expand Down Expand Up @@ -364,12 +379,11 @@ def load_issue_fix_pr_review_ack_receipts(
) -> list[dict[str, Any]]:
receipts: list[dict[str, Any]] = []
seen_todos: set[str] = set()
events = load_rollout_events(rollout_event_log_path(runtime_root, goal_id))
for event in reversed(events):
if (
event.get("event_kind") != PR_REVIEW_ACK_EVENT_KIND
or str(event.get("agent_id") or "") != agent_id
):
for event in iter_reverse_rollout_events_by_kind(
rollout_event_log_path(runtime_root, goal_id),
event_kind=PR_REVIEW_ACK_EVENT_KIND,
):
if str(event.get("agent_id") or "") != agent_id:
continue
todo_id = str(event.get("todo_id") or "")
if not todo_id or todo_id in seen_todos:
Expand Down
6 changes: 4 additions & 2 deletions loopx/capabilities/issue_fix/pr_wait_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def _availability(registry_path: Path, runtime_root: Path, goal_id: str,

def turn_start_hook(*, registry_path: Path, runtime_root: Path, goal_id: str,
agent_id: str, available: Any,
fetch_timeout_seconds: int = 10) -> TurnStartHookRegistration | None:
fetch_timeout_seconds: int = 10,
shared_agent_todos_loader: Any = None) -> TurnStartHookRegistration | None:
# The existing typed owner resolves declarations, remembered host observations
# and revocation. A Todo requirement is never a capability grant.
try:
Expand Down Expand Up @@ -95,7 +96,8 @@ def produce() -> dict[str, Any]:

readback = reconcile_pr_wait_dependencies(registry_path=registry_path,
runtime_root_arg=str(runtime_root), goal_id=goal_id, agent_id=agent_id,
fetch_timeout_seconds=fetch_timeout_seconds)
fetch_timeout_seconds=fetch_timeout_seconds,
shared_agent_todos_loader=shared_agent_todos_loader)
count = readback["external_read_count"]
written = count > 0
status = "partial" if readback.get("degraded") else "observed" if count else "empty"
Expand Down
19 changes: 16 additions & 3 deletions loopx/capabilities/issue_fix/pr_wait_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from __future__ import annotations

from collections.abc import Callable, Mapping, Sequence
from pathlib import Path
from subprocess import TimeoutExpired
from typing import Any
Expand All @@ -27,6 +28,7 @@
def reconcile_pr_wait_dependencies(
*, registry_path: Path, runtime_root_arg: str | None, goal_id: str,
agent_id: str, fetch_timeout_seconds: int = 10,
shared_agent_todos_loader: Callable[[], Sequence[Mapping[str, Any]] | None] | None = None,
) -> dict[str, Any]:
if agent_id not in registered_agent_ids_from_registry(registry_path, goal_id):
raise ValueError("PR dependency observation requires a registered agent")
Expand All @@ -40,7 +42,8 @@ def reconcile_pr_wait_dependencies(
policy=LockAcquisitionPolicy.SINGLE_FLIGHT,
agent_id=agent_id, operation="pr_wait_observation"):
return _reconcile_locked(registry_path=registry_path, runtime_root_arg=str(runtime),
goal_id=goal_id, agent_id=agent_id, fetch_timeout_seconds=fetch_timeout_seconds)
goal_id=goal_id, agent_id=agent_id, fetch_timeout_seconds=fetch_timeout_seconds,
shared_agent_todos_loader=shared_agent_todos_loader)
except LockAcquireTimeoutError:
return {"ok": True, "degraded": False, "failure_count": 0, "external_read_count": 0,
"observation_in_progress": True, "merged_count": 0, "results": [],
Expand All @@ -50,13 +53,23 @@ def reconcile_pr_wait_dependencies(
def _reconcile_locked(
*, registry_path: Path, runtime_root_arg: str | None, goal_id: str,
agent_id: str, fetch_timeout_seconds: int,
shared_agent_todos_loader: Callable[[], Sequence[Mapping[str, Any]] | None] | None = None,
) -> dict[str, Any]:
runtime = (Path(runtime_root_arg).expanduser() if runtime_root_arg
else resolve_runtime_root(load_registry(registry_path), None))
# Deliberately omit list --agent-id and display limits. The typed owner must
# see archived/deferred/claimed facts before choosing this actor's subjects.
todos = list_goal_todos(registry_path=registry_path, runtime_root_arg=str(runtime),
goal_id=goal_id, role="agent")["todos"]
prefetched = shared_agent_todos_loader() if shared_agent_todos_loader is not None else None
todos = (
[dict(item) for item in prefetched]
if prefetched is not None
else list_goal_todos(
registry_path=registry_path,
runtime_root_arg=str(runtime),
goal_id=goal_id,
role="agent",
)["todos"]
)
compact = compact_todo_resume_items(todos)
for row, original in zip(compact, todos, strict=True):
if "excluded_agents" in original:
Expand Down
Loading
Loading