This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch fork-ci-time-based-status-budget
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 3d0f7c04f25351695f1daf84b4056cfb8aeffda2
Author: Jarek Potiuk <[email protected]>
AuthorDate: Sun Nov 15 23:05:52 2026 +0900

    Post fork CI job statuses at the intended hourly rate
    
    GitHub starts the 5-minute monitor only every 9-10 minutes, so a fixed
    13 writes a run posted about half the job statuses an hour the budget
    was sized for, and a few full-matrix fork runs at once took hours to
    show their jobs. A run that only deferred them also alerted Slack after
    nine runs, although each run made progress; a run whose statuses are
    still missing an hour after it finished is already reported on its own.
    
    Generated-by: Claude Opus 5
    Claude-Session: https://claude.ai/code/session_01NyQq9U7i5HhjDgrQ9qd3gq
---
 dev/AGENTS.md                                      |  8 +-
 .../doc/ci/08_fork_ci_for_external_contributors.md | 54 +++++++------
 .../src/airflow_breeze/utils/fork_ci_reconcile.py  | 79 ++++++++++++-------
 dev/breeze/tests/test_fork_ci_reconcile.py         | 90 ++++++++++++++++------
 4 files changed, 149 insertions(+), 82 deletions(-)

diff --git a/dev/AGENTS.md b/dev/AGENTS.md
index c2ac0cbbb2f..d0e6889b90e 100644
--- a/dev/AGENTS.md
+++ b/dev/AGENTS.md
@@ -53,11 +53,9 @@ workflow of `apache/airflow` shares (see "Write budget" in
 `dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md`).
 
 If the budget is exceeded, turn the job statuses off: set `job_statuses = 
false` in
-`.github/fork-ci-gate.toml`. Signs that it is exceeded are the monitor's Slack 
note that job
-statuses "have not fit in the budget" (posted only after job statuses were 
deferred for about 45 minutes in a row)
-staying for hours, pull requests listed because their fork run finished more 
than an hour ago with
-job statuses still not posted, its Slack note that GitHub rate-limited it for 
about 30 minutes in a row, the
+`.github/fork-ci-gate.toml`. Signs that it is exceeded are pull requests 
listed because their fork
+run finished more than an hour ago with job statuses still not posted, staying 
for hours, its Slack note that GitHub rate-limited it for about 30 minutes in a 
row, the
 "Rate limits left" line of its run log approaching zero, or other workflows of 
the repository
 failing on secondary rate limits. The aggregate `Fork CI / Tests (AMD)`
 status and `CI passed` are posted either way, so turning job statuses off 
loses detail, not gating.
-Lowering `MAX_JOB_STATUSES_PER_RUN` in `fork_ci_reconcile.py` is the softer 
alternative.
+Lowering `JOB_STATUSES_PER_HOUR` in `fork_ci_reconcile.py` is the softer 
alternative.
diff --git a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md 
b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
index cfee5eae88a..ed71a62c95b 100644
--- a/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
+++ b/dev/breeze/doc/ci/08_fork_ci_for_external_contributors.md
@@ -915,19 +915,16 @@ suites); its jobs come from `GET 
/repos/<fork>/actions/runs/<id>/jobs?filter=lat
   (`GET /repos/apache/airflow/commits/<sha>/status`), and only differences are 
posted, failures
   first. GraphQL has no mutation for commit statuses (`createCheckRun` exists, 
but see above for
   why check runs do not fit), so each one is a REST call, and they count 
towards GitHub's limits on
-  content-creating requests: a reconciler run posts at most 
`MAX_JOB_STATUSES_PER_RUN` (13) of
-  them, each write charged as it is made. What does not fit is posted by the 
next run, and once
-  the budget is spent no further pull request's jobs or statuses are read in 
that run. One fork run
-  of the full matrix (about 130 jobs) needs more than a run's budget on its 
own, which the next
-  run catches up on, so deferring alone is not a problem: the run log lists 
the deferred pull
-  requests, and only when nine runs in a row deferred, about 45 minutes of the 
budget not
-  keeping up, is a note added to the Slack message. The count is kept in the 
record the monitor
-  carries between runs (`--reported-file`); without a readable record the note 
is added at once.
-  Its wording is fixed, so it is posted once while deferrals persist. A pull 
request whose fork run
-  finished more than an hour ago (the `updatedAt` of its completed check 
suite) and still has
-  statuses waiting is listed on its own as an attention item, so a delay that 
a busy backlog keeps
-  pushing back shows which pull request it holds up; the item names the run, 
so it is posted once
-  for that run.
+  content-creating requests: a reconciler run posts at most its share of 
`JOB_STATUSES_PER_HOUR`
+  (240) for the time since the previous run, each write charged as it is made 
(see "Write
+  budget"). What does not fit is posted by a later run, and once the budget is 
spent no further
+  pull request's jobs or statuses are read in that run. One fork run of the 
full matrix (about
+  130 jobs) needs several runs' budget on its own, and a few of them at once 
keep the monitor
+  deferring for hours while every run makes progress, so deferring alone is 
not reported: the run
+  log lists the deferred pull requests. A pull request whose fork run finished 
more than an hour
+  ago (the `updatedAt` of its completed check suite) and still has statuses 
waiting is listed on
+  its own as an attention item, so a delay that a busy backlog keeps pushing 
back shows which pull
+  request it holds up; the item names the run, so it is posted once for that 
run.
 - Commit statuses cannot be deleted and have no neutral state. When a run has 
completed, a job
   context posted for an earlier run of the same commit that the completed run 
does not have, or
   skipped, is overwritten with `success` and the description "Not in the 
latest fork run - see
@@ -941,7 +938,7 @@ suites); its jobs come from `GET 
/repos/<fork>/actions/runs/<id>/jobs?filter=lat
   run's jobs fails now and then on GitHub server errors (HTTP 502, a reset 
