Skip to content

optimize: stop paginating listWorkflowRuns once 24h cutoff is reached#38779

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/optimize-ai-credits-computation
Draft

optimize: stop paginating listWorkflowRuns once 24h cutoff is reached#38779
Copilot wants to merge 2 commits into
mainfrom
copilot/optimize-ai-credits-computation

Conversation

Copilot AI commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

The daily AIC guardrail was fetching up to 10 pages of workflow runs even when earlier pages already contained runs past the 24h window. Since listWorkflowRuns returns runs newest-first, encountering a stale run means all remaining runs on that page and every subsequent page are also outside the window.

Changes

  • Early pagination exit — introduce a reachedCutoff flag that breaks both the inner and outer loops on the first run older than the cutoff, cutting up to 9 unnecessary API calls per guardrail invocation:
    // Before: skipped the run but kept paginating
    if (!Number.isFinite(createdAtMs) || createdAtMs < cutoffMs) continue;
    
    // After: stops all further page fetches
    if (!Number.isFinite(createdAtMs) || createdAtMs < cutoffMs) {
      reachedCutoff = true;
      break;
    }
  • Remove dead guardcountedRuns.length >= maxInspectableRuns in the artifact-inspection loop was unreachable; candidateRuns is already capped to maxInspectableRuns by the time that loop runs.
  • Trim per-page logging — replace runIds: runs.map(...) (up to 100 IDs/page) with firstRunId/lastRunId.
  • New test — verifies that a page containing one recent run followed by one stale run causes exactly one listWorkflowRuns call and one getRunAIC call.

Copilot AI and others added 2 commits June 12, 2026 04:26
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
- Add early exit when a stale run is found (API returns newest-first),
  avoiding unnecessary page fetches and GitHub API calls
- Remove dead countedRuns >= maxInspectableRuns guard in artifact loop
- Replace verbose per-page runIds log (up to 100 IDs) with firstRunId/lastRunId
- Add test proving pagination stops after the first stale run

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants