From fb1c7f6ecf5a393fbe5aa447884aa860f71bae37 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:52:51 +0800 Subject: [PATCH 1/3] feat(steward): refine stalled PR signal clock, author-reply bypass, and bucket wakeups Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/github_pr_evidence.py | 153 ++++++++++++++++- .../capabilities/native_chat/steward_turn.py | 2 +- .../collaboration/steward_managed_mode.ts | 49 +++++- .../steward_managed_mode.test.ts | 155 ++++++++++++++++++ ...test_steward_pr_merge_and_review_signal.py | 153 +++++++++++++++++ 5 files changed, 500 insertions(+), 12 deletions(-) diff --git a/loopx/capabilities/native_chat/github_pr_evidence.py b/loopx/capabilities/native_chat/github_pr_evidence.py index 94834bfdc2..7aafc696ba 100644 --- a/loopx/capabilities/native_chat/github_pr_evidence.py +++ b/loopx/capabilities/native_chat/github_pr_evidence.py @@ -3,6 +3,7 @@ from __future__ import annotations import configparser +from datetime import datetime, timezone import json from pathlib import Path import re @@ -41,6 +42,30 @@ {"FAILURE", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE"} ) _CHECK_PASS_CONCLUSIONS = frozenset({"SUCCESS", "NEUTRAL", "SKIPPED"}) +_KNOWN_BOT_LOGINS = frozenset( + {"mergify", "codecov", "codecov-commenter", "github-actions", "dependabot"} +) + + +def _parse_iso_timestamp(value: Any) -> tuple[datetime, str] | None: + raw = str(value or "").strip() + if not raw: + return None + normalized = f"{raw[:-1]}+00:00" if raw.endswith("Z") else raw + try: + dt = datetime.fromisoformat(normalized) + except ValueError: + return None + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + else: + dt = dt.astimezone(timezone.utc) + return dt, raw + + +def _is_bot_login(login: str) -> bool: + cleaned = str(login or "").strip().casefold() + return not cleaned or cleaned.endswith("[bot]") or cleaned in _KNOWN_BOT_LOGINS def normalize_github_repo_slug(value: Any) -> str | None: @@ -275,6 +300,70 @@ def _actor_actionable_verdict(record: Mapping[str, Any]) -> str | None: return None +def _summarize_request_changes_stall( + row: Mapping[str, Any], + *, + raw_reviews: Sequence[Mapping[str, Any]], + latest_record_by_actor: Mapping[tuple[str, str], tuple[str, dict[str, Any]]], + head_oid: str, + pr_author: str, +) -> tuple[str | None, str | None, bool]: + rc_candidates: list[tuple[datetime, str, dict[str, Any], str]] = [] + for verdict_val, rec in latest_record_by_actor.values(): + if verdict_val != "REQUEST_CHANGES": + continue + parsed = _parse_iso_timestamp(rec.get("submitted_at")) + if parsed is not None: + rc_candidates.append((parsed[0], str(rec.get("id") or ""), rec, parsed[1])) + if not rc_candidates: + return None, None, False + latest_rc_dt, _latest_id, latest_rec, rc_iso = max( + rc_candidates, key=lambda item: (item[0], item[1]) + ) + latest_rc_review_id = latest_rec.get("id") + anchors: list[tuple[datetime, str]] = [(latest_rc_dt, rc_iso)] + for commit_key in ("headCommitPushedAt", "headCommitCommittedAt"): + parsed_commit = _parse_iso_timestamp(row.get(commit_key)) + if parsed_commit is not None: + anchors.append(parsed_commit) + stalled_anchor_dt, stalled_since = max(anchors, key=lambda pair: pair[0]) + + post_replies: list[tuple[datetime, int, str]] = [] + for comment in row.get("comments") or []: + if not isinstance(comment, Mapping): + continue + parsed_ts = _parse_iso_timestamp(comment.get("createdAt") or comment.get("created_at")) + if parsed_ts is None or parsed_ts[0] <= stalled_anchor_dt: + continue + raw_user = comment.get("author") or comment.get("user") or {} + login = str(raw_user.get("login") if isinstance(raw_user, Mapping) else "").strip() + if _is_bot_login(login): + continue + is_author = 1 if (pr_author and login.casefold() == pr_author.casefold()) else 0 + post_replies.append((parsed_ts[0], is_author, login)) + + for review in raw_reviews: + if str(review.get("state") or "").strip().upper() != "COMMENTED": + continue + parsed_ts = _parse_iso_timestamp(review.get("submittedAt") or review.get("submitted_at")) + if parsed_ts is None or parsed_ts[0] <= stalled_anchor_dt: + continue + raw_user = review.get("author") or review.get("user") or {} + login = str(raw_user.get("login") if isinstance(raw_user, Mapping) else "").strip() + if _is_bot_login(login): + continue + if _extract_review_record(review, head_oid=head_oid, pr_author=pr_author)["valid_verdict_review"]: + continue + is_author = 1 if (pr_author and login.casefold() == pr_author.casefold()) else 0 + post_replies.append((parsed_ts[0], is_author, login)) + + post_replies.sort(key=lambda item: (item[0], item[1])) + author_replied = bool( + pr_author and post_replies and post_replies[-1][2].casefold() == pr_author.casefold() + ) + return latest_rc_review_id, stalled_since, author_replied + + def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: """Project compact, deterministic exact-head review, check, and gate facts.""" head_oid = str(row.get("headRefOid") or "").strip() @@ -297,6 +386,7 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: exact_reviews.sort(key=lambda item: (item["submitted_at"] or "", item["id"] or "")) latest_by_actor: dict[tuple[str, str], str | None] = {} + latest_record_by_actor: dict[tuple[str, str], tuple[str, dict[str, Any]]] = {} inconsistent_verdict = False for record in exact_reviews: actionable = _actor_actionable_verdict(record) @@ -308,11 +398,22 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: ) if actionable == "DISMISSED": latest_by_actor.pop(actor_key, None) + latest_record_by_actor.pop(actor_key, None) elif actionable == "CONFLICT": inconsistent_verdict = True latest_by_actor[actor_key] = "REQUEST_CHANGES" + latest_record_by_actor[actor_key] = ("REQUEST_CHANGES", record) else: latest_by_actor[actor_key] = actionable + latest_record_by_actor[actor_key] = (actionable, record) + + latest_rc_review_id, stalled_since, author_replied_after_rc = _summarize_request_changes_stall( + row, + raw_reviews=raw_reviews, + latest_record_by_actor=latest_record_by_actor, + head_oid=head_oid, + pr_author=pr_author, + ) verdicts = {val for val in latest_by_actor.values() if val} review_decision = str(row.get("reviewDecision") or "").strip().upper() @@ -363,11 +464,16 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: "mergeStateStatus": row.get("mergeStateStatus"), "mergeable": row.get("mergeable"), "updatedAt": row.get("updatedAt"), + "stalled_since": stalled_since, + "author_replied_after_request_changes": author_replied_after_rc, "checks_summary": checks_summary, "review_summary": { "effective_verdict": effective_verdict, "has_review_conflict": has_conflict, "stale_review_count": stale_count, + "latest_request_changes_review_id": latest_rc_review_id, + "stalled_since": stalled_since, + "author_replied_after_request_changes": author_replied_after_rc, "exact_head_reviews": exact_reviews, }, "bypassed_gates": bypassed_gates, @@ -409,6 +515,14 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: author { login } + comments(last: 20) { + nodes { + createdAt + author { + login + } + } + } reviews(last: 20) { nodes { id @@ -426,6 +540,9 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: commits(last: 1) { nodes { commit { + oid + committedDate + pushedDate statusCheckRollup { contexts(first: 30) { nodes { @@ -460,15 +577,34 @@ def _normalize_graphql_pr_node(node: Mapping[str, Any]) -> dict[str, Any]: else: reviews = [] + raw_comments = node.get("comments") + if isinstance(raw_comments, Mapping) and isinstance(raw_comments.get("nodes"), list): + comments = [item for item in raw_comments["nodes"] if isinstance(item, Mapping)] + elif isinstance(raw_comments, list): + comments = [item for item in raw_comments if isinstance(item, Mapping)] + else: + comments = [] + + 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 + ) + head_oid = str(node.get("headRefOid") or "").strip() + head_committed_at = node.get("headCommitCommittedAt") + head_pushed_at = node.get("headCommitPushedAt") + if isinstance(first_commit, Mapping): + commit_oid = str(first_commit.get("oid") or "").strip() + if not commit_oid or not head_oid or commit_oid.casefold() == head_oid.casefold(): + if head_committed_at is None: + head_committed_at = first_commit.get("committedDate") + if head_pushed_at is None: + head_pushed_at = first_commit.get("pushedDate") + 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 @@ -487,6 +623,9 @@ def _normalize_graphql_pr_node(node: Mapping[str, Any]) -> dict[str, Any]: "mergeStateStatus": node.get("mergeStateStatus"), "mergeable": node.get("mergeable"), "updatedAt": node.get("updatedAt"), + "headCommitCommittedAt": head_committed_at, + "headCommitPushedAt": head_pushed_at, + "comments": comments, "reviews": reviews, "statusCheckRollup": rollup, } diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py index 900e993e7e..2662706298 100644 --- a/loopx/capabilities/native_chat/steward_turn.py +++ b/loopx/capabilities/native_chat/steward_turn.py @@ -1588,7 +1588,7 @@ def run(native_request: Mapping[str, Any], launch_scope: Any) -> dict[str, Any]: followup_ctx = self._steward_followup_context(actor, session_id, request, authority) prompt += ( "\nSteward advancement instructions: Evaluate the monitor prompt and signal snapshot against authorized recipients (`context_delegation.targets`), prior receipts (`prior_handoff_receipts`, avoid duplicate delivery for unchanged PR@headRefOid), and open user todos (`open_user_todos`). " - "When `signal_snapshot.repository_snapshot.items` is present, each PR carries `mergeable`, `checks_summary`, `bypassed_gates`, and `review_summary` (`effective_verdict`, `has_review_conflict`, `stale_review_count`, `exact_head_reviews` with `reviewer_declaration`, `valid_verdict_review`, `author_owned_fallback`). Keep triage read-only; never execute merges here. " + "When `signal_snapshot.repository_snapshot.items` is present, each PR carries `mergeable`, `checks_summary`, `bypassed_gates`, `stalled_since`, `author_replied_after_request_changes`, optional `stalled_bucket` (`awaiting_maintainer_reply` | `24h_to_72h` | `72h_plus` | `7d_plus`), and `review_summary` (`effective_verdict`, `has_review_conflict`, `stale_review_count`, `latest_request_changes_review_id`, `stalled_since`, `author_replied_after_request_changes`, `exact_head_reviews` with `reviewer_declaration`, `valid_verdict_review`, `author_owned_fallback`). Keep triage read-only; never execute merges here. " "You may include optional top-level JSON fields alongside the normal kernel fields: " "`context_handoff` (single object or array <=32 of {goal_id, agent_id, brief} with `schema_version='collaboration_brief_v0'` and `inputs=[]` when referencing URLs/PRs, restricted to `context_delegation.targets`), " "`steward_owner_report` (optional non-empty Markdown string <=24000 chars or `{markdown}` delivered once directly to the bound Steward App and Feishu conversation; omit or set `null` when silent/no report should be sent), " diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts index fe40887e83..fa9ba4c6c7 100644 --- a/loopx/control_plane/collaboration/steward_managed_mode.ts +++ b/loopx/control_plane/collaboration/steward_managed_mode.ts @@ -986,6 +986,13 @@ function hasMatchingApprovedReviewerDeclaration(item: JsonObject, requiredTokens ); } +function classifyStalledBucket(stalledAgeMs: number, authorReplyBypass: boolean): string { + if (authorReplyBypass) return "awaiting_maintainer_reply"; + if (stalledAgeMs >= 168 * 3_600_000) return "7d_plus"; + if (stalledAgeMs >= 72 * 3_600_000) return "72h_plus"; + return "24h_to_72h"; +} + function filterScopedRepositoryItems( rawItems: unknown[], filter: JsonObject | null, @@ -1033,11 +1040,35 @@ function filterScopedRepositoryItems( if (authorMode === "exclude" && login && authors.has(login)) continue; } if (minStalledHours !== null) { - const updatedMs = typeof item.updatedAt === "string" ? Date.parse(item.updatedAt) : NaN; - if (!Number.isFinite(updatedMs) || observedAtMs - updatedMs < minStalledHours * 3_600_000) { + const authorReplied = item.author_replied_after_request_changes === true + || reviewSummary?.author_replied_after_request_changes === true; + const hasExplicitStalledSince = Object.prototype.hasOwnProperty.call(item, "stalled_since") + || (reviewSummary !== null && Object.prototype.hasOwnProperty.call(reviewSummary, "stalled_since")); + const rawStalledSince = typeof item.stalled_since === "string" + ? item.stalled_since + : (typeof reviewSummary?.stalled_since === "string" ? reviewSummary.stalled_since : null); + if ( + hasExplicitStalledSince + && rawStalledSince === null + && effectiveVerdicts !== null + && effectiveVerdicts.size === 1 + && effectiveVerdicts.has("REQUEST_CHANGES") + ) { continue; } - matched.push({ ...item, stalled_threshold_met: true }); + const basisIso = rawStalledSince ?? (typeof item.updatedAt === "string" ? item.updatedAt : null); + const basisMs = basisIso !== null ? Date.parse(basisIso) : NaN; + const authorReplyBypass = authorReplied && authorMode === "exclude"; + const thresholdMet = Number.isFinite(basisMs) && observedAtMs - basisMs >= minStalledHours * 3_600_000; + if (!authorReplyBypass && !thresholdMet) { + continue; + } + const stalledAgeMs = Number.isFinite(basisMs) ? Math.max(0, observedAtMs - basisMs) : 0; + matched.push({ + ...item, + stalled_threshold_met: thresholdMet, + stalled_bucket: classifyStalledBucket(stalledAgeMs, authorReplyBypass), + }); } else { matched.push(item); } @@ -1073,6 +1104,16 @@ export function observeStewardSignalMonitor(params: JsonObject): JsonObject { params.observed_at ?? repo?.observed_at, ) : null; + const hashedRepoItems = signalScope !== null && scopedRepoItems !== null + ? scopedRepoItems.map((item) => { + const reviewSummary = jsonObject(item.review_summary); + const hasStalledSinceClock = typeof item.stalled_since === "string" + || typeof reviewSummary?.stalled_since === "string"; + if (!hasStalledSinceClock) return item; + const { updatedAt: _ignoredUpdatedAt, ...rest } = item; + return rest; + }) + : rawRepoItems; const includePortfolio = signalScope === null ? true : signalScope.include_portfolio === true; const includeInbox = signalScope === null ? true : signalScope.include_inbox === true; const facts = signalScope === null @@ -1085,7 +1126,7 @@ export function observeStewardSignalMonitor(params: JsonObject): JsonObject { signal_scope: signalScope, portfolio_hash: includePortfolio ? (portfolio?.result_hash ?? null) : null, inbox_items: includeInbox ? inboxItems : [], - repository_items: scopedRepoItems, + repository_items: hashedRepoItems, }; const clocks = new Set(["snapshot_id", "portfolio_snapshot_id", "collected_at", "collection_completed_at", "generated_at", "evaluated_at", "age_seconds", "observed_at"]); diff --git a/tests/control_plane_ts/steward_managed_mode.test.ts b/tests/control_plane_ts/steward_managed_mode.test.ts index 538ec92d2b..939201418e 100644 --- a/tests/control_plane_ts/steward_managed_mode.test.ts +++ b/tests/control_plane_ts/steward_managed_mode.test.ts @@ -671,3 +671,158 @@ test("observeStewardSignalMonitor preserves feature-off parity and isolates scop repository_snapshot: sampleRepo, })); }); + +test("observeStewardSignalMonitor uses stalled_since, author-reply bypass, and stalled_bucket wakeups", () => { + const stalledInternalScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "include", + authors: ["alice"], + min_stalled_hours: 24, + }, + }; + const stalledExternalScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "exclude", + authors: ["alice"], + min_stalled_hours: 24, + }, + }; + + const makeRepo = (botUpdatedAt: string) => ({ + status: "ok", + repository: "acme/widgets", + items: [ + { + number: 201, + title: "Internal RC with Mergify bot ping", + isDraft: false, + mergeable: "MERGEABLE", + author: {login: "alice"}, + updatedAt: botUpdatedAt, + stalled_since: "2026-11-20T09:00:00Z", + author_replied_after_request_changes: false, + review_summary: { + effective_verdict: "REQUEST_CHANGES", + has_review_conflict: false, + latest_request_changes_review_id: "PRR_201", + stalled_since: "2026-11-20T09:00:00Z", + author_replied_after_request_changes: false, + exact_head_reviews: [], + }, + }, + { + number: 202, + title: "External author replied right after RC", + isDraft: false, + mergeable: "MERGEABLE", + author: {login: "ext-user"}, + updatedAt: "2026-11-21T08:30:00Z", + stalled_since: "2026-11-21T07:00:00Z", + author_replied_after_request_changes: true, + review_summary: { + effective_verdict: "REQUEST_CHANGES", + has_review_conflict: false, + latest_request_changes_review_id: "PRR_202", + stalled_since: "2026-11-21T07:00:00Z", + author_replied_after_request_changes: true, + exact_head_reviews: [], + }, + }, + { + number: 203, + title: "Stale RC on previous commit after new head push", + isDraft: false, + mergeable: "MERGEABLE", + author: {login: "ext-user"}, + updatedAt: "2026-11-18T00:00:00Z", + stalled_since: null, + author_replied_after_request_changes: false, + review_summary: { + effective_verdict: "REQUEST_CHANGES", + has_review_conflict: false, + latest_request_changes_review_id: null, + stalled_since: null, + author_replied_after_request_changes: false, + exact_head_reviews: [], + }, + }, + ], + }); + + // At 23h since stalled_since (2026-11-20T09:00 -> 2026-11-21T08:00), PR #201 is excluded even with newer updatedAt. + const before24h = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + repository_snapshot: makeRepo("2026-11-21T08:30:00Z"), + observed_at: "2026-11-21T08:00:00Z", + }); + assert.deepEqual(before24h.scoped_repository_items, []); + + // At 24h exact (2026-11-21T09:00), PR #201 enters 24h_to_72h bucket. + const at24h = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + previous_hash: before24h.result_hash, + repository_snapshot: makeRepo("2026-11-21T08:30:00Z"), + observed_at: "2026-11-21T09:00:00Z", + }); + assert.equal(at24h.material_change, true); + assert.equal(at24h.scoped_repository_items[0].number, 201); + assert.equal(at24h.scoped_repository_items[0].stalled_threshold_met, true); + assert.equal(at24h.scoped_repository_items[0].stalled_bucket, "24h_to_72h"); + + // Bot comment bumping updatedAt at 71h produces zero hash change in the same bucket. + const at71hBotBump = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + previous_hash: at24h.result_hash, + repository_snapshot: makeRepo("2026-11-23T07:55:00Z"), + observed_at: "2026-11-23T08:00:00Z", + }); + assert.equal(at71hBotBump.result_hash, at24h.result_hash); + assert.equal(at71hBotBump.material_change, false); + + // Crossing into 72h_plus and 7d_plus wakes the monitor exactly at bucket boundaries. + const at72h = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + previous_hash: at71hBotBump.result_hash, + repository_snapshot: makeRepo("2026-11-23T07:55:00Z"), + observed_at: "2026-11-23T09:00:00Z", + }); + assert.equal(at72h.material_change, true); + assert.equal(at72h.scoped_repository_items[0].stalled_bucket, "72h_plus"); + + const at73h = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + previous_hash: at72h.result_hash, + repository_snapshot: makeRepo("2026-11-23T07:55:00Z"), + observed_at: "2026-11-23T10:00:00Z", + }); + assert.equal(at73h.material_change, false); + + const at7d = observeStewardSignalMonitor({ + signal_scope: stalledInternalScope, + previous_hash: at73h.result_hash, + repository_snapshot: makeRepo("2026-11-23T07:55:00Z"), + observed_at: "2026-11-27T09:00:00Z", + }); + assert.equal(at7d.material_change, true); + assert.equal(at7d.scoped_repository_items[0].stalled_bucket, "7d_plus"); + + // External monitor prioritizes awaiting_maintainer_reply immediately (<24h) and drops stale-commit RC (#203). + const externalObs = observeStewardSignalMonitor({ + signal_scope: stalledExternalScope, + repository_snapshot: makeRepo("2026-11-21T08:30:00Z"), + observed_at: "2026-11-21T09:00:00Z", + }); + assert.deepEqual(externalObs.scoped_repository_items.map((row: {number: number}) => row.number), [202]); + assert.equal(externalObs.scoped_repository_items[0].stalled_threshold_met, false); + assert.equal(externalObs.scoped_repository_items[0].stalled_bucket, "awaiting_maintainer_reply"); +}); diff --git a/tests/test_steward_pr_merge_and_review_signal.py b/tests/test_steward_pr_merge_and_review_signal.py index 0d216334da..a2df571bdc 100644 --- a/tests/test_steward_pr_merge_and_review_signal.py +++ b/tests/test_steward_pr_merge_and_review_signal.py @@ -562,3 +562,156 @@ def fake_gh(cmd, **_kwargs): assert recovered["status"] == "applied" assert recovered["receipt"]["outcome"] == "pr_merged_recovered" assert recovered["receipt"]["resource_ids"]["merge_commit_oid"] == "9" * 40 + + +def test_summarize_pr_stalled_since_and_author_reply_detection(monkeypatch) -> None: + head = "e" * 40 + old = "0" * 40 + gpt_decl = "model_agent · gpt-6.2-sol · OpenAI (runtime_reported, max)" + + # Mergify bot comment bumps updatedAt without resetting stalled_since or flagging author reply. + bot_bumped = summarize_github_pr_signal( + { + **_sample_pr( + number=301, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc1", + "author": {"login": "maintainer"}, + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + } + ], + ), + "updatedAt": "2030-01-02T00:00:00Z", + "comments": [ + {"createdAt": "2030-01-01T09:00:00Z", "author": {"login": "mergify[bot]"}}, + ], + } + ) + assert bot_bumped["stalled_since"] == "2030-01-01T08:00:00Z" + assert bot_bumped["author_replied_after_request_changes"] is False + assert bot_bumped["review_summary"]["latest_request_changes_review_id"] == "PRR_rc1" + + # External author replies after REQUEST_CHANGES -> awaiting maintainer reply; maintainer reply resets it. + author_replied = summarize_github_pr_signal( + { + **_sample_pr( + number=302, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc2", + "author": {"login": "maintainer"}, + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + } + ], + ), + "comments": [ + {"createdAt": "2030-01-01T10:00:00Z", "author": {"login": "ext-contributor"}}, + {"createdAt": "2030-01-01T10:30:00Z", "author": {"login": "mergify[bot]"}}, + ], + } + ) + assert author_replied["author_replied_after_request_changes"] is True + + maintainer_replied = summarize_github_pr_signal( + { + **_sample_pr( + number=302, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc2", + "author": {"login": "maintainer"}, + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + }, + { + "id": "PRR_reply", + "author": {"login": "maintainer"}, + "state": "COMMENTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T11:00:00Z", + "body": "Please keep the existing state field unchanged.", + }, + ], + ), + "comments": [ + {"createdAt": "2030-01-01T10:00:00Z", "author": {"login": "ext-contributor"}}, + ], + } + ) + assert maintainer_replied["author_replied_after_request_changes"] is False + + # Stale-commit CHANGES_REQUESTED after new head commit push leaves stalled_since as None. + stale_after_push = summarize_github_pr_signal( + _sample_pr( + number=303, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_stale_rc", + "author": {"login": "maintainer"}, + "state": "CHANGES_REQUESTED", + "commit": {"oid": old}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=old, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + } + ], + ) + ) + assert stale_after_push["stalled_since"] is None + assert stale_after_push["review_summary"]["latest_request_changes_review_id"] is None + assert stale_after_push["author_replied_after_request_changes"] is False + + # GraphQL page normalization retains comments and head commit pushedDate. + gql_node = _graphql_node( + _sample_pr( + number=304, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc4", + "author": {"login": "maintainer"}, + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + } + ], + ) + ) + gql_node["comments"] = {"nodes": [{"createdAt": "2030-01-01T12:00:00Z", "author": {"login": "ext-contributor"}}]} + gql_node["commits"]["nodes"][0]["commit"]["oid"] = head + gql_node["commits"]["nodes"][0]["commit"]["committedDate"] = "2030-01-01T07:00:00Z" + gql_node["commits"]["nodes"][0]["commit"]["pushedDate"] = "2030-01-01T09:00:00Z" + + def fake_gql(cmd, **_kwargs): + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([gql_node])), stderr="") + + monkeypatch.setattr("loopx.capabilities.native_chat.steward_monitor.subprocess.run", fake_gql) + observed = _observe_github_repository_prs("org/repo") + assert observed["status"] == "ok" + item = observed["items"][0] + assert item["stalled_since"] == "2030-01-01T09:00:00Z" + assert item["author_replied_after_request_changes"] is True + assert item["review_summary"]["latest_request_changes_review_id"] == "PRR_rc4" From 5e848a5d29bb02f55eef7a8d0538d4fc1266d5b3 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:59:04 +0800 Subject: [PATCH 2/3] fix(steward): scope stalled-age hash suppression, under_24h bucket, and maintainer reply check Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/github_pr_evidence.py | 65 ++++++++++++---- .../capabilities/native_chat/steward_turn.py | 2 +- .../collaboration/steward_managed_mode.ts | 14 +++- .../steward_managed_mode.test.ts | 55 ++++++++++++++ ...test_steward_pr_merge_and_review_signal.py | 75 ++++++++++++++++++- 5 files changed, 190 insertions(+), 21 deletions(-) diff --git a/loopx/capabilities/native_chat/github_pr_evidence.py b/loopx/capabilities/native_chat/github_pr_evidence.py index 7aafc696ba..40cb57b8b0 100644 --- a/loopx/capabilities/native_chat/github_pr_evidence.py +++ b/loopx/capabilities/native_chat/github_pr_evidence.py @@ -45,6 +45,10 @@ _KNOWN_BOT_LOGINS = frozenset( {"mergify", "codecov", "codecov-commenter", "github-actions", "dependabot"} ) +_MAINTAINER_ASSOCIATIONS = frozenset({"OWNER", "MEMBER", "COLLABORATOR"}) +_NON_MAINTAINER_ASSOCIATIONS = frozenset( + {"NONE", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MANNEQUIN"} +) def _parse_iso_timestamp(value: Any) -> tuple[datetime, str] | None: @@ -68,6 +72,16 @@ def _is_bot_login(login: str) -> bool: return not cleaned or cleaned.endswith("[bot]") or cleaned in _KNOWN_BOT_LOGINS +def _is_bot_actor(raw_user: Any, login: str) -> bool: + if _is_bot_login(login): + return True + if isinstance(raw_user, Mapping): + actor_type = str(raw_user.get("__typename") or raw_user.get("type") or "").strip().casefold() + if actor_type == "bot": + return True + return False + + def normalize_github_repo_slug(value: Any) -> str | None: """Normalize a `git:github.com/`, HTTPS, or SSH remote string to `owner/repo`.""" if not isinstance(value, str): @@ -300,6 +314,26 @@ def _actor_actionable_verdict(record: Mapping[str, Any]) -> str | None: return None +def _post_reply_role( + entry: Mapping[str, Any], + *, + pr_author: str, + rc_reviewer_logins: set[str], +) -> tuple[int, str] | None: + raw_user = entry.get("author") or entry.get("user") or {} + login = str(raw_user.get("login") if isinstance(raw_user, Mapping) else "").strip() + if _is_bot_actor(raw_user, login): + return None + if pr_author and login.casefold() == pr_author.casefold(): + return 1, "author" + assoc = str(entry.get("authorAssociation") or entry.get("author_association") or "").strip().upper() + if assoc in _MAINTAINER_ASSOCIATIONS or ( + bool(login) and login.casefold() in rc_reviewer_logins and assoc not in _NON_MAINTAINER_ASSOCIATIONS + ): + return 0, "maintainer" + return None + + def _summarize_request_changes_stall( row: Mapping[str, Any], *, @@ -321,6 +355,11 @@ def _summarize_request_changes_stall( rc_candidates, key=lambda item: (item[0], item[1]) ) latest_rc_review_id = latest_rec.get("id") + rc_reviewer_logins = { + str(rec.get("author_login") or "").strip().casefold() + for _dt, _rid, rec, _iso in rc_candidates + if rec.get("author_login") and not rec.get("author_is_pr_author") + } anchors: list[tuple[datetime, str]] = [(latest_rc_dt, rc_iso)] for commit_key in ("headCommitPushedAt", "headCommitCommittedAt"): parsed_commit = _parse_iso_timestamp(row.get(commit_key)) @@ -335,12 +374,9 @@ def _summarize_request_changes_stall( parsed_ts = _parse_iso_timestamp(comment.get("createdAt") or comment.get("created_at")) if parsed_ts is None or parsed_ts[0] <= stalled_anchor_dt: continue - raw_user = comment.get("author") or comment.get("user") or {} - login = str(raw_user.get("login") if isinstance(raw_user, Mapping) else "").strip() - if _is_bot_login(login): - continue - is_author = 1 if (pr_author and login.casefold() == pr_author.casefold()) else 0 - post_replies.append((parsed_ts[0], is_author, login)) + role_info = _post_reply_role(comment, pr_author=pr_author, rc_reviewer_logins=rc_reviewer_logins) + if role_info is not None: + post_replies.append((parsed_ts[0], role_info[0], role_info[1])) for review in raw_reviews: if str(review.get("state") or "").strip().upper() != "COMMENTED": @@ -348,19 +384,14 @@ def _summarize_request_changes_stall( parsed_ts = _parse_iso_timestamp(review.get("submittedAt") or review.get("submitted_at")) if parsed_ts is None or parsed_ts[0] <= stalled_anchor_dt: continue - raw_user = review.get("author") or review.get("user") or {} - login = str(raw_user.get("login") if isinstance(raw_user, Mapping) else "").strip() - if _is_bot_login(login): - continue if _extract_review_record(review, head_oid=head_oid, pr_author=pr_author)["valid_verdict_review"]: continue - is_author = 1 if (pr_author and login.casefold() == pr_author.casefold()) else 0 - post_replies.append((parsed_ts[0], is_author, login)) + role_info = _post_reply_role(review, pr_author=pr_author, rc_reviewer_logins=rc_reviewer_logins) + if role_info is not None: + post_replies.append((parsed_ts[0], role_info[0], role_info[1])) post_replies.sort(key=lambda item: (item[0], item[1])) - author_replied = bool( - pr_author and post_replies and post_replies[-1][2].casefold() == pr_author.casefold() - ) + author_replied = bool(pr_author and post_replies and post_replies[-1][2] == "author") return latest_rc_review_id, stalled_since, author_replied @@ -518,7 +549,9 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: comments(last: 20) { nodes { createdAt + authorAssociation author { + __typename login } } @@ -529,7 +562,9 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: state submittedAt body + authorAssociation author { + __typename login } commit { diff --git a/loopx/capabilities/native_chat/steward_turn.py b/loopx/capabilities/native_chat/steward_turn.py index 2662706298..fc9384f200 100644 --- a/loopx/capabilities/native_chat/steward_turn.py +++ b/loopx/capabilities/native_chat/steward_turn.py @@ -1588,7 +1588,7 @@ def run(native_request: Mapping[str, Any], launch_scope: Any) -> dict[str, Any]: followup_ctx = self._steward_followup_context(actor, session_id, request, authority) prompt += ( "\nSteward advancement instructions: Evaluate the monitor prompt and signal snapshot against authorized recipients (`context_delegation.targets`), prior receipts (`prior_handoff_receipts`, avoid duplicate delivery for unchanged PR@headRefOid), and open user todos (`open_user_todos`). " - "When `signal_snapshot.repository_snapshot.items` is present, each PR carries `mergeable`, `checks_summary`, `bypassed_gates`, `stalled_since`, `author_replied_after_request_changes`, optional `stalled_bucket` (`awaiting_maintainer_reply` | `24h_to_72h` | `72h_plus` | `7d_plus`), and `review_summary` (`effective_verdict`, `has_review_conflict`, `stale_review_count`, `latest_request_changes_review_id`, `stalled_since`, `author_replied_after_request_changes`, `exact_head_reviews` with `reviewer_declaration`, `valid_verdict_review`, `author_owned_fallback`). Keep triage read-only; never execute merges here. " + "When `signal_snapshot.repository_snapshot.items` is present, each PR carries `mergeable`, `checks_summary`, `bypassed_gates`, `stalled_since`, `author_replied_after_request_changes`, optional `stalled_bucket` (`awaiting_maintainer_reply` | `under_24h` | `24h_to_72h` | `72h_plus` | `7d_plus`), and `review_summary` (`effective_verdict`, `has_review_conflict`, `stale_review_count`, `latest_request_changes_review_id`, `stalled_since`, `author_replied_after_request_changes`, `exact_head_reviews` with `reviewer_declaration`, `valid_verdict_review`, `author_owned_fallback`). Keep triage read-only; never execute merges here. " "You may include optional top-level JSON fields alongside the normal kernel fields: " "`context_handoff` (single object or array <=32 of {goal_id, agent_id, brief} with `schema_version='collaboration_brief_v0'` and `inputs=[]` when referencing URLs/PRs, restricted to `context_delegation.targets`), " "`steward_owner_report` (optional non-empty Markdown string <=24000 chars or `{markdown}` delivered once directly to the bound Steward App and Feishu conversation; omit or set `null` when silent/no report should be sent), " diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts index fa9ba4c6c7..350936ccf8 100644 --- a/loopx/control_plane/collaboration/steward_managed_mode.ts +++ b/loopx/control_plane/collaboration/steward_managed_mode.ts @@ -990,7 +990,8 @@ function classifyStalledBucket(stalledAgeMs: number, authorReplyBypass: boolean) if (authorReplyBypass) return "awaiting_maintainer_reply"; if (stalledAgeMs >= 168 * 3_600_000) return "7d_plus"; if (stalledAgeMs >= 72 * 3_600_000) return "72h_plus"; - return "24h_to_72h"; + if (stalledAgeMs >= 24 * 3_600_000) return "24h_to_72h"; + return "under_24h"; } function filterScopedRepositoryItems( @@ -1097,14 +1098,19 @@ export function observeStewardSignalMonitor(params: JsonObject): JsonObject { : requireJsonObject(params.portfolio_observation, "portfolio observation"); const inboxItems = Array.isArray(params.inbox_items) ? params.inbox_items : []; const rawRepoItems = repo === null ? null : (Array.isArray(repo.items) ? repo.items : []); + const prFilter = signalScope === null ? null : jsonObject(signalScope.pr_filter); + const rawMinStalledHours = prFilter?.min_stalled_hours; + const usesStalledAgeFilter = typeof rawMinStalledHours === "number" + && Number.isFinite(rawMinStalledHours) + && rawMinStalledHours > 0; const scopedRepoItems = signalScope !== null && rawRepoItems !== null ? filterScopedRepositoryItems( rawRepoItems, - signalScope.pr_filter === undefined ? null : jsonObject(signalScope.pr_filter), + prFilter, params.observed_at ?? repo?.observed_at, ) : null; - const hashedRepoItems = signalScope !== null && scopedRepoItems !== null + const hashedRepoItems = signalScope !== null && scopedRepoItems !== null && usesStalledAgeFilter ? scopedRepoItems.map((item) => { const reviewSummary = jsonObject(item.review_summary); const hasStalledSinceClock = typeof item.stalled_since === "string" @@ -1113,7 +1119,7 @@ export function observeStewardSignalMonitor(params: JsonObject): JsonObject { const { updatedAt: _ignoredUpdatedAt, ...rest } = item; return rest; }) - : rawRepoItems; + : (scopedRepoItems ?? rawRepoItems); const includePortfolio = signalScope === null ? true : signalScope.include_portfolio === true; const includeInbox = signalScope === null ? true : signalScope.include_inbox === true; const facts = signalScope === null diff --git a/tests/control_plane_ts/steward_managed_mode.test.ts b/tests/control_plane_ts/steward_managed_mode.test.ts index 939201418e..72f90be045 100644 --- a/tests/control_plane_ts/steward_managed_mode.test.ts +++ b/tests/control_plane_ts/steward_managed_mode.test.ts @@ -825,4 +825,59 @@ test("observeStewardSignalMonitor uses stalled_since, author-reply bypass, and s assert.deepEqual(externalObs.scoped_repository_items.map((row: {number: number}) => row.number), [202]); assert.equal(externalObs.scoped_repository_items[0].stalled_threshold_met, false); assert.equal(externalObs.scoped_repository_items[0].stalled_bucket, "awaiting_maintainer_reply"); + + // Scoped monitor WITHOUT min_stalled_hours retains updatedAt sensitivity even when items carry stalled_since. + const noAgeScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "include", + authors: ["alice"], + }, + }; + const noAgeInitial = observeStewardSignalMonitor({ + signal_scope: noAgeScope, + repository_snapshot: makeRepo("2026-11-21T08:30:00Z"), + observed_at: "2026-11-21T09:00:00Z", + }); + const noAgeUpdated = observeStewardSignalMonitor({ + signal_scope: noAgeScope, + previous_hash: noAgeInitial.result_hash, + repository_snapshot: makeRepo("2026-11-21T08:45:00Z"), + observed_at: "2026-11-21T09:15:00Z", + }); + assert.equal(noAgeUpdated.material_change, true); + + // Scoped monitor with sub-24h min_stalled_hours (e.g. 6h) emits under_24h at 7h and transitions to 24h_to_72h at 24h. + const sixHourScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "include", + authors: ["alice"], + min_stalled_hours: 6, + }, + }; + const at7h = observeStewardSignalMonitor({ + signal_scope: sixHourScope, + repository_snapshot: makeRepo("2026-11-20T15:00:00Z"), + observed_at: "2026-11-20T16:00:00Z", + }); + assert.equal(at7h.scoped_repository_items.length, 1); + assert.equal(at7h.scoped_repository_items[0].stalled_threshold_met, true); + assert.equal(at7h.scoped_repository_items[0].stalled_bucket, "under_24h"); + const at24hFromSixHour = observeStewardSignalMonitor({ + signal_scope: sixHourScope, + previous_hash: at7h.result_hash, + repository_snapshot: makeRepo("2026-11-21T08:30:00Z"), + observed_at: "2026-11-21T09:00:00Z", + }); + assert.equal(at24hFromSixHour.material_change, true); + assert.equal(at24hFromSixHour.scoped_repository_items[0].stalled_bucket, "24h_to_72h"); }); diff --git a/tests/test_steward_pr_merge_and_review_signal.py b/tests/test_steward_pr_merge_and_review_signal.py index a2df571bdc..7ba0c1e05e 100644 --- a/tests/test_steward_pr_merge_and_review_signal.py +++ b/tests/test_steward_pr_merge_and_review_signal.py @@ -658,6 +658,73 @@ def test_summarize_pr_stalled_since_and_author_reply_detection(monkeypatch) -> N ) assert maintainer_replied["author_replied_after_request_changes"] is False + # Visitor (authorAssociation=NONE), suffixless Bot (__typename=Bot), and unknown commenter never clear author reply; + # another maintainer (authorAssociation=MEMBER) DOES clear it. + visitor_and_bot_ignored = summarize_github_pr_signal( + { + **_sample_pr( + number=305, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc5", + "author": {"__typename": "User", "login": "orig-reviewer"}, + "authorAssociation": "MEMBER", + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + }, + { + "id": "PRR_bystander", + "author": {"__typename": "User", "login": "random-reviewer"}, + "authorAssociation": "CONTRIBUTOR", + "state": "COMMENTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T11:30:00Z", + "body": "Looks interesting!", + }, + ], + ), + "comments": [ + {"createdAt": "2030-01-01T10:00:00Z", "authorAssociation": "NONE", "author": {"__typename": "User", "login": "ext-contributor"}}, + {"createdAt": "2030-01-01T10:20:00Z", "authorAssociation": "NONE", "author": {"__typename": "User", "login": "random-visitor"}}, + {"createdAt": "2030-01-01T10:40:00Z", "authorAssociation": "NONE", "author": {"__typename": "Bot", "login": "custom-ci-helper"}}, + {"createdAt": "2030-01-01T11:00:00Z", "author": {"login": "missing-association-user"}}, + ], + } + ) + assert visitor_and_bot_ignored["author_replied_after_request_changes"] is True + + other_maintainer_replied = summarize_github_pr_signal( + { + **_sample_pr( + number=305, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[ + { + "id": "PRR_rc5", + "author": {"__typename": "User", "login": "orig-reviewer"}, + "authorAssociation": "MEMBER", + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + }, + ], + ), + "comments": [ + {"createdAt": "2030-01-01T10:00:00Z", "authorAssociation": "NONE", "author": {"__typename": "User", "login": "ext-contributor"}}, + {"createdAt": "2030-01-01T12:00:00Z", "authorAssociation": "MEMBER", "author": {"__typename": "User", "login": "second-maintainer"}}, + ], + } + ) + assert other_maintainer_replied["author_replied_after_request_changes"] is False + # Stale-commit CHANGES_REQUESTED after new head commit push leaves stalled_since as None. stale_after_push = summarize_github_pr_signal( _sample_pr( @@ -700,7 +767,13 @@ def test_summarize_pr_stalled_since_and_author_reply_detection(monkeypatch) -> N ], ) ) - gql_node["comments"] = {"nodes": [{"createdAt": "2030-01-01T12:00:00Z", "author": {"login": "ext-contributor"}}]} + gql_node["comments"] = { + "nodes": [ + {"createdAt": "2030-01-01T12:00:00Z", "authorAssociation": "NONE", "author": {"__typename": "User", "login": "ext-contributor"}}, + {"createdAt": "2030-01-01T12:15:00Z", "authorAssociation": "NONE", "author": {"__typename": "User", "login": "random-visitor"}}, + {"createdAt": "2030-01-01T12:30:00Z", "authorAssociation": "NONE", "author": {"__typename": "Bot", "login": "custom-ci-helper"}}, + ] + } gql_node["commits"]["nodes"][0]["commit"]["oid"] = head gql_node["commits"]["nodes"][0]["commit"]["committedDate"] = "2030-01-01T07:00:00Z" gql_node["commits"]["nodes"][0]["commit"]["pushedDate"] = "2030-01-01T09:00:00Z" From fba7ba07f5b224c7b46b6a70ba616395219cae85 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:06:43 +0800 Subject: [PATCH 3/3] fix(steward): paginate stalled-PR comment suffix and preserve incomplete coverage Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/github_pr_evidence.py | 168 ++++++++- .../collaboration/steward_managed_mode.ts | 30 +- .../steward_managed_mode.test.ts | 207 ++++++++++++ ...test_steward_pr_merge_and_review_signal.py | 318 ++++++++++++++++++ 4 files changed, 699 insertions(+), 24 deletions(-) diff --git a/loopx/capabilities/native_chat/github_pr_evidence.py b/loopx/capabilities/native_chat/github_pr_evidence.py index 40cb57b8b0..dd09adb443 100644 --- a/loopx/capabilities/native_chat/github_pr_evidence.py +++ b/loopx/capabilities/native_chat/github_pr_evidence.py @@ -341,7 +341,7 @@ def _summarize_request_changes_stall( latest_record_by_actor: Mapping[tuple[str, str], tuple[str, dict[str, Any]]], head_oid: str, pr_author: str, -) -> tuple[str | None, str | None, bool]: +) -> tuple[str | None, str | None, bool, bool]: rc_candidates: list[tuple[datetime, str, dict[str, Any], str]] = [] for verdict_val, rec in latest_record_by_actor.values(): if verdict_val != "REQUEST_CHANGES": @@ -350,7 +350,7 @@ def _summarize_request_changes_stall( if parsed is not None: rc_candidates.append((parsed[0], str(rec.get("id") or ""), rec, parsed[1])) if not rc_candidates: - return None, None, False + return None, None, False, False latest_rc_dt, _latest_id, latest_rec, rc_iso = max( rc_candidates, key=lambda item: (item[0], item[1]) ) @@ -367,15 +367,22 @@ def _summarize_request_changes_stall( anchors.append(parsed_commit) stalled_anchor_dt, stalled_since = max(anchors, key=lambda pair: pair[0]) + earliest_comment_dt: datetime | None = None + has_post_anchor_comment_reply = False post_replies: list[tuple[datetime, int, str]] = [] for comment in row.get("comments") or []: if not isinstance(comment, Mapping): continue parsed_ts = _parse_iso_timestamp(comment.get("createdAt") or comment.get("created_at")) - if parsed_ts is None or parsed_ts[0] <= stalled_anchor_dt: + if parsed_ts is None: + continue + if earliest_comment_dt is None or parsed_ts[0] < earliest_comment_dt: + earliest_comment_dt = parsed_ts[0] + if parsed_ts[0] <= stalled_anchor_dt: continue role_info = _post_reply_role(comment, pr_author=pr_author, rc_reviewer_logins=rc_reviewer_logins) if role_info is not None: + has_post_anchor_comment_reply = True post_replies.append((parsed_ts[0], role_info[0], role_info[1])) for review in raw_reviews: @@ -392,7 +399,15 @@ def _summarize_request_changes_stall( post_replies.sort(key=lambda item: (item[0], item[1])) author_replied = bool(pr_author and post_replies and post_replies[-1][2] == "author") - return latest_rc_review_id, stalled_since, author_replied + # GraphQL comments(last: N) returns a newest-last suffix, so coverage is complete when + # there is no earlier page, the fetched suffix reaches the anchor, or a newer reply + # from the PR author or a maintainer in the visible suffix supersedes older comments. + comments_coverage_complete = ( + row.get("commentsHasPreviousPage") is not True + or (earliest_comment_dt is not None and earliest_comment_dt <= stalled_anchor_dt) + or has_post_anchor_comment_reply + ) + return latest_rc_review_id, stalled_since, author_replied, not comments_coverage_complete def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: @@ -438,12 +453,14 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: latest_by_actor[actor_key] = actionable latest_record_by_actor[actor_key] = (actionable, record) - latest_rc_review_id, stalled_since, author_replied_after_rc = _summarize_request_changes_stall( - row, - raw_reviews=raw_reviews, - latest_record_by_actor=latest_record_by_actor, - head_oid=head_oid, - pr_author=pr_author, + latest_rc_review_id, stalled_since, author_replied_after_rc, comments_coverage_incomplete = ( + _summarize_request_changes_stall( + row, + raw_reviews=raw_reviews, + latest_record_by_actor=latest_record_by_actor, + head_oid=head_oid, + pr_author=pr_author, + ) ) verdicts = {val for val in latest_by_actor.values() if val} @@ -497,6 +514,7 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: "updatedAt": row.get("updatedAt"), "stalled_since": stalled_since, "author_replied_after_request_changes": author_replied_after_rc, + "comments_coverage_incomplete": comments_coverage_incomplete, "checks_summary": checks_summary, "review_summary": { "effective_verdict": effective_verdict, @@ -505,6 +523,7 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: "latest_request_changes_review_id": latest_rc_review_id, "stalled_since": stalled_since, "author_replied_after_request_changes": author_replied_after_rc, + "comments_coverage_incomplete": comments_coverage_incomplete, "exact_head_reviews": exact_reviews, }, "bypassed_gates": bypassed_gates, @@ -519,6 +538,27 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: _GITHUB_PR_LIST_PAGE_SIZE = 20 _GITHUB_PR_LIST_MAX_PAGES = 5 +_GITHUB_PR_COMMENT_MAX_BACKFILL_PAGES = 3 +_GITHUB_PR_COMMENTS_PAGE_QUERY = """query($owner: String!, $name: String!, $number: Int!, $before: String!) { + repository(owner: $owner, name: $name) { + pullRequest(number: $number) { + comments(last: 50, before: $before) { + pageInfo { + hasPreviousPage + startCursor + } + nodes { + createdAt + authorAssociation + author { + __typename + login + } + } + } + } + } +}""" _GITHUB_OPEN_PRS_GRAPHQL_QUERY = """query($owner: String!, $name: String!, $cursor: String) { repository(owner: $owner, name: $name) { pullRequests( @@ -547,6 +587,10 @@ def summarize_github_pr_signal(row: Mapping[str, Any]) -> dict[str, Any]: login } comments(last: 20) { + pageInfo { + hasPreviousPage + startCursor + } nodes { createdAt authorAssociation @@ -613,12 +657,19 @@ def _normalize_graphql_pr_node(node: Mapping[str, Any]) -> dict[str, Any]: reviews = [] raw_comments = node.get("comments") - if isinstance(raw_comments, Mapping) and isinstance(raw_comments.get("nodes"), list): - comments = [item for item in raw_comments["nodes"] if isinstance(item, Mapping)] + comments_page_info: Mapping[str, Any] = {} + if isinstance(raw_comments, Mapping): + raw_nodes = raw_comments.get("nodes") + comments = [item for item in raw_nodes if isinstance(item, Mapping)] if isinstance(raw_nodes, list) else [] + raw_page_info = raw_comments.get("pageInfo") + if isinstance(raw_page_info, Mapping): + comments_page_info = raw_page_info elif isinstance(raw_comments, list): comments = [item for item in raw_comments if isinstance(item, Mapping)] else: comments = [] + comments_has_previous_page = bool(comments_page_info.get("hasPreviousPage") is True) + comments_start_cursor = str(comments_page_info.get("startCursor") or "").strip() or None raw_commits = node.get("commits") commit_nodes = raw_commits.get("nodes") if isinstance(raw_commits, Mapping) else None @@ -661,11 +712,85 @@ def _normalize_graphql_pr_node(node: Mapping[str, Any]) -> dict[str, Any]: "headCommitCommittedAt": head_committed_at, "headCommitPushedAt": head_pushed_at, "comments": comments, + "commentsHasPreviousPage": comments_has_previous_page, + "commentsStartCursor": comments_start_cursor, "reviews": reviews, "statusCheckRollup": rollup, } +def _backfill_pr_comments_if_needed( + norm_node: dict[str, Any], + summary: dict[str, Any], + *, + owner: str, + name: str, + number: int, + runner: Callable[..., Any], +) -> dict[str, Any]: + """Boundedly backfill older PR comment pages when active RC reply coverage is truncated.""" + attempts = 0 + while ( + summary.get("comments_coverage_incomplete") is True + and attempts < _GITHUB_PR_COMMENT_MAX_BACKFILL_PAGES + ): + comment_cursor = str(norm_node.get("commentsStartCursor") or "").strip() + if not comment_cursor: + break + attempts += 1 + try: + completed = runner( + [ + "gh", + "api", + "graphql", + "-f", + f"query={_GITHUB_PR_COMMENTS_PAGE_QUERY}", + "-f", + f"owner={owner}", + "-f", + f"name={name}", + "-F", + f"number={number}", + "-f", + f"before={comment_cursor}", + ], + capture_output=True, + text=True, + check=False, + timeout=20, + ) + except (OSError, ValueError, TypeError, subprocess.TimeoutExpired): + break + if completed.returncode != 0: + break + try: + payload = json.loads(completed.stdout or "{}") + except (ValueError, TypeError): + break + if not isinstance(payload, Mapping) or payload.get("errors"): + break + data = payload.get("data") + repo = data.get("repository") if isinstance(data, Mapping) else None + pr_data = repo.get("pullRequest") if isinstance(repo, Mapping) else None + comments_conn = pr_data.get("comments") if isinstance(pr_data, Mapping) else None + older_nodes = comments_conn.get("nodes") if isinstance(comments_conn, Mapping) else None + page_info = comments_conn.get("pageInfo") if isinstance(comments_conn, Mapping) else None + if not isinstance(older_nodes, list) or not isinstance(page_info, Mapping): + break + older_comments = [item for item in older_nodes if isinstance(item, Mapping)] + next_has_prev = page_info.get("hasPreviousPage") is True + next_cursor = str(page_info.get("startCursor") or "").strip() or None + stalled_cursor = next_has_prev and (not next_cursor or next_cursor == comment_cursor) + norm_node["comments"] = [*older_comments, *norm_node["comments"]] + norm_node["commentsHasPreviousPage"] = next_has_prev + norm_node["commentsStartCursor"] = None if stalled_cursor else next_cursor + summary = summarize_github_pr_signal(norm_node) + if stalled_cursor: + break + return summary + + def observe_github_repository_prs( repo_slug: str, *, runner: Callable[..., Any] = subprocess.run ) -> dict[str, Any]: @@ -674,7 +799,7 @@ def observe_github_repository_prs( 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]] = {} + raw_nodes_by_number: dict[int, Mapping[str, Any]] = {} cursor: str | None = None try: for page_index in range(_GITHUB_PR_LIST_MAX_PAGES): @@ -714,7 +839,7 @@ def observe_github_repository_prs( continue number = int(node.get("number") or 0) if number > 0: - by_number[number] = summarize_github_pr_signal(_normalize_graphql_pr_node(node)) + raw_nodes_by_number[number] = node page_info = pull_requests.get("pageInfo") if not isinstance(page_info, Mapping) or page_info.get("hasNextPage") is not True: break @@ -724,7 +849,20 @@ def observe_github_repository_prs( return {"status": "unavailable", "repository": repo_slug} break cursor = next_cursor - items = [by_number[num] for num in sorted(by_number)] + items: list[dict[str, Any]] = [] + for number in sorted(raw_nodes_by_number): + norm_node = _normalize_graphql_pr_node(raw_nodes_by_number[number]) + summary = summarize_github_pr_signal(norm_node) + items.append( + _backfill_pr_comments_if_needed( + norm_node, + summary, + owner=owner, + name=name, + number=number, + runner=runner, + ) + ) return {"status": "ok", "repository": repo_slug, "items": items} except (OSError, ValueError, TypeError, subprocess.TimeoutExpired): return {"status": "unavailable", "repository": repo_slug} diff --git a/loopx/control_plane/collaboration/steward_managed_mode.ts b/loopx/control_plane/collaboration/steward_managed_mode.ts index 350936ccf8..105a62ed02 100644 --- a/loopx/control_plane/collaboration/steward_managed_mode.ts +++ b/loopx/control_plane/collaboration/steward_managed_mode.ts @@ -998,7 +998,7 @@ function filterScopedRepositoryItems( rawItems: unknown[], filter: JsonObject | null, observedAtValue: unknown, -): JsonObject[] { +): { items: JsonObject[]; observationIncomplete: boolean } { const minStalledHours = typeof filter?.min_stalled_hours === "number" ? filter.min_stalled_hours : null; let observedAtMs = NaN; if (minStalledHours !== null) { @@ -1057,6 +1057,13 @@ function filterScopedRepositoryItems( ) { continue; } + // Fail closed when comment pagination did not reach the stalled anchor or a newer + // author/maintainer reply, so evicted replies cannot drop awaiting_maintainer_reply. + const commentsCoverageIncomplete = item.comments_coverage_incomplete === true + || reviewSummary?.comments_coverage_incomplete === true; + if (commentsCoverageIncomplete) { + return { items: [], observationIncomplete: true }; + } const basisIso = rawStalledSince ?? (typeof item.updatedAt === "string" ? item.updatedAt : null); const basisMs = basisIso !== null ? Date.parse(basisIso) : NaN; const authorReplyBypass = authorReplied && authorMode === "exclude"; @@ -1074,7 +1081,7 @@ function filterScopedRepositoryItems( matched.push(item); } } - return matched; + return { items: matched, observationIncomplete: false }; } /** Combine authorized local portfolio, inbox and optional repository signal @@ -1084,15 +1091,16 @@ function filterScopedRepositoryItems( export function observeStewardSignalMonitor(params: JsonObject): JsonObject { const previousHash = typeof params.previous_hash === "string" && params.previous_hash ? params.previous_hash : null; + const incompleteObservation: JsonObject = { + result_hash: previousHash, + material_change: false, + observation_incomplete: true, + own_monitor_bookkeeping_excluded: true, + }; const signalScope = normalizeStewardSignalMonitorScope(params.signal_scope); const repo = params.repository_snapshot == null ? null : requireJsonObject(params.repository_snapshot, "repository signal"); if (repo !== null && repo.status === "unavailable") { - return { - result_hash: previousHash, - material_change: false, - observation_incomplete: true, - own_monitor_bookkeeping_excluded: true, - }; + return incompleteObservation; } const portfolio = params.portfolio_observation == null ? null : requireJsonObject(params.portfolio_observation, "portfolio observation"); @@ -1103,13 +1111,17 @@ export function observeStewardSignalMonitor(params: JsonObject): JsonObject { const usesStalledAgeFilter = typeof rawMinStalledHours === "number" && Number.isFinite(rawMinStalledHours) && rawMinStalledHours > 0; - const scopedRepoItems = signalScope !== null && rawRepoItems !== null + const scopedResult = signalScope !== null && rawRepoItems !== null ? filterScopedRepositoryItems( rawRepoItems, prFilter, params.observed_at ?? repo?.observed_at, ) : null; + if (scopedResult?.observationIncomplete) { + return incompleteObservation; + } + const scopedRepoItems = scopedResult === null ? null : scopedResult.items; const hashedRepoItems = signalScope !== null && scopedRepoItems !== null && usesStalledAgeFilter ? scopedRepoItems.map((item) => { const reviewSummary = jsonObject(item.review_summary); diff --git a/tests/control_plane_ts/steward_managed_mode.test.ts b/tests/control_plane_ts/steward_managed_mode.test.ts index 72f90be045..061a9b7edd 100644 --- a/tests/control_plane_ts/steward_managed_mode.test.ts +++ b/tests/control_plane_ts/steward_managed_mode.test.ts @@ -881,3 +881,210 @@ test("observeStewardSignalMonitor uses stalled_since, author-reply bypass, and s assert.equal(at24hFromSixHour.material_change, true); assert.equal(at24hFromSixHour.scoped_repository_items[0].stalled_bucket, "24h_to_72h"); }); + +test("observeStewardSignalMonitor fails closed on comments_coverage_incomplete only for matching stalled-age PRs", () => { + const externalStalledScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "exclude", + authors: ["alice"], + min_stalled_hours: 24, + }, + }; + const internalStalledScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "include", + authors: ["alice"], + min_stalled_hours: 24, + }, + }; + const noAgeScope = { + schema_version: "steward_signal_monitor_scope_v0", + include_portfolio: false, + include_inbox: false, + pr_filter: { + exclude_drafts: true, + effective_verdicts: ["REQUEST_CHANGES"], + author_mode: "exclude", + authors: ["alice"], + }, + }; + + const makeExternalPr = (overrides: Record = {}) => ({ + number: 302, + title: "External RC awaiting maintainer reply", + isDraft: false, + mergeable: "MERGEABLE", + author: {login: "ext-user"}, + updatedAt: "2026-11-21T08:30:00Z", + stalled_since: "2026-11-21T07:00:00Z", + author_replied_after_request_changes: true, + comments_coverage_incomplete: false, + review_summary: { + effective_verdict: "REQUEST_CHANGES", + has_review_conflict: false, + latest_request_changes_review_id: "PRR_302", + stalled_since: "2026-11-21T07:00:00Z", + author_replied_after_request_changes: true, + comments_coverage_incomplete: false, + exact_head_reviews: [], + }, + ...overrides, + }); + + const baseline = observeStewardSignalMonitor({ + signal_scope: externalStalledScope, + repository_snapshot: {status: "ok", repository: "acme/widgets", items: [makeExternalPr()]}, + observed_at: "2026-11-21T09:00:00Z", + }); + assert.equal(baseline.observation_incomplete, false); + assert.equal(baseline.scoped_repository_items[0].stalled_bucket, "awaiting_maintainer_reply"); + + // Truncated comment suffix (<24h and >=24h) fails closed and preserves previous_hash, + // whether comments_coverage_incomplete is surfaced at top-level or inside review_summary. + for (const [observedAt, truncatedItem] of [ + [ + "2026-11-21T10:00:00Z", + makeExternalPr({ + updatedAt: "2026-11-21T09:50:00Z", + author_replied_after_request_changes: false, + comments_coverage_incomplete: true, + review_summary: { + ...makeExternalPr().review_summary, + author_replied_after_request_changes: false, + comments_coverage_incomplete: true, + }, + }), + ], + [ + "2026-11-22T09:00:00Z", + makeExternalPr({ + updatedAt: "2026-11-22T08:30:00Z", + author_replied_after_request_changes: false, + comments_coverage_incomplete: false, + review_summary: { + ...makeExternalPr().review_summary, + author_replied_after_request_changes: false, + comments_coverage_incomplete: true, + }, + }), + ], + ] as const) { + const truncated = observeStewardSignalMonitor({ + signal_scope: externalStalledScope, + previous_hash: baseline.result_hash, + repository_snapshot: {status: "ok", repository: "acme/widgets", items: [truncatedItem]}, + observed_at: observedAt, + }); + assert.deepEqual(truncated, { + result_hash: baseline.result_hash, + material_change: false, + observation_incomplete: true, + own_monitor_bookkeeping_excluded: true, + }); + } + + // Recovered complete coverage with bumped updatedAt yields identical hash and material_change=false. + const recovered = observeStewardSignalMonitor({ + signal_scope: externalStalledScope, + previous_hash: baseline.result_hash, + repository_snapshot: { + status: "ok", + repository: "acme/widgets", + items: [makeExternalPr({updatedAt: "2026-11-21T10:30:00Z"})], + }, + observed_at: "2026-11-21T11:00:00Z", + }); + assert.equal(recovered.result_hash, baseline.result_hash); + assert.equal(recovered.material_change, false); + assert.equal(recovered.observation_incomplete, false); + + // Maintainer reply with complete coverage exits awaiting_maintainer_reply. + const maintainerCleared = observeStewardSignalMonitor({ + signal_scope: externalStalledScope, + previous_hash: baseline.result_hash, + repository_snapshot: { + status: "ok", + repository: "acme/widgets", + items: [ + makeExternalPr({ + author_replied_after_request_changes: false, + review_summary: { + ...makeExternalPr().review_summary, + author_replied_after_request_changes: false, + }, + }), + ], + }, + observed_at: "2026-11-21T11:00:00Z", + }); + assert.equal(maintainerCleared.material_change, true); + assert.equal(maintainerCleared.observation_incomplete, false); + assert.deepEqual(maintainerCleared.scoped_repository_items, []); + + // Non-age-filtered scopes and non-matching items are unaffected by comments_coverage_incomplete. + const truncatedExternal = makeExternalPr({ + author_replied_after_request_changes: false, + comments_coverage_incomplete: true, + review_summary: { + ...makeExternalPr().review_summary, + author_replied_after_request_changes: false, + comments_coverage_incomplete: true, + }, + }); + const noAgeObs = observeStewardSignalMonitor({ + signal_scope: noAgeScope, + repository_snapshot: {status: "ok", repository: "acme/widgets", items: [truncatedExternal]}, + observed_at: "2026-11-21T11:00:00Z", + }); + assert.equal(noAgeObs.observation_incomplete, false); + assert.equal(noAgeObs.scoped_repository_items.length, 1); + + const internalObs = observeStewardSignalMonitor({ + signal_scope: internalStalledScope, + repository_snapshot: { + status: "ok", + repository: "acme/widgets", + items: [ + truncatedExternal, + makeExternalPr({number: 303, isDraft: true, comments_coverage_incomplete: true}), + ], + }, + observed_at: "2026-11-21T11:00:00Z", + }); + assert.equal(internalObs.observation_incomplete, false); + assert.deepEqual(internalObs.scoped_repository_items, []); + + const externalIgnoringDraftOrApprove = observeStewardSignalMonitor({ + signal_scope: externalStalledScope, + repository_snapshot: { + status: "ok", + repository: "acme/widgets", + items: [ + makeExternalPr({number: 304, isDraft: true, comments_coverage_incomplete: true}), + makeExternalPr({ + number: 305, + comments_coverage_incomplete: true, + review_summary: { + effective_verdict: "APPROVE", + has_review_conflict: false, + comments_coverage_incomplete: true, + exact_head_reviews: [], + }, + }), + ], + }, + observed_at: "2026-11-21T11:00:00Z", + }); + assert.equal(externalIgnoringDraftOrApprove.observation_incomplete, false); + assert.deepEqual(externalIgnoringDraftOrApprove.scoped_repository_items, []); +}); diff --git a/tests/test_steward_pr_merge_and_review_signal.py b/tests/test_steward_pr_merge_and_review_signal.py index 7ba0c1e05e..c54fb007c7 100644 --- a/tests/test_steward_pr_merge_and_review_signal.py +++ b/tests/test_steward_pr_merge_and_review_signal.py @@ -8,7 +8,9 @@ import pytest +from loopx.capabilities.native_chat.conversation_bindings import ChatConversationBindings from loopx.capabilities.native_chat.github_pr_evidence import ( + _normalize_graphql_pr_node, inspect_github_pull_request, parse_github_pr_target, summarize_github_pr_signal, @@ -787,4 +789,320 @@ def fake_gql(cmd, **_kwargs): item = observed["items"][0] assert item["stalled_since"] == "2030-01-01T09:00:00Z" assert item["author_replied_after_request_changes"] is True + assert item["comments_coverage_incomplete"] is False + assert item["review_summary"]["comments_coverage_incomplete"] is False assert item["review_summary"]["latest_request_changes_review_id"] == "PRR_rc4" + + +def test_observe_github_prs_backfills_evicted_author_reply_and_preserves_incomplete_coverage( + monkeypatch, +) -> None: + head = "f" * 40 + gpt_decl = "model_agent · gpt-6.2-sol · OpenAI (runtime_reported, max)" + rc_review = { + "id": "PRR_rc_401", + "author": {"__typename": "User", "login": "maintainer"}, + "authorAssociation": "MEMBER", + "state": "CHANGES_REQUESTED", + "commit": {"oid": head}, + "submittedAt": "2030-01-01T08:00:00Z", + "body": _valid_bilingual_body(head_oid=head, verdict="REQUEST_CHANGES", reviewer=gpt_decl), + } + + def _visitor_comments(count: int, *, start_minute: int = 1, prefix_hour: str = "10") -> list[dict[str, Any]]: + return [ + { + "createdAt": f"2030-01-01T{prefix_hour}:{start_minute + idx:02d}:00Z", + "authorAssociation": "NONE", + "author": {"__typename": "User", "login": f"visitor-{idx}"}, + } + for idx in range(count) + ] + + author_comment = { + "createdAt": "2030-01-01T09:00:00Z", + "authorAssociation": "NONE", + "author": {"__typename": "User", "login": "ext-contributor"}, + } + maintainer_comment = { + "createdAt": "2030-01-01T09:30:00Z", + "authorAssociation": "MEMBER", + "author": {"__typename": "User", "login": "maintainer"}, + } + + # Normalization defaults vs pageInfo propagation. + plain_norm = _normalize_graphql_pr_node(_sample_pr(number=400, head_oid=head)) + assert plain_norm["commentsHasPreviousPage"] is False + assert plain_norm["commentsStartCursor"] is None + + # Pre-anchor comment inside truncated window proves completeness without backfill. + pre_anchor_summary = summarize_github_pr_signal( + { + **_sample_pr(number=400, head_oid=head, author="ext-contributor", reviews=[rc_review]), + "commentsHasPreviousPage": True, + "commentsStartCursor": "c0", + "comments": [ + {"createdAt": "2030-01-01T07:30:00Z", "authorAssociation": "NONE", "author": {"login": "old-visitor"}}, + *_visitor_comments(19), + ], + } + ) + assert pre_anchor_summary["comments_coverage_incomplete"] is False + assert pre_anchor_summary["review_summary"]["comments_coverage_incomplete"] is False + + # No exact-head REQUEST_CHANGES means comments_coverage_incomplete is always False. + no_rc_summary = summarize_github_pr_signal( + { + **_sample_pr(number=400, head_oid=head, author="ext-contributor", reviews=[]), + "commentsHasPreviousPage": True, + "comments": _visitor_comments(20), + } + ) + assert no_rc_summary["comments_coverage_incomplete"] is False + + def _base_node(*, updated_at: str, comments_conn: dict[str, Any]) -> dict[str, Any]: + node = _graphql_node( + _sample_pr( + number=401, + head_oid=head, + author="ext-contributor", + review_decision="CHANGES_REQUESTED", + reviews=[rc_review], + ) + ) + node["updatedAt"] = updated_at + node["comments"] = comments_conn + return node + + external_scope = { + "schema_version": "steward_signal_monitor_scope_v0", + "include_portfolio": False, + "include_inbox": False, + "pr_filter": { + "exclude_drafts": True, + "effective_verdicts": ["REQUEST_CHANGES"], + "author_mode": "exclude", + "authors": ["maintainer"], + "min_stalled_hours": 24, + }, + } + + # 1. Snapshot 1 -> awaiting_maintainer_reply; Snapshot 2 evicts author reply into page 2 -> + # backfill recovers author_replied_after_request_changes=True and identical TS result_hash. + snap1_node = _base_node( + updated_at="2030-01-01T10:19:00Z", + comments_conn={ + "pageInfo": {"hasPreviousPage": False, "startCursor": "c1"}, + "nodes": [author_comment, *_visitor_comments(19)], + }, + ) + snap2_node = _base_node( + updated_at="2030-01-01T10:20:00Z", + comments_conn={ + "pageInfo": {"hasPreviousPage": True, "startCursor": "c20"}, + "nodes": _visitor_comments(20), + }, + ) + + def _observe_with(fake_runner) -> dict[str, Any]: + with monkeypatch.context() as mp: + mp.setattr("loopx.capabilities.native_chat.steward_monitor.subprocess.run", fake_runner) + return _observe_github_repository_prs("org/repo") + + calls_snap1: list[list[str]] = [] + repo_snap1 = _observe_with( + lambda cmd, **_kw: ( + calls_snap1.append(list(cmd)) + or subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([snap1_node])), stderr="") + ) + ) + assert len(calls_snap1) == 1 + assert repo_snap1["items"][0]["author_replied_after_request_changes"] is True + assert repo_snap1["items"][0]["comments_coverage_incomplete"] is False + obs1 = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_snap1, + "observed_at": "2030-01-01T11:00:00Z", + "previous_hash": None, + }, + ) + assert obs1["observation_incomplete"] is False + assert obs1["scoped_repository_items"][0]["stalled_bucket"] == "awaiting_maintainer_reply" + + calls_snap2: list[list[str]] = [] + + def run_snap2(cmd, **_kwargs): + calls_snap2.append(list(cmd)) + if any(arg == "before=c20" for arg in cmd): + assert "-F" in cmd and "number=401" in cmd + body = { + "data": { + "repository": { + "pullRequest": { + "comments": { + "pageInfo": {"hasPreviousPage": False, "startCursor": "c1"}, + "nodes": [author_comment], + } + } + } + } + } + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(body), stderr="") + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([snap2_node])), stderr="") + + repo_snap2 = _observe_with(run_snap2) + assert len(calls_snap2) == 2 + assert repo_snap2["items"][0]["author_replied_after_request_changes"] is True + assert repo_snap2["items"][0]["comments_coverage_incomplete"] is False + assert repo_snap2["items"][0]["review_summary"]["comments_coverage_incomplete"] is False + obs2 = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_snap2, + "observed_at": "2030-01-01T11:05:00Z", + "previous_hash": obs1["result_hash"], + }, + ) + assert obs2["result_hash"] == obs1["result_hash"] + assert obs2["material_change"] is False + assert obs2["observation_incomplete"] is False + + # 2a. Maintainer (MEMBER) reply in visible page 1 (hasPreviousPage=True) requires 0 backfill calls. + page1_maintainer_node = _base_node( + updated_at="2030-01-01T10:25:00Z", + comments_conn={ + "pageInfo": {"hasPreviousPage": True, "startCursor": "c20"}, + "nodes": [maintainer_comment, *_visitor_comments(19)], + }, + ) + calls_maintainer_p1: list[list[str]] = [] + repo_maintainer_p1 = _observe_with( + lambda cmd, **_kw: ( + calls_maintainer_p1.append(list(cmd)) + or subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([page1_maintainer_node])), stderr="") + ) + ) + assert len(calls_maintainer_p1) == 1 + assert repo_maintainer_p1["items"][0]["author_replied_after_request_changes"] is False + assert repo_maintainer_p1["items"][0]["comments_coverage_incomplete"] is False + obs_maintainer_p1 = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_maintainer_p1, + "observed_at": "2030-01-01T11:10:00Z", + "previous_hash": obs1["result_hash"], + }, + ) + assert obs_maintainer_p1["material_change"] is True + assert obs_maintainer_p1["observation_incomplete"] is False + assert obs_maintainer_p1["scoped_repository_items"] == [] + + # 2b. Maintainer reply after author on backfill page 2 exits awaiting_maintainer_reply. + calls_maintainer_p2: list[list[str]] = [] + + def run_maintainer_p2(cmd, **_kwargs): + calls_maintainer_p2.append(list(cmd)) + if any(arg == "before=c20" for arg in cmd): + body = { + "data": { + "repository": { + "pullRequest": { + "comments": { + "pageInfo": {"hasPreviousPage": True, "startCursor": "c0"}, + "nodes": [author_comment, maintainer_comment], + } + } + } + } + } + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(body), stderr="") + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([snap2_node])), stderr="") + + repo_maintainer_p2 = _observe_with(run_maintainer_p2) + assert len(calls_maintainer_p2) == 2 + assert repo_maintainer_p2["items"][0]["author_replied_after_request_changes"] is False + assert repo_maintainer_p2["items"][0]["comments_coverage_incomplete"] is False + obs_maintainer_p2 = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_maintainer_p2, + "observed_at": "2030-01-01T11:15:00Z", + "previous_hash": obs1["result_hash"], + }, + ) + assert obs_maintainer_p2["material_change"] is True + assert obs_maintainer_p2["observation_incomplete"] is False + assert obs_maintainer_p2["scoped_repository_items"] == [] + + # 3. Backfill budget exhausted (3 pages of visitor comments, still hasPreviousPage=True) -> + # comments_coverage_incomplete=True and TS retains previous_hash with observation_incomplete=True. + backfill_cursors: list[str] = [] + + def run_exhausted(cmd, **_kwargs): + before_args = [arg.split("=", 1)[1] for arg in cmd if arg.startswith("before=")] + if before_args: + cursor = before_args[0] + backfill_cursors.append(cursor) + next_map = {"c20": ("c70", "11"), "c70": ("c120", "12"), "c120": ("c170", "13")} + next_cursor, hour = next_map[cursor] + body = { + "data": { + "repository": { + "pullRequest": { + "comments": { + "pageInfo": {"hasPreviousPage": True, "startCursor": next_cursor}, + "nodes": _visitor_comments(5, prefix_hour=hour), + } + } + } + } + } + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(body), stderr="") + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([snap2_node])), stderr="") + + repo_exhausted = _observe_with(run_exhausted) + assert backfill_cursors == ["c20", "c70", "c120"] + assert repo_exhausted["status"] == "ok" + assert repo_exhausted["items"][0]["comments_coverage_incomplete"] is True + assert repo_exhausted["items"][0]["review_summary"]["comments_coverage_incomplete"] is True + obs_exhausted = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_exhausted, + "observed_at": "2030-01-01T11:20:00Z", + "previous_hash": obs1["result_hash"], + }, + ) + assert obs_exhausted == { + "result_hash": obs1["result_hash"], + "material_change": False, + "observation_incomplete": True, + "own_monitor_bookkeeping_excluded": True, + } + + # Backfill RPC error also fails closed with comments_coverage_incomplete=True. + def run_backfill_error(cmd, **_kwargs): + if any(arg.startswith("before=") for arg in cmd): + return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="rate limited") + return subprocess.CompletedProcess(cmd, 0, stdout=json.dumps(_graphql_page([snap2_node])), stderr="") + + repo_rpc_err = _observe_with(run_backfill_error) + assert repo_rpc_err["status"] == "ok" + assert repo_rpc_err["items"][0]["comments_coverage_incomplete"] is True + obs_rpc_err = ChatConversationBindings._core( + "collaboration.steward.monitor.signal_observation", + { + "signal_scope": external_scope, + "repository_snapshot": repo_rpc_err, + "observed_at": "2030-01-01T11:25:00Z", + "previous_hash": obs1["result_hash"], + }, + ) + assert obs_rpc_err["observation_incomplete"] is True + assert obs_rpc_err["result_hash"] == obs1["result_hash"]