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
183 changes: 165 additions & 18 deletions loopx/capabilities/native_chat/github_pr_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,30 +378,177 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]:
return summary


_GITHUB_PR_LIST_PAGE_SIZE = 20
_GITHUB_PR_LIST_MAX_PAGES = 5
_GITHUB_OPEN_PRS_GRAPHQL_QUERY = """query($owner: String!, $name: String!, $cursor: String) {
repository(owner: $owner, name: $name) {
pullRequests(
states: OPEN,
first: 20,
after: $cursor,
orderBy: {field: UPDATED_AT, direction: DESC}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
title
url
headRefOid
headRefName
baseRefName
isDraft
reviewDecision
mergeStateStatus
mergeable
updatedAt
author {
login
}
reviews(last: 20) {
nodes {
id
state
submittedAt
body
author {
login
}
commit {
oid
}
}
}
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
contexts(first: 30) {
nodes {
__typename
... on CheckRun {
name
status
conclusion
}
... on StatusContext {
context
state
}
}
}
}
}
}
}
}
}
}
}"""


def _normalize_graphql_pr_node(node: Mapping[str, Any]) -> dict[str, Any]:
raw_reviews = node.get("reviews")
if isinstance(raw_reviews, Mapping) and isinstance(raw_reviews.get("nodes"), list):
reviews = [item for item in raw_reviews["nodes"] if isinstance(item, Mapping)]
elif isinstance(raw_reviews, list):
reviews = [item for item in raw_reviews if isinstance(item, Mapping)]
else:
reviews = []

rollup = node.get("statusCheckRollup")
if rollup is None:
raw_commits = node.get("commits")
commit_nodes = raw_commits.get("nodes") if isinstance(raw_commits, Mapping) else None
first_commit = (
commit_nodes[0].get("commit")
if isinstance(commit_nodes, list) and commit_nodes and isinstance(commit_nodes[0], Mapping)
else None
)
raw_rollup = first_commit.get("statusCheckRollup") if isinstance(first_commit, Mapping) else None
raw_contexts = raw_rollup.get("contexts") if isinstance(raw_rollup, Mapping) else None
context_nodes = raw_contexts.get("nodes") if isinstance(raw_contexts, Mapping) else None
rollup = [item for item in context_nodes if isinstance(item, Mapping)] if isinstance(context_nodes, list) else []

return {
"number": node.get("number"),
"title": node.get("title"),
"url": node.get("url"),
"headRefOid": node.get("headRefOid"),
"headRefName": node.get("headRefName"),
"baseRefName": node.get("baseRefName"),
"isDraft": node.get("isDraft"),
"author": node.get("author"),
"reviewDecision": node.get("reviewDecision"),
"mergeStateStatus": node.get("mergeStateStatus"),
"mergeable": node.get("mergeable"),
"updatedAt": node.get("updatedAt"),
"reviews": reviews,
"statusCheckRollup": rollup,
}


