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): research pending source refreshes at settlement (#126581)

Mikayla Thompson53a91c8f036b
diff --git a/docs/internal/signals-pr-lifecycle.md b/docs/internal/signals-pr-lifecycle.md
index df4f4fc9e9b4..4892f525df38 100644
--- a/docs/internal/signals-pr-lifecycle.md
+++ b/docs/internal/signals-pr-lifecycle.md
@@ -212,6 +212,10 @@ Reopening implementation clears the measurement anchor so a later Monitoring per
New metric checks validate numeric goals and baselines against their metric kind, format, unit, and query. Existing check configurations remain readable.
+## Pending research refreshes
+
+When a source refresh arrives during research, finishing the current pass schedules another pass even if the next signal-count bucket has not been reached. A pending marker already included in the fetched snapshot does not force redundant research. New workflow histories retain the marker while waiting up to ten minutes for the triggering signal to become visible, before any paid research. The bounded 100-signal snapshot prioritizes the triggering and pending signals, including refreshes beyond the oldest rows. A successful fetch consumes only its own visible marker, retaining newer pending refreshes and allowing later bucket crossings to reserve their owner. Retries still require the original trigger after its marker is consumed. A visibility timeout reports a technical failure instead of researching an incomplete snapshot. A later source refresh replaces the marker so it cannot be hidden by an earlier bucket crossing. Replacing a pending signal settles its handed-off spend unless it still owns the active research pass. Ordinary new signals continue to follow the existing bucket thresholds.
+
## Research without code
Research without a repository retains the writing rules, attached-image inspection, remediation guidance, report history, intent checks, work-in-flight checks, and per-signal tool budget. The opening prompt states once that no repository is checked out. It uses project data and connected sources to find a concrete engineering starting point. Code-specific instructions are omitted, findings allow empty code and commit evidence, and the output omits pull request planning. Later turns use the same evidence-based verification guidance without repeating access warnings.
diff --git a/products/signals/backend/signal_metadata.py b/products/signals/backend/signal_metadata.py
index e548a5eb1506..533711ea08c3 100644
--- a/products/signals/backend/signal_metadata.py
+++ b/products/signals/backend/signal_metadata.py
@@ -457,7 +457,11 @@ def fetch_origin_sources_for_report(team: Team, report_id: str) -> list[OriginSo
def _signals_for_report_query(
- *, include_deleted: bool = False, limit: int | None = None, newest_first: bool = False
+ *,
+ include_deleted: bool = False,
+ limit: int | None = None,
+ newest_first: bool = False,
+ prioritize_signal_ids: bool = False,
) -> str:
"""Build a HogQL query that fetches signal rows for a single report.
@@ -466,9 +470,11 @@ def _signals_for_report_query(
Used by soft-delete which intentionally re-processes already-deleted rows.
limit: Optional row cap appended as a LIMIT clause.
newest_first: Order by newest timestamp first, so a limit keeps the newest rows.
+ prioritize_signal_ids: Keep the IDs in the priority_signal_ids placeholder before applying the cap.
"""
deleted_filter = "" if include_deleted else "\n AND NOT JSONExtractBool(metadata, 'deleted')"
limit_clause = "" if limit is None else f"\n LIMIT {limit}"
+ priority_order = "document_id IN {priority_signal_ids} DESC, " if prioritize_signal_ids else ""
return f"""
SELECT
@@ -479,7 +485,7 @@ def _signals_for_report_query(
latest_inserted_at
FROM ({_deduped_signals_subquery(candidate_document_filter="JSONExtractString(metadata, 'report_id') = {report_id}")})
WHERE JSONExtractString(metadata, 'report_id') = {{report_id}}{deleted_filter}
- ORDER BY timestamp {"DESC" if newest_first else "ASC"}{limit_clause}
+ ORDER BY {priority_order}timestamp {"DESC" if newest_first else "ASC"}{limit_clause}
"""
diff --git a/products/signals/backend/temporal/grouping.py b/products/signals/backend/temporal/grouping.py
index b9179554595d..1231664d49b7 100644
--- a/products/signals/backend/temporal/grouping.py
+++ b/products/signals/backend/temporal/grouping.py
@@ -1024,13 +1024,23 @@ async def assign_and_emit_signal_activity(input: AssignAndEmitSignalInput) -> As
promoted = True
pending_bucket = next_research_bucket(report.researching_signal_count or 0)
- if (
- report.status == SignalReport.Status.IN_PROGRESS
- and report.pending_triggering_signal_id is None
- and (source_refresh or (pending_bucket is not None and report.signal_count >= pending_bucket))
+ if report.status == SignalReport.Status.IN_PROGRESS and (
+ source_refresh
+ or (
+ report.pending_triggering_signal_id is None
+ and pending_bucket is not None
+ and report.signal_count >= pending_bucket
+ )
):
+ displaced_signal_id = report.pending_triggering_signal_id
report.pending_triggering_signal_id = input.signal_id
report.save(update_fields=["pending_triggering_signal_id"])
+ if displaced_signal_id is not None and str(displaced_signal_id) != input.signal_id:
+ complete_signal_pipeline(
+ team_id=input.team_id,
+ signal_id=str(displaced_signal_id),
+ report_id=str(report.id),
+ )
report_id = str(report.id)
if input.sync_with_source:
diff --git a/products/signals/backend/temporal/signal_queries.py b/products/signals/backend/temporal/signal_queries.py
index 1e887e5883ba..6aede297cc1f 100644
--- a/products/signals/backend/temporal/signal_queries.py
+++ b/products/signals/backend/temporal/signal_queries.py
@@ -8,6 +8,7 @@ from typing import Union
import structlog
import temporalio
from asgiref.sync import async_to_sync
+from temporalio.exceptions import ApplicationError
from posthog.hogql import ast
from posthog.hogql.query import execute_hogql_query
@@ -41,6 +42,7 @@ from products.signals.backend.temporal.types import SignalCandidate, SignalData,
logger = structlog.get_logger(__name__)
+REPORT_RESEARCH_SIGNAL_LIMIT = 100
WAIT_POLL_INTERVAL_SECONDS = 10
# For this long, ClickHouse is polled only when the recently-seen store confirms the
@@ -604,15 +606,17 @@ async def wait_for_signal_in_clickhouse_activity(input: WaitForClickHouseInput)
# ---------------------------------------------------------------------------
-@dataclass
+@frozen
class FetchSignalsForReportInput:
team_id: int
report_id: str
+ required_signal_id: str | None = None
-@dataclass
+@frozen
class FetchSignalsForReportOutput:
signals: list[SignalData]
+ processed_pending_signal_id: str | None = None
@temporalio.activity.defn
@@ -620,16 +624,54 @@ class FetchSignalsForReportOutput:
@close_db_connections
async def fetch_signals_for_report_activity(input: FetchSignalsForReportInput) -> FetchSignalsForReportOutput:
try:
- team = await Team.objects.aget(pk=input.team_id)
+ team = await Team.objects.select_related("organization").aget(pk=input.team_id)
+ pending_id = await (
+ SignalReport.objects.using("default")
+ .filter(team_id=input.team_id, id=input.report_id)
+ .values_list("pending_triggering_signal_id", flat=True)
+ .afirst()
+ )
+ pending_signal_id = str(pending_id) if pending_id else None
+ required_ids = set(filter(None, [input.required_signal_id]))
+ priority_ids = required_ids | set(filter(None, [pending_signal_id]))
+ placeholders = {
+ **_report_placeholders(input.report_id),
+ "priority_signal_ids": ast.Tuple(
+ exprs=[ast.Constant(value=signal_id) for signal_id in sorted(priority_ids)]
+ ),
+ }
result = await execute_hogql_query_with_retry(
query_type="SignalsFetchForReport",
- query=_signals_for_report_query(),
+ query=_signals_for_report_query(
+ limit=REPORT_RESEARCH_SIGNAL_LIMIT, prioritize_signal_ids=bool(priority_ids)
+ ),
team=team,
- placeholders=_report_placeholders(input.report_id),
+ placeholders=placeholders,
)
- signals = [_parse_signal_row(row) for row in (result.results or [])]
+ signals = sorted(
+ (_parse_signal_row(row) for row in (result.results or [])),
+ key=lambda signal: (signal.timestamp, signal.signal_id),
+ )
+ fetched_ids = {signal.signal_id for signal in signals}
+ if not required_ids.issubset(fetched_ids):
+ raise ApplicationError(
+ "Required report signal is not visible in ClickHouse yet", type="RequiredReportSignalNotVisible"
+ )
+
+ if required_ids:
+ await (
+ SignalReport.objects.using("default")
+ .filter(
+ team_id=input.team_id,
+ id=input.report_id,
+ status=SignalReport.Status.IN_PROGRESS,
+ triggering_signal_id=input.required_signal_id,
+ pending_triggering_signal_id=input.required_signal_id,
+ )
+ .aupdate(pending_triggering_signal_id=None)
+ )
logger.debug(
f"Fetched {len(signals)} signals for report {input.report_id}",
@@ -637,7 +679,12 @@ async def fetch_signals_for_report_activity(input: FetchSignalsForReportInput) -
report_id=input.report_id,
signal_count=len(signals),
)
- return FetchSignalsForReportOutput(signals=signals)
+ return FetchSignalsForReportOutput(
+ signals=signals,
+ processed_pending_signal_id=pending_signal_id if pending_signal_id in fetched_ids else None,
+ )
+ except ApplicationError:
+ raise
except Exception as e:
logger.exception(
f"Failed to fetch signals for report {input.report_id}: {e}",
diff --git a/products/signals/backend/temporal/summary.py b/products/signals/backend/temporal/summary.py
index a68401649002..9904de689a53 100644
--- a/products/signals/backend/temporal/summary.py
+++ b/products/signals/backend/temporal/summary.py
@@ -444,12 +444,20 @@ class SignalReportSummaryWorkflow:
async def _fetch_signals(
self, inputs: SignalReportSummaryWorkflowInputs, log: FilteringBoundLogger
) -> FetchSignalsForReportOutput:
- fetch_input = FetchSignalsForReportInput(team_id=inputs.team_id, report_id=inputs.report_id)
+ required_signal_id = inputs.signal_id if workflow.patched("signals-required-refresh-snapshot") else None
+ fetch_input = FetchSignalsForReportInput(
+ team_id=inputs.team_id, report_id=inputs.report_id, required_signal_id=required_signal_id
+ )
fetch_result: FetchSignalsForReportOutput = await workflow.execute_activity(
fetch_signals_for_report_activity,
fetch_input,
start_to_close_timeout=timedelta(minutes=5),
- retry_policy=RetryPolicy(maximum_attempts=3),
+ schedule_to_close_timeout=timedelta(minutes=10) if required_signal_id else None,
+ retry_policy=(
+ RetryPolicy(initial_interval=timedelta(seconds=10), maximum_interval=timedelta(seconds=30))
+ if required_signal_id
+ else RetryPolicy(maximum_attempts=3)
+ ),
)
# patched(): histories recorded before the retry loop have no timers to replay.
if fetch_result.signals or not workflow.patched("signals-empty-fetch-retry"):
@@ -515,13 +523,18 @@ class SignalReportSummaryWorkflow:
log.info("Report run paused: org over self-driving credits quota", stage="summary_entry")
return False
claim_before_fetch = workflow.patched("signals-spend-explicit-owner")
+ signal_count = 0
+ source_products: list[str] = []
try:
if claim_before_fetch:
# Grouping must reserve arrivals for the next pass before we take the ClickHouse snapshot.
await workflow.execute_activity(
mark_report_in_progress_activity,
MarkReportInProgressInput(
- team_id=inputs.team_id, report_id=inputs.report_id, signal_id=inputs.signal_id
+ team_id=inputs.team_id,
+ report_id=inputs.report_id,
+ signal_id=inputs.signal_id,
+ preserve_pending_refresh=workflow.patched("signals-required-refresh-snapshot"),
),
start_to_close_timeout=timedelta(minutes=1),
retry_policy=RetryPolicy(maximum_attempts=3),
@@ -761,6 +774,7 @@ class SignalReportSummaryWorkflow:
title=decision.title,
summary=decision.summary,
processed_signal_count=signal_count,
+ processed_pending_signal_id=fetch_result.processed_pending_signal_id,
source_products=source_products,
charts=decision.charts,
metrics=decision.metrics,
@@ -1017,6 +1031,7 @@ class MarkReportInProgressInput:
signal_count: int | None = None
source_products: list[str] = field(default_factory=list)
signal_id: str | None = None
+ preserve_pending_refresh: bool = False
@temporalio.activity.defn
@@ -1042,7 +1057,8 @@ async def mark_report_in_progress_activity(input: MarkReportInProgressInput) ->
updated_fields = report.transition_to(
SignalReport.Status.IN_PROGRESS, signals_at_run_increment=SIGNALS_AT_RUN_INCREMENT
)
- report.pending_triggering_signal_id = None
+ if not input.preserve_pending_refresh:
+ report.pending_triggering_signal_id = None
if input.signal_count is None:
input.signal_count = report.signal_count
report.researching_signal_count = input.signal_count
@@ -1103,6 +1119,7 @@ class MarkReportReadyInput:
title: str
summary: str
processed_signal_count: int
+ processed_pending_signal_id: str | None = None
source_products: list[str] = field(default_factory=list)
# Chart payload to write alongside title/summary, in the same transaction: the JSON set to store,
# `[]` to clear, or `None` to leave the column untouched. Defaults to `None` so an older workflow
@@ -1277,9 +1294,13 @@ async def mark_report_ready_activity(input: MarkReportReadyInput) -> bool:
# to its next bucket. Same predicate as the grouping promotion gate, so a signal landing
# mid-run is researched on the schedule it would have had if it had landed after. The
# bucket is strictly above what this run processed, so reaching it also means new
- # signals arrived.
+ # signals arrived. A pending source refresh needs research even between buckets.
bucket = next_research_bucket(report.researched_signal_count)
- has_new_signals = bucket is not None and report.signal_count >= bucket
+ has_pending_refresh = (
+ report.pending_triggering_signal_id is not None
+ and str(report.pending_triggering_signal_id) != input.processed_pending_signal_id
+ )
+ has_new_signals = has_pending_refresh or (bucket is not None and report.signal_count >= bucket)
if has_new_signals:
# If more signals arrived while the report was being processed, we want to
# re-promote it back to candidate and loop to also process new signals
diff --git a/products/signals/backend/test/test_assign_and_emit_signal.py b/products/signals/backend/test/test_assign_and_emit_signal.py
index 397ba8cec760..823edfcf93a8 100644
--- a/products/signals/backend/test/test_assign_and_emit_signal.py
+++ b/products/signals/backend/test/test_assign_and_emit_signal.py
@@ -911,23 +911,50 @@ async def test_source_sync_matches_without_semantic_rejudgment(ateam):
@pytest.mark.asyncio
@pytest.mark.django_db
-async def test_source_refresh_bypasses_research_buckets_and_emits_telemetry(ateam, patch_side_effects):
[email protected]("researching,pending_is_driver", [(False, False), (True, False), (True, True)])
+async def test_source_refresh_bypasses_research_buckets_and_emits_telemetry(
+ ateam, patch_side_effects, researching, pending_is_driver
+):
+ pending_signal_id = uuid.uuid4() if researching else None
report = await database_sync_to_async(SignalReport.objects.create)(
team=ateam,
- status=SignalReport.Status.READY,
+ status=SignalReport.Status.IN_PROGRESS if researching else SignalReport.Status.READY,
total_weight=2,
signal_count=2,
signals_researched=2,
+ triggering_signal_id=pending_signal_id if pending_is_driver else uuid.uuid4(),
+ pending_triggering_signal_id=pending_signal_id,
)
+ if pending_signal_id is not None:
+ await database_sync_to_async(record_llm_spend)(
+ team_id=ateam.id, signal_id=str(pending_signal_id), stage="grouping", token_cost_microusd=100
+ )
+ await database_sync_to_async(complete_signal_pipeline)(
+ team_id=ateam.id, signal_id=str(pending_signal_id), report_id=str(report.id)
+ )
+ pending_spend = await database_sync_to_async(
+ lambda: SignalSpend.objects.for_team(ateam.id).get(signal_id=pending_signal_id)
+ )()
+ assert pending_spend.finalized_at is None
input_ = _build_input(ateam.id, _existing_match(str(report.id), source_synced=True), weight=0.5)
input_.sync_with_source = True
result = await assign_and_emit_signal_activity(input_)
assert result.report_id == str(report.id)
- assert result.promoted is True
+ assert result.promoted is not researching
await database_sync_to_async(report.refresh_from_db)()
- assert report.status == SignalReport.Status.CANDIDATE
+ assert report.status == (SignalReport.Status.IN_PROGRESS if researching else SignalReport.Status.CANDIDATE)
+ if researching:
+ assert str(report.pending_triggering_signal_id) == input_.signal_id
+ await database_sync_to_async(pending_spend.refresh_from_db)()
+ assert (pending_spend.finalized_at is None) is pending_is_driver
+ assert pending_spend.spend > 0
+ await database_sync_to_async(complete_signal_pipeline)(
+ team_id=ateam.id, signal_id=str(pending_signal_id), report_id=str(report.id)
+ )
+ await database_sync_to_async(pending_spend.refresh_from_db)()
+ assert (pending_spend.finalized_at is None) is pending_is_driver
events = [call.kwargs["event"] for call in patch_side_effects["capture"].call_args_list]
assert "signal_source_report_refresh_requested" in events
refresh = next(
diff --git a/products/signals/backend/test/test_report_telemetry.py b/products/signals/backend/test/test_report_telemetry.py
index 3ee2dfd12274..3390d547a36e 100644
--- a/products/signals/backend/test/test_report_telemetry.py
+++ b/products/signals/backend/test/test_report_telemetry.py
@@ -91,7 +91,10 @@ async def test_startup_failure_is_durable_when_notification_delivery_fails(ateam
"charts_enabled,charts,expected_chart_count",
[(True, [CHART_PAYLOAD], 1), (False, None, 0)],
)
-async def test_started_and_ready_fire_expected_captures(ateam, charts_enabled, charts, expected_chart_count):
[email protected]("preserve_pending", [False, True])
+async def test_started_and_ready_fire_expected_captures(
+ ateam, charts_enabled, charts, expected_chart_count, preserve_pending
+):
driver = uuid4()
report = await database_sync_to_async(SignalReport.objects.create)(
team=ateam,
@@ -99,6 +102,7 @@ async def test_started_and_ready_fire_expected_captures(ateam, charts_enabled, c
signal_count=2,
total_weight=1.2,
triggering_signal_id=driver,
+ pending_triggering_signal_id=driver,
)
report_id = str(report.id)
source_products = ["conversations", "zendesk"]
@@ -110,8 +114,11 @@ async def test_started_and_ready_fire_expected_captures(ateam, charts_enabled, c
report_id=report_id,
signal_count=2,
source_products=source_products,
+ preserve_pending_refresh=preserve_pending,
)
)
+ await database_sync_to_async(report.refresh_from_db)()
+ assert (report.pending_triggering_signal_id == driver) is preserve_pending
await mark_report_ready_activity(
MarkReportReadyInput(
team_id=ateam.id,
@@ -119,6 +126,7 @@ async def test_started_and_ready_fire_expected_captures(ateam, charts_enabled, c
title="title",
summary="summary",
processed_signal_count=2,
+ processed_pending_signal_id=str(driver) if preserve_pending else None,
source_products=source_products,
charts=charts,
charts_enabled=charts_enabled,
@@ -460,23 +468,27 @@ def _seed_check_lane(team: Team) -> None:
@pytest.mark.asyncio
@pytest.mark.django_db
@pytest.mark.parametrize(
- ("run_count", "processed_signal_count", "signal_count", "expected_loop", "has_pending_driver"),
+ ("run_count", "processed_signal_count", "signal_count", "expected_loop", "has_pending_driver", "pending_processed"),
[
# Signals landed mid-run and carried the report to its next bucket: loop rather than make
# them wait for a signal that arrives after the run.
- (1, 1, 2, True, True),
- (1, 1, 9, True, True),
- (1, 1, 2, True, False),
+ (1, 1, 2, True, True, False),
+ (1, 1, 9, True, True, False),
+ (1, 1, 2, True, False, False),
# Signals landed but the report is still short of its next bucket, so the run settles.
- (2, 2, 3, False, False),
- (3, 4, 9, False, False),
+ (2, 2, 3, False, False, False),
+ (3, 4, 9, False, False, False),
+ (3, 4, 5, True, True, False),
+ (4, 10, 11, True, True, False),
+ (2, 4, 4, False, True, True),
+ (2, 4, 5, False, True, True),
# No new signals at all.
- (2, 2, 2, False, False),
+ (2, 2, 2, False, False, False),
# This pass covered the last bucket: the report stays READY however far past it grew.
- (4, 10, 15, False, False),
+ (4, 10, 15, False, False, False),
# Attempts are not passes: a report whose run_count was spent on runs that paused before
# researching still loops when this pass leaves it at a bucket.
- (9, 1, 2, True, True),
+ (9, 1, 2, True, True, False),
],
)
async def test_ready_loops_only_when_the_run_reached_the_next_bucket(
@@ -486,6 +498,7 @@ async def test_ready_loops_only_when_the_run_reached_the_next_bucket(
signal_count: int,
expected_loop: bool,
has_pending_driver: bool,
+ pending_processed: bool,
) -> None:
driver, pending_driver = uuid4(), uuid4()
await database_sync_to_async(_seed_check_lane)(ateam)
@@ -508,6 +521,7 @@ async def test_ready_loops_only_when_the_run_reached_the_next_bucket(
title="title",
summary="summary",
processed_signal_count=processed_signal_count,
+ processed_pending_signal_id=str(pending_driver) if pending_processed else None,
source_products=["zendesk"],
checks=[
{
@@ -529,7 +543,7 @@ async def test_ready_loops_only_when_the_run_reached_the_next_bucket(
refreshed = await database_sync_to_async(SignalReport.objects.get)(id=report_id)
expected_status = SignalReport.Status.CANDIDATE if expected_loop else SignalReport.Status.READY
assert refreshed.status == expected_status
- assert refreshed.triggering_signal_id == (pending_driver if has_pending_driver else driver)
+ assert refreshed.triggering_signal_id == (pending_driver if has_pending_driver and expected_loop else driver)
# Stamped by the pass that just completed, so the next bucket is measured against what this run
# actually covered rather than against how many times the workflow has started.
assert refreshed.signals_researched == processed_signal_count
diff --git a/products/signals/backend/test/test_signal_queries.py b/products/signals/backend/test/test_signal_queries.py
index fe2650322e28..aff7cee240aa 100644
--- a/products/signals/backend/test/test_signal_queries.py
+++ b/products/signals/backend/test/test_signal_queries.py
@@ -13,7 +13,9 @@ from django.db import connection
from django.test.utils import CaptureQueriesContext
from django.utils import timezone
+from asgiref.sync import async_to_sync
from parameterized import parameterized
+from temporalio.exceptions import ApplicationError
from posthog.schema import EmbeddingModelName
@@ -49,10 +51,12 @@ from products.signals.backend.signal_metadata import (
from products.signals.backend.spend import SignalMetadataChange, SignalSpendUpdate, record_llm_spend
from products.signals.backend.spend_metadata import confirm_signal_spend_metadata, publish_signal_spend_metadata
from products.signals.backend.temporal.signal_queries import (
+ FetchSignalsForReportInput,
SourceSignalMatch,
fetch_live_source_signal_matches,
fetch_report_ids_for_scout_names,
fetch_report_ids_for_scout_prefix,
+ fetch_signals_for_report_activity,
fetch_source_signal_matches,
retract_source_signals,
)
@@ -469,6 +473,50 @@ class TestFetchSignalsForReportSync(_SignalEmbeddingsTestBase):
assert self._signal_ids(query_report) == expected_ids
+ @parameterized.expand([(True, True, False), (False, True, False), (True, False, False), (True, True, True)])
+ def test_research_snapshot_keeps_required_and_pending_signals_beyond_the_row_cap(
+ self, pending_visible: bool, required_visible: bool, same_marker: bool
+ ) -> None:
+ signal_ids = [str(uuid.uuid4()) for _ in range(120)]
+ pending_id = signal_ids[-1] if pending_visible else str(uuid.uuid4())
+ required_id = pending_id if same_marker else signal_ids[-2] if required_visible else str(uuid.uuid4())
+ report = SignalReport.objects.create(
+ team=self.team,
+ status=SignalReport.Status.IN_PROGRESS,
+ triggering_signal_id=required_id,
+ pending_triggering_signal_id=pending_id,
+ )
+ for index, signal_id in enumerate(signal_ids):
+ self._emit_version(
+ document_id=signal_id,
+ report_id=str(report.id),
+ source_product="errors",
+ inserted_at=self.base + timedelta(minutes=index),
+ )
+ request = FetchSignalsForReportInput(
+ team_id=self.team.id, report_id=str(report.id), required_signal_id=required_id
+ )
+ if not required_visible:
+ with self.assertRaisesRegex(ApplicationError, "not visible"):
+ async_to_sync(fetch_signals_for_report_activity)(request)
+ report.refresh_from_db()
+ assert str(report.pending_triggering_signal_id) == pending_id
+ return
+ result = async_to_sync(fetch_signals_for_report_activity)(request)
+ fetched_ids = {signal.signal_id for signal in result.signals}
+ assert len(result.signals) == 100
+ assert required_id in fetched_ids
+ assert (pending_id in fetched_ids) is pending_visible
+ assert result.processed_pending_signal_id == (pending_id if pending_visible else None)
+ assert [signal.timestamp for signal in result.signals] == sorted(signal.timestamp for signal in result.signals)
+ report.refresh_from_db()
+ if same_marker:
+ assert report.pending_triggering_signal_id is None
+ retry = async_to_sync(fetch_signals_for_report_activity)(request)
+ assert required_id in {signal.signal_id for signal in retry.signals}
+ else:
+ assert str(report.pending_triggering_signal_id) == pending_id
+
def test_returns_latest_content_for_a_revised_signal(self) -> None:
# The dedup must surface the newest version's content, not an arbitrary one.
self._emit_version(
diff --git a/products/signals/backend/test/test_summary_workflow.py b/products/signals/backend/test/test_summary_workflow.py
index c85a226e7064..16b921024790 100644
--- a/products/signals/backend/test/test_summary_workflow.py
+++ b/products/signals/backend/test/test_summary_workflow.py
@@ -188,6 +188,7 @@ class _Recorder:
gate_answers: dict[str, bool] | None = None,
# Signals returned by successive fetches; the last entry repeats once exhausted.
fetch_results: list[list[SignalData]] | None = None,
+ fetch_failures: int = 0,
has_assigned_signals: bool = True,
research_choice: ActionabilityChoice = ActionabilityChoice.NOT_ACTIONABLE,
research_metrics: list[dict[str, object]] | None = None,
@@ -215,6 +216,9 @@ class _Recorder:
self.github_optional = github_optional
self.gate_checks: list[str] = []
self.fetches = 0
+ self.fetch_failures = fetch_failures
+ self.fetch_required_ids: list[str | None] = []
+ self.preserved_pending: list[bool] = []
self.claimed_before_fetch: list[bool] = []
self.assigned_signal_checks = 0
self.failure_reasons: list[str | None] = []
@@ -230,6 +234,7 @@ class _Recorder:
self.failures = 0
self.pending_inputs: list[MarkReportPendingInput] = []
self.ready_inputs: list[MarkReportReadyInput] = []
+ self.processed_pending_signal_id: str | None = None
def _signal_data() -> SignalData:
@@ -278,8 +283,13 @@ async def _run_summary_workflow(recorder: _Recorder) -> None:
async def fake_fetch(input: FetchSignalsForReportInput) -> FetchSignalsForReportOutput:
signals = recorder.fetch_results[min(recorder.fetches, len(recorder.fetch_results) - 1)]
recorder.fetches += 1
+ recorder.fetch_required_ids.append(input.required_signal_id)
recorder.claimed_before_fetch.append(recorder.marks_in_progress > 0)
- return FetchSignalsForReportOutput(signals=signals)
+ if recorder.fetches <= recorder.fetch_failures:
+ raise ApplicationError("Required signal is not visible yet", type="RequiredReportSignalNotVisible")
+ return FetchSignalsForReportOutput(
+ signals=signals, processed_pending_signal_id=recorder.processed_pending_signal_id
+ )
@activity.defn(name="report_has_assigned_signals_activity")
async def fake_has_assigned_signals(input: ReportHasAssignedSignalsInput) -> bool:
@@ -295,6 +305,7 @@ async def _run_summary_workflow(recorder: _Recorder) -> None:
@activity.defn(name="mark_report_in_progress_activity")
async def fake_mark_in_progress(input: MarkReportInProgressInput) -> None:
recorder.marks_in_progress += 1
+ recorder.preserved_pending.append(input.preserve_pending_refresh)
@activity.defn(name="report_safety_judge_activity")
async def fake_safety(input: SafetyJudgeInput) -> SafetyJudgeOutput:
@@ -536,6 +547,7 @@ async def test_metric_payload_reaches_the_report_transition(choice, target, chec
recorder.checks_summary = "Expected impact matches the reconciled checks."
recorder.checks_error = checks_error
recorder.checks_monitoring_enabled = True
+ recorder.processed_pending_signal_id = str(uuid.uuid4())
await _run_summary_workflow(recorder)
@@ -545,6 +557,8 @@ async def test_metric_payload_reaches_the_report_transition(choice, target, chec
assert inputs[0].checks_snapshot == snapshot
assert inputs[0].checks_monitoring_enabled is True
assert inputs[0].checks_summary == recorder.checks_summary
+ if target == "ready":
+ assert recorder.ready_inputs[0].processed_pending_signal_id == recorder.processed_pending_signal_id
assert inputs[0].checks_error == checks_error
@@ -592,10 +606,26 @@ async def test_summary_without_an_open_spend_owner_makes_no_paid_calls():
@pytest.mark.asyncio
-async def test_empty_first_fetch_is_retried_and_run_proceeds():
- recorder = _Recorder(fetch_results=[[], [], [_signal_data()]])
[email protected]("fetch_failures", [0, 4, 1000])
+async def test_incomplete_first_snapshot_is_retried_before_research(fetch_failures: int) -> None:
+ recorder = _Recorder(
+ fetch_results=[[_signal_data()]] if fetch_failures else [[], [], [_signal_data()]],
+ fetch_failures=fetch_failures,
+ )
+ if fetch_failures == 1000:
+ with pytest.raises(WorkflowFailureError):
+ await _run_summary_workflow(recorder)
+ assert recorder.failures == 1
+ assert recorder.researches == 0
+ assert recorder.safety_checks == 0
+ assert recorder.assigned_signal_checks == 0
+ return
await _run_summary_workflow(recorder)
- assert recorder.fetches == 3
+ assert recorder.fetches == (5 if fetch_failures else 3)
+ if fetch_failures:
+ assert all(recorder.fetch_required_ids)
+ assert len(set(recorder.fetch_required_ids)) == 1
+ assert all(recorder.preserved_pending)
assert all(recorder.claimed_before_fetch)
assert recorder.researches == 1
assert recorder.spend_publications == 1