HTTP/2 stream) that the
   next run gets past, so each failure is only logged, and the pull request is 
listed with the
   unknown items for Slack once its job statuses failed in nine runs in a row, 
about 45 minutes. The per pull request
-  count is kept in the same record as that of deferred job statuses; without a 
readable record it
+  count is kept in the record the monitor carries between runs 
(`--reported-file`); without a readable record it
   starts again. The item's wording is fixed whatever the error, so it is 
posted once while the
   failure persists, and the run log has each run's error.
 
@@ -956,7 +953,7 @@ records this for agents.
 Each run logs what is left of the token's REST and GraphQL quotas (`GET 
/rate_limit`, which does
 not count against them) and how many of its calls GitHub rate-limited; the 
client retries those with
 backoff. Rate limiting in six runs in a row, about 30 minutes, adds a note to 
the Slack message, with fixed wording so
-it is posted once while it persists; the count is kept in the same record as 
that of deferred job
+it is posted once while it persists; the count is kept in the same record as 
that of failing job
 statuses.
 
 #### Write budget
@@ -984,14 +981,23 @@ requests a minute and 500 an hour. Assuming about 35 
reported jobs for an averag
   monitor runs.
 - **Gate enabled**, assuming about half of the pull request runs come from 
gated pull requests
   (about 5-6 pushes an hour): about 200 writes an hour without a cap, 40% of 
the content-creating
-  limit and more at peaks. The cap of 13 writes a run, twelve runs an hour, 
holds job statuses to at
-  most 156 an hour: 31% of the content-creating limit, leaving the rest to the 
reconciler's other
-  writes (labels, comments, the aggregate statuses) and to the other workflows 
that share it. Each
-  run posts at most 13 statuses at about one a second, well under the 
80-a-minute limit. Job
-  statuses then lag behind at busy times, the aggregate status does not. The 
`CI run started`
-  statuses of pull requests whose CI runs in `apache/airflow` come out of the 
same 13 writes a run,
-  first and at most 6 of them (see "The `CI run started` check"), so they do 
not raise the total;
-  at about 11 pull request runs an hour they take about 1 write a run.
+  limit and more at peaks. `JOB_STATUSES_PER_HOUR` holds job statuses to at 
most 240 an hour: 48%
+  of the content-creating limit, leaving the rest to the reconciler's other 
writes (labels,
+  comments, the aggregate statuses) and to the other workflows that share it. 
Job statuses then
+  lag behind at busy times, the aggregate status does not. The `CI run 
started` statuses of pull
+  requests whose CI runs in `apache/airflow` come out of the same budget, 
first and at most 6 a
+  run (see "The `CI run started` check"), so they do not raise the total; at 
about 11 pull
+  request runs an hour they take about 1 write a run.
+
+A run gets the hourly budget for the time since the previous run, which it 
reads from the record
+the monitor carries between runs (`run_at` in `--reported-file`), not one 
twelfth of it. GitHub
+starts scheduled workflows later than their cron: on 2026-11-15 the 5-minute 
monitor ran every 9
+to 10 minutes, so a fixed 13 writes a run (160 an hour at the cron's cadence) 
posted only about 82
+an hour, and four full-matrix fork runs at once took about six hours to catch 
up. Runs queued
+behind a slow one get little, since the time between them is short. At most
+`MAX_JOB_STATUS_BUDGET_INTERVAL` (10 minutes) is counted, so a run after a 
gap, such as a
+disabled workflow or a lost record, posts at most 40 statuses at about one a 
second: half of the
+80-a-minute limit. Without a readable record a run gets one cron interval's 
share, 20 writes.
 
 The per-run caps and the run counts before a Slack note are derived from 
`MONITOR_INTERVAL` in
 `fork_ci_reconcile.py`, which must match the monitor's cron (a test checks 
it), so changing the
@@ -1232,7 +1238,7 @@ files, a jobs call and a compare call only per long 
`ci-amd.yml` run of a gated
 one changed-files call and one or two compare calls per head commit that turns 
green, plus one
 mutation per actual state change. Job statuses add a jobs call and a status 
call per pull request with a fork run in
 progress or a completed run not yet reported, and up to two status mutations 
per job, at most
-`MAX_JOB_STATUSES_PER_RUN` per run; see "Write budget". Pull requests that 
pass 1 labelled and
+the run's share of `JOB_STATUSES_PER_HOUR`; see "Write budget". Pull requests 
that pass 1 labelled and
 the search does not list yet add one GraphQL call per 25. Well inside
 the `GITHUB_TOKEN` limits at a 5-minute cadence. The reconciler logs its call 
counts so the
 budget stays observable.
diff --git a/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py 
b/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py
index 61a30366219..bd25b5469ec 100644
--- a/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py
+++ b/dev/breeze/src/airflow_breeze/utils/fork_ci_reconcile.py
@@ -199,9 +199,24 @@ UNREPORTED_JOB_NAMES = GATED_RUN_JOB_NAMES | 
{GATE_JOB_NAME, "Platform: AMD"}
 RUNNING_JOB_STATUSES = frozenset({"queued", "waiting", "pending", "requested", 
"in_progress"})
 JOB_STATE_RANKS = {"failure": 0, "pending": 1, "success": 2}
 # Commit statuses count towards GitHub's limit of 500 content-creating 
requests per hour, shared by
-# every workflow of the repository. This keeps job statuses to about a third 
of it; the rest follow
-# on the next run, failures first. See "Job statuses" in the design doc.
-MAX_JOB_STATUSES_PER_RUN = 160 // RUNS_PER_HOUR
+# every workflow of the repository. This keeps job statuses under half of it; 
the rest follow on a
+# later run, failures first. See "Write budget" in the design doc.
+JOB_STATUSES_PER_HOUR = 240
+# GitHub starts scheduled runs later than their cron, the monitor often only 
every 9-10 minutes, so a
+# run gets the hourly budget for the time since the previous run rather than 
for MONITOR_INTERVAL. At
+# most this long is counted, so a run after a gap (a disabled workflow, a lost 
record) stays at half
+# of GitHub's limit of 80 content-creating requests a minute.
+MAX_JOB_STATUS_BUDGET_INTERVAL = timedelta(minutes=10)
+
+
+def compute_job_status_budget(since_previous_run: timedelta | None) -> int:
+    """The job status writes a run may make; without a previous run, those of 
one ``MONITOR_INTERVAL``."""
+    if since_previous_run is None:
+        since_previous_run = MONITOR_INTERVAL
+    counted = min(max(since_previous_run, timedelta(0)), 
MAX_JOB_STATUS_BUDGET_INTERVAL)
+    return JOB_STATUSES_PER_HOUR * counted // timedelta(hours=1)
+
+
 # `CI run started` statuses of pull requests whose CI runs in apache/airflow 
come out of that budget
 # first: one write per new head commit, which merging waits for, while job 
statuses are informational.
 # At most this many, so job statuses keep the rest; the first runs after the 
check became required
@@ -212,10 +227,6 @@ MAX_CI_RUN_STARTED_PER_RUN = 80 // RUNS_PER_HOUR
 MAX_PROJECT_CI_PASSED_PER_RUN = max(20 // RUNS_PER_HOUR, 1)
 # The GitHub Actions app, whose check suites are the workflow runs of a commit.
 GITHUB_ACTIONS_APP_ID = 15368
-# A single large fork run can need more writes than one run's budget, which 
the next run catches up
-# on, so deferring is only reported when it lasts this many consecutive runs: 
then the budget does
-# not keep up with the pull requests.
-DEFERRED_RUNS_BEFORE_ALERT = count_runs_in(timedelta(minutes=45))
 # GitHub answering with rate limits in this many consecutive runs is reported: 
a single burst is
 # retried, while a persisting one means the repository's shared limits are 
close to exhausted.
 RATE_LIMITED_RUNS_BEFORE_ALERT = count_runs_in(timedelta(minutes=30))
@@ -230,20 +241,14 @@ JOB_STATUSES_FAILED_REASON = (
     "the run log has the errors"
 )
 # A pull request whose fork run finished this long ago with job statuses still 
not all posted is
-# reported by itself: deferring is expected to catch up within a few runs, not 
to leave one behind.
+# reported. Deferring alone is not: several full-matrix fork runs at once 
(about 130 statuses each)
+# keep later runs deferring for hours while every one of them makes progress.
 MIRROR_DELAY_BEFORE_ALERT = timedelta(hours=1)
 RATE_LIMITED_NOTE = (
     f"GitHub rate-limited the fork CI monitor in 
{RATE_LIMITED_RUNS_BEFORE_ALERT} runs in a row; the "
     "repository's shared GITHUB_TOKEN limits are close to exhausted. Consider 
`job_statuses = false` in "
     ".github/fork-ci-gate.toml (see dev/AGENTS.md)"
 )
-# Fixed text, so the note is posted to Slack once while deferrals persist; the 
run log lists the pull
-# requests.
-JOB_STATUSES_DEFERRED_NOTE = (
-    f"job statuses have not fit in the budget of {MAX_JOB_STATUSES_PER_RUN} 
writes per run for "
-    f"{DEFERRED_RUNS_BEFORE_ALERT} runs in a row and are posted by later runs; 
if this persists, set "
-    "`job_statuses = false` in .github/fork-ci-gate.toml (see dev/AGENTS.md)"
-)
 
 RUNNING_STATUSES = frozenset({"QUEUED", "IN_PROGRESS", "PENDING", "WAITING", 
"REQUESTED"})
 FAILED_CONCLUSIONS = frozenset({"FAILURE", "CANCELLED", "TIMED_OUT", 
"STARTUP_FAILURE", "ACTION_REQUIRED"})
@@ -1928,7 +1933,7 @@ class JobStatusReporter:
         client: GitHubClient,
         *,
         repo: str,
-        budget: int = MAX_JOB_STATUSES_PER_RUN,
+        budget: int = compute_job_status_budget(None),
         now: datetime | None = None,
         read_jobs: ReadJobs | None = None,
     ):
@@ -2721,7 +2726,7 @@ def drop_reported(
     attention: list[tuple[int, str]],
     notes: list[str],
     record: bool = True,
-    deferred_runs: int = 0,
+    run_at: datetime | None = None,
     rate_limited_runs: int = 0,
     job_status_failure_runs: dict[int, int] | None = None,
 ) -> tuple[list[tuple[int, str]], list[tuple[int, str]], list[str]]:
@@ -2731,7 +2736,7 @@ def drop_reported(
     Only the previous run's items are kept, so an item that went away and 
comes back is posted
     again. A missing or unreadable file reports everything.
 
-    :param deferred_runs: the consecutive runs that deferred job statuses, 
recorded for the next run.
+    :param run_at: when this run started, recorded for the next run's job 
status budget.
     :param rate_limited_runs: the consecutive runs GitHub rate-limited, 
recorded for the next run.
     :param job_status_failure_runs: per pull request, the consecutive runs 
whose job statuses failed,
         recorded for the next run.
@@ -2740,7 +2745,7 @@ def drop_reported(
         "unknown": [list(item) for item in unknown],
         "attention": [list(item) for item in attention],
         "notes": notes,
-        "deferred_runs": deferred_runs,
+        "run_at": run_at.isoformat() if run_at is not None else None,
         "rate_limited_runs": rate_limited_runs,
         "job_status_failure_runs": {
             str(number): runs for number, runs in (job_status_failure_runs or 
{}).items()
@@ -2777,6 +2782,24 @@ def read_run_count(reported_file: Path | None, key: str) 
-> int | None:
     return count if isinstance(count, int) and not isinstance(count, bool) 
else 0
 
 
+def read_previous_run_at(reported_file: Path | None) -> datetime | None:
+    """When the previous run started, as it recorded; None without a readable 
record of it."""
+    if reported_file is None:
+        return None
+    try:
+        previous = json.loads(reported_file.read_text())
+    except (OSError, ValueError):
+        return None
+    run_at = previous.get("run_at") if isinstance(previous, dict) else None
+    if not isinstance(run_at, str):
+        return None
+    try:
+        parsed = datetime.fromisoformat(run_at)
+    except ValueError:
+        return None
+    return parsed if parsed.tzinfo is not None else None
+
+
 def read_job_status_failure_runs(reported_file: Path | None) -> dict[int, int]:
     """Per pull request, the consecutive runs whose job statuses failed, as 
the previous run recorded."""
     if reported_file is None:
@@ -2905,10 +2928,14 @@ def run_reconcile(
             f"listing the pull requests with `{LABEL_USE_PROJECT_CI}` failed: 
{type(e).__name__}"
         )
     read_jobs: ReadJobs = {}
+    previous_run_at = read_previous_run_at(reported_file)
+    job_status_budget = compute_job_status_budget(
+        now - previous_run_at if previous_run_at is not None else None
+    )
     job_reporter = JobStatusReporter(
         client,
         repo=repo,
-        budget=max(MAX_JOB_STATUSES_PER_RUN - ci_run_started_writes - 
ci_passed_reserved, 0),
+        budget=max(job_status_budget - ci_run_started_writes - 
ci_passed_reserved, 0),
         now=now,
         read_jobs=read_jobs,
     )
@@ -3011,17 +3038,11 @@ def run_reconcile(
     attention.extend(job_reporter.delayed)
     notes = [bypass_finder.incomplete] if bypass_finder.incomplete else []
     notes.extend(note for note in (ci_run_started_note, *ci_passed_notes) if 
note)
-    previous_deferred_runs = read_run_count(reported_file, "deferred_runs")
-    deferred_runs = (previous_deferred_runs or 0) + 1 if job_reporter.deferred 
else 0
     if job_reporter.deferred:
         console_print(
-            f"[info]Job statuses of {len(job_reporter.deferred)} pull requests 
left for the next run "
-            f"({deferred_runs} runs in a row): {job_reporter.deferred}[/]"
+            f"[info]Job statuses of {len(job_reporter.deferred)} pull requests 
left for the next run, "
+            f"after a budget of {job_status_budget} writes: 
{job_reporter.deferred}[/]"
         )
-        # Without a record of earlier runs, deferring is reported at once, as 
it cannot be told apart
-        # from a budget that does not keep up.
-        if previous_deferred_runs is None or deferred_runs >= 
DEFERRED_RUNS_BEFORE_ALERT:
-            notes.append(JOB_STATUSES_DEFERRED_NOTE)
     # The full list for the run log; Slack may show only part of a very long 
one.
     for number, reason in [*unknown, *attention]:
         console_print(f"[warning]#{number}: {reason}[/]")
@@ -3042,7 +3063,7 @@ def run_reconcile(
             attention=attention,
             notes=notes,
             record=not client.dry_run,
-            deferred_runs=deferred_runs,
+            run_at=now,
             rate_limited_runs=rate_limited_runs,
             job_status_failure_runs=job_status_failure_runs,
         )
diff --git a/dev/breeze/tests/test_fork_ci_reconcile.py 
b/dev/breeze/tests/test_fork_ci_reconcile.py
index 4e2ddd2bf90..79f177df73c 100644
--- a/dev/breeze/tests/test_fork_ci_reconcile.py
+++ b/dev/breeze/tests/test_fork_ci_reconcile.py
@@ -47,13 +47,11 @@ from airflow_breeze.utils.fork_ci_messages import (
     render_marker,
 )
 from airflow_breeze.utils.fork_ci_reconcile import (
-    DEFERRED_RUNS_BEFORE_ALERT,
     GATE_PROTECTED_PATHS,
     JOB_STATUS_FAILURE_RUNS_BEFORE_ALERT,
-    JOB_STATUSES_DEFERRED_NOTE,
     JOB_STATUSES_FAILED_REASON,
     MAX_CI_RUN_STARTED_PER_RUN,
-    MAX_JOB_STATUSES_PER_RUN,
+    MAX_JOB_STATUS_BUDGET_INTERVAL,
     MAX_PROJECT_CI_PASSED_PER_RUN,
     MONITOR_INTERVAL,
     RATE_LIMITED_NOTE,
@@ -77,6 +75,7 @@ from airflow_breeze.utils.fork_ci_reconcile import (
     check_head_repo_unavailable,
     classify,
     compare_with_base,
+    compute_job_status_budget,
     compute_projection,
     discover,
     drop_reported,
@@ -103,6 +102,7 @@ from airflow_breeze.utils.fork_ci_reconcile import (
     read_failed_jobs,
     read_head_status,
     read_job_status_failure_runs,
+    read_previous_run_at,
     read_project_ci_passed_url,
     read_project_ci_run_url,
     report_project_ci_passed,
@@ -2790,7 +2790,7 @@ def 
test_drop_reported_reports_everything_without_a_readable_previous_run(tmp_pa
         "unknown": [[1, "a"]],
         "attention": [],
         "notes": [],
-        "deferred_runs": 0,
+        "run_at": None,
         "rate_limited_runs": 0,
         "job_status_failure_runs": {},
     }
@@ -3316,42 +3316,84 @@ def 
test_job_status_reporter_does_not_read_without_writes_left(client):
     assert reporter.deferred == [42]
 
 
[email protected](("deferred", "has_alerts"), [([1], True), ([], 
False)])
 @mock.patch("airflow_breeze.utils.fork_ci_reconcile.JobStatusReporter", 
autospec=True)
 @mock.patch("airflow_breeze.utils.fork_ci_reconcile.classify", autospec=True)
-def test_run_reconcile_reports_deferred_job_statuses_to_slack(
-    mock_classify, mock_reporter, client, tmp_path, deferred, has_alerts
+def test_run_reconcile_does_not_alert_on_deferred_job_statuses(
+    mock_classify, mock_reporter, client, tmp_path
 ):
     mock_classify.return_value = Plan(state=ForkState.RUNNING)
     mock_reporter.return_value.report.return_value = None
-    mock_reporter.return_value.deferred = deferred
+    mock_reporter.return_value.deferred = [1]
     mock_reporter.return_value.delayed = []
 
-    assert _reconcile_with_reporter(client, tmp_path) is has_alerts
+    assert _reconcile_with_reporter(client, tmp_path) is False
 
-    if has_alerts:
-        assert JOB_STATUSES_DEFERRED_NOTE in (tmp_path / "s.json").read_text()
+
[email protected](
+    ("since_previous_run", "budget"),
+    [
+        pytest.param(None, 20, id="no-previous-run"),
+        pytest.param(timedelta(minutes=5), 20, id="on-the-cron"),
+        pytest.param(timedelta(minutes=9, seconds=30), 38, id="started-late"),
+        pytest.param(timedelta(minutes=1), 4, 
id="queued-behind-the-previous-run"),
+        pytest.param(timedelta(hours=3), 40, id="after-a-gap"),
+        pytest.param(timedelta(minutes=-1), 0, id="clock-skew"),
+    ],
+)
+def test_compute_job_status_budget(since_previous_run, budget):
+    assert compute_job_status_budget(since_previous_run) == budget
+
+
+def test_job_status_budget_after_a_gap_is_capped():
+    assert compute_job_status_budget(MAX_JOB_STATUS_BUDGET_INTERVAL) == 
compute_job_status_budget(
+        MAX_JOB_STATUS_BUDGET_INTERVAL * 2
+    )
+
+
[email protected](
+    ("content", "expected"),
+    [
+        pytest.param(None, None, id="missing"),
+        pytest.param("not json", None, id="not-json"),
+        pytest.param("[]", None, id="not-a-dict"),
+        pytest.param("{}", None, id="recorded-before-the-time"),
+        pytest.param('{"run_at": 5}', None, id="not-a-string"),
+        pytest.param('{"run_at": "yesterday"}', None, id="not-a-time"),
+        pytest.param('{"run_at": "2026-11-10T11:51:00"}', None, 
id="without-a-timezone"),
+        pytest.param(
+            '{"run_at": "2026-11-10T11:51:00+00:00"}',
+            datetime(2026, 11, 10, 11, 51, tzinfo=UTC),
+            id="recorded",
+        ),
+    ],
+)
+def test_read_previous_run_at(tmp_path, content, expected):
+    reported_file = tmp_path / "reported.json"
+    if content is not None:
+        reported_file.write_text(content)
+
+    assert read_previous_run_at(reported_file) == expected
 
 
 @mock.patch("airflow_breeze.utils.fork_ci_reconcile.JobStatusReporter", 
autospec=True)
[email protected]("airflow_breeze.utils.fork_ci_reconcile.report_project_ci_runs_started",
 autospec=True)
 @mock.patch("airflow_breeze.utils.fork_ci_reconcile.classify", autospec=True)
-def 
test_run_reconcile_reports_deferred_job_statuses_only_when_deferring_persists(
-    mock_classify, mock_reporter, client, tmp_path
+def 
test_run_reconcile_budgets_job_statuses_for_the_time_since_the_previous_run(
+    mock_classify, mock_report, mock_reporter, client, tmp_path
 ):
     mock_classify.return_value = Plan(state=ForkState.RUNNING)
+    mock_report.return_value = []
     mock_reporter.return_value.report.return_value = None
+    mock_reporter.return_value.deferred = []
+    mock_reporter.return_value.delayed = []
     client.dry_run = False
     reported_file = tmp_path / "reported.json"
-    reported_file.write_text("{}")
-    alerts = []
-    for deferred in ([1], [1], [], *[[1]] * DEFERRED_RUNS_BEFORE_ALERT):
-        mock_reporter.return_value.deferred = deferred
-        mock_reporter.return_value.delayed = []
-        alerts.append(_reconcile_with_reporter(client, tmp_path, 
reported_file=reported_file))
+    reported_file.write_text(json.dumps({"run_at": (NOW - 
timedelta(minutes=9)).isoformat()}))
+
+    _reconcile_with_reporter(client, tmp_path, reported_file=reported_file)
 
-    assert alerts == [*[False] * (DEFERRED_RUNS_BEFORE_ALERT + 2), True]
-    assert JOB_STATUSES_DEFERRED_NOTE in (tmp_path / "s.json").read_text()
-    assert json.loads(reported_file.read_text())["deferred_runs"] == 
DEFERRED_RUNS_BEFORE_ALERT
+    assert mock_reporter.call_args.kwargs["budget"] == 
compute_job_status_budget(timedelta(minutes=9))
+    assert read_previous_run_at(reported_file) == NOW
 
 
 def test_build_job_statuses_treats_contexts_case_insensitively():
@@ -3998,8 +4040,8 @@ def 
test_run_reconcile_retries_a_pull_request_whose_head_moved(client, tmp_path)
 @pytest.mark.parametrize(
     ("fails", "job_status_budget"),
     [
-        pytest.param(False, MAX_JOB_STATUSES_PER_RUN - 2, id="reported"),
-        pytest.param(True, MAX_JOB_STATUSES_PER_RUN - 
MAX_CI_RUN_STARTED_PER_RUN, id="fails"),
+        pytest.param(False, compute_job_status_budget(None) - 2, 
id="reported"),
+        pytest.param(True, compute_job_status_budget(None) - 
MAX_CI_RUN_STARTED_PER_RUN, id="fails"),
     ],
 )
 @mock.patch("airflow_breeze.utils.fork_ci_reconcile.JobStatusReporter", 
autospec=True)

Reply via email to