Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(skill): reduce github waiter polling cost

cyh35e658826f29
diff --git a/skills/background-watch-hook/scripts/_github_wait_common.py b/skills/background-watch-hook/scripts/_github_wait_common.py
index 70a4065c9..3fd438762 100644
--- a/skills/background-watch-hook/scripts/_github_wait_common.py
+++ b/skills/background-watch-hook/scripts/_github_wait_common.py
@@ -72,21 +72,37 @@ def get_authenticated_login(token: str | None) -> str | None:
def list_paginated(base_url: str, token: str | None) -> list[dict[str, Any]]:
+ items, _request_count = list_paginated_with_count(base_url, token)
+ return items
+
+
+def list_paginated_with_count(
+ base_url: str,
+ token: str | None,
+ *,
+ stop_after_id: int | None = None,
+) -> tuple[list[dict[str, Any]], int]:
items: list[dict[str, Any]] = []
page = 1
+ request_count = 0
while True:
separator = "&" if "?" in base_url else "?"
url = f"{base_url}{separator}per_page=100&page={page}"
payload = github_get(url, token)
+ request_count += 1
if not isinstance(payload, list):
raise RuntimeError(f"Expected a JSON list from {url}")
if not payload:
break
- items.extend(item for item in payload if isinstance(item, dict))
+ page_items = [item for item in payload if isinstance(item, dict)]
+ items.extend(page_items)
+ if stop_after_id is not None and stop_after_id > 0:
+ if any(isinstance(item.get("id"), int) and int(item["id"]) <= stop_after_id for item in page_items):
+ break
if len(payload) < 100:
break
page += 1
- return items
+ return items, request_count
def squash(text: str | None, *, limit: int = 140) -> str:
diff --git a/skills/background-watch-hook/scripts/wait_issue.py b/skills/background-watch-hook/scripts/wait_issue.py
index 9b3961ecb..a783ceb11 100644
--- a/skills/background-watch-hook/scripts/wait_issue.py
+++ b/skills/background-watch-hook/scripts/wait_issue.py
@@ -20,7 +20,7 @@ from _github_wait_common import ( # noqa: E402
filter_new,
get_authenticated_login,
get_token,
- list_paginated,
+ list_paginated_with_count,
max_id,
min_interval_for_unauthenticated,
requests_per_poll,
@@ -56,23 +56,33 @@ def _is_self_authored_comment(comment: dict[str, Any], viewer_login: str | None)
return str(author).casefold() == viewer_login.casefold()
-def _fetch_new_issue_state(repo: str, token: str | None) -> dict[str, list[dict[str, Any]]]:
+def _fetch_new_issue_state(
+ repo: str,
+ token: str | None,
+ *,
+ stop_after_id: int | None = None,
+) -> tuple[dict[str, list[dict[str, Any]]], int]:
encoded_repo = urllib.parse.quote(repo, safe="/")
- raw_issues = list_paginated(
+ raw_issues, request_count = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/issues?state=all&sort=created&direction=desc",
token,
+ stop_after_id=stop_after_id,
)
issues = [item for item in raw_issues if not _is_pull_request_issue(item)]
- return {"issues": issues}
+ return {"issues": issues}, request_count
-def _fetch_issue_comment_state(repo: str, issue_number: int, token: str | None) -> dict[str, list[dict[str, Any]]]:
+def _fetch_issue_comment_state(
+ repo: str,
+ issue_number: int,
+ token: str | None,
+) -> tuple[dict[str, list[dict[str, Any]]], int]:
encoded_repo = urllib.parse.quote(repo, safe="/")
- comments = list_paginated(
+ comments, request_count = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/issues/{issue_number}/comments",
token,
)
- return {"issue_comments": comments}
+ return {"issue_comments": comments}, request_count
def _render_new_issues(
@@ -196,9 +206,9 @@ def main() -> int:
try:
if args.issue is not None:
- state = _fetch_issue_comment_state(args.repo, args.issue, token)
+ state, requests_per_poll_count = _fetch_issue_comment_state(args.repo, args.issue, token)
else:
- state = _fetch_new_issue_state(args.repo, token)
+ state, requests_per_poll_count = _fetch_new_issue_state(args.repo, token)
except urllib.error.HTTPError as err:
print(f"GitHub API error: {err.code} {err.reason}", file=sys.stderr)
return 1
@@ -207,7 +217,6 @@ def main() -> int:
return 1
if token is None:
- requests_per_poll_count = requests_per_poll(*state.values())
bootstrap_requests = requests_per_poll_count
unauthenticated_min = min_interval_for_unauthenticated(
requests_per_poll_count,
@@ -283,9 +292,13 @@ def main() -> int:
try:
if args.issue is not None:
- state = _fetch_issue_comment_state(args.repo, args.issue, token)
+ state, requests_per_poll_count = _fetch_issue_comment_state(args.repo, args.issue, token)
else:
- state = _fetch_new_issue_state(args.repo, token)
+ state, requests_per_poll_count = _fetch_new_issue_state(
+ args.repo,
+ token,
+ stop_after_id=issue_cursor if issue_cursor > 0 else None,
+ )
except urllib.error.HTTPError as err:
if token is None and err.code in {403, 429}:
print(
@@ -303,7 +316,6 @@ def main() -> int:
continue
if token is None:
- requests_per_poll_count = requests_per_poll(*state.values())
unauthenticated_min = min_interval_for_unauthenticated(
requests_per_poll_count,
bootstrap_requests=bootstrap_requests,
diff --git a/skills/background-watch-hook/scripts/wait_pr.py b/skills/background-watch-hook/scripts/wait_pr.py
index ce5d6a295..8954c9a10 100755
--- a/skills/background-watch-hook/scripts/wait_pr.py
+++ b/skills/background-watch-hook/scripts/wait_pr.py
@@ -21,6 +21,7 @@ from _github_wait_common import ( # noqa: E402
get_authenticated_login,
get_token,
list_paginated,
+ list_paginated_with_count,
max_id,
min_interval_for_unauthenticated,
requests_per_poll,
@@ -90,36 +91,48 @@ def _format_pull_request(pr: dict[str, Any]) -> str:
return f"- pull_request #{pr_number} by {author} ({state})\n {title}\n {url}"
-def _fetch_state(repo: str, pr_number: int, token: str | None) -> dict[str, list[dict[str, Any]]]:
+def _fetch_state(repo: str, pr_number: int, token: str | None) -> tuple[dict[str, list[dict[str, Any]]], int]:
encoded_repo = urllib.parse.quote(repo, safe="/")
- reviews = list_paginated(f"https://api.github.com/repos/{encoded_repo}/pulls/{pr_number}/reviews", token)
- review_comments = list_paginated(
+ reviews, review_requests = list_paginated_with_count(
+ f"https://api.github.com/repos/{encoded_repo}/pulls/{pr_number}/reviews",
+ token,
+ )
+ review_comments, review_comment_requests = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/pulls/{pr_number}/comments",
token,
)
- issue_comments = list_paginated(
+ issue_comments, issue_comment_requests = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/issues/{pr_number}/comments",
token,
)
- reactions = list_paginated(
+ reactions, reaction_requests = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/issues/{pr_number}/reactions",
token,
)
- return {
- "reviews": reviews,
- "review_comments": review_comments,
- "issue_comments": issue_comments,
- "reactions": reactions,
- }
+ return (
+ {
+ "reviews": reviews,
+ "review_comments": review_comments,
+ "issue_comments": issue_comments,
+ "reactions": reactions,
+ },
+ review_requests + review_comment_requests + issue_comment_requests + reaction_requests,
+ )
-def _fetch_new_pr_state(repo: str, token: str | None) -> dict[str, list[dict[str, Any]]]:
+def _fetch_new_pr_state(
+ repo: str,
+ token: str | None,
+ *,
+ stop_after_id: int | None = None,
+) -> tuple[dict[str, list[dict[str, Any]]], int]:
encoded_repo = urllib.parse.quote(repo, safe="/")
- pull_requests = list_paginated(
+ pull_requests, request_count = list_paginated_with_count(
f"https://api.github.com/repos/{encoded_repo}/pulls?state=all&sort=created&direction=desc",
token,
+ stop_after_id=stop_after_id,
)
- return {"pull_requests": pull_requests}
+ return {"pull_requests": pull_requests}, request_count
def _render_activity(
@@ -307,9 +320,9 @@ def main() -> int:
try:
if args.pr is not None:
- state = _fetch_state(args.repo, args.pr, token)
+ state, requests_per_poll_count = _fetch_state(args.repo, args.pr, token)
else:
- state = _fetch_new_pr_state(args.repo, token)
+ state, requests_per_poll_count = _fetch_new_pr_state(args.repo, token)
except urllib.error.HTTPError as err:
print(f"GitHub API error: {err.code} {err.reason}", file=sys.stderr)
return 1
@@ -318,7 +331,6 @@ def main() -> int:
return 1
if token is None:
- requests_per_poll_count = requests_per_poll(*state.values())
bootstrap_requests = requests_per_poll_count
unauthenticated_min = min_interval_for_unauthenticated(
requests_per_poll_count,
@@ -428,9 +440,13 @@ def main() -> int:
try:
if args.pr is not None:
- state = _fetch_state(args.repo, args.pr, token)
+ state, requests_per_poll_count = _fetch_state(args.repo, args.pr, token)
else:
- state = _fetch_new_pr_state(args.repo, token)
+ state, requests_per_poll_count = _fetch_new_pr_state(
+ args.repo,
+ token,
+ stop_after_id=pr_cursor if pr_cursor > 0 else None,
+ )
except urllib.error.HTTPError as err:
if token is None and err.code in {403, 429}:
print(
@@ -448,7 +464,6 @@ def main() -> int:
continue
if token is None:
- requests_per_poll_count = requests_per_poll(*state.values())
unauthenticated_min = min_interval_for_unauthenticated(
requests_per_poll_count,
bootstrap_requests=bootstrap_requests,
diff --git a/tests/test_wait_for_github_issue_activity.py b/tests/test_wait_for_github_issue_activity.py
index 943c61948..c00823b17 100644
--- a/tests/test_wait_for_github_issue_activity.py
+++ b/tests/test_wait_for_github_issue_activity.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import importlib.util
from pathlib import Path
+from unittest.mock import patch
def _load_module():
@@ -97,3 +98,38 @@ def test_render_issue_comments_ignores_self_authored_comment_but_advances_cursor
assert output is None
assert issue_comment_cursor == 302
+
+
+def test_fetch_new_issue_state_keeps_request_count_from_raw_issue_pages() -> None:
+ module = _load_module()
+ raw_items = [
+ {
+ "id": 401,
+ "number": 41,
+ "title": "PR disguised by /issues",
+ "state": "open",
+ "pull_request": {"url": "https://api.github.com/repos/example/repo/pulls/41"},
+ },
+ {
+ "id": 400,
+ "number": 40,
+ "title": "Real issue",
+ "state": "open",
+ },
+ ]
+
+ def _fake_list_paginated_with_count(base_url, token, *, stop_after_id=None):
+ assert "issues?state=all" in base_url
+ assert stop_after_id == 400
+ return raw_items, 3
+
+ with patch.object(module, "list_paginated_with_count", side_effect=_fake_list_paginated_with_count):
+ state, request_count = module._fetch_new_issue_state(
+ "cyhhao/vibe-remote",
+ token="token",
+ stop_after_id=400,
+ )
+
+ assert len(state["issues"]) == 1
+ assert state["issues"][0]["id"] == 400
+ assert request_count == 3
diff --git a/tests/test_wait_for_github_pr_activity.py b/tests/test_wait_for_github_pr_activity.py
index eec1d4d0f..e20b5f2bd 100644
--- a/tests/test_wait_for_github_pr_activity.py
+++ b/tests/test_wait_for_github_pr_activity.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import importlib.util
from pathlib import Path
+from unittest.mock import patch
def _load_module():
@@ -184,3 +185,28 @@ def test_render_new_pull_requests_includes_new_prs() -> None:
assert output is not None
assert "pull_request #157" in output
assert pr_cursor == 401
+
+
+def test_fetch_new_pr_state_stops_after_cursor() -> None:
+ module = _load_module()
+ responses = [
+ [
+ {"id": 410, "number": 2, "title": "Newest", "state": "open"},
+ {"id": 405, "number": 1, "title": "Known", "state": "open"},
+ ]
+ ]
+
+ def _fake_list_paginated_with_count(base_url, token, *, stop_after_id=None):
+ assert "pulls?state=all" in base_url
+ assert stop_after_id == 405
+ return responses[0], 1
+
+ with patch.object(module, "list_paginated_with_count", side_effect=_fake_list_paginated_with_count):
+ state, request_count = module._fetch_new_pr_state(
+ "cyhhao/vibe-remote",
+ token="token",
+ stop_after_id=405,
+ )
+
+ assert state["pull_requests"][0]["id"] == 410
+ assert request_count == 1