def observe_github_repository_prs(
repo_slug: str, *, runner: Callable[..., Any] = subprocess.run
) -> dict[str, Any]:
"""Read bounded public/authorized open PR signal metadata without side effects."""
normalized = normalize_github_repo_slug(repo_slug)
if not normalized or "/" not in normalized:
return {"status": "unavailable", "repository": repo_slug}
owner, name = normalized.split("/", 1)
by_number: dict[int, dict[str, Any]] = {}
cursor: str | None = None
try:
completed = runner(
[
"gh", "pr", "list", "--repo", repo_slug, "--state", "open",
"--limit", "100", "--json", GITHUB_PR_LIST_FIELDS,
],
capture_output=True, text=True, check=False, timeout=15,
)
if completed.returncode != 0:
return {"status": "unavailable", "repository": repo_slug}
rows = json.loads(completed.stdout or "[]")
if not isinstance(rows, list):
return {"status": "unavailable", "repository": repo_slug}
items = [
summarize_github_pr_signal(row)
for row in sorted(
(r for r in rows if isinstance(r, Mapping)),
key=lambda r: int(r.get("number") or 0),
for page_index in range(_GITHUB_PR_LIST_MAX_PAGES):
cmd = [
"gh",
"api",
"graphql",
"-f",
f"query={_GITHUB_OPEN_PRS_GRAPHQL_QUERY}",
"-f",
f"owner={owner}",
"-f",
f"name={name}",
]
if cursor is not None:
cmd.extend(["-f", f"cursor={cursor}"])
completed = runner(
cmd,
capture_output=True,
text=True,
check=False,
timeout=20,
)
]
if completed.returncode != 0:
return {"status": "unavailable", "repository": repo_slug}
payload = json.loads(completed.stdout or "{}")
if not isinstance(payload, Mapping) or payload.get("errors"):
return {"status": "unavailable", "repository": repo_slug}
data = payload.get("data")
repo = data.get("repository") if isinstance(data, Mapping) else None
pull_requests = repo.get("pullRequests") if isinstance(repo, Mapping) else None
nodes = pull_requests.get("nodes") if isinstance(pull_requests, Mapping) else None
if not isinstance(nodes, list):
return {"status": "unavailable", "repository": repo_slug}
for node in nodes:
if not isinstance(node, Mapping):
continue
number = int(node.get("number") or 0)
if number > 0:
by_number[number] = summarize_github_pr_signal(_normalize_graphql_pr_node(node))
page_info = pull_requests.get("pageInfo")
if not isinstance(page_info, Mapping) or page_info.get("hasNextPage") is not True:
break
next_cursor = str(page_info.get("endCursor") or "").strip()
if not next_cursor or next_cursor == cursor:
if page_index < _GITHUB_PR_LIST_MAX_PAGES - 1:
return {"status": "unavailable", "repository": repo_slug}
break
cursor = next_cursor
items = [by_number[num] for num in sorted(by_number)]
return {"status": "ok", "repository": repo_slug, "items": items}
except (OSError, ValueError, TypeError, subprocess.TimeoutExpired):
return {"status": "unavailable", "repository": repo_slug}
Expand Down
36 changes: 27 additions & 9 deletions loopx/capabilities/native_chat/steward_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,30 @@ def pump_steward_monitors(controller: Any, *, cancelled=lambda: False) -> list[d
_LOG.warning("An original steward report is unavailable; its window and other work are retained")
due = service.due_monitors(actor)
if due:
todo = due[0]
accepted, created = StewardTurnHost(controller).submit(binding["binding_id"],
turn_instance_id=monitor_turn_id(todo), todo_id=todo["todo_id"], work_kind="monitor",
monitor_deadline=todo["next_due_at"])
if created:
changed.append(accepted)
continue
dispatched = False
for todo in due:
try:
accepted, created = StewardTurnHost(controller).submit(
binding["binding_id"],
turn_instance_id=monitor_turn_id(todo),
todo_id=todo["todo_id"],
work_kind="monitor",
monitor_deadline=todo["next_due_at"],
)
except (OSError, ValueError, KeyError, TypeError, RuntimeError):
_LOG.warning(
"A native steward monitor is unavailable; original work retained"
)
continue
if created:
changed.append(accepted)
dispatched = True
break
if isinstance(accepted, dict) and accepted.get("status") in {"queued", "running"}:
dispatched = True
break
if dispatched:
continue
for adv in service.runnable_advancement_todos(actor):
from .steward_turn import native_steward_client_id
turn_id_token = advancement_turn_id(adv)
Expand All @@ -473,8 +490,9 @@ def pump_steward_monitors(controller: Any, *, cancelled=lambda: False) -> list[d
if all_monitors:
soonest = min(all_monitors, key=lambda row: datetime.fromisoformat(row["next_due_at"].replace("Z", "+00:00")))
due_dt = datetime.fromisoformat(soonest["next_due_at"].replace("Z", "+00:00"))
remaining = max(0, (due_dt - datetime.now(timezone.utc)).total_seconds())
cache[binding["binding_id"]] = time.monotonic() + min(60, remaining)
remaining = (due_dt - datetime.now(timezone.utc)).total_seconds()
if remaining > 0:
cache[binding["binding_id"]] = time.monotonic() + min(60, remaining)
except (OSError, ValueError, KeyError, TypeError, RuntimeError):
# One unreadable conversation or unavailable provider must not starve
# other native monitors; retain its original data and deadline.
Expand Down
18 changes: 17 additions & 1 deletion loopx/control_plane/collaboration/steward_managed_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ function admitMonitorRestartRecovery(params: JsonObject, request: JsonObject, se
&& Array.isArray(basis.phases) && basis.phases.length === 1
&& requireJsonObject(basis.phases[0], "original watch phase").phase === "manager_context"
&& Array.isArray(events) && events.filter(kind => kind === "agent.phase").length === 1;
// A custom signal monitor may fail inside the read-only external signal
// check after manager_context completes and before any poll receipt is written.
// Allow at most one bounded recovery when zero-effect proofs hold.
let signalUnavailable = false;
if (original.error_code === "runtime_error"
&& original.error === "steward monitor external signal source temporarily unavailable"
&& requireJsonObject(prior.actor, "original watch actor").schema_version === "loopx_steward_managed_actor_v0"
&& Array.isArray(basis.contexts) && basis.contexts.length === 1
&& Array.isArray(basis.phases) && basis.phases.length === 1
&& requireJsonObject(basis.phases[0], "original watch phase").phase === "manager_context"
&& Array.isArray(events) && events.filter(kind => kind === "agent.phase").length === 1
&& Array.isArray(events) && events.filter(kind => kind === "manager.context").length === 1) {
const context = requireJsonObject(basis.contexts[0], "signal unavailable watch context");
signalUnavailable = context.schema_version === "manager_turn_context_v1";
}
if (Object.keys(recovery).join() !== "original_turn_id"
|| recovery.original_turn_id !== original.turn_id || typeof original.turn_id !== "string"
|| !/^[a-f0-9]{32}$/.test(original.turn_id) || original.session_id !== session.session_id
|| original.status !== "failed" || !(original.error_code === "server_restarted" || scopeRefused || identityRefused)
|| original.status !== "failed"
|| !(original.error_code === "server_restarted" || scopeRefused || identityRefused || signalUnavailable)
|| original.origin !== "native_loopx" || original.loopx_execution !== true
|| original.response !== null || original.upstream_turn_id !== null || original.delta_count !== 0
|| prior.work_kind !== "monitor" || request.work_kind !== "monitor" || request.validation_command !== null
Expand Down
9 changes: 6 additions & 3 deletions loopx/control_plane/quota/monitor_poll_admission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ function dueMonitorAllowed(
): boolean {
if (!facts.must_attempt_work) return false;
const candidate = decision.due_monitor_candidates.find((item) => candidateMatches(item, observation));
if (facts.obligation === "attempt_due_monitor") return Boolean(candidate);
if (facts.reason_codes.has("due_monitor_context") && candidate) return true;
const registry = decision.registry_due_monitor;
return facts.reason_codes.has("due_monitor_context") && registry.due === true &&
const registryAllowed = registry.due === true &&
registry.claimed_by === decision.agent_id && candidateMatches(registry, observation);
if (facts.obligation === "attempt_due_monitor") {
return Boolean(candidate) || (!decision.requires_user_action && registryAllowed);
}
if (facts.reason_codes.has("due_monitor_context") && candidate) return true;
return facts.reason_codes.has("due_monitor_context") && registryAllowed;
}

function monitorGateAllowsObservation(decision: MonitorDecision): boolean {
Expand Down
34 changes: 32 additions & 2 deletions loopx/extensions/lark/private_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import re
import subprocess
import time
import zlib
from collections.abc import Callable, Iterator, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
Expand Down Expand Up @@ -123,6 +124,8 @@ def __init__(self, *, controller: Any, runtime_root: Path, runner: Any, cli_bin:
self.root = controller.store.root / "lark-private-deliveries"
self._context_recovery_probe_at: dict[Path, float] = {}
self._historical_image_probe_at: dict[Path, float] = {}
self._planned_repairs_stamp: tuple[bool, int | None] | None = None
self._planned_repairs_names: frozenset[str] = frozenset()
self._delivery_discovery = PrivateDeliveryDiscovery(self.root)
self._body_discovery = PrivateDeliveryDiscovery(self.root)
self._web_projection_scan_at = 0.0
Expand Down Expand Up @@ -1525,6 +1528,27 @@ def authorized() -> bool:
_atomic_write_json(path, record)
return 1

def _planned_historical_repairs(self) -> frozenset[str]:
repairs_dir = self.root / "historical-answer-images"
try:
stamp: tuple[bool, int | None] = (True, repairs_dir.stat().st_mtime_ns)
except OSError:
stamp = (False, None)
if stamp == getattr(self, "_planned_repairs_stamp", None):
return getattr(self, "_planned_repairs_names", frozenset())
if not stamp[0]:
names: frozenset[str] = frozenset()
else:
names = frozenset(item.name for item in repairs_dir.glob("*.json"))
try:
stamp = (True, repairs_dir.stat().st_mtime_ns)
except OSError:
stamp = (False, None)
names = frozenset()
self._planned_repairs_stamp = stamp
self._planned_repairs_names = names
return names

def pending_body_paths(self) -> list[Path]:
"""Find unsent bodies without waiting for historical recovery probes."""
paths = [path for path, row, _ in self._body_discovery.rows()
Expand All @@ -1537,7 +1561,7 @@ def pending_delivery_paths(self) -> list[Path]:
paths = []
recovery_candidate = self.core.context_recovery_observer()
now = datetime.now(timezone.utc).timestamp()
planned_repairs = {item.name for item in (self.root / "historical-answer-images").glob("*.json")}
planned_repairs = self._planned_historical_repairs()
for path, row, repairs in self._delivery_discovery.rows():
if row["status"] != "delivered":
if triage.pending(row) and not _answer_image_retry_waits(row, now):
Expand All @@ -1552,8 +1576,14 @@ def pending_delivery_paths(self) -> list[Path]:
# Only a discovery/backoff hint, never execution authority.
# The canonical request, provider effect inventory and source
# are independently re-read by recover_failed_request.
self._context_recovery_probe_at[path] = time.monotonic() + 60
# Stagger steady-state ineligible re-probes across [60, 120)s by filename
# hash so cold-start bursts do not stay synchronized on the same tick,
# while keeping eligible candidates on the exact 60s retry cadence.
now_mono = time.monotonic()
stagger = zlib.crc32(path.name.encode("utf-8")) % 60
self._context_recovery_probe_at[path] = now_mono + 60 + stagger
if repairs or recovery_candidate(row["request_ref"]):
self._context_recovery_probe_at[path] = now_mono + 60
paths.append(path)
return [*paths, *self._web_projection_paths()]

Expand Down
Loading
Loading