Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions workers/nos-runner-watchdog/src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ export async function listRuns(token, repo, status, fetchFn) {
}));
}

/** Failed runs created since sinceIso, newest first, with the run fields lostCandidate() reads. */
export async function listFailedRuns(token, repo, sinceIso, fetchFn) {
const since = sinceIso.replace(/\.\d{3}Z$/, "Z");
const path = `/repos/${repo}/actions/runs?status=failure&created=>=${since}&per_page=20&exclude_pull_requests=true`;
const out = await call(token, "GET", path, "runs failure", fetchFn);
return (out.workflow_runs ?? []).map((r) => ({
id: r.id,
// No default: a run without run_attempt is never re-run.
run_attempt: r.run_attempt,
conclusion: r.conclusion,
head_repository: { full_name: r.head_repository?.full_name ?? null },
repository: { full_name: r.repository?.full_name ?? null },
actor: { login: r.actor?.login ?? null },
}));
}

export async function getJobs(token, repo, runId, fetchFn) {
const out = await call(
token,
Expand Down
80 changes: 79 additions & 1 deletion workers/nos-runner-watchdog/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { decide } from "./decide.js";
import * as gh from "./github.js";
import { APPS, POOLS, poolForCron, appEnv } from "./pools.js";
import {
LOST_WINDOW_MS,
lostCandidate,
lostRunner,
pruneChecked,
} from "./lost.js";

// Free plan allows 50 subrequests a run; KV calls are counted too, for headroom.
export const CAP = 40;
Expand All @@ -17,6 +23,12 @@ const TOKEN_TTL = 3000;
const SKIP_ALERT = 3;
// Sends per alert. A tick sends at most two alerts, so retries fit in the CAP headroom.
const NTFY_TRIES = 3;
// Lost-runner phase, per probe: the jobs GET, the lostChecked put, the rerun POST, and the last lostChecked put.
const PROBE_COST = 4;
// Left free by the scan on a rerunLost pool: the lostChecked get, one failed-runs list, one probe.
const LOST_SHARE = 1 + 1 + PROBE_COST;
// lostChecked also holds the lost-phase repo cursor under this key; a "<repo>#<runId>" key can never equal it.
const CURSOR_KEY = "_cursor";

// Logs name the failed call and status only, never a URL, token or topic.
const describe = (e) =>
Expand Down Expand Up @@ -148,7 +160,10 @@ export async function runTick(env, { fetch: rawFetch, now, pool = POOLS.nos }) {
prev.lastVar === null ||
(prev.misses ?? 0) >= 1;
const scanReserve =
RESERVE_REQUIRED + (skips ? 2 : 0) + (sweepLikely ? OPTIONAL_SHARE : 0);
RESERVE_REQUIRED +
(skips ? 2 : 0) +
(sweepLikely ? OPTIONAL_SHARE : 0) +
(pool.rerunLost ? LOST_SHARE : 0);
let observed;
let scan;
let repoOldest;
Expand Down Expand Up @@ -320,6 +335,69 @@ export async function runTick(env, { fetch: rawFetch, now, pool = POOLS.nos }) {
for (const p of planned) {
await optional((t) => gh.cancelRun(t, p.repo, p.runId, fetchFn), 0);
}

// Lost runners, after the cancels so the sweep keeps its budget. NOS has no rerunLost, so its tick ends above.
if (!pool.rerunLost || scan.all.length === 0) return;
if (stopped || !budget.canAfford(LOST_SHARE)) return;
const loaded = parseOr(await kv.get("lostChecked"), {}, "lostChecked");
const { [CURSOR_KEY]: savedCursor, ...entries } = loaded ?? {};
const lostChecked = pruneChecked(entries, now);
let dirty = Object.keys(lostChecked).length !== Object.keys(entries).length;
const sinceIso = new Date(now - LOST_WINDOW_MS).toISOString();
// Runs the pending list re-runs (a down-sweep run can fail first) are left to it, so no run gets two re-runs.
const pendingKeys = new Set([...pending, ...toWrite].map(runKey));
// Own cursor: the next tick starts after the last repo listed. Mod n, so a changed repo count is safe.
const n = scan.all.length;
const prevCursor = Number.isInteger(savedCursor) ? savedCursor : 0;
const start = ((prevCursor % n) + n) % n;
let cursor = start;
const save = () =>
kv.put(
"lostChecked",
JSON.stringify({ ...lostChecked, [CURSOR_KEY]: cursor }),
);
let out = false;
for (let i = 0; i < n && !out; i += 1) {
const repo = scan.all[(start + i) % n];
const list = await optional(
(t) => gh.listFailedRuns(t, repo, sinceIso, fetchFn),
PROBE_COST,
);
if (list.skipped) break;
// Listed (or failed): the next tick starts after this repo, even if its probes are cut short.
cursor = (start + i + 1) % n;
if (!list.ok) continue;
for (const run of list.value) {
const k = `${repo}#${run.id}`;
if (k in lostChecked || pendingKeys.has(k) || !lostCandidate(run))
continue;
const jobs = await optional(
(t) => gh.getJobs(t, repo, run.id, fetchFn),
PROBE_COST - 1,
);
if (jobs.skipped) {
out = true;
break;
}
// A failed jobs GET is not marked, so a later tick probes the run again.
if (!jobs.ok) continue;
lostChecked[k] = now;
dirty = true;
if (!lostRunner(run, jobs.value, pool)) continue;
// Marked before the POST: a crash after it can never re-run the run twice.
await save();
dirty = false;
const rr = await optional(
(t) => gh.rerunFailed(t, repo, run.id, fetchFn),
RESERVE_FINAL,
);
// No alert (plan decision 9): re-runs after a restart are routine.
log(
rr.ok ? `lost runner re-run: ${k}` : `lost runner re-run failed: ${k}`,
);
}
}
if ((dirty || cursor !== prevCursor) && budget.canAfford(1)) await save();
}

// A run we cancelled is re-run; a failure only when the down sweep cancelled it (a hung job can fail first).
Expand Down
35 changes: 35 additions & 0 deletions workers/nos-runner-watchdog/src/lost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Lost runner: a restart stopped the job. It failed, no step did. A hypothesis until the restart drill.

// Failed runs are listed from this far back, and lostChecked entries are kept this long.
export const LOST_WINDOW_MS = 6 * 3600000;

/** Run-level checks only: a first attempt that failed, from this repo, not from Dependabot. Read from the runs list. */
export function lostCandidate(run) {
return (
run?.run_attempt === 1 &&
run.conclusion === "failure" &&
typeof run.repository?.full_name === "string" &&
run.head_repository?.full_name === run.repository.full_name &&
run.actor?.login !== "dependabot[bot]"
);
}

/** True when the run is a candidate and one of its pool-label jobs failed with no failed step. */
export function lostRunner(run, jobs, pool) {
if (!lostCandidate(run)) return false;
return (jobs ?? []).some(
(j) =>
(j.labels ?? []).includes(pool.label) &&
j.conclusion === "failure" &&
!(j.steps ?? []).some((s) => s.conclusion === "failure"),
);
}

/** lostChecked without entries older than the window. */
export function pruneChecked(checked, now) {
const out = {};
for (const [k, t] of Object.entries(checked ?? {})) {
if (typeof t === "number" && now - t <= LOST_WINDOW_MS) out[k] = t;
}
return out;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"source": "GET /repos/smartwatermelon/dotfiles/actions/runs/44443375980 and /jobs, captured 2026-11-23, redacted to the fields the predicate reads",
"note": "dotfiles#511 fork-PR drill, Bash Tests: the fork guard refused the job in Set up runner.",
"run": {
"actor": {
"login": "smartwatermelon-agent[bot]"
},
"conclusion": "failure",
"event": "pull_request",
"head_repository": {
"full_name": "nightowlstudiollc/dotfiles"
},
"id": 44443375980,
"repository": {
"full_name": "smartwatermelon/dotfiles"
},
"run_attempt": 1
},
"jobs": [
{
"conclusion": "failure",
"labels": ["mimolette-macos"],
"runner_name": "mimolette-macos-1795054747",
"status": "completed",
"steps": [
{
"conclusion": "success",
"name": "Set up job"
},
{
"conclusion": "failure",
"name": "Set up runner"
},
{
"conclusion": "skipped",
"name": "Checkout"
},
{
"conclusion": "skipped",
"name": "Install bash 5"
},
{
"conclusion": "skipped",
"name": "Install linters the lint tests exercise"
},
{
"conclusion": "skipped",
"name": "Clone github-workflows for the upstream config comparison"
},
{
"conclusion": "skipped",
"name": "Show bash version"
},
{
"conclusion": "skipped",
"name": "Run bash test suite"
},
{
"conclusion": "success",
"name": "Complete job"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"source": "HYPOTHESIS, not captured. Written 2026-11-23 from the plan (decision 8): no real runner-lost job exists yet. The batch's restart drill (step F) replaces this file with a captured one.",
"note": "Assumed shape: a same-repo push run whose mimolette-macos job stopped when the runner restarted. The job is failure, but no step is failure: the interrupted step and the later ones have no failure conclusion.",
"run": {
"actor": {
"login": "smartwatermelon"
},
"conclusion": "failure",
"event": "push",
"head_repository": {
"full_name": "smartwatermelon/dotfiles"
},
"id": 90000000001,
"repository": {
"full_name": "smartwatermelon/dotfiles"
},
"run_attempt": 1
},
"jobs": [
{
"conclusion": "failure",
"labels": ["mimolette-macos"],
"runner_name": "mimolette-macos-1795099999",
"status": "completed",
"steps": [
{
"conclusion": "success",
"name": "Set up job"
},
{
"conclusion": "success",
"name": "Set up runner"
},
{
"conclusion": "success",
"name": "Checkout"
},
{
"conclusion": null,
"name": "Run bash test suite"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"source": "GET /repos/smartwatermelon/mac-server-setup/actions/runs/44651519680 and /jobs, captured 2026-11-23, redacted to the fields the predicate reads",
"note": "A real test failure (Run BATS failed). No failed-step job on mimolette-macos exists yet; this one ran on macos-latest.",
"run": {
"actor": {
"login": "smartwatermelon-agent[bot]"
},
"conclusion": "failure",
"event": "pull_request",
"head_repository": {
"full_name": "smartwatermelon/mac-server-setup"
},
"id": 44651519680,
"repository": {
"full_name": "smartwatermelon/mac-server-setup"
},
"run_attempt": 1
},
"jobs": [
{
"conclusion": "failure",
"labels": ["macos-latest"],
"runner_name": "GitHub Actions 1000012195",
"status": "completed",
"steps": [
{
"conclusion": "success",
"name": "Set up job"
},
{
"conclusion": "success",
"name": "Run actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1"
},
{
"conclusion": "success",
"name": "Install test dependencies"
},
{
"conclusion": "success",
"name": "Show tool versions"
},
{
"conclusion": "failure",
"name": "Run BATS"
},
{
"conclusion": "success",
"name": "Post Run actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1"
},
{
"conclusion": "success",
"name": "Complete job"
}
]
}
]
}
Loading
Loading