The first question when a pull request turns red is whether a retry will help. If it fails again, the engineer needs to know where it failed, whether it has happened before and whether someone is tracking it.
In code mode, an agent can write TypeScript to look up recent runs, inspect jobs and search issues. Those calls and filters are the same on the next pull request. Save them as three tested actions. The agent can then choose which actions to run and use the results to decide what deserves a closer look.
Start with the decision an engineer has to make
Suppose the integration check on a pull request times out. A retry passes and you can move on. If it fails again in “Start test database”, seeing the same step in an earlier run gives you somewhere to look. A link to an open issue can save the maintainer from filing the same report twice.
Choose a tests-only workflow and replace sample/service in all three actions with its repository. Set the workflow filename in the first action. Validate each body with its input schema, save it as a standalone action, then attach and run its tests. Keep each action ID and version.
Handle a completed CI attempt
The completion event supplies the run ID, attempt number and update timestamp. The action checks GitHub’s current run against that timestamp and looks at the last ten runs of ci.yml on the branch. A first-attempt timeout inside that window gets one retry request.
The result carries the current run ID and timestamp. When an earlier run in the window also failed or timed out, previous contains its ID and timestamp too. The next action accepts exactly those two inputs.
retry-timeout.ts
// Read the details GitHub sent when this attempt finished.
const { runId, attempt, updatedAt } = defineParams(
z.object({
runId: z.number().int().positive(),
attempt: z.number().int().positive(),
updatedAt: z.string(),
}),
);
// Use your own repository owner and name here.
const repo = { owner: "sample", repo: "service" };
const target = { ...repo, runId };
const run = await github.getWorkflowRun(target);
const current = { runId, updatedAt: run.updatedAt, url: run.htmlUrl };
// Stop if GitHub has already started a newer attempt.
if (run.status !== "completed" || run.updatedAt !== updatedAt) {
return { ...current, outcome: "stale_event" };
}
if (!run.headBranch) return { ...current, outcome: "unknown_branch" };
// Check this attempt against the last ten runs on its branch.
const recent = await github.listWorkflowRuns({
...repo,
actionId: "ci.yml",
branch: run.headBranch,
maxResults: 10,
});
const observed = recent.find((item) => item.id === runId);
if (!observed) return { ...current, outcome: "outside_window" };
if (
observed.updatedAt !== updatedAt ||
observed.status !== "completed" ||
observed.conclusion !== run.conclusion
) {
return { ...current, outcome: "stale_event" };
}
// Keep the most recent earlier failure for the person investigating.
const failures = recent.filter(
(item) =>
item.conclusion === "failure" || item.conclusion === "timed_out",
);
const earlier = failures
.filter((item) => item.runNumber < run.runNumber)
.sort((left, right) => right.runNumber - left.runNumber)[0];
const previous = earlier
? { runId: earlier.id, updatedAt: earlier.updatedAt }
: null;
const report = { ...current, previous, recentFailures: failures.length };
// Only the first timeout gets an automatic retry.
if (attempt === 1 && run.conclusion === "timed_out") {
await github.rerunWorkflowFailedJobs(target);
return { ...report, outcome: "retry_requested" };
}
return { ...report, outcome: run.conclusion };Inspect tool types
github.listWorkflowRuns(args: {
owner: string;
repo: string;
actionId?: string | number;
branch?: string;
status?: string;
maxResults?: number;
}): Promise<Array<{ id: number; name: string | null; status: string | null; conclusion: string | null; headBranch: string | null; headSha: string; event: string; htmlUrl: string; runNumber: number; createdAt: string; updatedAt: string }>>github.getWorkflowRun(args: {
owner: string;
repo: string;
runId: number;
}): Promise<{ id: number; name: string | null; status: string | null; conclusion: string | null; headBranch: string | null; headSha: string; event: string; htmlUrl: string; runNumber: number; createdAt: string; updatedAt: string }>github.rerunWorkflowFailedJobs(args: {
owner: string;
repo: string;
runId: number;
}): Promise<{ rerunned: true }>Send each completion to the first saved version
Create a project webhook for the saved version of Handle a completed CI attempt. Put its endpoint and secret in the two GitHub Actions secrets below. Add the forwarding workflow on the repository’s default branch. CI is the watched workflow’s display name. The runner must be able to reach the endpoint over HTTPS.
Keep the receipt printed by the forwarder. A replay with the same delivery ID and payload returns the original OrgSDK run ID. Use that ID in signed-in Studio. In OrgSDK 2.7.1 an MCP token reads the runs it created, so give your agent the completed webhook result for the follow-up.
forward-ci.yml
# Tell OrgSDK when the workflow named CI finishes.
name: Send CI result to OrgSDK
on:
workflow_run:
workflows: [CI]
types: [completed]
permissions: {}
jobs:
forward:
runs-on: ubuntu-latest
steps:
- name: Send the completed attempt
env:
HOOK: ${{ secrets.ORGSDK_CI_URL }}
SECRET: ${{ secrets.ORGSDK_CI_SECRET }}
run: |
# Read the run details from the event GitHub supplied.
payload=$(jq -c '{
inputs: {
runId: .workflow_run.id,
attempt: .workflow_run.run_attempt,
updatedAt: .workflow_run.updated_at
}
}' "$GITHUB_EVENT_PATH")
# Reuse this ID if GitHub sends the same attempt again.
delivery=$(jq -r '"ci-\(.repository.id)-\(.workflow_run.id)-\(.workflow_run.run_attempt)"' "$GITHUB_EVENT_PATH")
# Send the event using the saved webhook address and secret.
curl --fail-with-body --silent --show-error \
-H 'Content-Type: application/json' \
-H "x-webhook-secret: $SECRET" \
-H "x-webhook-delivery: $delivery" \
--data-binary "$payload" "$HOOK"Read the failed steps in either run
For a failure or a second timeout, open the completed webhook run in signed-in Studio and give its result to your agent. Ask it to run this saved version for the current GitHub run and, when previous is present, for that earlier run. This is the same operation a weekly branch review can use on its own.
The action reads up to 100 jobs from the latest attempt and checks the run timestamp on both sides of the read. A changed run returns its link for review. A job with no failed step still contributes its job link.
read-failed-steps.ts
// Use the run ID and timestamp from the completed attempt.
const { runId, updatedAt } = defineParams(
z.object({
runId: z.number().int().positive(),
updatedAt: z.string(),
}),
);
// Set the same repository you used for the timeout check.
const target = { owner: "sample", repo: "service", runId };
const before = await github.getWorkflowRun(target);
if (before.updatedAt !== updatedAt || before.status !== "completed") {
return { outcome: "run_changed", url: before.htmlUrl };
}
// Read the jobs, then check again in case a retry started.
const jobs = await github.listWorkflowJobs({
...target,
filter: "latest",
maxResults: 100,
});
const after = await github.getWorkflowRun(target);
if (after.updatedAt !== updatedAt || after.status !== "completed") {
return { outcome: "run_changed", url: after.htmlUrl };
}
// Keep the job links and the names of failed or timed-out steps.
const failed = (conclusion: string | null) =>
conclusion === "failure" || conclusion === "timed_out";
return {
outcome: "inspected",
url: after.htmlUrl,
jobs: jobs
.filter((job) => failed(job.conclusion))
.map((job) => ({
jobName: job.name,
url: job.htmlUrl || after.htmlUrl,
steps: job.steps
.filter((step) => failed(step.conclusion))
.map((step) => step.name),
})),
};Inspect tool types
github.getWorkflowRun(args: {
owner: string;
repo: string;
runId: number;
}): Promise<{ id: number; name: string | null; status: string | null; conclusion: string | null; headBranch: string | null; headSha: string; event: string; htmlUrl: string; runNumber: number; createdAt: string; updatedAt: string }>github.listWorkflowJobs(args: {
owner: string;
repo: string;
runId: number;
filter?: string;
maxResults?: number;
}): Promise<Array<{ id: number; runId: number; name: string; status: string; conclusion: string | null; startedAt: string | null; completedAt: string | null; htmlUrl: string; runnerName: string | null; steps: Array<{ name: string; status: string; conclusion: string | null; number: number }> }>>Find a possible tracking issue
For each failed step worth investigating, pass its jobName and step name to this saved action. It checks up to 30 open issues with the ci label. Matching names identify a candidate for the maintainer to inspect. The job links supply the evidence.
If there is no named failed step, finish with the job link. An empty issue search still leaves the engineer with the failed step and any earlier occurrence.
find-ci-issues.ts
// Take the job and step names from the failed run.
const { jobName, stepName } = defineParams(
z.object({
jobName: z.string().trim().min(1),
stepName: z.string().trim().min(1),
}),
);
// Search recent open issues labelled ci in your repository.
const issues = await github.listIssues({
owner: "sample",
repo: "service",
state: "open",
labels: "ci",
maxResults: 30,
});
// Keep issues that mention both names so a person can review them.
const candidates = issues.filter((issue) => {
const text = [issue.title, issue.body ?? ""].join(" ").toLowerCase();
return (
text.includes(jobName.toLowerCase()) &&
text.includes(stepName.toLowerCase())
);
});
return {
checked: issues.length,
candidates: candidates.map((issue) => ({
number: issue.number,
title: issue.title,
url: issue.htmlUrl,
})),
};Inspect tool types
github.listIssues(args: {
owner: string;
repo: string;
state?: string;
labels?: string;
assignee?: string;
maxResults?: number;
}): Promise<Array<{ id: number; number: number; title: string; body: string | null; state: string; labels: string[]; assignees: string[]; user: string | null; htmlUrl: string; commentsCount: number; createdAt: string; updatedAt: string; closedAt: string | null }>>Run the saved versions and improve one at a time
The agent starts each follow-up with run_action using the project, saved action ID, version, inputs and an idempotency key. It polls its own OrgSDK run ID with get_run until completion, then passes the result into the next call. A successful CI completion ends the investigation.
If your team begins labelling flaky tests ci-flake, add an example to the issue lookup’s tests, save the revised lookup and pin its new version. The timeout rule and failed-step reader can keep using their tested versions.
A timeout, a failed retry and a tracking issue
Illustrative run history for one pull request.
| Time | GitHub | What the engineer gets |
|---|---|---|
| 09:12 | Run 8421, attempt 1 times out. | retry_requested. GitHub accepted the request to rerun failed jobs. |
| 09:18 | Attempt 2 reports failure. | The Studio result identifies current run 8421 and earlier failed run 8390. |
| 09:19 | The agent reads both runs. | Both point to the integration job and its Start test database step. The maintainer gets the two job links. |
| 09:20 | The agent checks open CI issues. | Issue #73 mentions both names. The maintainer can compare its symptoms with the logs. |
If attempt 2 succeeds, that completion confirms recovery. An ordinary failure on attempt 1 goes straight to investigation. Similar step names alone cannot establish the cause.
Test it before the next run
Use one tests-only repository, an earlier failing run and an open ci issue that names the failed job and step.
- Exercise a timeout whose retry fails at that step. Check the current and earlier job links, then open the candidate issue.
- Try a timeout followed by success, an ordinary first-attempt failure, and a branch with no earlier failure. Check which follow-up actions each case needs.
- Replay the same completion and confirm that the webhook returns the original OrgSDK run ID. If GitHub’s retry response is ambiguous, inspect the GitHub run before deciding whether to act again.
Check the result
- Did the retry run only for a first-attempt workflow timeout?
- Can the maintainer verify the failed step and decide whether the suggested issue describes the same problem?
Common questions
What counts as a timeout?
GitHub must mark the whole workflow run timed_out. An individual job can time out while the workflow reports failure. That result goes to investigation.
What if the event arrives late?
If the current run is still running or its update timestamp differs from the completed event, the first action returns stale_event and a run link. A missing branch or a run outside the ten-run window also goes to review.
Can this retry a deployment?
Choose a tests-only workflow for this rule. GitHub can rerun dependent jobs as well as failed jobs.
Sources and service documentation
Provider documentation supports the service behaviour described here. The proposed workflow and illustrative examples are OrgSDK designs.
- GitHub workflow_run events ↗
Completion events and default-branch requirements.
- GitHub workflow runs API ↗
Rerun permissions and which jobs GitHub repeats.
- GitHub workflow jobs API ↗
Job conclusions, steps and logs.
- GitHub repository issues API ↗
Open issue and label filters, including repository access.
- OrgSDK 2.7.1 saved action replay ↗
Saving, testing and running immutable versions.
- OrgSDK 2.7.1 run permissions ↗
Client-token access to run results.
