: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(signals): turn github issue dependency lines into report links (#126337)
posthog[bot]5dd74ea5ac2f
diff --git a/products/signals/backend/auto_start.py b/products/signals/backend/auto_start.pyindex 8c78cf2aaf1d..90a01c5a31a6 100644--- a/products/signals/backend/auto_start.py+++ b/products/signals/backend/auto_start.py@@ -42,6 +42,7 @@ from products.signals.backend.enums import ReportLinkKindfrom products.signals.backend.free_trial import capture_signal_report_free_trial_paused, self_driving_free_trial_enabledfrom products.signals.backend.github_optional import ResearchAccessPolicyfrom products.signals.backend.implementation_repositories import implementation_access_repositories+from products.signals.backend.issue_dependencies import resolve_issue_dependencies, unresolved_dependency_skipfrom products.signals.backend.models import (MAX_SCOUT_CONTENT_REVISIONS,SignalReport,@@ -1548,10 +1549,17 @@ async def maybe_autostart_implementation_task()(team_id, repository, priority=EgressPriority.BATCH):return AutostartOutcome(status="blocked", reason="Connect or repair GitHub access to the selected repository")+ # Dependency lines in the report's GitHub issues become `depends_on` links first, so the link+ # gates below already see them. A dependency issue that no report holds yet holds the report too.+ unresolved_issues = await database_sync_to_async(resolve_issue_dependencies, thread_sensitive=False)(+ team_id=team_id, report_id=report_id, ch_user=ch_user+ )# Between the team's master switch and the quota gate. A team that opted out of auto-start# entirely gets no artefact and no gate reason, and a duplicate or a plan parent never counts# as quota-held work, because the work was never this report's to do.link_skip = await database_sync_to_async(_evaluate_link_gates, thread_sensitive=False)(team_id, report_id)+ if link_skip is None and unresolved_issues:+ link_skip = unresolved_dependency_skip(unresolved_issues)if link_skip is not None:logger.info("self-driving auto-start skipped",diff --git a/products/signals/backend/contracts.py b/products/signals/backend/contracts.pyindex f9ea350fc5c4..ed095d320a21 100644--- a/products/signals/backend/contracts.py+++ b/products/signals/backend/contracts.py@@ -169,6 +169,12 @@ class ZendeskTicketSignalInput(SignalInputBase):# ── GitHub ──────────────────────────────────────────────────────────────────────+class GithubIssueDependencyRef(ContractModel):+ # Lowercase `owner/repo`, so two spellings of one repository name one issue.+ repository: str+ number: int++class GithubIssueSignalExtra(SignalExtraBase):html_url: strnumber: int@@ -184,6 +190,9 @@ class GithubIssueSignalExtra(SignalExtraBase):# FIRST_TIMER, MANNEQUIN, NONE. Kept as a plain string so a value GitHub adds later widens the# taxonomy instead of failing validation and dropping the signal.author_association: str | None = None+ # The issues this one names on a "Depends on" or "Blocked by" line. Research turns them into+ # `depends_on` report links before auto-start decides.+ depends_on_issues: list[GithubIssueDependencyRef] = []class GithubIssueSignalInput(SignalInputBase):diff --git a/products/signals/backend/emission/github_issues.py b/products/signals/backend/emission/github_issues.pyindex e0f634bbbc73..109601d37404 100644--- a/products/signals/backend/emission/github_issues.py+++ b/products/signals/backend/emission/github_issues.py@@ -1,9 +1,12 @@+import reimport jsonimport hashlibfrom typing import Anyfrom structlog import get_logger+from posthog.models.github_integration_base import GitHubIntegrationBase+from products.signals.backend.emission.fetchers.data_warehouse import data_warehouse_record_fetcherfrom products.signals.backend.emission.registry import SignalEmitterOutput, SignalSourceTableConfig, redacted_record@@ -65,7 +68,26 @@ AUTHOR_FIELDS = ("author_association", "user")# avatar and API URLs, and the shared pipeline logs whole records when an emitter raises.UNLOGGABLE_FIELDS = ("user",)-EXTRA_FIELDS = (*PASSTHROUGH_FIELDS, "author_login", "author_association")+EXTRA_FIELDS = (*PASSTHROUGH_FIELDS, "author_login", "author_association", "depends_on_issues")++MAX_DEPENDS_ON_ISSUES = 10++# A dependency line starts a line or a list item, so a sentence that only mentions an issue in+# passing is not one. Bold markers and a trailing colon are allowed around the phrase.+_DEPENDENCY_LINE_RE = re.compile(+ r"^\s*(?:(?:[-*+]|\d+[.)])\s+)?(?:\*\*|__)?(?:depends\s+on|blocked\s+by)(?:\*\*|__)?\s*:?(?:\*\*|__)?\s+(?P<refs>.+)$",+ re.IGNORECASE,+)+_ISSUE_REF_RE = re.compile(+ r"https?://(?:www\.)?github\.com/(?P<url_repo>[\w.-]+/[\w.-]+)/issues/(?P<url_number>\d+)(?:[/?#]\S*)?"+ r"|(?P<repo>[\w.-]+/[\w.-]+)?#(?P<number>\d+)\b",+ re.IGNORECASE,+)+# Commas, "and", and "&" join several refs on one line. Any other text ends the list, so an issue+# mentioned later in the same sentence is not read as a dependency.+_REF_SEPARATOR_RE = re.compile(r"\s*(?:,|&|\band\b)?\s*", re.IGNORECASE)+_FENCE_RE = re.compile(r"^\s*(```|~~~)")+_INLINE_CODE_RE = re.compile(r"`[^`]*`")def _loggable(record: dict[str, Any]) -> dict[str, Any]:@@ -144,6 +166,7 @@ def _build_extra(record: dict[str, Any]) -> dict[str, Any]:# Always present, so the contract shape doesn't shift when a repo or a sync withholds the author.extra["author_login"] = _author_login(record.get("user"))extra["author_association"] = record.get("author_association") or None+ extra["depends_on_issues"] = parse_depends_on_issues(record.get("body") or "", extra.get("html_url"))raw_labels = extra.get("labels")if raw_labels is None:extra["labels"] = []@@ -166,6 +189,51 @@ def _build_extra(record: dict[str, Any]) -> dict[str, Any]:return extra+def parse_depends_on_issues(body: str, html_url: Any) -> list[dict[str, Any]]:+ """The issues that `body` names on "Depends on" or "Blocked by" lines, as `{repository, number}`.++ Code blocks, quotes, and inline code are skipped, because they quote other text rather than+ state this issue's dependencies. A `#123` ref takes the repository of the issue itself, so it is+ dropped when `html_url` does not parse. References to the issue itself are dropped.+ """+ own = GitHubIntegrationBase.parse_issue_url(html_url) if isinstance(html_url, str) else None+ own_repository = f"{own.owner}/{own.repo}".lower() if own else None+ refs: list[dict[str, Any]] = []+ in_fence = False+ for line in body.splitlines():+ if _FENCE_RE.match(line):+ in_fence = not in_fence+ continue+ if in_fence or line.lstrip().startswith(">"):+ continue+ match = _DEPENDENCY_LINE_RE.match(_INLINE_CODE_RE.sub("", line))+ if match is None:+ continue+ for repository, number in _leading_refs(match.group("refs"), own_repository):+ ref = {"repository": repository, "number": number}+ if (repository == own_repository and own is not None and number == own.number) or ref in refs:+ continue+ refs.append(ref)+ if len(refs) >= MAX_DEPENDS_ON_ISSUES:+ return refs+ return refs+++def _leading_refs(text: str, own_repository: str | None) -> list[tuple[str, int]]:+ refs: list[tuple[str, int]] = []+ position = 0+ while True:+ separator = _REF_SEPARATOR_RE.match(text, position)+ match = _ISSUE_REF_RE.match(text, separator.end() if separator else position)+ if match is None:+ return refs+ repository = match.group("url_repo") or match.group("repo") or own_repository+ number = match.group("url_number") or match.group("number")+ if repository:+ refs.append((repository.lower(), int(number)))+ position = match.end()++GITHUB_ISSUES_CONFIG = SignalSourceTableConfig(source_product="github",source_type="issue",diff --git a/products/signals/backend/emission/tests/test_github_issues.py b/products/signals/backend/emission/tests/test_github_issues.pyindex eb61ff976cf6..661ef07104c9 100644--- a/products/signals/backend/emission/tests/test_github_issues.py+++ b/products/signals/backend/emission/tests/test_github_issues.py@@ -64,6 +64,38 @@ class TestGithubIssueEmitter:assert result.existing_source_only is Trueassert "Charts fail to render" in result.description+ @pytest.mark.parametrize(+ ("body", "expected"),+ [+ pytest.param("Depends on #86", [("acme/analytics", 86)], id="same_repo_ref"),+ pytest.param("- blocked by Acme/Other#5", [("acme/other", 5)], id="list_item_cross_repo_ref"),+ pytest.param(+ "**Depends on:** https://github.com/acme/web/issues/9", [("acme/web", 9)], id="bold_phrase_url_ref"+ ),+ pytest.param(+ "1. Depends on #1, #2 and #3", [("acme/analytics", n) for n in (1, 2, 3)], id="several_refs_on_a_line"+ ),+ pytest.param("Depends on #4 because #5 broke", [("acme/analytics", 4)], id="later_mention_ignored"),+ pytest.param("We saw this after #4 shipped", [], id="bare_mention_ignored"),+ pytest.param("> Depends on #4", [], id="quote_ignored"),+ pytest.param("```\nDepends on #4\n```", [], id="code_block_ignored"),+ pytest.param("Depends on `#4`", [], id="inline_code_ignored"),+ pytest.param("Depends on #87", [], id="self_reference_ignored"),+ pytest.param(+ "\n".join(f"Depends on #{n}" for n in range(1, 13)),+ [("acme/analytics", n) for n in range(1, 11)],+ id="capped",+ ),+ ],+ )+ def test_dependency_lines_parse_into_depends_on_issues(self, github_issue_record, body, expected):+ github_issue_record["body"] = body+ result = github_issue_emitter(team_id=1, record=github_issue_record)++ assert result is not None+ assert result.extra["depends_on_issues"] == [{"repository": r, "number": n} for r, n in expected]+ GithubIssueSignalExtra(**result.extra)+@pytest.mark.parametrize("missing_field", ["id", "title"])def test_raises_when_required_field_falsy(self, github_issue_record, missing_field):github_issue_record[missing_field] = Nonediff --git a/products/signals/backend/issue_dependencies.py b/products/signals/backend/issue_dependencies.pynew file mode 100644index 000000000000..1b0078547b96--- /dev/null+++ b/products/signals/backend/issue_dependencies.py@@ -0,0 +1,330 @@+"""Turn the dependency lines of GitHub issues into `depends_on` report links.++The GitHub issues emitter stores the issues a body names on "Depends on" and "Blocked by" lines as+`depends_on_issues` on the signal. Before auto-start decides, `resolve_issue_dependencies` maps each+of them to the report that holds that issue and writes the link, so the existing link gate holds the+report until the dependency has a pull request.++The dependency issue can be filed in the same minute as the dependent, so its report may not exist+yet. Such a ref holds auto-start too. When the dependency's report settles later,+`reports_waiting_on_issues_of` finds the reports that wait on it, so they can be evaluated again.+"""++import re+import json+from collections.abc import Collection+from datetime import timedelta++from django.db.models import Q+from django.utils import timezone++import structlog++from posthog.hogql import ast+from posthog.hogql.query import execute_hogql_query++from posthog.clickhouse.client.connection import ClickHouseUser+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries+from posthog.dataclasses import frozen+from posthog.models import Team+from posthog.models.github_integration_base import GitHubIntegrationBase++from products.signals.backend.artefact_attribution import ArtefactAttribution+from products.signals.backend.artefact_schemas import (+ ArtefactContentValidationError,+ AutostartSkip,+ NoteArtefact,+ ReportLink,+)+from products.signals.backend.enums import ReportLinkKind, ReportLinkWritePath+from products.signals.backend.models import SignalReport, SignalReportArtefact+from products.signals.backend.signal_metadata import EMBEDDING_MODEL, _deduped_signals_subquery, _signals_query_context+from products.signals.backend.typed_report_links import (+ ReportEdge,+ duplicate_root,+ has_open_or_merged_pull_request,+ outgoing_links,+)++logger = structlog.get_logger(__name__)++LINK_REASON_PREFIX = "Depends on GitHub issue "+UNRESOLVED_DETAIL_PREFIX = "No work started here because no report holds GitHub issue "+# A held report is evaluated again only while its hold is this recent. Older holds stay held until+# their own report is researched again or a person presses Implement.+WAITING_LOOKBACK = timedelta(days=30)+_ROW_CAP = 500+_UNUSABLE_STATUSES = (SignalReport.Status.DELETED, SignalReport.Status.SUPPRESSED, SignalReport.Status.FAILED)+++@frozen+class IssueRef:+ repository: str+ number: int++ @property+ def label(self) -> str:+ return f"{self.repository}#{self.number}"++ @property+ def url(self) -> str:+ return f"https://github.com/{self.repository}/issues/{self.number}"++ @classmethod+ def from_url(cls, url: str) -> "IssueRef | None":+ parsed = GitHubIntegrationBase.parse_issue_url(url)+ if parsed is None:+ return None+ return cls(repository=f"{parsed.owner}/{parsed.repo}".lower(), number=parsed.number)+++@frozen+class ReportIssue:+ """One GitHub issue of a report, read from the newest signal of that issue."""++ ref: IssueRef+ depends_on: tuple[IssueRef, ...]+++@frozen+class IssueMatch:+ report_ids: tuple[str, ...]+ closed: bool+++def _parse_refs(raw: object) -> tuple[IssueRef, ...]:+ if not isinstance(raw, list):+ return ()+ refs: list[IssueRef] = []+ for item in raw:+ if not isinstance(item, dict):+ continue+ repository, number = item.get("repository"), item.get("number")+ if isinstance(repository, str) and isinstance(number, int):+ refs.append(IssueRef(repository=repository.lower(), number=number))+ return tuple(refs)+++def report_issues(team: Team, report_id: str, *, ch_user: ClickHouseUser) -> list[ReportIssue]:+ """The GitHub issues whose signals sit in this report, each with its current dependency refs."""+ query = f"""+ SELECT metadata+ FROM ({_deduped_signals_subquery(include_content=False, candidate_document_filter="JSONExtractString(metadata, 'report_id') = {report_id}")})+ WHERE JSONExtractString(metadata, 'report_id') = {{report_id}}+ AND NOT JSONExtractBool(metadata, 'deleted')+ AND JSONExtractString(metadata, 'source_product') = 'github'+ AND JSONExtractString(metadata, 'source_type') = 'issue'+ ORDER BY timestamp DESC+ LIMIT {{row_cap}}+ """+ tag_queries(product=Product.SIGNALS, feature=Feature.QUERY)+ result = execute_hogql_query(+ ch_user=ch_user,+ query_type="SignalsFetchReportGithubIssues",+ query=query,+ team=team,+ placeholders={+ "model_name": ast.Constant(value=EMBEDDING_MODEL.value),+ "report_id": ast.Constant(value=report_id),+ "row_cap": ast.Constant(value=_ROW_CAP),+ },+ context=_signals_query_context(team),+ )+ issues: list[ReportIssue] = []+ seen_sources: set[str] = set()+ for (metadata_str,) in result.results or []:+ metadata = json.loads(metadata_str)+ # An edited issue emits a new signal, so only the newest one says what the body lists now.+ source_id = metadata.get("source_id")+ if source_id in seen_sources:+ continue+ seen_sources.add(source_id)+ extra = metadata.get("extra") or {}+ ref = IssueRef.from_url(extra.get("html_url") or "")+ if ref is not None:+ issues.append(ReportIssue(ref=ref, depends_on=_parse_refs(extra.get("depends_on_issues"))))+ return issues+++def _issue_matches(team: Team, refs: Collection[IssueRef], *, ch_user: ClickHouseUser) -> dict[IssueRef, IssueMatch]:+ """For each ref, the reports that hold a signal of that issue, newest first, and its last state."""+ urls = {ref.url: ref for ref in refs}+ url_filter = (+ "JSONExtractString(metadata, 'source_product') = 'github' "+ "AND JSONExtractString(metadata, 'source_type') = 'issue' "+ "AND lower(JSONExtractString(metadata, 'extra', 'html_url')) IN {issue_urls}"+ )+ query = f"""+ SELECT+ lower(JSONExtractString(metadata, 'extra', 'html_url')),+ JSONExtractString(metadata, 'report_id'),+ JSONExtractString(metadata, 'extra', 'state')+ FROM ({_deduped_signals_subquery(include_content=False, candidate_document_filter=url_filter)})+ WHERE NOT JSONExtractBool(metadata, 'deleted')+ AND {url_filter}+ ORDER BY timestamp DESC+ LIMIT {{row_cap}}+ """+ tag_queries(product=Product.SIGNALS, feature=Feature.QUERY)+ result = execute_hogql_query(+ ch_user=ch_user,+ query_type="SignalsFetchReportsForGithubIssues",+ query=query,+ team=team,+ placeholders={+ "model_name": ast.Constant(value=EMBEDDING_MODEL.value),+ "issue_urls": ast.Tuple(exprs=[ast.Constant(value=url) for url in urls]),+ "row_cap": ast.Constant(value=_ROW_CAP),+ },+ context=_signals_query_context(team),+ )+ report_ids: dict[IssueRef, list[str]] = {}+ closed: dict[IssueRef, bool] = {}+ for url, report_id, state in result.results or []:+ ref = urls.get(url)+ if ref is None:+ continue+ # Rows arrive newest first, so the first row of an issue carries its current state.+ closed.setdefault(ref, state == "closed")+ if report_id and report_id not in report_ids.setdefault(ref, []):+ report_ids[ref].append(report_id)+ return {+ ref: IssueMatch(report_ids=tuple(report_ids.get(ref, ())), closed=closed[ref])+ for ref in urls.values()+ if ref in closed+ }+++def _pick_target(*, team_id: int, report_id: str, candidate_ids: Collection[str]) -> str | None:+ """The report a dependency points at: the canonical report, one with a pull request first, then the oldest."""+ canonical_ids = list(dict.fromkeys(duplicate_root(team_id=team_id, report_id=rid) for rid in candidate_ids))+ rows = list(+ SignalReport.objects.using("default")+ .filter(team_id=team_id, id__in=canonical_ids)+ .exclude(status__in=_UNUSABLE_STATUSES)+ .exclude(id=report_id)+ .values_list("id", "created_at")+ )+ if not rows:+ return None+ with_work = has_open_or_merged_pull_request(team_id=team_id, report_ids=[str(rid) for rid, _ in rows])+ rows.sort(key=lambda row: (str(row[0]) not in with_work, row[1]))+ return str(rows[0][0])+++def resolve_issue_dependencies(+ *, team_id: int, report_id: str, ch_user: ClickHouseUser = ClickHouseUser.DEFAULT+) -> tuple[IssueRef, ...]:+ """Write a `depends_on` link for each dependency ref of the report's GitHub issues.++ Returns the refs that hold auto-start without a link: no usable report holds the issue yet. A+ closed dependency issue counts as met and gets no link. A ref that the issue no longer lists+ keeps its link, and the report's work log gets a note about it.+ """+ team = Team.objects.get(pk=team_id)+ issues = report_issues(team, report_id, ch_user=ch_user)+ if not issues:+ return ()+ own_refs = {issue.ref for issue in issues}+ refs = list(dict.fromkeys(ref for issue in issues for ref in issue.depends_on if ref not in own_refs))+ existing = outgoing_links(team_id=team_id, report_id=report_id, kinds=(ReportLinkKind.DEPENDS_ON,))+ _note_removed_refs(team_id=team_id, report_id=report_id, existing=existing, refs=refs)+ if not refs:+ return ()++ linked_ids = {edge.target_id for edge in existing}+ matches = _issue_matches(team, refs, ch_user=ch_user)+ unresolved: list[IssueRef] = []+ for ref in refs:+ match = matches.get(ref)+ if match is not None and match.closed:+ continue+ target_id = (+ _pick_target(team_id=team_id, report_id=report_id, candidate_ids=match.report_ids) if match else None+ )+ if target_id is None:+ unresolved.append(ref)+ continue+ if target_id in linked_ids:+ continue+ try:+ SignalReportArtefact.add_log(+ team_id=team_id,+ report_id=report_id,+ content=ReportLink(+ kind=ReportLinkKind.DEPENDS_ON, report_id=target_id, reason=f"{LINK_REASON_PREFIX}{ref.label}."+ ),+ attribution=ArtefactAttribution.system(),+ write_path=ReportLinkWritePath.PIPELINE,+ )+ except ArtefactContentValidationError:+ # The target already depends on this report. A person linked it the other way, and an+ # inferred link must not hold either report forever.+ logger.warning(+ "signals.issue_dependencies.link_rejected", team_id=team_id, report_id=report_id, target_id=target_id+ )+ continue+ linked_ids.add(target_id)+ return tuple(unresolved)+++def _note_removed_refs(+ *, team_id: int, report_id: str, existing: Collection[ReportEdge], refs: Collection[IssueRef]+) -> None:+ labels = {ref.label for ref in refs}+ for edge in existing:+ reason = edge.reason or ""+ if not reason.startswith(LINK_REASON_PREFIX):+ continue+ label = reason.removeprefix(LINK_REASON_PREFIX).rstrip(".")+ if label in labels:+ continue+ note = (+ f"The GitHub issue no longer lists {label} as a dependency. "+ f"The `depends_on` link to report {edge.target_id} stays in place."+ )+ already_noted = SignalReportArtefact.objects.filter(+ team_id=team_id,+ report_id=report_id,+ type=SignalReportArtefact.ArtefactType.NOTE,+ content__contains=json.dumps(note)[1:-1],+ ).exists()+ if not already_noted:+ SignalReportArtefact.add_log(+ team_id=team_id,+ report_id=report_id,+ content=NoteArtefact(note=note, author="Signals pipeline"),+ attribution=ArtefactAttribution.system(),+ )+++def unresolved_dependency_skip(refs: Collection[IssueRef]) -> AutostartSkip:+ labels = [ref.label for ref in refs]+ shown = ", ".join(labels[:5]) + (f" and {len(labels) - 5} more" if len(labels) > 5 else "")+ return AutostartSkip(+ skip_reason="blocked_by_dependency",+ detail=f"{UNRESOLVED_DETAIL_PREFIX}{shown} yet. Work starts after that issue has a report with a pull request.",+ )+++def reports_waiting_on_issues_of(+ *, team_id: int, report_id: str, ch_user: ClickHouseUser = ClickHouseUser.DEFAULT+) -> list[str]:+ """The reports whose recent auto-start hold names a GitHub issue that this report holds."""+ holds = SignalReportArtefact.objects.using("default").filter(+ team_id=team_id,+ type=SignalReportArtefact.ArtefactType.AUTOSTART_SKIP,+ created_at__gte=timezone.now() - WAITING_LOOKBACK,+ content__contains=UNRESOLVED_DETAIL_PREFIX,+ )+ if not holds.exists():+ return []+ issues = report_issues(Team.objects.get(pk=team_id), report_id, ch_user=ch_user)+ if not issues:+ return []+ # The digit guard stops `owner/repo#12` from matching a hold on `owner/repo#123`.+ label_filter = Q()+ for issue in issues:+ label_filter |= Q(content__iregex=rf"{re.escape(issue.ref.label)}([^0-9]|$)")+ waiting = holds.filter(label_filter).exclude(report_id=report_id).values_list("report_id", flat=True)+ return list(dict.fromkeys(str(rid) for rid in waiting))diff --git a/products/signals/backend/temporal/summary.py b/products/signals/backend/temporal/summary.pyindex 60446ab0eccc..261592f90c5a 100644--- a/products/signals/backend/temporal/summary.py+++ b/products/signals/backend/temporal/summary.py@@ -45,6 +45,7 @@ from products.signals.backend.auto_start import ()from products.signals.backend.daily_limit import capture_signal_report_daily_limit_paused, daily_report_limit_gatefrom products.signals.backend.github_optional import RepositoryAccessTemporarilyUnavailable+from products.signals.backend.issue_dependencies import reports_waiting_on_issues_offrom products.signals.backend.models import (SIGNALS_AT_RUN_INCREMENT,SignalReport,@@ -68,7 +69,11 @@ from products.signals.backend.spend import (report_triggering_signal,submit_signal_metadata_updates,)-from products.signals.backend.stack_plan import create_layer_reports, start_unblocked_layers_of_plan+from products.signals.backend.stack_plan import (+ create_layer_reports,+ start_unblocked_layers,+ start_unblocked_layers_of_plan,+)from products.signals.backend.startup_failure import (ResearchStartupFailure,notify_research_startup_failure,@@ -1398,6 +1403,13 @@ async def maybe_autostart_implementation_activity(input: MaybeAutostartImplementawait database_sync_to_async(start_unblocked_layers_of_plan, thread_sensitive=False)(team_id=input.team_id, parent_report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS)+ # Reports held because no report held a GitHub issue they depend on can link to this one now.+ waiting_ids = await database_sync_to_async(reports_waiting_on_issues_of, thread_sensitive=False)(+ team_id=input.team_id, report_id=input.report_id, ch_user=ClickHouseUser.SIGNALS+ )+ await database_sync_to_async(start_unblocked_layers, thread_sensitive=False)(+ team_id=input.team_id, report_ids=waiting_ids, ch_user=ClickHouseUser.SIGNALS+ )except RepositoryAccessTemporarilyUnavailable as error:raise ApplicationError(str(error),diff --git a/products/signals/backend/test/test_auto_start.py b/products/signals/backend/test/test_auto_start.pyindex 961c5525f81e..c9ffe6b05ef2 100644--- a/products/signals/backend/test/test_auto_start.py+++ b/products/signals/backend/test/test_auto_start.py@@ -51,6 +51,7 @@ from products.signals.backend.auto_start import (maybe_autostart_implementation_task,)from products.signals.backend.enums import ReportLinkKind+from products.signals.backend.issue_dependencies import IssueReffrom products.signals.backend.models import (MAX_SCOUT_CONTENT_REVISIONS,ArtefactAttribution,@@ -1227,6 +1228,7 @@ def _link(team_id: int, source: SignalReport, target: SignalReport, kind: Report("later_duplicate_of_with_pr", "duplicate_of"),("depends_on_without_pr", "blocked_by_dependency"),("depends_on_with_open_pr", None),+ ("unresolved_github_issue", "blocked_by_dependency"),("incoming_part_of", "plan_parent"),("none", None),],@@ -1349,6 +1351,10 @@ async def test_typed_links_hold_back_autostart(link, expect_skip_reason, link_bereturn_value=AgentRuntime(),),patch("products.signals.backend.auto_start.posthoganalytics.capture") as capture_mock,+ patch(+ "products.signals.backend.auto_start.resolve_issue_dependencies",+ return_value=(IssueRef(repository="owner/repo", number=7),) if link == "unresolved_github_issue" else (),+ ),):outcome = await maybe_autostart_implementation_task(team_id=team.id,diff --git a/products/signals/backend/test/test_issue_dependencies.py b/products/signals/backend/test/test_issue_dependencies.pynew file mode 100644index 000000000000..058a06ae6dad--- /dev/null+++ b/products/signals/backend/test/test_issue_dependencies.py@@ -0,0 +1,173 @@+from datetime import timedelta++from posthog.test.base import BaseTest+from unittest.mock import patch++from posthog.clickhouse.client.connection import ClickHouseUser++from products.signals.backend.artefact_schemas import ReportLink+from products.signals.backend.auto_start import _evaluate_link_gates+from products.signals.backend.enums import ReportLinkKind+from products.signals.backend.issue_dependencies import (+ IssueMatch,+ IssueRef,+ ReportIssue,+ _issue_matches,+ report_issues,+ reports_waiting_on_issues_of,+ resolve_issue_dependencies,+ unresolved_dependency_skip,+)+from products.signals.backend.models import ArtefactAttribution, SignalReport, SignalReportArtefact+from products.signals.backend.test.test_signal_queries import _SignalEmbeddingsTestBase+from products.signals.backend.typed_report_links import outgoing_links++MODULE = "products.signals.backend.issue_dependencies"+DEPENDENT_ISSUE = IssueRef(repository="acme/app", number=2)+DEPENDENCY_ISSUE = IssueRef(repository="acme/app", number=1)+++class TestIssueDependencies(BaseTest):+ def _report(self, status: str = SignalReport.Status.READY) -> SignalReport:+ return SignalReport.objects.create(+ team=self.team, status=status, title="t", summary="s", signal_count=1, total_weight=1.0+ )++ def _resolve(+ self, report: SignalReport, *, depends_on: tuple[IssueRef, ...], matches: dict[IssueRef, IssueMatch]+ ) -> tuple[IssueRef, ...]:+ with (+ patch(+ f"{MODULE}.report_issues",+ return_value=[ReportIssue(ref=DEPENDENT_ISSUE, depends_on=depends_on)],+ ),+ patch(f"{MODULE}._issue_matches", return_value=matches),+ ):+ return resolve_issue_dependencies(team_id=self.team.id, report_id=str(report.id))++ def _targets(self, report: SignalReport) -> list[str]:+ return [+ edge.target_id+ for edge in outgoing_links(team_id=self.team.id, report_id=report.id, kinds=(ReportLinkKind.DEPENDS_ON,))+ ]++ def test_dependency_report_without_pull_request_gets_a_link_that_holds_autostart(self) -> None:+ dependent, dependency = self._report(), self._report()+ matches = {DEPENDENCY_ISSUE: IssueMatch(report_ids=(str(dependency.id),), closed=False)}++ assert self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches) == ()+ assert self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches) == ()++ assert self._targets(dependent) == [str(dependency.id)]+ skip = _evaluate_link_gates(self.team.id, str(dependent.id))+ assert skip is not None+ assert skip.skip_reason == "blocked_by_dependency"+ assert skip.linked_report_id == str(dependency.id)++ def test_closed_dependency_issue_counts_as_met(self) -> None:+ dependent, dependency = self._report(), self._report()+ matches = {DEPENDENCY_ISSUE: IssueMatch(report_ids=(str(dependency.id),), closed=True)}++ assert self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches) == ()+ assert self._targets(dependent) == []++ def test_missing_dependency_holds_until_its_report_appears(self) -> None:+ dependent = self._report()+ unrelated = self._report()++ unresolved = self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches={})+ assert unresolved == (DEPENDENCY_ISSUE,)+ SignalReportArtefact.add_log(+ team_id=self.team.id,+ report_id=str(dependent.id),+ content=unresolved_dependency_skip(unresolved),+ attribution=ArtefactAttribution.system(),+ )+ # A hold on `acme/app#12` must not read as a hold on `acme/app#1`.+ SignalReportArtefact.add_log(+ team_id=self.team.id,+ report_id=str(unrelated.id),+ content=unresolved_dependency_skip([IssueRef(repository="acme/app", number=12)]),+ attribution=ArtefactAttribution.system(),+ )++ dependency = self._report()+ with patch(f"{MODULE}.report_issues", return_value=[ReportIssue(ref=DEPENDENCY_ISSUE, depends_on=())]):+ waiting = reports_waiting_on_issues_of(team_id=self.team.id, report_id=str(dependency.id))+ assert waiting == [str(dependent.id)]++ matches = {DEPENDENCY_ISSUE: IssueMatch(report_ids=(str(dependency.id),), closed=False)}+ assert self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches) == ()+ assert self._targets(dependent) == [str(dependency.id)]++ def test_dependency_on_a_duplicate_links_to_the_canonical_report(self) -> None:+ dependent, canonical, duplicate = self._report(), self._report(), self._report()+ suppressed = self._report(SignalReport.Status.SUPPRESSED)+ SignalReportArtefact.add_log(+ team_id=self.team.id,+ report_id=str(duplicate.id),+ content=ReportLink(kind=ReportLinkKind.DUPLICATE_OF, report_id=str(canonical.id)),+ attribution=ArtefactAttribution.system(),+ )+ matches = {+ DEPENDENCY_ISSUE: IssueMatch(report_ids=(str(suppressed.id), str(duplicate.id)), closed=False),+ }++ assert self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches) == ()+ assert self._targets(dependent) == [str(canonical.id)]++ def test_removed_dependency_line_keeps_the_link_and_notes_it_once(self) -> None:+ dependent, dependency = self._report(), self._report()+ matches = {DEPENDENCY_ISSUE: IssueMatch(report_ids=(str(dependency.id),), closed=False)}+ self._resolve(dependent, depends_on=(DEPENDENCY_ISSUE,), matches=matches)++ self._resolve(dependent, depends_on=(), matches={})+ self._resolve(dependent, depends_on=(), matches={})++ assert self._targets(dependent) == [str(dependency.id)]+ notes = SignalReportArtefact.objects.filter(+ team_id=self.team.id, report_id=dependent.id, type=SignalReportArtefact.ArtefactType.NOTE+ )+ assert notes.count() == 1+ assert "acme/app#1" in notes.get().content+++class TestIssueDependencyQueries(_SignalEmbeddingsTestBase):+ def test_reads_current_refs_and_matches_issues_by_url(self) -> None:+ dependent_url = "https://github.com/Acme/App/issues/2"+ self._emit_version(+ document_id="dep-v1",+ report_id="r-dependent",+ source_product="github",+ source_type="issue",+ source_id="issue-2",+ inserted_at=self.base,+ extra={"html_url": dependent_url, "depends_on_issues": [{"repository": "acme/app", "number": 9}]},+ )+ # The edited body emits a new signal, and only that one says what the issue depends on now.+ self._emit_version(+ document_id="dep-v2",+ report_id="r-dependent",+ source_product="github",+ source_type="issue",+ source_id="issue-2",+ inserted_at=self.base + timedelta(minutes=1),+ extra={"html_url": dependent_url, "depends_on_issues": [{"repository": "acme/app", "number": 1}]},+ )+ self._emit_version(+ document_id="target",+ report_id="r-dependency",+ source_product="github",+ source_type="issue",+ source_id="issue-1",+ inserted_at=self.base,+ extra={"html_url": "https://github.com/ACME/app/issues/1", "state": "open"},+ )++ assert report_issues(self.team, "r-dependent", ch_user=ClickHouseUser.DEFAULT) == [+ ReportIssue(ref=DEPENDENT_ISSUE, depends_on=(DEPENDENCY_ISSUE,))+ ]+ missing = IssueRef(repository="acme/app", number=3)+ assert _issue_matches(self.team, [DEPENDENCY_ISSUE, missing], ch_user=ClickHouseUser.DEFAULT) == {+ DEPENDENCY_ISSUE: IssueMatch(report_ids=("r-dependency",), closed=False)+ }diff --git a/products/signals/frontend/generated/api.schemas.ts b/products/signals/frontend/generated/api.schemas.tsindex e2b1b06eb0c2..449e1cf7cd3c 100644--- a/products/signals/frontend/generated/api.schemas.ts+++ b/products/signals/frontend/generated/api.schemas.ts@@ -3825,6 +3825,11 @@ export interface ZendeskTicketSignalExtraApi {status: string}+export interface GithubIssueDependencyRefApi {+ repository: string+ number: number+}+export interface GithubIssueSignalExtraApi {html_url: stringnumber: number@@ -3835,6 +3840,7 @@ export interface GithubIssueSignalExtraApi {state: stringauthor_login?: string | nullauthor_association?: string | null+ depends_on_issues?: GithubIssueDependencyRefApi[]}export interface LinearIssueSignalExtraApi {diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 2558557e5c02..1d7cc8765685 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -57952,6 +57952,11 @@ export namespace Schemas {created_at: string | null;}+ export interface GithubIssueDependencyRef {+ repository: string;+ number: number;+ }+export interface GithubIssueSignalExtra {html_url: string;number: number;@@ -57962,6 +57967,7 @@ export namespace Schemas {state: string;author_login?: string | null;author_association?: string | null;+ depends_on_issues?: GithubIssueDependencyRef[];}export interface GitlabIssueSignalExtra {