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
21 changes: 19 additions & 2 deletions plugins/stewards/assessment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ export function assessWorker(workers: readonly Worker[]): AssessedWorker {
return { state, inFlight: anyWorkerInFlight(workers), running, parked, reason: runningWorkerReason(workers) };
}

/**
* The idle answer (BB-83), in one place: `assess` and the reconcile's idle
* wake both call it, so no surface calls `idleInActiveStage` directly.
*/
export function assessIdle(idleBinding: IdleBinding, observed: Observed, now: number = Date.now()): boolean {
return idleInActiveStage(idleBinding, observed, now);
}

/**
* The live-wake-source answer (BB-154), in one place: `assess` and the report
* path's `liveWakeSourceFor` both call it, so no surface calls
* `hasLiveWakeSource` directly.
*/
export function assessLiveWakeSource(sources: WakeSourceInput): boolean {
return hasLiveWakeSource(sources);
}

/** Derive the one assessment for a binding from the rows the reconcile already read. */
export function assess(input: AssessmentInput): Assessment {
const { binding, observed, ownPulls, decisions, projectApprovals, workers, escalations, needs, sources, playbook, currentSpecHash, now } = input;
Expand Down Expand Up @@ -203,7 +220,7 @@ export function assess(input: AssessmentInput): Assessment {
escalations,
needs,
};
const live = hasLiveWakeSource(sources);
const live = assessLiveWakeSource(sources);
return {
bindingId: binding.id,
entityKey: binding.entityKey,
Expand All @@ -217,7 +234,7 @@ export function assess(input: AssessmentInput): Assessment {
worker: assessWorker(workers),
liveness: {
live,
idle: idleInActiveStage(idleBinding, observed, now),
idle: assessIdle(idleBinding, observed, now),
stuck: stuckDue(binding, playbook, [...escalations], now) !== null,
stalled: observedStall(binding, live, now),
reportedNoWakeSource: binding.noWakeSource,
Expand Down
42 changes: 27 additions & 15 deletions plugins/stewards/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import {
type RolesFile,
} from "./roles.js";
import { decideForPull, explainDecision, formatDecisionListing, globMatches, type BigDecision } from "./decisions.js";
import { ASSESSMENT_DIVERGENCE_MAX_AGE_MS, ASSESSMENT_DIVERGENCE_MAX_ROWS, assess, compare, ownPullEntities, type Assessment, type AssessmentInput } from "./assessment.js";
import { ASSESSMENT_DIVERGENCE_MAX_AGE_MS, ASSESSMENT_DIVERGENCE_MAX_ROWS, assess, assessIdle, assessLiveWakeSource, assessWorker, compare, ownPullEntities, type Assessment, type AssessmentInput } from "./assessment.js";
import { assessedGate, currentStoredGate, storedGates } from "./assessed-gate.js";
import { ADOPTION_CHOICES, adoptionChoices, adoptionHow, adoptionQuestion, alertClockAt, alertFingerprint, batchAlert, deadlineAt, depsLookSchema, dueDate, isBatchable, overdue, priorityFor, priorityLabel, priorityLabelForSeverity, reminderText, repoOfFingerprint, sightingOf, snoozeUntil, type DepsFinding, type DepsLook, type DepsTicket } from "./deps.js";
import { hostContract } from "./host-contract.js";
Expand All @@ -101,7 +101,7 @@ import { EntityCache, type EntityRecord } from "./entity-cache.js";
import { classify, hasFingerprintLine, lookChanges, opsTargetKey, parseOpsTargetKey, redactLine, scrubCredentials, ticketResolved, type AlertmanagerTarget, type DependabotTarget, type FingerprintRow, type K8sTarget, type OpsLook, type OpsTarget, type SentryTarget, type Sighting, type TicketState } from "./ops.js";
import { EVENTS_PER_LOOK, NO_TOKEN, RECENT_PER_LOOK, SENTRY_API_URL, TOKEN_SCOPES, createSentryReader, emptySentryLook, issueInOrg, recentLine, sentryChanges, sentryResolveTargets, sentrySightings, sentrySummary, type SentryFinding, type SentryLook, type SentryReader } from "./sentry.js";
import { ALERTMANAGER_URL_ENV, alertmanagerChanges, alertmanagerSightings, alertmanagerSummary, createAlertmanagerReader, emptyAlertmanagerLook, resolveAlertmanagerUrl, type AlertmanagerFinding, type AlertmanagerLook, type AlertmanagerReader, type AlertmanagerRung } from "./alertmanager.js";
import { advanced, answerDiffers, anyWorkerInFlight, asksForOpenApproval, commentDiffersFromRecommendation, compactDue, deploymentSettled, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, hasLabel, IDLE_WAKE_TEXT, idleInActiveStage, isCommentNews, isOwnStageChange, lifecycle, mechanicalStatus, namesUserAsBlocker, newHumanComments, newestCommentAt, openUserFacingItems, ownChecksFinished, ownPullClosed, ownPullMerged, phaseAndStage, productGateOptions, runningWorkerReason, newestEditComment, specDiff, specHash, specText, stuckDue, unheardWait, workerInFlight, workerReportedTerminal, type Change, type MechanicalEvent, type OpenUserItem } from "./reconcile.js";
import { advanced, answerDiffers, asksForOpenApproval, commentDiffersFromRecommendation, compactDue, deploymentSettled, diffObserved, endingDeadline, followingThrough, gateKnownUnmet, hasLabel, IDLE_WAKE_TEXT, isCommentNews, isOwnStageChange, lifecycle, mechanicalStatus, namesUserAsBlocker, newHumanComments, newestCommentAt, openUserFacingItems, ownChecksFinished, ownPullClosed, ownPullMerged, phaseAndStage, productGateOptions, newestEditComment, specDiff, specHash, specText, stuckDue, unheardWait, workerInFlight, workerReportedTerminal, type Change, type MechanicalEvent, type OpenUserItem } from "./reconcile.js";
import { classifyApproval, latestProjectApprovalMarker, latestReviewMarker, prGate, reviewMarker } from "./gate.js";
import { approveOwnerReviewedPull, approvePrPageAction, declineOwnerReviewedPull, deliverApproval, fileForwardedApprovePrNeed, isDelegateWait, isProjectSteward, isUserThread, markerKey, normalizeText, specApprovedByProject, syncPullApproval, userApprovedOnEntity, userSaid, type ApprovalsContext } from "./approvals.js";
import { deliverEscalation, nextRoute, routedTo, type EscalationsContext } from "./escalations.js";
Expand Down Expand Up @@ -135,7 +135,7 @@ import { createGhWriter, parsePullRef, type BranchProtection, type GithubWriter,
import { allPullsMerged, atWork, conflictsOf, finished, normalizeTouches, openPull, overlapping, touchesOverlap, type Conflict } from "./schedule.js";
import { AUTOMATIC_NAVIGATION_PROVIDER, AUTOMATIC_THREAD_LIST_PROVIDER, BUILTIN_NAVIGATION_ID, BUILTIN_THREAD_LIST_ID, NAVIGATION_PROVIDER_KEY, THREAD_LIST_PROVIDER_KEY } from "./sidebar-provider.js";
import { waitingOnNamesWorker } from "./sidebar.js";
import { advanceStall, hasLiveWakeSource, parseUntil, pullWatchCounts, type WakeSourceInput } from "./wake-source.js";
import { advanceStall, parseUntil, pullWatchCounts, type WakeSourceInput } from "./wake-source.js";
import { PROJECT_CHANGED } from "./channels.js";
import { LINEAR_TICKETS, PLAYBOOK_TEMPLATES, projectFileTemplate } from "./templates.js";
import type { DeploymentTarget } from "./config.js";
Expand Down Expand Up @@ -3136,7 +3136,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
* same read the judgement used.
*/
async function atWorkWithRead(b: Binding): Promise<{ atWork: boolean; observed: Observed; complete: boolean } | null> {
const running = store.workers(b.id).some((w) => w.status === "running");
const running = assessWorker(store.workers(b.id)).running;
const fresh = await freshObserved(b);
if (fresh === null) return null;
// An incomplete read cannot prove the work is finished (a linked pull
Expand All @@ -3156,7 +3156,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
const conflicts: Array<Conflict & { observed: Observed | null; complete: boolean }> = [];
for (const c of overlapping(b, store.open(b.projectId))) {
const read = await atWorkWithRead(c.with);
if (read === null ? atWork(c.with, { runningWorker: store.workers(c.with.id).some((w) => w.status === "running") }) : read.atWork) {
if (read === null ? atWork(c.with, { runningWorker: assessWorker(store.workers(c.with.id)).running }) : read.atWork) {
conflicts.push({ ...c, observed: read?.observed ?? null, complete: read?.complete ?? false });
}
}
Expand Down Expand Up @@ -3222,7 +3222,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
}

function liveWakeSourceFor(b: Binding, ownPulls: ReadonlySet<string>): boolean {
return hasLiveWakeSource(wakeSourcesFor(b, ownPulls));
return assessLiveWakeSource(wakeSourcesFor(b, ownPulls));
}

/**
Expand Down Expand Up @@ -4947,7 +4947,6 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
changes.push({ text: `Approved in ${sourceLabel} (now ${entity.state})`, actionable: true });
}
const workers = store.workers(b.id);
const workerInFlight = anyWorkerInFlight(workers);
// Rule 0: which linked pull requests are the worker's own. Read the
// branches and reported URLs whether or not a worker is in flight: the
// diff only quiets them while one is, and the idle rule (BB-83) needs the
Expand All @@ -4964,6 +4963,11 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
} catch (error) {
bb.log.warn(`stewards: assessment failed for ${b.entityKey}: ${errorMessage(error)}`);
}
// BB-329: one worker-busy predicate. Every surface reads the assessment's
// worker state (assessment.ts `assessWorker`); the old `anyWorkerInFlight`
// / `runningWorkerReason` call sites are gone. `inFlight` is still
// running-or-parked today, so this is the same answer.
const workerInFlight = assessment?.worker.inFlight ?? assessWorker(workers).inFlight;
// BB-285: which of the binding's own pull requests are gate-met. A
// gate-met own pull request is the plugin's to act on, so it does not
// suppress the idle wake (BB-83) the way an open one otherwise does.
Expand All @@ -4987,7 +4991,9 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// later of the last worker's end, the last own pull request's close and
// the last wake; a worker finish asks for the look at once, without the
// grace. An idle wake ends the period until one of those happens again.
const idle = idleInActiveStage(
// BB-329: the idle answer is derived in assessment.ts (`assessIdle`), so
// the surface and the stored assessment share one derivation.
const idle = assessIdle(
{ stage, ending: b.ending, hold: b.hold, deployments, workers, ownPulls, gateMetPulls, escalations: store.openEscalations(b.id), needs: store.openNeeds(b.id), requeueUntil: b.requeueUntil },
observed,
now(),
Expand Down Expand Up @@ -5149,7 +5155,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
const startStillDue =
b.stage === "spec" &&
gateState(playbook, b.ready, currentSpec).passed &&
!anyWorkerInFlight(store.workers(b.id)) &&
!assessWorker(store.workers(b.id)).inFlight &&
b.hold === null &&
!store.openEscalations(b.id).some((x) => x.kind === "approval" || x.kind === "schedule");
if (!startStillDue) {
Expand Down Expand Up @@ -7706,7 +7712,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
if (!gate.passed) return withdraw("Withdrawn: the ready gate no longer passes.", false);
// Finished work is not scheduled against anything: there is nothing left to start.
if (b.lastSnapshot !== null && finished(b, b.lastSnapshot)) return withdraw("Withdrawn: the work is finished.", false);
if (b.stage === "review" || store.workers(b.id).some((w) => w.status === "running")) return withdraw("Withdrawn: the work has started.", true);
if (b.stage === "review" || assessWorker(store.workers(b.id)).running) return withdraw("Withdrawn: the work has started.", true);
const conflicts = await conflictsFresh(b);
if (conflicts.length === 0) return withdraw("Withdrawn: nothing in flight overlaps it now.", true);
const keys = conflicts.map((c) => c.with.entityKey);
Expand Down Expand Up @@ -8190,6 +8196,12 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// cross-repo ticket lands the consumed repo first), so the plugin must
// not merge out of order or retire the worker that owns the rest.
const openOwnPulls = observedPulls(b, observed).filter((p) => (ownPulls === null || pullBelongsToTicket(b, p, ownPulls)) && p.pull?.merged !== true && lc(p.state) !== "closed");
// BB-329: left inline for the behaviour PR, not routed through `assessWorker`.
// `assessWorker(…).parked` is `!running && some(finished)`, while this
// captures `some(finished)` before the loop's awaits; a running worker that
// ends its turn inside that window would flip the answer, which is a merge
// path behaviour change. The explicit `parked` state in the behaviour PR
// reshapes it.
const parkedWorkerOwnsAnother = openOwnPulls.length > 1 && store.workers(b.id).some((w) => w.status === "finished");
// BB-264: the steward's own merge holds name open bindings in this
// project, or a stacked base pull request by its key; the plugin's
Expand Down Expand Up @@ -8356,7 +8368,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// A steward-filed Merge item is a hand-off to a person (a worker
// whose permission system refused the merge, or a refused plugin
// merge): leave it alone and do not merge over it (BB-170/BB-257).
} else if (runningWorkerReason(store.workers(b.id)) !== null) {
} else if (assessWorker(store.workers(b.id)).reason !== null) {
// A worker's turn is in progress: it merges or handles the pull
// request, so the plugin files nothing and merges nothing. A
// `finished` (parked) worker is not running (BB-266).
Expand Down Expand Up @@ -8450,7 +8462,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// nor waiting on a person; after the window it is stuck. A retarget
// episode is criterion 4's, not this one.
if (mergedBase === null && !mergeHeld && !mergedThisLook && gateMetStuckMs > 0) {
const unattended = runningWorkerReason(store.workers(b.id)) === null && store.openNeeds(b.id).length === 0;
const unattended = assessWorker(store.workers(b.id)).reason === null && store.openNeeds(b.id).length === 0;
if (unattended) {
pullStuckState.set(lc(pull.key), "gate-met");
const since = gateMetSince(pull, g);
Expand Down Expand Up @@ -9583,7 +9595,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// merge` cannot disagree.
const gate = assessedGate(store, b, pull, now());
if (!gate.gateMet) return { ok: false, message: `The gate is not met: ${gate.missing ?? "the pull request could not be read"}.` };
const busy = runningWorkerReason(store.workers(b.id).filter((w) => w.threadId !== opts.exceptThreadId));
const busy = assessWorker(store.workers(b.id).filter((w) => w.threadId !== opts.exceptThreadId)).reason;
if (busy !== null) return { ok: false, message: `${busy.charAt(0).toUpperCase()}${busy.slice(1)} on this pull request.` };
// BB-257/BB-279: only the repository's default branch is merged. A read of
// the default branch that fails is not the default (fail closed), so the
Expand Down Expand Up @@ -11593,7 +11605,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
b.endedAt === null &&
b.ending === null &&
b.stage === "spec" &&
!store.workers(b.id).some((w) => w.status === "running") &&
!assessWorker(store.workers(b.id)).running &&
nothingPending;
if (startDue) {
store.note(b.id, START_WORKER_WAKE, false);
Expand Down Expand Up @@ -12309,7 +12321,7 @@ export function createStewardsPlugin(deps: StewardsDeps): (bb: BbPluginApi) => P
// BB-175: only a running turn can race a push; a `finished` worker
// is a pause, and a steward-filed merge item exists because it
// could not merge, so it must not keep the item not actionable.
const busy = runningWorkerReason(store.workers(b.id));
const busy = assessWorker(store.workers(b.id)).reason;
actionable = (gate?.gateMet ?? false) && busy === null;
why = actionable ? null : busy ?? (gate?.missing ?? "the pull request has not been observed yet");
}
Expand Down
Loading