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
29 changes: 22 additions & 7 deletions .github/scripts/work-unblock/sync-status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// issue's labels fresh (several label events can land together) and its
// field, and write the state the labels collapse to unless the field
// already holds it.
// - `schedule` (work-unblock's hourly run): the same for every open issue, one
// - `schedule`: the same for every open issue updated in the last two hours
// (the hourly entries), or every open issue (the daily entry), one
// at a time, a short pause apart, within CALL_BUDGET calls, ending on
// `swept N: wrote W, unchanged U, failed F`.
//
Expand Down Expand Up @@ -87,9 +88,10 @@ export async function onLabelEvent({ event, github, repository, config, trustedA
return mirrorIssue({ github, repository, number: Number(event.issue.number), config, trustedActors, status, log });
}

// Every open issue, oldest first. `budget` is the most calls the sweep makes;
// past it the sweep stops and says so.
export async function sweep({ github, repository, config, trustedActors, status, budget = 800, pause = () => delay(250), log = console.log }) {
// Every open issue, oldest first, or with `since` (an ISO time) only those
// updated since then: a label change updates an issue. `budget` is the most
// calls the sweep makes; past it the sweep stops and says so.
export async function sweep({ github, repository, config, trustedActors, status, since, budget = 800, pause = () => delay(250), log = console.log }) {
let used = 0;
const call = (...args) => {
if (used >= budget) {
Expand All @@ -102,7 +104,8 @@ export async function sweep({ github, repository, config, trustedActors, status,
let stopped = "";
const issues = [];
for (let page = 1; page <= MAX_ISSUE_PAGES; page += 1) {
const batch = await call("GET", `/repos/${repository}/issues?state=open&direction=asc&per_page=100&page=${page}`);
const window = since ? `&since=${encodeURIComponent(since)}` : "";
const batch = await call("GET", `/repos/${repository}/issues?state=open&direction=asc&per_page=100&page=${page}${window}`);
issues.push(...batch.filter((item) => !item.pull_request));
if (batch.length < 100) {
break;
Expand Down Expand Up @@ -134,7 +137,19 @@ export async function sweep({ github, repository, config, trustedActors, status,
return { ...counts, stopped, used };
}

export async function main({ env = process.env, log = console.log } = {}) {
// Label events mirror each change as it lands, so an hourly run only catches
// what they missed: issues updated in the last two hours, a window that
// covers a late scheduled run. The run started by work-unblock.yml's daily
// FULL_SWEEP_CRON entry reads every open issue to catch any other drift; the
// trigger decides, not the clock, so a late start still sweeps in full.
// Returns the `since` time, or undefined for the full sweep.
export const FULL_SWEEP_CRON = "47 4 * * *";
export const SWEEP_WINDOW_MS = 2 * 60 * 60 * 1000;
export function sweepSince(at, schedule) {
return schedule === FULL_SWEEP_CRON ? undefined : new Date(at.getTime() - SWEEP_WINDOW_MS).toISOString();
}

export async function main({ env = process.env, log = console.log, now = () => new Date() } = {}) {
try {
if (env.KILL_SWITCH !== "proceed") {
log("sync-status: the kill switch stops this run; nothing read or written");
Expand All @@ -159,7 +174,7 @@ export async function main({ env = process.env, log = console.log } = {}) {
const args = { github, repository, config, trustedActors, status, log };
if (env.GITHUB_EVENT_NAME === "schedule") {
const budget = /^[1-9][0-9]{0,4}$/.test(env.CALL_BUDGET ?? "") ? Number(env.CALL_BUDGET) : undefined;
const result = await sweep({ ...args, budget });
const result = await sweep({ ...args, budget, since: sweepSince(now(), env.SCHEDULE) });
return result.failed > 0 || result.stopped ? 1 : 0;
}
if (env.GITHUB_EVENT_NAME !== "issues") {
Expand Down
23 changes: 22 additions & 1 deletion .github/scripts/work-unblock/sync-status.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
// already holds the state, set by a trusted account, is not written again;
// the hourly sweep counts what it wrote, left and failed.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "node:test";

import { isStatusLabel, mirrorIssue, onLabelEvent, sweep } from "./sync-status.mjs";
import { FULL_SWEEP_CRON, isStatusLabel, mirrorIssue, onLabelEvent, sweep, sweepSince } from "./sync-status.mjs";

const REPO = "o/r";
const FIELD = 7;
Expand Down Expand Up @@ -163,3 +164,23 @@ test("a failed field write counts as failed, and the sweep stops at its call bud
assert.equal(result.swept, 1);
assert.match(lines.at(-1), /^swept 1: wrote 1, unchanged 0, failed 0; stopped at #2: call budget of 4 spent/);
});

test("an hourly entry sweeps issues updated in the last two hours; the daily entry sweeps every open issue, even when it starts late", () => {
assert.equal(sweepSince(new Date("2026-11-05T11:47:00Z"), "47 0-3,5-23 * * *"), "2026-11-05T09:47:00.000Z");
assert.equal(sweepSince(new Date("2026-11-05T05:20:00Z"), FULL_SWEEP_CRON), undefined);
});

test("work-unblock.yml schedules the daily full-sweep entry and passes the firing entry to the script", () => {
const workflow = readFileSync(new URL("../../workflows/work-unblock.yml", import.meta.url), "utf8");
assert.ok(workflow.includes(`- cron: "${FULL_SWEEP_CRON}"`), "the daily cron entry");
assert.match(workflow, /SCHEDULE: \$\{\{ github\.event\.schedule \}\}/);
});

test("a windowed sweep lists only issues updated since its start", async () => {
const { github, calls } = fakeGitHub({ 1: { labels: ["agent-ready"], field: 101, setter: TRUSTED_ID } });
await sweep({ ...args(github, []), since: "2026-11-05T09:47:00.000Z", pause: async () => {} });
assert.ok(calls.some((call) => call.includes("&since=2026-11-05T09%3A47%3A00.000Z")), calls.join("\n"));
const full = fakeGitHub({ 1: { labels: ["agent-ready"], field: 101, setter: TRUSTED_ID } });
await sweep({ ...args(full.github, []), pause: async () => {} });
assert.ok(full.calls.every((call) => !call.includes("since=")), full.calls.join("\n"));
});
13 changes: 9 additions & 4 deletions .github/workflows/work-unblock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ name: work-unblock
# until it is marked `status: ready` again.
#
# Its sync-status job writes an issue's Work status field after a listed human
# adds or removes a status label, and the hourly run does so for every open
# issue whose field the labels disagree with
# adds or removes a status label; the hourly run does so for every open issue
# updated in the last two hours, and the daily 04:47 UTC run for every open issue
# (.github/scripts/work-unblock/sync-status.mjs).

on:
Expand All @@ -96,7 +96,10 @@ on:
types: [closed, labeled, unlabeled]
# Off the top of the hour, at a minute no other schedule here uses.
schedule:
- cron: "47 * * * *"
- cron: "47 0-3,5-23 * * *"
# The daily full status sweep (sync-status). Its own entry, so a late start
# still runs the full sweep.
- cron: "47 4 * * *"
# A finished issue lane run may free a slot, so the sweep runs at once and
# admits the next deferred issue instead of waiting for the hour. It runs
# main's code only, for a same-repository run, and reads no artifact.
Expand Down Expand Up @@ -338,7 +341,8 @@ jobs:
tokens: lanes-app github-token

# Mirrors an issue's status labels into its Work status field after a person
# adds or removes one, and hourly for every open issue
# adds or removes one, hourly for issues updated in the last two hours, and
# daily (the 47 4 * * * entry) for every open issue
# (.github/scripts/work-unblock/sync-status.mjs). It writes with the lanes
# App token: the field's reader counts only a value a listed human or the App
# set. A label change by a bot (the lanes App, whose scripts mirror their own
Expand Down Expand Up @@ -468,6 +472,7 @@ jobs:
KILL_SWITCH: ${{ steps.kill.outputs.proceed == 'true' && 'proceed' || '' }}
TRUSTED_ACTORS_PATH: .base/.github/standards/trusted-actors/trusted-actors.json
CALL_BUDGET: "800"
SCHEDULE: ${{ github.event.schedule }}
run: node ".base/$SCRIPT"

# The token is cancelled as soon as the job is done; it would otherwise
Expand Down
Loading