Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): rebuild repo activity when the window has no commits (#115413)

posthog[bot]0cec36d029e0
diff --git a/products/tasks/backend/logic/services/repo_commit_activity.py b/products/tasks/backend/logic/services/repo_commit_activity.py
index 704888a268ef..05e725e90ccf 100644
--- a/products/tasks/backend/logic/services/repo_commit_activity.py
+++ b/products/tasks/backend/logic/services/repo_commit_activity.py
@@ -43,6 +43,9 @@ DEFAULT_SINCE_DAYS = 90
# half, making its contributors look fully stale instead of decayed.
DEFAULT_MAX_COMMITS = 20_000
+# Protocol v2 (GitHub) reports the server's refusal as a shallow-info error; v0 names it.
+_EMPTY_SHALLOW_WINDOW_RE = re.compile(r"error processing shallow info|no commits selected for shallow requests")
+
class RepositoryCommitActivityError(Exception):
pass
@@ -94,7 +97,8 @@ def collect_repository_commit_activity(
)
)
try:
- _clone_history(sandbox, repository, github_token, since_days)
+ if not _clone_history(sandbox, repository, github_token, since_days):
+ return []
return _read_log(sandbox, repository, since_days, max_commits)
finally:
try:
@@ -103,7 +107,37 @@ def collect_repository_commit_activity(
logger.warning("repo_commit_activity: sandbox destroy failed", sandbox_id=sandbox.id, exc_info=True)
-def _clone_history(sandbox: SandboxBase, repository: str, github_token: str, since_days: int) -> None:
+def _clone_history(sandbox: SandboxBase, repository: str, github_token: str, since_days: int) -> bool:
+ """Clone the window's history. Return False when the default branch has no commits in the window."""
+ result = sandbox.execute(
+ _clone_command(repository, f"--shallow-since={shlex.quote(f'{since_days} days')}"),
+ timeout_seconds=_CLONE_TIMEOUT_SECONDS,
+ )
+ if result.exit_code == 0:
+ return True
+ # Git argv sees the expanded URL, so fatal messages can still echo the token.
+ stderr = result.stderr.replace(github_token, "<redacted>") if github_token else result.stderr
+ # The server refuses --shallow-since when no commit falls inside the window. Accept an
+ # empty result only after a tip-only clone proves the latest commit predates the window.
+ if _EMPTY_SHALLOW_WINDOW_RE.search(stderr) and _tip_predates_window(sandbox, repository, since_days):
+ logger.info("repo_commit_activity: no commits inside the window", repository=repository, since_days=since_days)
+ return False
+ raise RepositoryCommitActivityError(
+ f"History clone of {repository} failed with exit code {result.exit_code}: {stderr[:300]}"
+ )
+
+
+def _tip_predates_window(sandbox: SandboxBase, repository: str, since_days: int) -> bool:
+ target_path = sandbox_repo_path(repository)
+ command = (
+ f"{_clone_command(repository, '--depth=1')} && "
+ f"git -C {shlex.quote(target_path)} log -1 --since={shlex.quote(f'{since_days} days')} --format=%H"
+ )
+ result = sandbox.execute(command, timeout_seconds=_CLONE_TIMEOUT_SECONDS)
+ return result.exit_code == 0 and not result.stdout.strip()
+
+
+def _clone_command(repository: str, history_option: str) -> str:
# `repository` passed first_for_team_repository's safe-path check, so it's plain
# owner/repo and safe inside double quotes. The token placeholder expands from the
# sandbox environment; the command string itself never contains the credential.
@@ -111,20 +145,13 @@ def _clone_history(sandbox: SandboxBase, repository: str, github_token: str, sin
repo_url = f'"https://x-access-token:${{GITHUB_TOKEN}}@github.com/{org}/{repo}.git"'
target_path = sandbox_repo_path(repository)
org_path = f"{WORKING_DIR}/repos/{org}"
- command = (
+ return (
f"rm -rf {shlex.quote(target_path)} && "
f"mkdir -p {shlex.quote(org_path)} && "
f"cd {shlex.quote(org_path)} && "
f"git clone --single-branch --no-checkout --filter=blob:none "
- f"--shallow-since={shlex.quote(f'{since_days} days')} {repo_url} {shlex.quote(repo)}"
+ f"{history_option} {repo_url} {shlex.quote(repo)}"
)
- result = sandbox.execute(command, timeout_seconds=_CLONE_TIMEOUT_SECONDS)
- if result.exit_code != 0:
- # Git argv sees the expanded URL, so fatal messages can still echo the token.
- stderr = result.stderr.replace(github_token, "<redacted>") if github_token else result.stderr
- raise RepositoryCommitActivityError(
- f"History clone of {repository} failed with exit code {result.exit_code}: {stderr[:300]}"
- )
def _read_log(
diff --git a/products/tasks/backend/logic/services/test_repo_commit_activity.py b/products/tasks/backend/logic/services/test_repo_commit_activity.py
index 7e60d6197fce..a3bfd0f24dc3 100644
--- a/products/tasks/backend/logic/services/test_repo_commit_activity.py
+++ b/products/tasks/backend/logic/services/test_repo_commit_activity.py
@@ -1,4 +1,14 @@
-from products.tasks.backend.logic.services.repo_commit_activity import _parse_log
+import pytest
+from unittest.mock import MagicMock, patch
+
+from parameterized import parameterized
+
+from products.tasks.backend.logic.services.repo_commit_activity import (
+ RepositoryCommitActivityError,
+ _parse_log,
+ collect_repository_commit_activity,
+)
+from products.tasks.backend.logic.services.sandbox import ExecutionResult
_SHA_A = "a" * 40
_SHA_B = "b" * 40
@@ -35,3 +45,79 @@ def test_parse_log_skips_malformed_records_and_handles_empty_output():
commits = _parse_log(f"\x01{_SHA_C}\x1f2026-07-05T08:00:00+00:00\n")
assert len(commits) == 1
assert commits[0].paths == []
+
+
+_TOKEN = "ghs_fake_token_value"
+_SHALLOW_INFO_FAILURE = ExecutionResult(
+ stdout="", stderr=f"fatal: error processing shallow info: 4 for {_TOKEN}", exit_code=128
+)
+_OK = ExecutionResult(stdout="", stderr="", exit_code=0)
+
+
+class _FakeSandbox:
+ id = "sandbox-1"
+
+ def __init__(self, shallow_clone: ExecutionResult, tip_probe: ExecutionResult | None = None) -> None:
+ self.shallow_clone = shallow_clone
+ self.tip_probe = tip_probe
+ self.commands: list[str] = []
+ self.destroyed = False
+
+ def execute(self, command: str, timeout_seconds: int | None = None) -> ExecutionResult:
+ self.commands.append(command)
+ if "--shallow-since" in command:
+ return self.shallow_clone
+ if "--depth=1" in command:
+ assert self.tip_probe is not None
+ return self.tip_probe
+ return ExecutionResult(stdout=f"\x01{_SHA_A}\x1f2026-07-10T10:00:00+00:00\npath.py\n", stderr="", exit_code=0)
+
+ def destroy(self) -> None:
+ self.destroyed = True
+
+
+def _collect(sandbox: _FakeSandbox):
+ github = MagicMock()
+ github.get_access_token.return_value = _TOKEN
+ sandbox_class = MagicMock()
+ sandbox_class.create.return_value = sandbox
+ module = "products.tasks.backend.logic.services.repo_commit_activity"
+ with (
+ patch(f"{module}.GitHubIntegration.first_for_team_repository", return_value=github),
+ patch(f"{module}.get_sandbox_class", return_value=sandbox_class),
+ ):
+ return collect_repository_commit_activity(1, "acme/widgets")
+
+
+def test_collect_reads_log_when_window_has_recent_commits():
+ sandbox = _FakeSandbox(shallow_clone=_OK)
+
+ commits = _collect(sandbox)
+
+ assert [c.sha for c in commits] == [_SHA_A]
+ assert not any("--depth=1" in command for command in sandbox.commands)
+ assert sandbox.destroyed
+
+
+def test_collect_returns_empty_when_latest_commit_predates_window():
+ sandbox = _FakeSandbox(shallow_clone=_SHALLOW_INFO_FAILURE, tip_probe=_OK)
+
+ assert _collect(sandbox) == []
+ assert sandbox.destroyed
+
+
+ [
+ ("tip_inside_window", _SHALLOW_INFO_FAILURE, ExecutionResult(stdout=f"{_SHA_A}\n", stderr="", exit_code=0)),
+ ("tip_probe_fails", _SHALLOW_INFO_FAILURE, ExecutionResult(stdout="", stderr="boom", exit_code=128)),
+ ("unrelated_clone_failure", ExecutionResult(stdout="", stderr=f"denied {_TOKEN}", exit_code=128), None),
+ ]
+)
+def test_collect_raises_when_history_is_unavailable(_name, shallow_clone, tip_probe):
+ sandbox = _FakeSandbox(shallow_clone=shallow_clone, tip_probe=tip_probe)
+
+ with pytest.raises(RepositoryCommitActivityError) as error:
+ _collect(sandbox)
+
+ assert _TOKEN not in str(error.value)
+ assert sandbox.destroyed