pydantic-ai/.github/scripts/test_issue_pr_attention_monitor.py

3028 lines
126 KiB
Python

from __future__ import annotations
import datetime as dt
import io
import json
import re
import sys
import urllib.error
import urllib.parse
from pathlib import Path
from typing import Any
import pytest
import yaml
sys.path.insert(0, str(Path(__file__).parent))
import issue_pr_attention_monitor as monitor
NOW = dt.datetime(2026, 7, 20, tzinfo=dt.UTC)
TRIAGE_NOW = dt.datetime(2026, 8, 25, tzinfo=dt.UTC)
TRIAGE_OWNERS = monitor.MAINTAINER_OWNERS
OLD = '2026-07-16T00:00:00Z'
def item(
number: int,
*,
labels: list[str] | None = None,
assignees: list[str] | None = None,
updated_at: str = OLD,
) -> dict[str, Any]:
return {
'number': number,
'state': 'open',
'updated_at': updated_at,
'title': f'Item {number}',
'body': 'Please decide the project direction.',
'comments': 0,
'user': {'login': 'contributor'},
'author_association': 'NONE',
'labels': [{'name': label} for label in labels or []],
'assignees': [{'login': login} for login in assignees or []],
}
def _MaintainerProbe(client: Any) -> Any:
return monitor._MaintainerProbe(client, 'r')
def label_event(
label: str, *, actor: str = 'github-actions[bot]', created_at: str = OLD, event_id: str | None = None
) -> dict[str, Any]:
event = {'event': 'labeled', 'created_at': created_at, 'actor': {'login': actor}, 'label': {'name': label}}
return {**event, 'id': event_id} if event_id else event
class FakeClient(monitor.GitHubClient):
"""Duck-typed transport over `GitHubClient`, keeping its real lookup logic."""
def __init__(self, items: dict[int, dict[str, Any]] | None = None) -> None:
super().__init__('token')
self.items = items or {}
self.calls: list[tuple[str, str, object | None]] = []
self.fail_get: set[int] = set()
self.fail_get_network: set[int] = set()
self.fail_delete_labels: set[str] = set()
self.assignment_succeeds = True
self.permissions: dict[str, str] = {}
self.deleted_logins: set[str] = set()
self.comments: dict[int, list[dict[str, Any]]] = {}
self.review_comments: dict[int, list[dict[str, Any]]] = {}
self.reviews: dict[int, list[dict[str, Any]]] = {}
self.truncated: set[int] = set()
self.timelines: dict[int, list[dict[str, Any]]] = {}
self.label_descriptions: dict[str, str] = {}
def get(self, path: str) -> Any:
self.calls.append(('GET', path, None))
if path.endswith('/permission'):
login = urllib.parse.unquote(path.split('/collaborators/')[1].removesuffix('/permission'))
if login in self.deleted_logins:
raise urllib.error.HTTPError(path, 404, 'not found', {}, None)
# GitHub reports `none` for anyone without repository access, and
# never `maintain`/`triage`: those collapse to `write`/`read` here.
return {'permission': {monitor._FALLBACK_OWNER: 'write', **self.permissions}.get(login, 'none')}
if '/pulls/' in path and '/reviews' in path:
return self.reviews.get(int(path.split('/pulls/')[1].split('/')[0]), [])
if '/pulls/' in path and '/' not in path.split('/pulls/')[1]:
number = int(path.split('/pulls/')[1])
return {**self.items[number], 'review_comments': len(self.review_comments.get(number, []))}
if path.startswith('/search/issues?'):
query = urllib.parse.parse_qs(urllib.parse.urlparse(path).query)
terms = query.get('q', [''])[0]
if 'is:closed' in terms:
states = {'closed'}
elif 'is:open' in terms:
states = {'open'}
else:
states = {'open', 'closed'}
positive = re.search(r'(?<!-)label:"([^"]+)"', terms)
requested_label = positive.group(1) if positive else monitor._ACTION_LABEL
values = [
value
for value in self.items.values()
if value['state'] in states and requested_label in {str(label['name']) for label in value['labels']}
]
per_page = int(query.get('per_page', ['30'])[0])
page = int(query.get('page', ['1'])[0])
start = (page - 1) * per_page
return {'total_count': len(values), 'items': values[start : start + per_page]}
if '/labels/' in path:
name = urllib.parse.unquote(path.rsplit('/', 1)[-1])
return {'name': name, 'description': self.label_descriptions.get(name, monitor._LABELS[name][1])}
if '/issues?state=' in path and 'labels=' in path:
requested = urllib.parse.unquote(path.split('labels=')[1].split('&')[0])
state = path.split('/issues?state=')[1].split('&')[0]
return [
value
for value in self.items.values()
if requested in {str(label['name']) for label in value['labels']}
and (state == 'all' or value['state'] == state)
]
if '/issues/' in path and '/comments?' not in path:
number = int(path.split('/issues/')[1].split('/')[0])
if number in self.fail_get:
raise urllib.error.HTTPError(path, 500, 'boom', {}, None)
if number in self.fail_get_network:
raise urllib.error.URLError(OSError('certificate verify failed'))
return self.items[number]
if '/comments?' in path:
return []
raise AssertionError(path)
def post(self, path: str, payload: object) -> Any:
self.calls.append(('POST', path, payload))
if path.endswith('/assignees'):
assert isinstance(payload, dict)
number = int(path.split('/issues/')[1].split('/')[0])
requested = [str(login) for login in payload['assignees']] if self.assignment_succeeds else []
existing = [str(value['login']) for value in self.items[number]['assignees']]
response = {
**self.items[number],
'assignees': [{'login': login} for login in dict.fromkeys([*existing, *requested])],
}
self.items[number] = response
return response
if path.endswith('/labels'):
assert isinstance(payload, dict)
number = int(path.split('/issues/')[1].split('/')[0])
existing = {str(value['name']) for value in self.items[number]['labels']}
labels = [str(label) for label in payload['labels']]
self.items[number]['labels'].extend({'name': label} for label in labels if label not in existing)
return {}
def patch(self, path: str, payload: object) -> Any:
self.calls.append(('PATCH', path, payload))
return {}
def delete(self, path: str, payload: object | None = None) -> None:
self.calls.append(('DELETE', path, payload))
if path.endswith('/assignees'):
assert isinstance(payload, dict)
number = int(path.split('/issues/')[1].split('/')[0])
removed = {str(login).casefold() for login in payload['assignees']}
self.items[number]['assignees'] = [
value for value in self.items[number]['assignees'] if str(value['login']).casefold() not in removed
]
return
if '/labels/' in path:
number = int(path.split('/issues/')[1].split('/')[0])
removed = urllib.parse.unquote(path.rsplit('/', 1)[-1])
if removed in self.fail_delete_labels:
raise urllib.error.HTTPError(path, 500, 'boom', {}, None)
self.items[number]['labels'] = [
value for value in self.items[number]['labels'] if str(value['name']) != removed
]
def last_page(self, path: str) -> list[dict[str, Any]]:
self.calls.append(('LAST', path, None))
number = int(path.split('/issues/')[1].split('/')[0])
if number in self.timelines:
return [
{**event, 'id': event.get('id', f'event-{index}')} for index, event in enumerate(self.timelines[number])
]
labels = {label['name'] for label in self.items[number]['labels']}
stage = monitor._stage(labels)
label = monitor._ACTION_LABEL if stage == 0 else monitor._STAGE_LABELS[stage - 1]
events = [label_event(label, event_id=f'default-stage-{stage}')]
return events
def last_pages(self, path: str, *, count: int = 1) -> list[dict[str, Any]]:
return self.last_page(path)
def first_pages(self, path: str, *, count: int) -> tuple[list[dict[str, Any]], bool]:
self.calls.append(('FIRST', path, None))
if '/pulls/' in path:
number = int(path.split('/pulls/')[1].split('/')[0])
source = self.reviews if path.endswith('/reviews') else self.review_comments
else:
number, source = int(path.split('/issues/')[1].split('/')[0]), self.comments
return source.get(number, []), number not in self.truncated
def permission_reads(self) -> list[str]:
return [path for method, path, _ in self.calls if method == 'GET' and path.endswith('/permission')]
class SnapshotClient(FakeClient):
def __init__(self, values: dict[int, dict[str, Any]]) -> None:
super().__init__(values)
self.search_results = list(values.values())
def get(self, path: str) -> Any:
if path.startswith('/search/issues?'):
self.calls.append(('GET', path, None))
if 'per_page=1&' in path or path.endswith('per_page=1'):
return {'total_count': len(self.search_results), 'items': self.search_results[:1]}
return {'total_count': len(self.search_results), 'items': self.search_results}
if '/check-runs?' in path:
self.calls.append(('GET', path, None))
return {'check_runs': [{'name': 'CI', 'status': 'completed', 'conclusion': 'success'}]}
if '/pulls/' in path and '/comments?' not in path and '/reviews' not in path:
self.calls.append(('GET', path, None))
number = int(path.split('/pulls/')[1])
return {
**self.items[number],
'review_comments': 0,
'draft': False,
'mergeable_state': 'clean',
'requested_reviewers': [],
'head': {'sha': f'sha-{number}'},
}
return super().get(path)
def last_page(self, path: str) -> list[dict[str, Any]]:
if '/pulls/' in path and path.endswith('/reviews'):
self.calls.append(('LAST', path, None))
return []
return super().last_page(path)
def write_snapshot(path: Path, values: list[dict[str, object]]) -> None:
path.write_text(json.dumps({'generated_at': NOW.isoformat(), 'candidates': values}), encoding='utf-8')
def write_output(
path: Path,
numbers: list[str],
*,
next_actor: str = 'maintainer',
confidence: str = 'high',
) -> None:
path.write_text(
json.dumps(
{
'items': [
{
'type': 'record_attention_decision',
'item_number': n,
'next_actor': next_actor,
'confidence': confidence,
}
for n in numbers
]
}
),
encoding='utf-8',
)
def test_last_page_uses_the_page_containing_the_newest_activity():
assert monitor._last_page(0, 8) == 1
assert monitor._last_page(8, 8) == 1
assert monitor._last_page(9, 8) == 2
def test_build_and_write_snapshot_are_bounded_and_agent_readable(tmp_path: Path):
pull = {**item(8), 'pull_request': {'url': 'https://api.github.test/pulls/8'}}
client = SnapshotClient({7: item(7), 8: pull})
snapshot = monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)
assert [value['number'] for value in snapshot['candidates']] == [7, 8]
assert [value['kind'] for value in snapshot['candidates']] == ['issue', 'pull_request']
path = tmp_path / 'attention-candidates.json'
assert monitor.write_snapshot(client, 'pydantic/pydantic-ai', str(path), now=NOW) == [
'wrote 2 attention candidate(s)'
]
assert json.loads(path.read_text(encoding='utf-8'))['candidates'][1]['kind'] == 'pull_request'
def test_pull_request_context_includes_newest_review_state():
pull = {**item(8), 'pull_request': {'url': 'https://api.github.test/pulls/8'}}
client = SnapshotClient({8: pull})
def reviews(path: str) -> list[dict[str, Any]]:
assert path.endswith('/pulls/8/reviews')
return [
{
'submitted_at': '2026-07-16T01:00:00Z',
'user': {'login': 'maintainer'},
'author_association': 'MEMBER',
'state': 'CHANGES_REQUESTED',
'body': '',
}
]
client.last_page = reviews # type: ignore[method-assign]
snapshot = monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)
review = snapshot['candidates'][0]['recent_activity'][0]
assert review['kind'] == 'review'
assert review['state'] == 'CHANGES_REQUESTED'
def test_candidate_discovery_returns_empty_without_stale_items():
client = SnapshotClient({})
assert monitor._candidate_page(client, 'pydantic/pydantic-ai', now=NOW) == []
def test_snapshot_skips_active_recent_and_escalated_items():
client = SnapshotClient(
{
1: item(1, labels=[monitor._ACTION_LABEL]),
2: item(2, labels=[monitor._PINGED_LABEL]),
3: item(3, updated_at='2026-07-19T00:00:00Z'),
4: item(4, labels=[monitor._ESCALATED_LABEL]),
}
)
candidates = monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)['candidates']
assert [candidate['number'] for candidate in candidates] == [2]
def test_snapshot_skips_signed_operational_incidents_but_keeps_human_items_and_bot_prs():
incident = item(9, labels=['area:agentic-workflows', 'pydanty:meta'])
incident['body'] = (
'<!-- pydantic-ai-provider-health:v1 '
'{"version":1,"scope":"provider","key":"minimax","kind":"balance",'
'"run_id":"123","reset_at":null} -->'
)
human_issue = item(10, labels=['pydanty:meta'])
bot_pr = {
**item(11, labels=['area:agentic-workflows', 'pydanty:meta']),
'pull_request': {'url': 'https://api.github.test/pulls/11'},
}
client = SnapshotClient({9: incident, 10: human_issue, 11: bot_pr})
candidates = monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)['candidates']
assert [candidate['number'] for candidate in candidates] == [10, 11]
assert [candidate['kind'] for candidate in candidates] == ['issue', 'pull_request']
@pytest.mark.parametrize(
'marker',
[
'{"version":true,"scope":"provider","key":"minimax","kind":"balance"}',
'{"version":1.0,"scope":"provider","key":"minimax","kind":"balance"}',
'{"version":2,"scope":"provider","key":"minimax","kind":"balance"}',
'{"version":1,"scope":"provider","key":"minimax"}',
'{"version":1,"scope":"provider","key":"minimax","kind":"balance","reset_at":"2026-08-25T00:00:00"}',
],
)
def test_operational_labels_without_a_valid_controller_marker_are_not_filtered(marker: str):
issue = item(9, labels=['area:agentic-workflows', 'pydanty:meta'])
issue['body'] = f'<!-- pydantic-ai-provider-health:v1 {marker} -->'
assert not monitor._is_provider_health_incident(issue)
snapshot = monitor.build_snapshot(SnapshotClient({9: issue}), 'pydantic/pydantic-ai', now=NOW)
assert [candidate['number'] for candidate in snapshot['candidates']] == [9]
def test_deeply_nested_operational_marker_does_not_abort_snapshot():
nested = '[' * 10_000 + '0' + ']' * 10_000
issue = item(9, labels=['area:agentic-workflows', 'pydanty:meta'])
issue['body'] = (
'<!-- pydantic-ai-provider-health:v1 '
'{"version":1,"scope":"provider","key":"minimax","kind":"balance",'
f'"extra":{nested}}} -->'
)
snapshot = monitor.build_snapshot(SnapshotClient({9: issue}), 'pydantic/pydantic-ai', now=NOW)
assert [candidate['number'] for candidate in snapshot['candidates']] == [9]
def test_candidate_search_covers_recent_activity_and_the_backlog():
client = SnapshotClient({})
monitor._candidate_page(client, 'pydantic/pydantic-ai', now=NOW)
searches = [path for method, path, _ in client.calls if method == 'GET' and path.startswith('/search/issues?')]
recent = [path for path in searches if 'order=desc' in path]
backlog = [path for path in searches if 'order=asc' in path]
assert recent and all('updated%3A2026-06-05..2026-07-16' in path for path in recent)
assert all(path.count('updated%3A') == 1 for path in recent)
assert backlog and all('updated%3A%3C' in path for path in backlog)
assert all(path.count('updated%3A') == 1 for path in backlog)
assert all(f'-label%3A%22{monitor._ACTION_LABEL}%22' in path for path in backlog)
assert all(f'-label%3A%22{monitor._ESCALATED_LABEL}%22' in path for path in searches)
def test_snapshot_recheck_skips_items_closed_after_search():
closed = item(7)
closed['state'] = 'closed'
client = SnapshotClient({7: closed})
assert monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)['candidates'] == []
def test_snapshot_rejects_aggregate_oversize(monkeypatch: pytest.MonkeyPatch):
client = SnapshotClient({7: item(7)})
monkeypatch.setattr(monitor, '_SNAPSHOT_LIMIT', 1)
with pytest.raises(RuntimeError, match='snapshot exceeds'):
monitor.build_snapshot(client, 'pydantic/pydantic-ai', now=NOW)
def test_snapshot_uses_utf8_without_ascii_escape_inflation(tmp_path: Path):
value = item(7)
value['body'] = '🤖' * 100
client = SnapshotClient({7: value})
path = tmp_path / 'snapshot.json'
monitor.write_snapshot(client, 'pydantic/pydantic-ai', str(path), now=NOW)
assert '🤖' in path.read_text(encoding='utf-8')
assert path.stat().st_size <= monitor._SNAPSHOT_LIMIT
def test_parse_decisions_rejects_injection_and_duplicates(tmp_path: Path):
output = tmp_path / 'output.json'
write_output(output, ['1; echo pwned'])
with pytest.raises(ValueError, match='positive decimal'):
monitor.agent_items(
str(output), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
write_output(output, ['1', '1'])
with pytest.raises(ValueError, match='duplicate'):
monitor.agent_items(
str(output), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
@pytest.mark.parametrize(
('contents', 'message'),
[
([], 'Input should be an object'),
({}, r'candidates\s+Field required'),
({'candidates': [None]}, r'candidates\.0\s+Input should be an object'),
({'candidates': [{'number': 0, 'updated_at': OLD}]}, r'number\s+Input should be greater than or equal to 1'),
({'candidates': [{'number': '7', 'updated_at': OLD}]}, r'number\s+Input should be a valid integer'),
({'candidates': [{'number': 7, 'updated_at': OLD}, {'number': 7, 'updated_at': OLD}]}, 'unique numbers'),
],
)
def test_snapshot_validation_rejects_invalid_shapes(tmp_path: Path, contents: object, message: str):
path = tmp_path / 'snapshot.json'
path.write_text(json.dumps(contents), encoding='utf-8')
with pytest.raises(ValueError, match=message):
monitor.snapshot_candidates(str(path), limit=monitor._CANDIDATE_LIMIT)
def test_agent_output_requires_items_but_ignores_other_safe_outputs(tmp_path: Path):
path = tmp_path / 'output.json'
path.write_text('{}', encoding='utf-8')
with pytest.raises(ValueError, match=r'items\s+Field required'):
monitor.agent_items(
str(path), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
path.write_text(json.dumps({'items': [{'type': 'noop'}]}), encoding='utf-8')
assert (
monitor.agent_items(
str(path), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
== []
)
# A non-object entry means the file is corrupted, not that another tool
# wrote it; the strict boundary fails the run instead of guessing.
path.write_text(json.dumps({'items': [None, {'type': 'noop'}]}), encoding='utf-8')
with pytest.raises(ValueError, match=r'items\.0\s+Input should be an object'):
monitor.agent_items(
str(path), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
def test_item_labels_skips_malformed_entries_rather_than_failing_the_item():
# GitHub labels always carry a string name; if one ever does not, reading
# the rest of the item beats failing the whole run. Pinned so the tolerant
# posture stays deliberate.
assert monitor.item_labels({'labels': [{'name': 'bug'}, {}, {'name': 3}, 'garbage']}) == {'bug'}
def test_apply_revalidates_then_assigns_and_labels(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7)})
lines = monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW)
assert lines == ['#7: requested maintainer attention from @adtyavrdhn']
assert (
'POST',
'/repos/pydantic/pydantic-ai/issues/7/assignees',
{'assignees': ['adtyavrdhn']},
) in client.calls
def test_apply_never_assigns_a_pull_request(tmp_path: Path):
# Pull requests are never auto-assigned: a human assigns one when an
# issue warrants it. The item stays tracked, silently.
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 8, 'updated_at': OLD}])
write_output(output, ['8'])
pull = {**item(8), 'pull_request': {'url': 'https://api.github.test/pulls/8'}}
client = FakeClient({8: pull})
lines = monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW)
assert lines == ['#8: deferred until its owner can be identified']
assert monitor._ACTION_LABEL in {label['name'] for label in client.items[8]['labels']}
assert not any(call[0] == 'POST' and call[1].endswith('/assignees') for call in client.calls)
def test_apply_notifies_the_human_assigned_owner_of_a_pull_request(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 8, 'updated_at': OLD}])
write_output(output, ['8'])
pull = {**item(8, assignees=['DouweM']), 'pull_request': {'url': 'https://api.github.test/pulls/8'}}
client = FakeClient({8: pull})
client.permissions = {'DouweM': 'write'}
lines = monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW)
assert lines == ['#8: requested maintainer attention from @DouweM']
assert not any(call[0] == 'POST' and call[1].endswith('/assignees') for call in client.calls)
def test_owner_selection_reads_the_live_item_not_the_classified_copy():
stale = item(7, labels=[monitor._ACTION_LABEL])
current = item(
7,
labels=[monitor._ACTION_LABEL],
updated_at='2026-07-17T00:00:00Z',
)
current['comments'] = 1
client = FakeClient({7: current})
client.permissions = {'DouweM': 'write'}
client.comments[7] = [{'user': {'login': 'DouweM'}, 'created_at': '2026-07-17T00:00:00Z'}]
assert monitor._ensure_recipients(client, 'r', stale, now=NOW) == ['DouweM']
assert ('POST', '/repos/r/issues/7/assignees', {'assignees': ['DouweM']}) in client.calls
def test_owner_selection_finds_a_maintainer_hidden_from_the_collaborator_list():
# The workflow token cannot see organization members whose membership is
# private, so the collaborator list omits most of the maintainer team and
# would silently route their own items to the fallback owner instead.
issue = item(7, labels=[monitor._ACTION_LABEL])
issue['user'] = {'login': 'DouweM'}
issue['author_association'] = 'CONTRIBUTOR'
client = FakeClient({7: issue})
client.permissions = {'DouweM': 'admin'}
assert monitor._first_maintainer_in_discussion(client, 'r', issue) == ('DouweM', True)
assert client.permission_reads() == ['/repos/r/collaborators/DouweM/permission']
assert not any('/collaborators?' in path for _, path, _ in client.calls)
def test_owner_selection_never_assigns_a_pull_request_even_to_its_maintainer_author():
pull = {**item(7, labels=[monitor._ACTION_LABEL]), 'pull_request': {'url': 'https://api.github.com/pulls/7'}}
pull['user'] = {'login': 'DouweM'}
client = FakeClient({7: pull})
client.permissions = {'DouweM': 'admin'}
assert monitor._ensure_recipients(client, 'r', pull, now=NOW) is None
assert not any(call[0] == 'POST' and call[1].endswith('/assignees') for call in client.calls)
def test_owner_selection_reads_every_pull_request_discussion_surface():
# Maintainers usually join a PR by reviewing it, and neither reviews nor
# code comments appear under the issue comments endpoint.
pull = {**item(7, labels=[monitor._ACTION_LABEL]), 'pull_request': {'url': 'https://api.github.com/pulls/7'}}
pull['comments'] = 1
client = FakeClient({7: pull})
client.permissions = {'DouweM': 'admin', 'dsfaccini': 'write'}
client.comments[7] = [{'user': {'login': 'dsfaccini'}, 'created_at': '2026-07-03T00:00:00Z'}]
client.review_comments[7] = [{'user': {'login': 'contributor'}, 'created_at': '2026-07-02T00:00:00Z'}]
client.reviews[7] = [{'user': {'login': 'DouweM'}, 'submitted_at': '2026-07-01T00:00:00Z'}]
# Ordered across all three surfaces, so the earliest reviewer wins over the
# later issue-comment maintainer.
assert monitor._first_maintainer_in_discussion(client, 'r', pull) == ('DouweM', True)
def test_owner_selection_never_overrides_an_explicit_assignment():
issue = item(7, labels=[monitor._ACTION_LABEL], assignees=['alice'])
issue['user'] = {'login': 'DouweM'}
client = FakeClient({7: issue})
client.permissions = {'DouweM': 'admin', 'alice': 'write'}
assert monitor._ensure_recipients(client, 'r', issue, now=NOW) == ['alice']
assert not any(path.endswith('/assignees') for _, path, _ in client.calls)
def test_owner_selection_checks_each_participant_once_within_a_budget():
issue = item(7, labels=[monitor._ACTION_LABEL])
issue['comments'] = 2 * monitor._ITEM_PROBE_LIMIT
client = FakeClient({7: issue})
client.permissions = {'DouweM': 'admin'}
client.comments[7] = [
*[{'user': {'login': f'contributor-{number % 4}'}} for number in range(2 * monitor._ITEM_PROBE_LIMIT)],
{'user': {'login': 'DouweM'}},
]
assert monitor._first_maintainer_in_discussion(client, 'r', issue) == ('DouweM', True)
# `contributor` authored the issue and each of the four repeats is resolved
# once, so a long thread costs a handful of requests rather than one per comment.
assert len(client.permission_reads()) == 6
def test_a_flood_of_unknown_participants_defers_instead_of_reassigning():
# Padding a discussion with throwaway accounts must not be a way to push an
# item off its real owner, so an exhausted sweep decides nothing.
issue = item(7, labels=[monitor._ACTION_LABEL])
flood = 2 * monitor._ITEM_PROBE_LIMIT
issue['comments'] = flood + 1
client = FakeClient({7: issue})
client.permissions = {'DouweM': 'admin'}
client.comments[7] = [
*[{'user': {'login': f'contributor-{number}'}} for number in range(flood)],
{'user': {'login': 'DouweM'}},
]
assert monitor._first_maintainer_in_discussion(client, 'r', issue) == (None, False)
assert len(client.permission_reads()) == monitor._ITEM_PROBE_LIMIT
assert monitor._ensure_recipients(client, 'r', issue, now=NOW) is None
assert not any(path.endswith('/assignees') for _, path, _ in client.calls)
# The quota is per item, so the flood cannot hide the next item's maintainer.
followup = item(8)
followup['user'] = {'login': 'DouweM'}
assert monitor._first_maintainer_in_discussion(client, 'r', followup) == ('DouweM', True)
def test_a_truncated_discussion_defers_instead_of_reassigning():
issue = item(7, labels=[monitor._ACTION_LABEL])
issue['comments'] = 1
client = FakeClient({7: issue})
client.comments[7] = [{'user': {'login': 'contributor-1'}}]
client.truncated = {7}
assert monitor._first_maintainer_in_discussion(client, 'r', issue) == (None, False)
assert monitor._ensure_recipients(client, 'r', issue, now=NOW) is None
def test_apply_defers_an_item_whose_owner_cannot_be_identified(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7)})
client.truncated = {7}
client.items[7]['comments'] = 1
client.comments[7] = [{'user': {'login': 'contributor-1'}}]
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: deferred until its owner can be identified'
]
assert not any(path.endswith('/assignees') for _, path, _ in client.calls)
def test_first_pages_truncates_a_huge_thread_instead_of_aborting(monkeypatch: pytest.MonkeyPatch):
# A thread longer than the page cap must cost a bounded prefix, not raise
# and take down every other item in the run with it.
client = monitor.GitHubClient('token')
seen: list[str] = []
def request(method: str, path: str, payload: object = None) -> tuple[Any, str]:
seen.append(path)
return [{'user': {'login': f'contributor-{len(seen)}'}}], '<https://api.github.com/x?page=99>; rel="next"'
monkeypatch.setattr(client, '_request', request)
entries, complete = client.first_pages('/repos/r/issues/7/comments', count=3)
assert len(entries) == 3
assert complete is False
assert len(seen) == 3
def test_maintainer_probes_stop_at_the_run_ceiling():
client = FakeClient()
client.permissions = {'DouweM': 'admin', 'alice': 'write'}
# Resolve one maintainer up front so it is cached before the ceiling is hit.
assert _MaintainerProbe(client).login('alice') == 'alice'
for index in range(monitor._RUN_PROBE_LIMIT - 1):
assert _MaintainerProbe(client).login(f'contributor-{index}') is None
assert len(client.permission_reads()) == monitor._RUN_PROBE_LIMIT
spent = _MaintainerProbe(client)
assert spent.login('DouweM') is None
assert spent.exhausted is True
# A cached login still answers, and does not make its sweep inconclusive.
cached = _MaintainerProbe(client)
assert cached.login('alice') == 'alice'
assert cached.exhausted is False
# Lookups that decide real state stay exact no matter how many probes ran.
assert client.maintainer_login('r', 'DouweM') == 'DouweM'
def test_owner_selection_treats_a_deleted_account_as_a_non_maintainer():
issue = item(7, labels=[monitor._ACTION_LABEL])
issue['user'] = {'login': 'ghost'}
client = FakeClient({7: issue})
client.deleted_logins = {'ghost'}
assert monitor._first_maintainer_in_discussion(client, 'r', issue) == (None, True)
def test_maintainer_lookup_is_cached_across_items():
client = FakeClient({7: item(7, assignees=['alice']), 8: item(8, assignees=['alice'])})
client.permissions = {'alice': 'admin'}
assert monitor._maintainer_assignees(client, 'r', client.items[7]) == ['alice']
assert monitor._maintainer_assignees(client, 'r', client.items[8]) == ['alice']
assert client.permission_reads() == ['/repos/r/collaborators/alice/permission']
def test_maintainer_lookup_can_refresh_a_cached_permission():
client = FakeClient()
client.permissions = {'alice': 'admin'}
assert client.maintainer_login('r', 'alice') == 'alice'
client.permissions['alice'] = 'read'
assert client.maintainer_login('r', 'alice') == 'alice'
assert client.maintainer_login('r', 'alice', refresh=True) is None
assert client.permission_reads() == [
'/repos/r/collaborators/alice/permission',
'/repos/r/collaborators/alice/permission',
]
def test_apply_pings_all_assigned_maintainers_without_reassigning(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7, assignees=['alice', 'bob', 'reader'])})
# `admin`/`write`/`read`/`none` are the only values the legacy permission
# field returns; `maintain` appears only in role_name, never here.
client.permissions = {'alice': 'admin', 'bob': 'write', 'reader': 'read'}
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: requested maintainer attention from @alice @bob'
]
assert not any(call[1].endswith('/assignees') for call in client.calls)
def test_ensure_labels_updates_a_stale_description_but_not_the_color():
client = FakeClient()
client.label_descriptions[monitor.COMMUNITY_LABEL] = 'An outdated description'
monitor.ensure_labels(client, 'r')
patches = [call for call in client.calls if call[0] == 'PATCH']
assert patches == [
('PATCH', '/repos/r/labels/community-backed', {'description': monitor._LABELS[monitor.COMMUNITY_LABEL][1]})
]
assert not any(call[0] == 'POST' for call in client.calls)
def test_apply_restarts_a_prior_terminal_escalation(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL])})
monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW)
assert any(call[0] == 'DELETE' and monitor._ESCALATED_LABEL in call[1] for call in client.calls)
assert any(call[0] == 'POST' and call[2] == {'labels': [monitor._ACTION_LABEL]} for call in client.calls)
def test_apply_records_settled_negative_without_requesting_attention(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'], next_actor='contributor')
client = FakeClient({7: item(7)})
assert monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW) == [
'#7: did not request maintainer attention'
]
assert not any(call[0] == 'POST' and call[1].endswith('/labels') for call in client.calls)
assert not any(call[1].endswith('/assignees') for call in client.calls)
def test_apply_leaves_uncertain_or_low_confidence_item_for_reconsideration(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'], next_actor='uncertain', confidence='high')
client = FakeClient({7: item(7)})
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: left unclassified for a future run'
]
write_output(output, ['7'], confidence='medium')
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: left unclassified for a future run'
]
def test_apply_rejects_numbers_outside_the_immutable_snapshot(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['8'])
client = FakeClient()
with pytest.raises(ValueError, match='outside the snapshot'):
monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW)
assert client.calls == []
def test_apply_requires_one_decision_per_candidate(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}, {'number': 8, 'updated_at': OLD}])
write_output(output, ['7'])
with pytest.raises(ValueError, match='classify every'):
monitor.apply_decisions(FakeClient(), 'r', str(output), str(snapshot), now=NOW)
def test_apply_abstains_when_item_changed_after_classification(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7, updated_at='2026-07-19T00:00:00Z')})
lines = monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW)
assert lines == ['#7: skipped because the item changed after classification']
assert not any(call[0] == 'POST' and '/issues/7/' in call[1] for call in client.calls)
def test_apply_fails_if_github_silently_ignores_assignment(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7)})
client.assignment_succeeds = False
with pytest.raises(RuntimeError, match=r'#7: RuntimeError: GitHub did not assign'):
monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW)
assert any(call[0] == 'POST' and call[1].endswith('/labels') for call in client.calls)
def test_apply_keeps_processing_after_one_item_fails(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 1, 'updated_at': OLD}, {'number': 2, 'updated_at': OLD}])
write_output(output, ['1', '2'])
client = FakeClient({1: item(1), 2: item(2)})
client.fail_get.add(1)
with pytest.raises(RuntimeError, match=r'#1: HTTPError'):
monitor.apply_decisions(client, 'pydantic/pydantic-ai', str(output), str(snapshot), now=NOW)
assert any(call[0] == 'POST' and call[1].endswith('/issues/2/labels') for call in client.calls)
def test_apply_rejects_unknown_actor_or_confidence(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'], next_actor='attacker')
with pytest.raises(ValueError, match=r"next_actor\s+Input should be 'maintainer'"):
monitor.apply_decisions(FakeClient({7: item(7)}), 'r', str(output), str(snapshot), now=NOW)
write_output(output, ['7'], confidence='certain')
with pytest.raises(ValueError, match=r"confidence\s+Input should be 'high'"):
monitor.apply_decisions(FakeClient({7: item(7)}), 'r', str(output), str(snapshot), now=NOW)
def test_apply_assigns_fallback_when_no_assignee_is_a_maintainer(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
client = FakeClient({7: item(7, assignees=['reader'])})
client.permissions = {'reader': 'read'}
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: requested maintainer attention from @adtyavrdhn'
]
assert ('POST', '/repos/r/issues/7/assignees', {'assignees': ['adtyavrdhn']}) in client.calls
def test_apply_skips_closed_or_already_actioned_items(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
output = tmp_path / 'output.json'
write_snapshot(snapshot, [{'number': 7, 'updated_at': OLD}])
write_output(output, ['7'])
closed = item(7)
closed['state'] = 'closed'
for changed in (closed, item(7, labels=[monitor._ACTION_LABEL])):
client = FakeClient({7: changed})
assert monitor.apply_decisions(client, 'r', str(output), str(snapshot), now=NOW) == [
'#7: skipped because the item changed after classification'
]
assert not any(call[0] == 'POST' and '/issues/7/' in call[1] for call in client.calls)
def notice_ref(
number: int,
stage: int,
*,
transition_id: int | str | None = None,
recipients: list[str] | None = None,
) -> dict[str, object]:
return {
'number': number,
'expected_stage': stage,
'transition_id': transition_id if transition_id is not None else f'default-stage-{stage}',
'recipients': recipients or [monitor._FALLBACK_OWNER],
}
def test_reconcile_queues_channel_reminder_for_assigned_maintainers():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['bob', 'alice'])})
client.permissions = {'alice': 'admin', 'bob': 'write'}
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW, notices=notices) == (
['#7: queued channel reminder'],
[],
)
assert notices == [
{
'number': 7,
'kind': 'reminder',
'expected_stage': 0,
'transition_id': 'default-stage-0',
'title': 'Item 7',
'recipients': ['alice', 'bob'],
'status': 'issue · no replies yet · going stale: no maintainer has touched it',
}
]
assert monitor._PINGED_LABEL not in {label['name'] for label in client.items[7]['labels']}
assert monitor.finalize_notices(
client,
'pydantic/pydantic-ai',
monitor._notice_refs({'items': [notice_ref(7, 0, recipients=['alice', 'bob'])]}),
now=NOW,
) == ['#7: recorded channel reminder']
assert monitor._PINGED_LABEL in {label['name'] for label in client.items[7]['labels']}
def test_reconcile_hands_a_placeholder_assignment_to_the_first_maintainer_participant():
# Only the agent-marked lane uses the placeholder heuristic; its stage-0
# notice stays gated, so the handoff is bookkeeping without a ping.
issue = item(4261, labels=[monitor._ACTION_LABEL], assignees=[monitor._FALLBACK_OWNER])
issue['comments'] = 2
client = FakeClient({4261: issue})
client.permissions = {'DouweM': 'admin', 'dsfaccini': 'write'}
client.comments[4261] = [
{'user': {'login': 'DouweM'}, 'created_at': '2026-02-09T16:48:57Z'},
{'user': {'login': 'dsfaccini'}, 'created_at': '2026-07-01T19:00:34Z'},
]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == ([], [])
assert notices == []
assert ('POST', '/repos/r/issues/4261/assignees', {'assignees': ['DouweM']}) in client.calls
assert [assignee['login'] for assignee in client.items[4261]['assignees']] == ['DouweM']
def test_reminder_lane_pings_the_real_owner_without_reassigning():
# A sole-fallback assignment on a priority issue is a routing decision,
# not the monitor's placeholder: nobody gets swapped in.
issue = item(4262, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=[monitor._FALLBACK_OWNER])
client = FakeClient({4262: issue})
client.permissions = {monitor._FALLBACK_OWNER: 'admin', 'DouweM': 'admin'}
client.comments[4262] = [{'user': {'login': 'DouweM'}, 'created_at': '2026-02-09T16:48:57Z'}]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (
['#4262: queued channel reminder'],
[],
)
assert notices[0]['recipients'] == [monitor._FALLBACK_OWNER]
assert not any(call[0] == 'POST' and call[1].endswith('/assignees') for call in client.calls)
def test_reminder_lane_stands_down_once_its_owner_is_unassigned():
client = FakeClient({4263: item(4263, labels=[monitor._ACTION_LABEL, 'p:1-highest'])})
notices: list[monitor.Notice] = []
lines, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
assert lines == ['#4263: stood down after its owner was unassigned']
assert failures == [] and notices == []
assert monitor._ACTION_LABEL not in {label['name'] for label in client.items[4263]['labels']}
def test_reconcile_drops_a_notice_if_the_owner_changes_before_queueing():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL], assignees=[monitor._FALLBACK_OWNER])})
original_get = client.get
item_reads = 0
def get(path: str) -> Any:
nonlocal item_reads
if path.endswith('/issues/7'):
item_reads += 1
if item_reads == 3:
client.items[7]['assignees'] = []
return original_get(path)
client.get = get # type: ignore[method-assign]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == ([], [])
assert notices == []
def test_reconcile_queues_channel_escalation_without_advancing_before_delivery():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL, 'p:1-highest'], assignees=['alice'])}
)
client.permissions['alice'] = 'write'
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW, notices=notices) == (
['#7: queued channel escalation'],
[],
)
assert notices[0]['kind'] == 'escalation'
assert monitor._ESCALATED_LABEL not in {label['name'] for label in client.items[7]['labels']}
ref = monitor.NoticeRef(
number=notices[0]['number'],
expected_stage=notices[0]['expected_stage'],
transition_id=notices[0]['transition_id'],
recipients=notices[0]['recipients'],
)
assert monitor.finalize_notices(client, 'pydantic/pydantic-ai', [ref], now=NOW) == [
'#7: recorded channel escalation'
]
assert monitor._ESCALATED_LABEL in {label['name'] for label in client.items[7]['labels']}
def test_reconcile_retries_preexisting_pending_escalation():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, *monitor._STAGE_LABELS, 'p:1-highest'], assignees=['alice'])}
)
client.permissions['alice'] = 'write'
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (
['#7: queued channel escalation'],
[],
)
assert notices[0]['expected_stage'] == 2
ref = monitor.NoticeRef(
number=notices[0]['number'],
expected_stage=notices[0]['expected_stage'],
transition_id=notices[0]['transition_id'],
recipients=notices[0]['recipients'],
)
assert monitor.finalize_notices(client, 'r', [ref], now=NOW) == ['#7: recorded channel escalation']
assert monitor._ACTION_LABEL not in {label['name'] for label in client.items[7]['labels']}
def test_reconcile_finishes_a_delivered_escalation_receipt_without_reposting():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL, monitor._DELIVERED_LABEL])})
client.timelines[7] = [
label_event(monitor._PINGED_LABEL),
label_event(monitor._DELIVERED_LABEL),
]
assert monitor.reconcile(client, 'r', now=NOW) == (
['#7: finished delivered channel escalation'],
[],
)
assert {label['name'] for label in client.items[7]['labels']} == {monitor._ESCALATED_LABEL}
def test_reconcile_ignores_a_foreign_delivery_receipt():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, monitor._DELIVERED_LABEL, 'p:1-highest'], assignees=['alice'])}
)
client.permissions['alice'] = 'write'
client.timelines[7] = [
label_event(monitor._ACTION_LABEL),
label_event(monitor._DELIVERED_LABEL, actor='maintainer'),
]
assert monitor.reconcile(client, 'r', now=NOW)[0] == ['#7: queued channel reminder']
def test_terminal_stage_preserves_the_reminder_acknowledgement_boundary():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._ESCALATED_LABEL])})
client.timelines[7] = [
label_event(monitor._PINGED_LABEL),
{
'event': 'commented',
'created_at': '2026-07-18T00:00:00Z',
'actor': {'login': monitor._FALLBACK_OWNER},
'body': 'I will handle this.',
},
label_event(monitor._ESCALATED_LABEL, created_at='2026-07-19T00:00:00Z'),
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_terminal_stage_rechecks_acknowledgement_after_owner_selection():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._ESCALATED_LABEL])})
initial = client.last_pages
timeline_reads = 0
def last_pages(path: str, *, count: int = 1) -> list[dict[str, Any]]:
nonlocal timeline_reads
values = initial(path, count=count)
if '/timeline' in path:
timeline_reads += 1
if timeline_reads == 2:
return [
*values,
{
'event': 'commented',
'created_at': '2026-07-18T00:00:00Z',
'actor': {'login': monitor._FALLBACK_OWNER},
},
]
return values
client.last_pages = last_pages # type: ignore[method-assign]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (
['#7: maintainer acknowledged the request'],
[],
)
assert notices == []
def test_terminal_finalize_retry_does_not_repost_the_delivered_escalation():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL], assignees=[monitor._FALLBACK_OWNER])}
)
client.fail_delete_labels.add(monitor._ACTION_LABEL)
with pytest.raises(RuntimeError, match='Failed to finalize attention'):
monitor.finalize_notices(client, 'r', monitor._notice_refs({'items': [notice_ref(7, 1)]}), now=NOW)
assert {'labels': [monitor._ESCALATED_LABEL, monitor._DELIVERED_LABEL]} in [call[2] for call in client.calls]
assert {label['name'] for label in client.items[7]['labels']} == {
monitor._ACTION_LABEL,
*monitor._STAGE_LABELS,
monitor._DELIVERED_LABEL,
}
client.fail_delete_labels.clear()
client.timelines[7] = [
label_event(monitor._ESCALATED_LABEL),
label_event(monitor._DELIVERED_LABEL),
]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (
['#7: finished delivered channel escalation'],
[],
)
assert notices == []
@pytest.mark.parametrize(
('labels', 'ref'),
[
([monitor._ACTION_LABEL, monitor._PINGED_LABEL], notice_ref(7, 0)),
([monitor._ACTION_LABEL], notice_ref(7, 0, transition_id='replacement-transition')),
([monitor._ACTION_LABEL], notice_ref(7, 0, recipients=['different-owner'])),
],
)
def test_finalize_skips_a_stale_notice(labels: list[str], ref: dict[str, object]):
client = FakeClient({7: item(7, labels=labels, assignees=[monitor._FALLBACK_OWNER])})
assert monitor.finalize_notices(client, 'r', monitor._notice_refs({'items': [ref]}), now=NOW) == []
assert {label['name'] for label in client.items[7]['labels']} == set(labels)
def test_prepare_notices_filters_stale_owners_immediately_before_delivery():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL], assignees=[monitor._FALLBACK_OWNER])})
refs = monitor._notice_refs({'items': [notice_ref(7, 0)]})
assert [notice['number'] for notice in monitor.prepare_notices(client, 'r', refs, now=NOW)] == [7]
client.permissions['bob'] = 'write'
client.items[7]['assignees'].append({'login': 'bob'})
assert monitor.prepare_notices(client, 'r', refs, now=NOW) == []
client.items[7]['assignees'] = []
assert monitor.prepare_notices(client, 'r', refs, now=NOW) == []
@pytest.mark.parametrize(
'contents',
[
[7],
{'items': [notice_ref(7, 0)], 'extra': 1},
{'items': 7},
{'items': [True]},
{'items': ['7']},
{'items': [notice_ref(0, 0)]},
{'items': [notice_ref(7, 0), notice_ref(7, 0)]},
{'items': [notice_ref(number, 0) for number in range(1, monitor._RECONCILE_LIMIT + 2)]},
{'items': [notice_ref(7, 3)]},
{'items': [notice_ref(7, 0, transition_id='')]},
{'items': [notice_ref(7, 0, recipients=['bad login'])]},
],
)
def test_notice_input_rejects_invalid_shapes(contents: object):
with pytest.raises(ValueError, match='Notice'):
monitor._notice_refs(contents)
def test_snapshot_and_decision_batch_limits_are_enforced(tmp_path: Path):
snapshot = tmp_path / 'snapshot.json'
write_snapshot(snapshot, [{'number': n, 'updated_at': OLD} for n in range(1, monitor._CANDIDATE_LIMIT + 2)])
with pytest.raises(ValueError, match='candidate limit'):
monitor.snapshot_candidates(str(snapshot), limit=monitor._CANDIDATE_LIMIT)
output = tmp_path / 'output.json'
write_output(output, [str(n) for n in range(1, monitor._CANDIDATE_LIMIT + 2)])
with pytest.raises(ValueError, match='too many or duplicate'):
monitor.agent_items(
str(output), monitor.Decision, tag='record_attention_decision', limit=monitor._CANDIDATE_LIMIT
)
def test_notice_output_pings_the_owner_directly(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
output = tmp_path / 'github-output'
monkeypatch.setenv('GITHUB_OUTPUT', str(output))
monkeypatch.setenv(
'PYDANTIC_AI_TRIAGE_SLACK_MENTIONS',
json.dumps({'adtyavrdhn': '<@UADITYA>', 'DouweM': '<@UDOUWE>'}),
)
monitor._write_notices(
'pydantic/pydantic-ai',
[
{
'number': 7,
'kind': 'reminder',
'expected_stage': 0,
'transition_id': 'event-7',
'title': 'Streaming hangs',
'recipients': ['DouweM'],
'status': 'issue · no replies yet',
}
],
)
values = dict(line.split('=', 1) for line in output.read_text(encoding='utf-8').splitlines())
text = json.loads(values['slack_payload'])['text']
assert 'owner <@UDOUWE>' in text
assert '<!channel>' not in text
def test_invalid_mention_map_degrades_to_plain_names_and_records_a_failure(monkeypatch: pytest.MonkeyPatch):
# A typo in the mentions variable would otherwise disable every owner ping
# while runs stay green; recording a failure trips the alert instead.
monkeypatch.setenv('PYDANTIC_AI_TRIAGE_SLACK_MENTIONS', 'not json')
failures: list[str] = []
assert monitor._notice_mentions(failures) == {}
assert failures and failures[0].startswith('mention map:')
def test_reconcile_marks_assigned_priority_issues_for_attention():
client = FakeClient(
{
7: item(7, labels=['p:1-highest'], assignees=['alice']),
8: item(8, labels=['p:2-high']),
}
)
client.permissions = {'alice': 'write'}
lines, failures = monitor.reconcile(client, 'r', now=NOW, notices=[])
assert failures == []
assert '#7: queued assigned p:1-highest issue for owner attention' in lines
assert monitor._ACTION_LABEL in {label['name'] for label in client.items[7]['labels']}
# The unassigned p:2 issue stays on the automation's plate.
assert monitor._ACTION_LABEL not in {label['name'] for label in client.items[8]['labels']}
@pytest.mark.parametrize('extra_labels', [[], [monitor.COMMUNITY_LABEL]])
def test_non_priority_attention_items_never_ping(extra_labels: list[str]):
"""Only assigned P1/P2 issues may interrupt a maintainer in Slack."""
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, *extra_labels], assignees=['alice'])})
client.permissions = {'alice': 'write'}
notices: list[monitor.Notice] = []
lines, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
assert failures == []
assert notices == []
assert all('queued channel' not in line for line in lines)
def test_priority_reminder_windows_follow_the_label():
def build(number: int, reminder_label: str, owner_quiet_days: int) -> FakeClient:
commented_at = (NOW - dt.timedelta(days=owner_quiet_days)).strftime('%Y-%m-%dT%H:%M:%SZ')
client = FakeClient({number: item(number, labels=[reminder_label], assignees=['alice'])})
client.permissions = {'alice': 'write'}
client.timelines[number] = [
{'event': 'commented', 'actor': {'login': 'alice'}, 'created_at': commented_at},
label_event(monitor._ACTION_LABEL, created_at=NOW.strftime('%Y-%m-%dT%H:%M:%SZ')),
]
return client
def queued(number: int, reminder_label: str, owner_quiet_days: int) -> list[int]:
notices: list[monitor.Notice] = []
_, failures = monitor.reconcile(build(number, reminder_label, owner_quiet_days), 'r', now=NOW, notices=notices)
assert failures == []
return [notice['number'] for notice in notices]
# The window measures the *owner's* quiet, from their own last comment.
# Four days is past the 3-day P1 window and inside the 5-day P2 window. A
# breached item is marked and pinged in the same pass.
assert queued(7, 'p:1-highest', 4) == [7]
assert queued(9, 'p:2-high', 4) == []
def test_non_priority_pinged_item_stands_down_before_escalation():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL], assignees=['alice'])})
client.permissions['alice'] = 'write'
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (
['#7: completed after losing reminder priority'],
[],
)
assert notices == []
assert {label['name'] for label in client.items[7]['labels']} == set()
def test_community_chatter_neither_suppresses_nor_hastens_the_owner_reminder():
def build(owner_comment_days: int, contributor_comment_days: int) -> FakeClient:
client = FakeClient({7: item(7, labels=['p:1-highest'], assignees=['alice'])})
client.permissions = {'alice': 'write'}
client.timelines[7] = [
{
'event': 'commented',
'actor': {'login': 'alice'},
'created_at': (NOW - dt.timedelta(days=owner_comment_days)).strftime('%Y-%m-%dT%H:%M:%SZ'),
},
{
'event': 'commented',
'actor': {'login': 'impatient-user'},
'created_at': (NOW - dt.timedelta(days=contributor_comment_days)).strftime('%Y-%m-%dT%H:%M:%SZ'),
},
label_event(monitor._ACTION_LABEL, created_at=NOW.strftime('%Y-%m-%dT%H:%M:%SZ')),
]
return client
def marked(client: FakeClient) -> bool:
_, failures = monitor.reconcile(client, 'r', now=NOW, notices=[])
assert failures == []
return monitor._ACTION_LABEL in {label['name'] for label in client.items[7]['labels']}
# Daily "any update?" comments must not shield a silent owner...
assert marked(build(owner_comment_days=4, contributor_comment_days=0)) is True
# ...and a responsive owner is not pinged, however loud the thread is.
assert marked(build(owner_comment_days=1, contributor_comment_days=0)) is False
def test_owner_activity_other_than_comments_counts_as_responding():
# An owner who triages daily through labels but last commented long ago
# must not be pinged and re-pinged: the quiet rule counts the same actions
# acknowledgment does, or the two would disagree and loop.
client = FakeClient({7: item(7, labels=['p:1-highest'], assignees=['alice'])})
client.permissions = {'alice': 'write'}
client.timelines[7] = [
{
'event': 'commented',
'actor': {'login': 'alice'},
'created_at': (NOW - dt.timedelta(days=6)).strftime('%Y-%m-%dT%H:%M:%SZ'),
},
{
'event': 'labeled',
'actor': {'login': 'alice'},
'created_at': (NOW - dt.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
'label': {'name': 'bug'},
},
]
_, failures = monitor.reconcile(client, 'r', now=NOW, notices=[])
assert failures == []
assert monitor._ACTION_LABEL not in {label['name'] for label in client.items[7]['labels']}
def test_agent_marked_reminder_items_respect_the_owner_quiet_window():
# The triage agent can mark an assigned p:2 issue after 3 total-quiet days;
# the owner-quiet gate at the notice seam still holds the ping until the
# owner has been silent for the label's full 5-day window.
client = FakeClient({7: item(7, labels=['p:2-high', monitor._ACTION_LABEL], assignees=['alice'])})
client.permissions = {'alice': 'write'}
client.timelines[7] = [
{
'event': 'commented',
'actor': {'login': 'alice'},
'created_at': (NOW - dt.timedelta(days=3)).strftime('%Y-%m-%dT%H:%M:%SZ'),
},
label_event(monitor._ACTION_LABEL, created_at=(NOW - dt.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ')),
]
notices: list[monitor.Notice] = []
_, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
assert failures == []
assert notices == []
def test_bot_unassignments_do_not_back_off_the_placeholder_heuristic():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.permissions = {'samuelcolvin': 'admin'}
client.comments[7] = [{'user': {'login': 'samuelcolvin'}, 'created_at': OLD}]
client.timelines[7] = [
label_event(monitor._ACTION_LABEL),
{
'event': 'unassigned',
'created_at': (NOW - dt.timedelta(days=2)).strftime('%Y-%m-%dT%H:%M:%SZ'),
'actor': {'login': 'samuelcolvin'},
'assignee': {'login': 'samuelcolvin'},
'assigner': {'login': 'github-actions[bot]', 'type': 'Bot'},
},
]
_, failures = monitor.reconcile(client, 'r', now=NOW)
# The sweep's own unassignment is cleanup, not a decision to back off
# from: the discussion heuristic may still hand the item to a maintainer.
assert failures == []
assert any(method == 'POST' and path.endswith('/assignees') for method, path, _ in client.calls)
def test_placeholder_heuristic_backs_off_after_a_recent_unassignment():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
# A maintainer outside the routing roster: the back-off must probe live
# permissions, because the discussion heuristic would recognize (and
# re-assign) exactly the same person.
client.permissions = {'samuelcolvin': 'admin'}
client.timelines[7] = [
label_event(monitor._ACTION_LABEL),
{
'event': 'commented',
'actor': {'login': 'samuelcolvin'},
'created_at': OLD,
},
{
'event': 'unassigned',
'created_at': (NOW - dt.timedelta(days=2)).strftime('%Y-%m-%dT%H:%M:%SZ'),
'actor': {'login': 'samuelcolvin'},
'assignee': {'login': 'samuelcolvin'},
'assigner': {'login': 'samuelcolvin'},
},
]
notices: list[monitor.Notice] = []
_, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
# He took himself off two days ago: the discussion heuristic must not
# hand the item straight back to him.
assert failures == []
assert notices == []
assert not any(method == 'POST' and path.endswith('/assignees') for method, path, _ in client.calls)
def test_owner_quiet_check_skips_events_without_timestamps():
# PR timelines open with `committed` events that carry no created_at; the
# coverage anchor must skip them instead of reading them as owner activity
# and silencing the reminder forever.
client = FakeClient({7: item(7, labels=['p:1-highest'], assignees=['alice'])})
client.permissions = {'alice': 'write'}
client.timelines[7] = [
{'event': 'committed'},
{
'event': 'commented',
'actor': {'login': 'alice'},
'created_at': (NOW - dt.timedelta(days=4)).strftime('%Y-%m-%dT%H:%M:%SZ'),
},
label_event(monitor._ACTION_LABEL, created_at=NOW.strftime('%Y-%m-%dT%H:%M:%SZ')),
]
notices: list[monitor.Notice] = []
_, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
assert failures == []
assert [notice['number'] for notice in notices] == [7]
def test_notice_output_is_actionable_and_escapes_untrusted_titles(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
output = tmp_path / 'github-output'
monkeypatch.setenv('GITHUB_OUTPUT', str(output))
monitor._write_notices(
'pydantic/pydantic-ai',
[
{
'number': 7,
'kind': 'reminder',
'expected_stage': 0,
'transition_id': 'event-7',
'title': 'Handle <unsafe>\n*fake owner* | <!channel>',
'recipients': ['DouweM'],
'status': 'issue · opened by @evil <!channel> · 2 replies · last from @evil 5d ago (author)',
}
],
)
values = dict(line.split('=', 1) for line in output.read_text(encoding='utf-8').splitlines())
assert values['has_notices'] == 'true'
assert json.loads(values['notice_items']) == [notice_ref(7, 0, transition_id='event-7', recipients=['DouweM'])]
text = json.loads(values['slack_payload'])['text']
# The notice targets its owner; the channel as a whole is never pinged.
assert '<!channel>' not in text
assert '*Maintainer attention requested in pydantic/pydantic-ai*' in text
assert '#7 Handle &lt;unsafe&gt; fake owner &lt;!channel&gt;' in text
assert 'owner @DouweM' in text
# A login is the only untrusted value the status line carries, and it is
# escaped on the same path as the title.
assert 'opened by @evil &lt;!channel&gt; · 2 replies · last from @evil 5d ago (author)' in text
assert 'why: it has been waiting on its owner past the reminder window' in text
assert '*Expected action:*' in text
assert 'If no work is needed, say so briefly' in text
assert 'Do not remove the attention labels' in text
class CensusClient(FakeClient):
"""Search counts only: the whole surface the coverage heartbeat touches."""
def __init__(self, counts: dict[str, int], *, stale_page: list[dict[str, Any]] | None = None) -> None:
super().__init__()
self.permissions = {owner: 'write' for owner in TRIAGE_OWNERS}
self.counts = counts
self.stale_page = stale_page or []
self.gate_first: int | None = None
self.correction_matches: list[dict[str, Any]] = []
def get(self, path: str) -> Any:
if '/collaborators/' in path:
return super().get(path)
raise AssertionError(path)
def last_page(self, path: str) -> list[dict[str, Any]]:
if int(path.split('/issues/')[1].split('/')[0]) in self.timelines:
return super().last_page(path)
self.calls.append(('LAST', path, None))
return []
def post(self, path: str, payload: dict[str, object]) -> Any:
self.calls.append(('POST', path, payload))
assert path == '/graphql'
variables = payload['variables']
assert isinstance(variables, dict)
terms = str(variables['query'])
if terms.endswith(' sort:updated-desc'):
return {'data': {'search': {'issueCount': len(self.correction_matches), 'nodes': self.correction_matches}}}
oldest = terms.endswith(' sort:created-asc')
if oldest:
terms = terms.removesuffix(' sort:created-asc')
assert terms == monitor._gate_query('pydantic/pydantic-ai', TRIAGE_OWNERS)
nodes = []
if oldest:
self.gate_first = int(str(variables['first']))
nodes = [{'number': item['number'], 'createdAt': item['created_at']} for item in self.stale_page]
return {'data': {'search': {'issueCount': self.counts[terms], 'nodes': nodes}}}
class WeeklyClient(FakeClient):
"""Implement GitHub's small search subset used by the weekly digest."""
def __init__(self, items: dict[int, dict[str, Any]] | None = None) -> None:
super().__init__(items)
self.permissions = {owner: 'write' for owner in TRIAGE_OWNERS}
def get(self, path: str) -> Any:
if not path.startswith('/search/issues?'):
return super().get(path)
self.calls.append(('GET', path, None))
query = urllib.parse.parse_qs(urllib.parse.urlparse(path).query)
terms = query['q'][0]
positive = set(re.findall(r'(?<!-)label:"([^"]+)"', terms))
negative = set(re.findall(r'-label:"([^"]+)"', terms))
values = []
owner_match = re.search(r'(?<!-)assignee:([^ ]+)', terms)
excluded_owners = {value.casefold() for value in re.findall(r'-assignee:([^ ]+)', terms)}
for value in self.items.values():
labels = {str(label['name']) for label in value['labels']}
assignees = {str(assignee['login']).casefold() for assignee in value['assignees']}
if (
value['state'] == 'open'
and positive <= labels
and not negative.intersection(labels)
and (owner_match is None or owner_match.group(1).casefold() in assignees)
and not excluded_owners.intersection(assignees)
and ('is:pr' not in terms or 'pull_request' in value)
and (re.search(r'(?<!-)draft:true', terms) is None or value.get('draft') is True)
and ('-draft:true' not in terms or value.get('draft') is not True)
):
created = str(value.get('created_at') or '')
if 'created:<' in terms and not created[:10] < terms.split('created:<', 1)[1].split()[0]:
continue
if 'created:>=' in terms and not created[:10] >= terms.split('created:>=', 1)[1].split()[0]:
continue
values.append(value)
values.sort(key=lambda value: str(value['updated_at']), reverse=query.get('order') == ['desc'])
per_page = int(query.get('per_page', ['30'])[0])
page = int(query.get('page', ['1'])[0])
start = (page - 1) * per_page
return {'total_count': len(values), 'items': values[start : start + per_page]}
def post(self, path: str, payload: dict[str, object]) -> Any:
if path != '/graphql':
return super().post(path, payload)
self.calls.append(('POST', path, payload))
variables = payload['variables']
assert isinstance(variables, dict)
terms = str(variables['query'])
first = int(variables['first'])
order = 'asc' if 'sort:updated-asc' in terms else 'desc'
terms = terms.replace(' sort:updated-asc', '')
result = self.get(
f'/search/issues?q={urllib.parse.quote_plus(terms)}&sort=updated&order={order}&per_page={first}'
)
return {
'data': {
'search': {
'issueCount': result['total_count'],
'nodes': [
{'number': value['number'], 'createdAt': value.get('created_at')} for value in result['items']
],
}
}
}
CENSUS_COUNTS = {
f'repo:pydantic/pydantic-ai is:open label:"{monitor._ACTION_LABEL}"': 14,
f'repo:pydantic/pydantic-ai is:open label:"{monitor._ESCALATED_LABEL}"': 9,
monitor._gate_query('pydantic/pydantic-ai', TRIAGE_OWNERS): 2,
monitor._untriaged_query('pydantic/pydantic-ai'): 240,
monitor._pull_intake_query('pydantic/pydantic-ai', TRIAGE_OWNERS): 3,
}
def test_census_counts_coverage_without_writing_or_reading_prose():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-20T00:00:00Z'}])
assert monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>') == (
'<@UADITYA> :rotating_light: Attention coverage for pydantic/pydantic-ai — '
'queue: 14 active, 9 cooling; assignment gate: 2 priority issues unassigned; '
'oldest #7740 in the gate 5d; triage pool: 240 unlabeled issues; PR intake: 3 unowned. '
'The Monday digest covers assigned, legacy, and draft work.'
)
# Count-only plus one event-page read vetting the oldest candidate: no
# writes, no REST search, no item bodies.
assert {method for method, _, _ in client.calls} == {'GET', 'POST', 'LAST'}
assert not any(path.startswith('/search/issues?') for _, path, _ in client.calls)
def test_census_reports_a_healthy_empty_intake_lane():
counts = {
**CENSUS_COUNTS,
monitor._gate_query('pydantic/pydantic-ai', TRIAGE_OWNERS): 0,
}
client = CensusClient(counts)
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
assert report.startswith(':telescope:')
assert 'assignment gate: 0 priority issues unassigned' in report
assert 'triage pool: 240 unlabeled issues' in report
assert 'PR intake: 3 unowned' in report
assert '<!channel>' not in report
def test_census_escalates_when_the_pull_intake_search_is_saturated():
counts = {
**CENSUS_COUNTS,
monitor._pull_intake_query('pydantic/pydantic-ai', TRIAGE_OWNERS): 101,
}
client = CensusClient(counts, stale_page=[{'number': 7800, 'created_at': '2026-08-25T00:00:00Z'}])
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
assert report.startswith('<@UADITYA> :rotating_light:')
assert 'PR intake: 101 unowned' in report
assert 'intake search saturated' in report
def test_census_escalates_when_the_assignment_gate_backs_up():
counts = {
**CENSUS_COUNTS,
monitor._gate_query('pydantic/pydantic-ai', TRIAGE_OWNERS): monitor._GATE_BATCH_BREACH + 1,
}
client = CensusClient(counts, stale_page=[{'number': 7800, 'created_at': '2026-08-25T00:00:00Z'}])
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
assert report.startswith('<@UADITYA> :rotating_light:')
assert f'assignment gate: {monitor._GATE_BATCH_BREACH + 1} priority issues unassigned' in report
def test_census_oldest_page_skips_a_deliberately_unassigned_issue():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-10T00:00:00Z'}])
client.timelines[7740] = [
{
'event': 'unassigned',
'created_at': '2026-08-24T00:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'adtyavrdhn'},
}
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
# A maintainer took this issue off someone's plate on purpose; paging about
# its age every morning would train everyone to ignore the heartbeat.
assert report.startswith(':telescope:')
assert 'oldest' not in report
def test_census_oldest_ignores_bot_and_cleanup_unassignments():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-20T00:00:00Z'}])
client.timelines[7740] = [
{
'event': 'unassigned',
'created_at': '2026-08-24T00:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'github-actions[bot]'},
},
{
'event': 'unassigned',
'created_at': '2026-08-24T01:00:00Z',
'actor': {'login': 'drive-by'},
'assignee': {'login': 'drive-by'},
'assigner': {'login': 'DouweM'},
},
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
# An automated sweep or stale-contributor cleanup is not a decision to
# leave the issue unassigned; the age alarm must keep firing.
assert 'oldest #7740' in report
def test_census_oldest_looks_past_vetoed_issues_across_the_whole_breach_window():
client = CensusClient(
CENSUS_COUNTS,
stale_page=[
{'number': 7740, 'created_at': '2026-08-10T00:00:00Z'},
{'number': 7741, 'created_at': '2026-08-24T12:00:00Z'},
],
)
client.timelines[7740] = [
{
'event': 'unassigned',
'created_at': '2026-08-24T00:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'adtyavrdhn'},
}
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
# Vetoed issues cannot suppress the alarm: the eligible one behind them is
# found, and the window spans the count-breach threshold.
assert 'oldest #7741' in report
assert client.gate_first == monitor._GATE_BATCH_BREACH
def test_census_gate_age_runs_from_the_priority_label_not_creation():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-10T00:00:00Z'}])
client.timelines[7740] = [{'event': 'labeled', 'created_at': '2026-08-24T18:00:00Z', 'label': {'name': 'p:2-high'}}]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
# Triage labeling an old backlog issue puts it in the gate *now*; its
# creation date must not fire the one-day breach.
assert report.startswith(':telescope:')
assert 'oldest #7740 in the gate 0d' in report
def test_census_oldest_is_the_stalest_in_gate_not_the_first_by_creation():
# The gate search sorts by creation, but the clock runs from the label: a
# just-labeled ancient issue must not mask a five-day-stale younger one.
client = CensusClient(
CENSUS_COUNTS,
stale_page=[
{'number': 100, 'created_at': '2023-01-01T00:00:00Z'},
{'number': 200, 'created_at': '2026-08-24T00:00:00Z'},
],
)
client.timelines[100] = [{'event': 'labeled', 'created_at': '2026-08-24T18:00:00Z', 'label': {'name': 'p:2-high'}}]
client.timelines[200] = [
{'event': 'labeled', 'created_at': '2026-08-20T00:00:00Z', 'label': {'name': 'p:1-highest'}}
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
assert report.startswith('<@UADITYA> :rotating_light:')
assert 'oldest #200 in the gate 5d' in report
def test_census_gate_clock_survives_a_priority_escalation_swap():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2023-01-01T00:00:00Z'}])
client.timelines[7740] = [
{'event': 'labeled', 'created_at': '2026-08-20T00:00:00Z', 'label': {'name': 'p:2-high'}},
{'event': 'unlabeled', 'created_at': '2026-08-24T12:00:00Z', 'label': {'name': 'p:2-high'}},
{'event': 'labeled', 'created_at': '2026-08-24T12:00:30Z', 'label': {'name': 'p:1-highest'}},
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
# Escalating p:2 → p:1 is more urgency, not a fresh arrival: the clock
# keeps running from the original gate entry.
assert 'oldest #7740 in the gate 5d' in report
def test_census_rejects_an_untrusted_urgent_mention():
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-20T00:00:00Z'}])
with pytest.raises(ValueError, match='valid Aditya Slack mention'):
monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<!channel>')
def test_census_reports_daily_corrections_and_emits_telemetry(monkeypatch: pytest.MonkeyPatch):
events: list[tuple[str, dict[str, Any]]] = []
monkeypatch.setattr(monitor, '_emit_event', lambda name, **attrs: events.append((name, attrs)))
client = CensusClient(CENSUS_COUNTS, stale_page=[{'number': 7740, 'created_at': '2026-08-20T00:00:00Z'}])
client.correction_matches = [{'number': 5}]
client.timelines[5] = [
{
# Outside the daily window: never a correction record, but it still
# proves the removed assignment was made by automation. GitHub's
# real event shape: `actor` mirrors the assignee, `assigner` acted.
'event': 'assigned',
'created_at': '2026-08-20T00:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'github-actions[bot]'},
},
{
'event': 'unassigned',
'created_at': '2026-08-24T12:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'DouweM'},
},
{
'event': 'labeled',
'created_at': '2026-08-24T13:00:00Z',
'actor': {'login': 'DouweM'},
'label': {'name': 'p:3-mid'},
},
# A human assigned dsfaccini, a human removed them: recorded for the
# event stream, excluded from the correction count.
{
'event': 'assigned',
'created_at': '2026-08-20T01:00:00Z',
'actor': {'login': 'dsfaccini'},
'assignee': {'login': 'dsfaccini'},
'assigner': {'login': 'DouweM'},
},
{
'event': 'unassigned',
'created_at': '2026-08-24T14:00:00Z',
'actor': {'login': 'dsfaccini'},
'assignee': {'login': 'dsfaccini'},
'assigner': {'login': 'adtyavrdhn'},
},
]
report = monitor.census(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW, urgent_mention='<@UADITYA>')
assert 'Maintainer corrections in the last day: 2 on #5.' in report
corrections = [attrs for name, attrs in events if name == 'triage.correction']
assert [attrs['kind'] for attrs in corrections] == ['unassigned', 'labeled', 'unassigned']
assert corrections[0]['bot_origin'] is True
assert corrections[0]['detail'] == 'DouweM'
assert corrections[1]['detail'] == 'p:3-mid'
assert corrections[2]['bot_origin'] is False
# The record names the performer (`assigner`), not the removed assignee.
assert corrections[2]['actor'] == 'adtyavrdhn'
assert all(attrs['number'] == 5 and attrs['event_id'] is not None for attrs in corrections)
assert [attrs for name, attrs in events if name == 'census.run'] == [
{
'repo': 'pydantic/pydantic-ai',
'active': 14,
'cooling': 9,
'gate_unassigned': 2,
'gate_oldest_age_days': 5,
'untriaged': 240,
'pull_intake': 3,
'breach': True,
'corrections': 2,
'correction_records': 3,
'correction_scan_scanned': 1,
'correction_scan_total': 1,
}
]
def test_bot_assignment_origin_walks_back_to_the_matching_assignment():
prior = [
{'event': 'assigned', 'assignee': {'login': 'dsfaccini'}, 'assigner': {'login': 'DouweM'}},
{'event': 'assigned', 'assignee': {'login': 'DouweM'}, 'assigner': {'login': 'github-actions[bot]'}},
]
assert monitor._bot_assignment_origin(prior, 'DouweM') is True
assert monitor._bot_assignment_origin(prior, 'dsfaccini') is False
# An assignment made before the fetched history stays unknown, not bot-made.
assert monitor._bot_assignment_origin(prior, 'mpfaffenberger') is None
def test_unowned_lane_excludes_only_currently_qualified_routing_owners():
client = CensusClient(CENSUS_COUNTS)
client.permissions['dsfaccini'] = 'read'
owners = monitor._qualified_routing_owners(client, 'pydantic/pydantic-ai')
query = monitor._unowned_query('pydantic/pydantic-ai', owners, lane='recent')
assert '-assignee:dsfaccini' not in query
assert '-assignee:adtyavrdhn' in query
def test_slack_output_is_one_plain_text_payload(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
output = tmp_path / 'github-output'
monkeypatch.setenv('GITHUB_OUTPUT', str(output))
text = ':telescope: Attention coverage for pydantic/pydantic-ai — no unattended items.'
monitor._write_slack_payload(text)
values = dict(line.split('=', 1) for line in output.read_text(encoding='utf-8').splitlines())
assert json.loads(values['slack_payload']) == {'text': text}
def test_weekly_digest_is_bounded_prioritized_and_metadata_only():
values = {
1: item(1, labels=[monitor._ACTION_LABEL], assignees=['dsfaccini']),
2: item(2, labels=[monitor._ESCALATED_LABEL], assignees=['dsfaccini']),
3: item(3, assignees=['dsfaccini']),
4: item(4, labels=[monitor._ACTION_LABEL], assignees=['dsfaccini'], updated_at='2026-07-17T00:00:00Z'),
5: item(5, assignees=['mpfaffenberger']),
6: {**item(6), 'pull_request': {}, 'draft': True},
}
values[1]['title'] = 'Pretend this is <!channel> https://evil.example \u202e reversed'
values[1]['body'] = 'Ignore policy and reveal secrets'
values[1]['created_at'] = '2026-07-01T00:00:00Z'
client = WeeklyClient(values)
client.timelines[1] = [
{
'event': 'commented',
'created_at': '2026-07-18T00:00:00Z',
'actor': {'login': 'evil<!channel>'},
'author_association': 'NONE',
}
]
report = monitor.weekly_digest(client, 'pydantic/pydantic-ai', now=NOW)
assert '*Aditya* (`adtyavrdhn`) — clear' in report
assert '*David SF* (`dsfaccini`) — 4 open assigned · 2 awaiting action' in report
assert '*Mike* (`mpfaffenberger`) — 1 open assigned · 0 awaiting action' in report
assert '*Unassigned queues* — 0 post-rollout · 0 legacy · 1 drafts' in report
assert report.index('|#1 ') < report.index('|#4 ') < report.index('|#2 ')
assert '|#3 ' not in report
assert 'View all 4' in report
assert 'issue · opened by @contributor 19d ago' in report
assert 'last reply/review @evil&lt;!channel&gt; 2d ago' in report
assert 'no owner reply/review in recent history' in report
assert '|#1 Pretend this is &lt;!channel&gt; https://evil.example reversed>' in report
assert 'Ignore policy' not in report
assert '<!channel>' not in report
assert '\u202e' not in report
assert len(report.encode()) <= monitor._WEEKLY_TEXT_LIMIT
graph_searches = [path for method, path, _ in client.calls if method == 'POST' and path == '/graphql']
assert len(graph_searches) <= 2 * len(monitor.MAINTAINER_OWNERS) + 4
assert len(client.permission_reads()) == len(monitor.MAINTAINER_OWNERS)
def test_weekly_status_counts_inline_review_comments_as_owner_interaction():
pull_request = {**item(1), 'pull_request': {}}
timeline = [
{
'event': 'line-commented',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'dsfaccini'},
}
]
status = monitor._weekly_status(pull_request, timeline, 'dsfaccini', now=NOW)
assert status == 'pull request · last reply/review @dsfaccini 24h ago · owner replied/reviewed 24h ago'
def test_weekly_digest_reports_maintainer_corrections_from_the_past_week():
client = WeeklyClient(
{
1: {**item(1), 'created_at': '2026-07-01T00:00:00Z'},
2: {**item(2), 'created_at': '2026-07-01T00:00:00Z'},
}
)
client.timelines[1] = [
{
'event': 'unlabeled',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'DouweM'},
'label': {'name': 'p:2-high'},
},
{
'event': 'labeled',
'created_at': '2026-07-19T00:05:00Z',
'actor': {'login': 'DouweM'},
'label': {'name': 'p:3-mid'},
},
{
'event': 'labeled',
'created_at': '2026-07-19T01:00:00Z',
'actor': {'login': 'pydanty'},
'label': {'name': 'p:1-highest'},
},
{
'event': 'labeled',
'created_at': '2026-07-19T02:00:00Z',
'actor': {'login': 'DouweM'},
'label': {'name': 'bug'},
},
{
# GitHub's real event shape: `actor` is the removed assignee,
# `assigner` is who acted. Out of the window, so excluded.
'event': 'unassigned',
'created_at': '2026-07-01T00:00:00Z',
'actor': {'login': 'dsfaccini'},
'assignee': {'login': 'dsfaccini'},
'assigner': {'login': 'DouweM'},
},
]
client.timelines[2] = [
{
'event': 'unassigned',
'created_at': '2026-07-18T00:00:00Z',
'actor': {'login': 'dsfaccini'},
'assignee': {'login': 'dsfaccini'},
'assigner': {'login': 'DouweM'},
},
{
# Removing a non-maintainer assignee is routine cleanup, not a
# correction of the routing automation.
'event': 'unassigned',
'created_at': '2026-07-18T01:00:00Z',
'actor': {'login': 'community-contributor'},
'assignee': {'login': 'community-contributor'},
'assigner': {'login': 'dsfaccini'},
},
{
# An automated unassignment is not a human correction.
'event': 'unassigned',
'created_at': '2026-07-18T02:00:00Z',
'actor': {'login': 'DouweM'},
'assignee': {'login': 'DouweM'},
'assigner': {'login': 'github-actions[bot]'},
},
]
report = monitor.weekly_digest(client, 'pydantic/pydantic-ai', now=NOW)
assert '*Maintainer corrections this week*' in report
assert '• #1: @DouweM removed `p:2-high`' in report
assert '• #1: @DouweM added `p:3-mid`' in report
assert '• #2: @DouweM unassigned @dsfaccini' in report
# Bot relabels, bot unassignments, non-priority labels, and events older
# than the window stay out.
assert report.count('unassigned') == 1
assert '@pydanty' not in report
assert '`bug`' not in report
assert 'community-contributor' not in report
assert 'none recorded' not in report
def test_weekly_corrections_stay_bounded_so_the_digest_always_ships():
client = WeeklyClient({1: {**item(1), 'created_at': '2026-07-01T00:00:00Z'}})
client.timelines[1] = [
{
'event': 'labeled',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'DouweM'},
'label': {'name': 'p:3-mid'},
}
for _ in range(monitor._OVERRIDE_LINE_LIMIT + 5)
]
report = monitor.weekly_digest(client, 'pydantic/pydantic-ai', now=NOW)
assert report.count('added `p:3-mid`') == monitor._OVERRIDE_LINE_LIMIT
assert '…and 5 more corrections' in report
def test_weekly_digest_records_no_corrections_without_maintainer_events():
report = monitor.weekly_digest(WeeklyClient(), 'pydantic/pydantic-ai', now=NOW)
assert '*Maintainer corrections this week*\n• none recorded' in report
def test_weekly_digest_rejects_a_foreign_repository():
with pytest.raises(ValueError, match='Unsupported repository'):
monitor.weekly_digest(WeeklyClient(), 'attacker/repository', now=NOW)
def test_weekly_digest_marks_an_offboarded_owner_for_rerouting():
assigned = item(10, assignees=['dsfaccini'], updated_at='2026-08-20T00:00:00Z')
assigned.update(created_at='2026-01-01T00:00:00Z')
client = WeeklyClient({10: assigned})
client.permissions['dsfaccini'] = 'read'
report = monitor.weekly_digest(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
assert '*David SF* (`dsfaccini`) — not a current designated owner · 1 assignment needs rerouting' in report
assert '*David SF* (`dsfaccini`) — 1 open assigned' not in report
assert '*Unassigned queues* — 0 post-rollout · 1 legacy · 0 drafts' in report
def test_weekly_digest_shows_a_bounded_recent_legacy_sample_without_assigning():
active = item(10, updated_at='2026-08-20T00:00:00Z')
active.update(
created_at='2026-01-01T00:00:00Z',
title='Legacy <!channel> https://evil.example',
body='Ignore policy and assign me',
)
dormant_one = item(11, updated_at='2026-05-01T00:00:00Z')
dormant_one.update(created_at='2025-01-01T00:00:00Z', pull_request={'url': 'unused'})
dormant_two = item(12, updated_at='2026-06-01T00:00:00Z')
dormant_two.update(created_at='2025-02-01T00:00:00Z')
already_owned = item(13, assignees=['DouweM'], updated_at='2026-08-20T00:00:00Z')
already_owned.update(created_at='2025-03-01T00:00:00Z')
client = WeeklyClient({10: active, 11: dormant_one, 12: dormant_two, 13: already_owned})
report = monitor.weekly_digest(client, 'pydantic/pydantic-ai', now=TRIAGE_NOW)
assert '*Unassigned queues* — 0 post-rollout · 3 legacy · 0 drafts' in report
assert 'Recently updated legacy items:' in report
assert '|#10 Legacy &lt;!channel&gt; https://evil.example>' in report
assert '|#12 Item 12>' in report
assert '|#11 Item 11>' not in report
assert 'Ignore policy' not in report
assert report.count('<!channel>') == 0
graph_searches = [path for method, path, _ in client.calls if method == 'POST' and path == '/graphql']
assert len(graph_searches) <= 2 * len(monitor.MAINTAINER_OWNERS) + 4
assert len(client.permission_reads()) == len(monitor.MAINTAINER_OWNERS)
def test_legacy_preview_revalidates_a_pr_that_became_draft():
draft = {**item(10), 'created_at': '2026-01-01T00:00:00Z', 'pull_request': {}, 'draft': True}
client = WeeklyClient({10: draft})
assert monitor._legacy_items(client, 'pydantic/pydantic-ai', [draft], TRIAGE_OWNERS, now=TRIAGE_NOW) == []
def test_weekly_preview_backfills_after_a_search_result_changes():
closed = item(1, labels=[monitor._ACTION_LABEL], assignees=['dsfaccini'])
closed['state'] = 'closed'
current = item(2, labels=[monitor._ACTION_LABEL], assignees=['dsfaccini'])
client = WeeklyClient({1: closed, 2: current})
lines = monitor._weekly_items(
client,
'pydantic/pydantic-ai',
'dsfaccini',
[item(1), item(2)],
set(),
attention_only=True,
limit=1,
now=NOW,
)
assert len(lines) == 1
assert '|#2 Item 2>' in lines[0]
def test_weekly_mode_writes_the_forwardable_payload(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
output = tmp_path / 'github-output'
monkeypatch.setattr(monitor, 'GitHubClient', lambda token: WeeklyClient())
monkeypatch.setattr(sys, 'argv', ['issue_pr_attention_monitor.py', 'weekly'])
monkeypatch.setenv('GITHUB_TOKEN', 'token')
monkeypatch.setenv('GITHUB_REPOSITORY', 'pydantic/pydantic-ai')
monkeypatch.setenv('GITHUB_OUTPUT', str(output))
monkeypatch.delenv('GITHUB_STEP_SUMMARY', raising=False)
assert monitor.main() == 0
payload = json.loads(dict(line.split('=', 1) for line in output.read_text().splitlines())['slack_payload'])
assert payload['text'].startswith(':spiral_calendar_pad: *Monday maintainer queues — pydantic/pydantic-ai*')
assert payload['text'].count('— clear') == len(monitor.MAINTAINER_OWNERS)
def test_census_mode_writes_the_heartbeat_payload(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
output = tmp_path / 'github-output'
monkeypatch.setattr(monitor, 'GitHubClient', lambda token: CensusClient(CENSUS_COUNTS))
monkeypatch.setattr(sys, 'argv', ['issue_pr_attention_monitor.py', 'census'])
monkeypatch.setenv('GITHUB_TOKEN', 'token')
monkeypatch.setenv('GITHUB_REPOSITORY', 'pydantic/pydantic-ai')
monkeypatch.setenv('GITHUB_OUTPUT', str(output))
monkeypatch.setenv('PYDANTIC_AI_TRIAGE_SLACK_MENTIONS', json.dumps({'adtyavrdhn': '<@UADITYA>'}))
monkeypatch.delenv('GITHUB_STEP_SUMMARY', raising=False)
assert monitor.main() == 0
values = dict(line.split('=', 1) for line in output.read_text(encoding='utf-8').splitlines())
assert json.loads(values['slack_payload']) == {
'text': ':telescope: Attention coverage for pydantic/pydantic-ai — queue: 14 active, 9 cooling; '
'assignment gate: 2 priority issues unassigned; triage pool: 240 unlabeled issues; '
'PR intake: 3 unowned. The Monday digest covers assigned, legacy, and draft work.'
}
@pytest.mark.parametrize('mentions', [None, '', '{', '{}'])
def test_healthy_census_does_not_require_a_slack_mention(
mentions: str | None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setattr(monitor, 'GitHubClient', lambda token: CensusClient(CENSUS_COUNTS))
monkeypatch.setattr(sys, 'argv', ['issue_pr_attention_monitor.py', 'census'])
monkeypatch.setenv('GITHUB_TOKEN', 'token')
monkeypatch.setenv('GITHUB_REPOSITORY', 'pydantic/pydantic-ai')
monkeypatch.setenv('GITHUB_OUTPUT', str(tmp_path / 'github-output'))
if mentions is None:
monkeypatch.delenv('PYDANTIC_AI_TRIAGE_SLACK_MENTIONS', raising=False)
else:
monkeypatch.setenv('PYDANTIC_AI_TRIAGE_SLACK_MENTIONS', mentions)
assert monitor.main() == 0
def test_reconcile_rejects_a_foreign_stage_label():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._ESCALATED_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'outside-collaborator'},
'label': {'name': monitor._ESCALATED_LABEL},
}
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: removed a foreign attention transition'], [])
assert any(call[0] == 'DELETE' and monitor._ACTION_LABEL in call[1] for call in client.calls)
def test_recent_activity_delays_the_next_reminder():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
}
]
assert monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW) == ([], [])
def test_maintainer_comment_completes_the_request():
client = FakeClient(
{
7: item(
7,
labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL, 'p:1-highest'],
assignees=[monitor._FALLBACK_OWNER],
)
}
)
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._PINGED_LABEL},
},
{
'event': 'commented',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': monitor._FALLBACK_OWNER},
'body': 'Decision made.',
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
assert sum(call[0] == 'DELETE' for call in client.calls) == 2
def test_member_acknowledgement_in_the_same_second_completes_the_request():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
{
# The real timeline API puts a review's author under `user`, not
# `actor` — exercising the `or event.get('user')` fallback in `_actor`.
'event': 'reviewed',
'submitted_at': OLD,
'user': {'login': 'another-maintainer'},
'author_association': 'MEMBER',
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_recipient_non_comment_event_completes_the_request():
# A recipient who labels, milestones, self-assigns, or closes while being
# reminded is engaging: any non-denylisted event by a recipient acknowledges.
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL], assignees=['alice'])})
client.permissions = {'alice': 'admin'}
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
{
'event': 'labeled',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'alice'},
'label': {'name': 'question'},
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_collaborator_comment_by_non_recipient_completes_the_request():
# An outside collaborator with repo access can acknowledge via a comment even
# when they are not one of the assigned recipients (COLLABORATOR association).
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
{
'event': 'commented',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'outside-collaborator'},
'author_association': 'COLLABORATOR',
'body': 'I can take this.',
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_inline_review_comment_by_non_recipient_maintainer_completes_the_request():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.timelines[7] = [
label_event(monitor._ACTION_LABEL),
{
'event': 'line-commented',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'outside-collaborator'},
'author_association': 'COLLABORATOR',
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_private_maintainer_reply_completes_the_request():
# `author_association` is computed for the caller, so a maintainer whose
# organization membership is private reports as CONTRIBUTOR. Trusting the
# association alone would keep reminding them about an item they answered.
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL])})
client.permissions = {'DouweM': 'admin'}
client.timelines[7] = [
label_event(monitor._ACTION_LABEL),
{
'event': 'commented',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'DouweM'},
'author_association': 'CONTRIBUTOR',
'body': 'Answered.',
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: maintainer acknowledged the request'], [])
def test_status_line_reports_the_last_reply_and_its_role():
issue = item(7, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['DouweM'])
issue['created_at'] = '2026-06-20T00:00:00Z'
issue['comments'] = 2
client = FakeClient({7: issue})
client.permissions = {'DouweM': 'admin'}
notices: list[monitor.Notice] = []
# The timeline holds only the newest pages, so engagement is asked of the
# discussion itself; a reply that fell off the window still counts.
client.comments[7] = [{'user': {'login': 'DouweM'}, 'created_at': '2026-07-01T00:00:00Z'}]
client.timelines[7] = [
label_event(monitor._ACTION_LABEL, created_at='2026-07-02T00:00:00Z'),
{
'event': 'commented',
'created_at': '2026-07-01T00:00:00Z',
'actor': {'login': 'DouweM'},
'author_association': 'CONTRIBUTOR',
},
{
'event': 'commented',
'created_at': '2026-07-01T12:00:00Z',
'actor': {'login': 'contributor'},
'author_association': 'NONE',
},
]
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (['#7: queued channel reminder'], [])
# A maintainer already replied, so the line stops short of "going stale".
assert notices[0]['status'] == (
'issue · opened by @contributor 30d ago · 2 comments · last from @contributor 18d ago (author)'
)
def test_status_line_does_not_count_a_bot_reply_as_engagement():
issue = item(7, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice'])
issue['created_at'] = '2026-07-01T00:00:00Z'
client = FakeClient({7: issue})
client.permissions['alice'] = 'write'
notices: list[monitor.Notice] = []
client.timelines[7] = [
label_event(monitor._ACTION_LABEL, created_at='2026-07-02T00:00:00Z'),
{
'event': 'commented',
'created_at': '2026-07-03T00:00:00Z',
'actor': {'login': 'pydanty[bot]', 'type': 'Bot'},
'author_association': 'CONTRIBUTOR',
},
]
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == (['#7: queued channel reminder'], [])
assert notices[0]['status'] == (
'issue · opened by @contributor 19d ago · last from @pydanty[bot] 17d ago (bot)'
' · going stale: no maintainer has touched it'
)
def test_closed_item_completes_and_strips_lifecycle_labels():
closed = item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL])
closed['state'] = 'closed'
client = FakeClient({7: closed})
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: completed after the item was closed'], [])
assert any(call[0] == 'DELETE' and monitor._ACTION_LABEL in call[1] for call in client.calls)
assert any(call[0] == 'DELETE' and monitor._PINGED_LABEL in call[1] for call in client.calls)
assert not any(call[1].endswith('/comments') for call in client.calls)
def test_close_and_reopen_between_runs_retires_the_old_lifecycle():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._PINGED_LABEL},
},
{'event': 'closed', 'created_at': '2026-07-18T00:00:00Z', 'actor': {'login': 'contributor'}},
{'event': 'reopened', 'created_at': '2026-07-18T00:01:00Z', 'actor': {'login': 'contributor'}},
]
assert monitor.reconcile(client, 'r', now=NOW) == (
['#7: completed after the item was closed'],
[],
)
assert not {monitor._ACTION_LABEL, monitor._PINGED_LABEL}.intersection(
{label['name'] for label in client.items[7]['labels']}
)
def test_cleanup_keeps_active_retry_state_if_stage_cleanup_fails():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL])})
client.fail_delete_labels.add(monitor._PINGED_LABEL)
with pytest.raises(urllib.error.HTTPError):
monitor._complete(client, 'r', 7, {monitor._ACTION_LABEL, monitor._PINGED_LABEL})
assert monitor._ACTION_LABEL in {label['name'] for label in client.items[7]['labels']}
assert not any(
call[0] == 'DELETE' and urllib.parse.unquote(call[1]).endswith(f'/{monitor._ACTION_LABEL}')
for call in client.calls
)
def test_reopened_item_without_action_label_fires_no_reminder():
# After the closed-item completion strips the labels, a reopen leaves no
# action label, so no stage transition can fire an instant reminder.
reopened = item(2)
client = FakeClient({2: reopened})
assert monitor.reconcile(client, 'r', now=NOW) == ([], [])
assert not any(call[1].endswith('/comments') for call in client.calls)
def test_full_page_processes_a_bounded_batch_instead_of_aborting():
client = FakeClient(
{
number: item(number, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice'])
for number in range(1, monitor._RECONCILE_LIMIT + 1)
}
)
client.permissions['alice'] = 'write'
lines, failures = monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW)
assert failures == []
assert sum('queued channel reminder' in line for line in lines) == monitor._ACTIVE_OPEN_LIMIT
assert lines[-1] == 'additional attention items remain for a later rotated batch'
def test_active_attention_pages_rotate_between_runs():
client = FakeClient(
{
number: item(number, labels=[monitor._ACTION_LABEL])
for number in range(1, monitor._ACTIVE_OPEN_LIMIT * 2 + 2)
}
)
monitor.reconcile(client, 'r', now=NOW)
monitor.reconcile(client, 'r', now=NOW + dt.timedelta(hours=6))
searches = [
path
for method, path, _ in client.calls
if method == 'GET' and path.startswith('/search/issues?') and f'per_page={monitor._ACTIVE_OPEN_LIMIT}&' in path
]
assert len(searches) == 2
assert searches[0] != searches[1]
def test_one_item_failure_does_not_block_later_items():
client = FakeClient(
{
1: item(1, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
2: item(2, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
}
)
client.permissions['alice'] = 'write'
client.fail_get.add(1)
lines, failures = monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW)
assert lines == ['#2: queued channel reminder']
assert any(failure.startswith('#1: HTTPError') for failure in failures)
assert not any(call[0] == 'POST' and call[1].endswith('/issues/2/comments') for call in client.calls)
def test_network_failure_on_dormant_item_does_not_abort_the_run():
client = FakeClient(
{
1: item(1, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
7: item(7, labels=[monitor._ESCALATED_LABEL]),
}
)
client.permissions['alice'] = 'write'
client.fail_get_network.add(7)
lines, failures = monitor.reconcile(client, 'pydantic/pydantic-ai', now=NOW)
assert lines == ['#1: queued channel reminder']
assert failures and failures[0].startswith('#7: URLError')
def test_invalid_event_timestamp_does_not_block_later_items():
client = FakeClient(
{
1: item(1, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
2: item(2, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
}
)
client.permissions['alice'] = 'write'
client.timelines[1] = [
{
'event': 'labeled',
'created_at': 'invalid',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
}
]
lines, failures = monitor.reconcile(client, 'r', now=NOW)
assert lines == ['#2: queued channel reminder']
assert failures and failures[0].startswith('#1: ValueError')
assert not any(call[0] == 'POST' and call[1].endswith('/issues/2/comments') for call in client.calls)
def test_one_item_failure_still_queues_other_notices():
client = FakeClient(
{
1: item(1, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice']),
2: item(2, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL, 'p:1-highest'], assignees=['alice']),
}
)
client.permissions['alice'] = 'write'
client.fail_get.add(1)
notices: list[monitor.Notice] = []
lines, failures = monitor.reconcile(client, 'r', now=NOW, notices=notices)
assert lines == ['#2: queued channel escalation']
assert [notice['number'] for notice in notices] == [2]
assert any(failure.startswith('#1: HTTPError') for failure in failures)
def test_bot_triggered_mention_event_is_not_an_acknowledgement():
client = FakeClient({7: item(7, labels=[monitor._ACTION_LABEL, 'p:1-highest'], assignees=['alice'])})
client.permissions['alice'] = 'write'
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
{'event': 'mentioned', 'created_at': '2026-07-17T00:00:00Z', 'actor': {'login': monitor._FALLBACK_OWNER}},
{'event': 'subscribed', 'created_at': '2026-07-17T00:00:00Z', 'actor': {'login': monitor._FALLBACK_OWNER}},
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: queued channel reminder'], [])
def test_latest_stage_transition_restarts_the_sla_clock():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, monitor._PINGED_LABEL, 'p:1-highest'], assignees=['alice'])}
)
client.permissions['alice'] = 'write'
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._PINGED_LABEL},
},
{
'event': 'labeled',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._PINGED_LABEL},
},
{
'event': 'labeled',
'created_at': '2026-07-19T00:00:00Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._PINGED_LABEL},
},
]
assert monitor._transition(client.last_pages('/repos/r/issues/7/events'), 1)[1]['id'] == 'event-2'
assert monitor.reconcile(client, 'r', now=NOW) == ([], [])
def test_sweep_restores_eligibility_after_new_activity():
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ESCALATED_LABEL},
},
{
'event': 'unlabeled',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
{'event': 'commented', 'created_at': OLD, 'actor': {'login': 'contributor'}},
]
assert monitor.reconcile(client, 'r', now=NOW) == (
['#7: restored attention eligibility after new activity'],
[],
)
assert any(call[0] == 'DELETE' and monitor._ESCALATED_LABEL in call[1] for call in client.calls)
assert any(
call[0] == 'GET'
and call[1].startswith('/search/issues?')
and monitor._ESCALATED_LABEL in urllib.parse.unquote_plus(call[1])
and 'order=desc' in call[1]
for call in client.calls
)
def test_sweep_keeps_untouched_escalated_item_dormant():
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ESCALATED_LABEL},
},
{
'event': 'unlabeled',
'created_at': '2026-07-17T00:00:00Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
]
assert monitor.reconcile(client, 'r', now=NOW) == ([], [])
assert not any(call[0] == 'DELETE' for call in client.calls)
def test_resurfaced_reminder_item_stands_down_instead_of_placeholder_assignment():
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL, 'p:1-highest'])})
client.timelines[7] = [
label_event(monitor._ESCALATED_LABEL, created_at='2026-07-12T00:00:00Z'),
{
'event': 'unlabeled',
'created_at': '2026-07-12T00:00:01Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
]
lines, failures = monitor.reconcile(client, 'r', now=NOW)
# A reminder-labeled item's owner was removed while it cooled off: the
# resurface must stand down, never re-assign the placeholder.
assert failures == []
assert lines == ['#7: stood down after its owner was unassigned']
assert not any(method == 'POST' and path.endswith('/assignees') for method, path, _ in client.calls)
def test_sweep_returns_unresolved_escalation_to_active_queue_after_cooldown():
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL])})
client.timelines[7] = [
label_event(monitor._ESCALATED_LABEL, created_at='2026-07-12T00:00:00Z'),
{
'event': 'unlabeled',
'created_at': '2026-07-12T00:00:01Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
]
assert monitor.reconcile(client, 'r', now=NOW) == (
['#7: returned unresolved attention to the active queue'],
[],
)
assert {label['name'] for label in client.items[7]['labels']} == {monitor._ACTION_LABEL}
assert ('POST', '/repos/r/issues/7/assignees', {'assignees': [monitor._FALLBACK_OWNER]}) in client.calls
def test_mixed_resurface_state_restarts_sla_instead_of_reescalating():
client = FakeClient(
{7: item(7, labels=[monitor._ACTION_LABEL, monitor._ESCALATED_LABEL], assignees=[monitor._FALLBACK_OWNER])}
)
client.timelines[7] = [
label_event(monitor._ESCALATED_LABEL, created_at='2026-07-10T00:00:00Z'),
label_event(monitor._ACTION_LABEL, created_at='2026-07-19T00:00:00Z'),
]
notices: list[monitor.Notice] = []
assert monitor.reconcile(client, 'r', now=NOW, notices=notices) == ([], [])
assert notices == []
assert {label['name'] for label in client.items[7]['labels']} == {monitor._ACTION_LABEL}
def test_dormant_sweep_rotation_reaches_escalations_behind_a_cooling_page():
def build_client() -> FakeClient:
cooling = '2026-07-19T00:00:00Z'
values = {
number: item(number, labels=[monitor._ESCALATED_LABEL], updated_at=cooling) for number in range(1, 26)
}
values[26] = item(26, labels=[monitor._ESCALATED_LABEL])
client = FakeClient(values)
for number in range(1, 26):
client.timelines[number] = [label_event(monitor._ESCALATED_LABEL, created_at=cooling)]
client.timelines[26] = [
label_event(monitor._ESCALATED_LABEL, created_at='2026-07-12T00:00:00Z'),
{
'event': 'unlabeled',
'created_at': '2026-07-12T00:00:01Z',
'actor': {'login': 'github-actions[bot]'},
'label': {'name': monitor._ACTION_LABEL},
},
]
return client
lines: list[str] = []
# Two consecutive slots alternate between the two dormant pages, so the
# eligible item behind a full page of cooling escalations is reached.
for offset in (dt.timedelta(), dt.timedelta(hours=6)):
swept, failures = monitor.reconcile(build_client(), 'r', now=NOW + offset)
assert failures == []
lines.extend(swept)
assert lines.count('#26: returned unresolved attention to the active queue') == 1
def test_sweep_removes_a_foreign_escalation_marker():
client = FakeClient({7: item(7, labels=[monitor._ESCALATED_LABEL])})
client.timelines[7] = [
{
'event': 'labeled',
'created_at': OLD,
'actor': {'login': 'outside-collaborator'},
'label': {'name': monitor._ESCALATED_LABEL},
}
]
assert monitor.reconcile(client, 'r', now=NOW) == (['#7: removed a foreign attention transition'], [])
assert any(call[0] == 'DELETE' and monitor._ESCALATED_LABEL in call[1] for call in client.calls)
def test_sweep_clears_escalation_marker_from_closed_items():
closed = item(7, labels=[monitor._ESCALATED_LABEL])
closed['state'] = 'closed'
client = FakeClient({7: closed})
assert monitor.reconcile(client, 'r', now=NOW) == (
['#7: cleared escalation marker after the item was closed'],
[],
)
assert any(call[0] == 'DELETE' and monitor._ESCALATED_LABEL in call[1] for call in client.calls)
def _script_sources() -> dict[str, str]:
scripts = Path(__file__).parent
return {
path.name: path.read_text(encoding='utf-8')
for path in scripts.glob('*.py')
if not path.name.startswith('test_')
}
def _local_import_closure(name: str, sources: dict[str, str]) -> set[str]:
"""Every sibling script `name` imports, directly or through another sibling."""
closure: set[str] = set()
stack = [name]
while stack:
for module in re.findall(r'^(?:import|from)\s+([A-Za-z_][A-Za-z0-9_]*)', sources[stack.pop()], flags=re.M):
filename = f'{module}.py'
if filename in sources and filename not in closure:
closure.add(filename)
stack.append(filename)
return closure
def _boundary_script_names(sources: dict[str, str]) -> set[str]:
"""The triage scripts: everything that imports the typed boundary, plus what those import."""
names = {name for name, text in sources.items() if 'from triage_models import' in text}
return names | {imported for name in names for imported in _local_import_closure(name, sources)}
def _workflow_jobs() -> list[tuple[str, str, list[dict[str, Any]]]]:
"""(file, job name, steps) for every job in every workflow file, compiled locks included."""
entries: list[tuple[str, str, list[dict[str, Any]]]] = []
for path in sorted((Path(__file__).parent.parent / 'workflows').glob('*.yml')):
jobs = (yaml.safe_load(path.read_text(encoding='utf-8')) or {}).get('jobs') or {}
for job_name, job in jobs.items():
steps = [step for step in job.get('steps', []) if isinstance(step, dict)]
entries.append((path.name, job_name, steps))
return entries
def test_every_script_running_job_installs_the_same_pinned_boundary_dependency():
"""pydantic is the scripts' one required package; every job that runs one pins it and its whole closure, to one set.
`--no-deps` keeps pip from resolving anything past the pins, so no fresh transitive release reaches a job.
"""
boundary = _boundary_script_names(_script_sources())
pins: set[str] = set()
checked: list[tuple[str, str]] = []
for file_name, job_name, steps in _workflow_jobs():
runs = [str(step.get('run', '')) for step in steps]
if not any(f'.github/scripts/{name}' in run for run in runs for name in boundary):
continue
checked.append((file_name, job_name))
commands = re.sub(r'\s+', ' ', '\n'.join(runs).replace('\\\n', ' '))
installs = re.findall(
r"pip install --quiet ('pydantic==[0-9][0-9a-z.]*'(?: '[a-z-]+==[0-9][0-9a-z.]*')+) --no-deps", commands
)
assert installs, (file_name, job_name)
pins.update(installs)
# The discovery itself is under test: if the glob or the import scan break,
# this fails instead of silently checking nothing.
assert len(checked) >= 4, checked
assert len(pins) == 1, pins
def test_every_sparse_checkout_fetches_the_whole_import_closure_of_the_scripts_it_runs():
"""A job that sparse-checks-out a script must also fetch every sibling module it imports."""
sources = _script_sources()
for file_name, job_name, steps in _workflow_jobs():
checkout = '\n'.join(
str((step.get('with') or {}).get('sparse-checkout', ''))
for step in steps
if isinstance(step.get('with'), dict)
)
if not checkout.strip():
continue
runs = '\n'.join(str(step.get('run', '')) for step in steps)
for name in sources:
if f'.github/scripts/{name}' in runs and f'.github/scripts/{name}' in checkout:
for imported in _local_import_closure(name, sources):
assert f'.github/scripts/{imported}' in checkout, (file_name, job_name, name, imported)
def test_boundary_scripts_import_only_the_stdlib_the_siblings_and_the_pinned_packages():
"""The workflow jobs install exactly pydantic (and logfire where used); nothing else may creep in."""
sources = _script_sources()
siblings = {name.removesuffix('.py') for name in sources}
for name in sorted(_boundary_script_names(sources)):
for module in re.findall(r'^(?:import|from)\s+([A-Za-z_][A-Za-z0-9_.]*)', sources[name], flags=re.M):
root = module.split('.')[0]
assert root in sys.stdlib_module_names or root in siblings or root in {'pydantic', 'logfire'}, (
name,
module,
)
def test_weekly_digest_workflow_is_monday_or_manual_read_only_and_secret_isolated():
workflow = Path(__file__).parent.parent / 'workflows' / 'weekly-maintainer-digest.yml'
text = workflow.read_text()
jobs = yaml.safe_load(text)['jobs']
assert "- cron: '23 14 * * 1'" in text
assert 'workflow_dispatch' in text
assert jobs['build']['permissions'] == {'contents': 'read', 'issues': 'read', 'pull-requests': 'read'}
assert jobs['notify']['permissions'] == {}
assert jobs['alert']['permissions'] == {}
assert "github.event.schedule == '23 14 * * 1'" in jobs['build']['if']
assert "github.event_name == 'workflow_dispatch'" in jobs['build']['if']
checkout = next(step for step in jobs['build']['steps'] if step.get('uses', '').startswith('actions/checkout@'))
assert checkout['with']['repository'] == '${{ job.workflow_repository }}'
assert checkout['with']['ref'] == '${{ job.workflow_sha }}'
assert checkout['with']['persist-credentials'] is False
# The import-closure test asserts the sibling modules ride along.
assert '.github/scripts/issue_pr_attention_monitor.py' in checkout['with']['sparse-checkout']
build_text = json.dumps(jobs['build'])
assert 'PYDANTIC_AI_TRIAGE_SLACK_WEBHOOK_URL' not in build_text
assert 'issue_pr_attention_monitor.py weekly' in build_text
for job_name in ('notify', 'alert'):
assert 'environment' not in jobs[job_name]
action = jobs[job_name]['steps'][0]
assert action['uses'] == 'slackapi/slack-github-action@dcb1066f776dd043e64d0e8ba94ca15cc7e1875d'
assert action['with']['webhook'] == '${{ secrets.PYDANTIC_AI_TRIAGE_SLACK_WEBHOOK_URL }}'
class StubResponse(io.BytesIO):
status = 200
headers: dict[str, str] = {}
def __enter__(self) -> StubResponse:
return self
def __exit__(self, *args: object) -> None:
self.close()
def test_github_client_bounds_response_parsing(monkeypatch: pytest.MonkeyPatch):
class StubOpener:
def __init__(self, response: bytes) -> None:
self.response = response
def open(self, request: object, timeout: int) -> StubResponse:
return StubResponse(self.response)
monkeypatch.setattr(
monitor.urllib.request,
'build_opener',
lambda handler: StubOpener(b'{"ok": true}'),
)
assert monitor.GitHubClient('token').get('/test') == {'ok': True}
monkeypatch.setattr(monitor, '_RESPONSE_LIMIT', 2)
monkeypatch.setattr(
monitor.urllib.request,
'build_opener',
lambda handler: StubOpener(b'{}\n'),
)
with pytest.raises(RuntimeError, match='response exceeds'):
monitor.GitHubClient('token').get('/test')
def test_github_client_disables_redirects(monkeypatch: pytest.MonkeyPatch):
seen: list[object] = []
class StubOpener:
def open(self, request: object, timeout: int) -> StubResponse:
return StubResponse(b'{}')
def build_opener(handler: object) -> StubOpener:
seen.append(handler)
return StubOpener()
monkeypatch.setattr(monitor.urllib.request, 'build_opener', build_opener)
monitor.GitHubClient('token').get('/test')
assert len(seen) == 1
assert seen[0] is monitor.NoRedirect
assert monitor.NoRedirect().redirect_request(None, None, 302, '', None, 'https://evil.example') is None