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
8 changes: 7 additions & 1 deletion pm-stack/exporters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ of exactly this kind of state machine.
deduped queue-task + incident-issue effects behind injected effectors,
records the refs on the incident, plans recovery (comment + close +
complete), and renders the NEEDS DREW surfacing line (immediate when
human-blocked, after 24 h unresolved otherwise).
human-blocked, after 24 h unresolved otherwise). The hand-off creates
the incident issue **first**: fleet queue validation only accepts a
`small_fix` packet whose fix-family dedupe key names an existing issue
(`repo-health:v1:fix:<repo>:issue:<n>`, payload.issue.number matching),
so `buildQueueTaskWrapper` translates the escalation packet into the
validated repo-task/v1 envelope — full escalation context riding inside
the payload — once the issue number is confirmed.

`src/adapters.ts` + `adapters/` — the **adapter loader**. An adapter is a
per-provider JSON file (origins allowlist, CDP port, producer document
Expand Down
145 changes: 138 additions & 7 deletions pm-stack/exporters/src/watchdog-escalation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
// the #6032 remainder — which calls planEscalation / planRecovery with
// the tick's pre-decision health record and persists whatever
// recordEscalation returns.
//
// Ordering (task 20118): the incident issue is created BEFORE the
// queue task is enqueued. Fleet queue validation (repo-task/v1) only
// accepts a small_fix packet whose dedupe key is the fix family —
// `repo-health:v1:fix:<repo>:issue:<n>` — with payload.issue.number
// matching the key, so the task cannot be built until the issue
// exists. applyEscalationActions stamps the confirmed issue number
// into the enqueue packet, and buildQueueTaskWrapper is the pure
// translation from an escalation packet to the validated envelope
// (the pre-fix production wrapper failed validation on 13 fields and
// the live hand-off created neither artifact).
import type { EscalationRecord, FailureClass, SourceHealth } from "./watchdog-state.ts";

/** How long an ordinary escalation stays quiet before it surfaces. */
Expand All @@ -47,6 +58,14 @@ export interface EscalationPacket {
schema: "watchdog-escalation/v1";
kind: "small_fix";
source: string;
/** Repo that owns the adapter: the incident issue's repo, and the
* queue task's payload.repo / dedupe-key repo. */
issueRepo: string;
/** Incident issue number once the issue exists. Null in a fresh
* plan (the issue is created first; applyEscalationActions stamps
* the confirmed number in), and carried from the escalation record
* on a partial-hand-off resume. */
issueNumber: number | null;
/** Dedupe pair: (source, failure class). */
dedupeKey: string;
outcomeWanted: string;
Expand Down Expand Up @@ -86,13 +105,22 @@ export function escalationDedupeKey(source: string, cls: FailureClass | null): s
return `watchdog-escalation:${source}:${cls ?? "unknown"}`;
}

/** Every hand-off record this incident carries, latest first — the
* dedupe view planEscalation and buildEscalationPacket share. */
function escalationRecords(health: SourceHealth): EscalationRecord[] {
const existing = health.incident?.escalation;
return existing ? [existing, ...(existing.previous ?? [])] : [];
}

/** Build the queue-task packet for an incident that is escalating. */
export function buildEscalationPacket(health: SourceHealth, ctx: EscalationContext): EscalationPacket {
const cls = health.currentClass;
return {
schema: "watchdog-escalation/v1",
kind: "small_fix",
source: health.source,
issueRepo: ctx.issueRepo,
issueNumber: escalationRecords(health).find((r) => r.class === cls)?.issueNumber ?? null,
dedupeKey: escalationDedupeKey(health.source, cls),
outcomeWanted: `source \`${health.source}\` produces a fresh \`latest.json\` with real quota windows again`,
failure: {
Expand Down Expand Up @@ -161,20 +189,115 @@ export function planEscalation(
// the latest (Codex P2 on PR #8357): a failure class that changes
// back finds its original pair in `previous` instead of creating a
// third hand-off.
const existing = incident.escalation;
const records: EscalationRecord[] = existing ? [existing, ...(existing.previous ?? [])] : [];
const records = escalationRecords(health);
const forClass = records.find((r) => r.class === health.currentClass);
if (forClass) {
// Resume a partial hand-off: plan only the creations that never
// confirmed, so a retry cannot duplicate the confirmed half.
// confirmed, so a retry cannot duplicate the confirmed half. The
// issue still plans before the task (the task packet names it).
const actions: EscalationAction[] = [];
if (forClass.queueTaskId === null) actions.push({ type: "enqueue_task", packet });
if (forClass.issueNumber === null) actions.push(openIssue);
if (forClass.queueTaskId === null) actions.push({ type: "enqueue_task", packet });
return { actions, deduped: actions.length === 0 };
}
return {
deduped: false,
actions: [{ type: "enqueue_task", packet }, openIssue],
actions: [openIssue, { type: "enqueue_task", packet }],
};
}

/** The repo-task/v1 envelope the fleet queue validates and enqueues.
* Pure translation of an escalation packet: the production
* enqueueTask effector writes this to a file and hands it to
* `fleet queue enqueue`. The full escalation packet rides inside the
* payload, so the claimed task still starts from the packet alone
* (#6033 acceptance: the queue task packet matches the context list). */
export interface QueueTaskWrapper {
title: string;
body: string;
dedupe_key: string;
priority: number;
payload: Record<string, unknown>;
}

export function buildQueueTaskWrapper(packet: EscalationPacket, at: string): QueueTaskWrapper {
if (packet.issueNumber === null) {
throw new Error(
`escalation packet for ${packet.source} carries no incident issue number — the issue must be created before the queue task can name it`,
);
}
const repo = packet.issueRepo;
const n = packet.issueNumber;
const cls = packet.failure.class ?? "unknown";
const repairLines = packet.failure.repairLog.map(
(e) => `rung ${e.rung} ${e.outcome} at ${e.at}: ${e.detail}`,
);
return {
title: `watchdog incident: ${packet.source} (${cls}) — repair the quota producer`,
body: [
packet.outcomeWanted,
"",
`Incident issue: ${repo}#${n} (the watchdog auto-closes it on the next producer success — do not close it by hand).`,
`Required capability: ${packet.requiredCapability}`,
`Acceptance: ${packet.acceptance}`,
].join("\n"),
dedupe_key: `repo-health:v1:fix:${repo}:issue:${n}`,
priority: 30,
payload: {
schema: "repo-task/v1",
kind: "small_fix",
repo,
issue: { number: n },
problem:
`Source \`${packet.source}\` quota producer is failing (${cls}) and every watchdog ` +
`repair rung is spent, so the watchdog escalated (watchdog dedupe ${packet.dedupeKey}). ` +
`Last detail: ${packet.failure.lastDetail || "(none)"}. The full context packet is in ` +
`payload.escalation_packet; the incident issue ${repo}#${n} is the reporting surface.`,
priority_reason:
"Quota watchdog rung-4 escalation (#6033): a dead quota producer blinds the fleet's " +
"provider-quota reads until a browser-enabled agent repairs it; the watchdog has " +
"already exhausted its automated repair ladder.",
evidence: [
`consecutive failures: ${packet.failure.consecutiveFailures}`,
...(repairLines.length > 0 ? repairLines : ["repair log empty — ladder spent without entries"]),
`watchdog state file: ${packet.failure.stateFilePath}`,
],
acceptance: [
packet.acceptance,
`incident issue ${repo}#${n} is auto-commented and auto-closed by the watchdog on producer success (the fixer closes nothing by hand)`,
],
tests: [
"the producer for this source writes a fresh latest.json with status ok and real quota windows",
"the watchdog state file shows the incident cleared after the recovery tick",
],
context_refs: [
packet.failure.stateFilePath,
packet.failure.historyRef,
packet.failure.adapterRef,
`${repo}#${n}`,
"developing-today/code#6033",
],
scope: {
include: [
`restore fresh quota documents for source ${packet.source} via its adapter's browser flow (adapter: ${packet.failure.adapterRef})`,
"report progress on the incident issue",
],
exclude: [
...packet.stopConditions,
"rebuilding the watchdog core or the escalation module — report on the incident issue instead if the ladder itself is broken",
],
},
risk: { level: "low", flags: [] },
timebox_minutes: 45,
dedupe: {
checked_at: at,
basis:
`watchdog escalation record for (${packet.source}, ${cls}): the incident issue ` +
`${repo}#${n} was created by this same hand-off, and the fix-family dedupe key ` +
"names it, so a second live task for the incident cannot be enqueued.",
},
escalation_packet: packet,
},
};
}

Expand Down Expand Up @@ -320,9 +443,17 @@ export async function applyEscalationActions(
for (const action of actions) {
try {
switch (action.type) {
case "enqueue_task":
queueTaskId = await effectors.enqueueTask(action.packet);
case "enqueue_task": {
// The queue task names the incident issue (fix-family
// dedupe): stamp the number this same run just confirmed
// into a packet planned before the issue existed. A resume
// packet already carries its recorded number.
const pkt = action.packet.issueNumber === null && issueNumber !== null
? { ...action.packet, issueNumber }
: action.packet;
queueTaskId = await effectors.enqueueTask(pkt);
break;
}
case "open_issue":
issueNumber = await effectors.openIssue(action);
break;
Expand Down
44 changes: 28 additions & 16 deletions pm-stack/exporters/src/watchdog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from "./watchdog-state.ts";
import {
applyEscalationActions,
buildQueueTaskWrapper,
EscalationPartialError,
planEscalation,
planRecovery,
Expand Down Expand Up @@ -279,17 +280,11 @@ function productionEscalationEffectors(live: boolean): EscalationEffectors {
return {
async enqueueTask(packet) {
guard();
const wrapper = {
title: `watchdog incident: ${packet.source} (${packet.failure.class ?? "unknown"})`,
body: packet.outcomeWanted,
dedupe_key: packet.dedupeKey,
priority: 30,
payload: {
schema: "repo-task/v1",
kind: packet.kind,
packet,
},
};
// The validated repo-task/v1 envelope (task 20118): the packet
// names the incident issue created earlier in this hand-off —
// buildQueueTaskWrapper throws when it does not, rather than
// enqueueing a packet the queue would reject.
const wrapper = buildQueueTaskWrapper(packet, new Date().toISOString());
const file = join("/tmp", `watchdog-escalation-${packet.source}-${Date.now()}.json`);
await writeFile(file, JSON.stringify(wrapper));
const out = await run(["fleet", "queue", "enqueue", "--queue", "repo-health-code-pilot", file]);
Expand Down Expand Up @@ -322,13 +317,30 @@ function productionEscalationEffectors(live: boolean): EscalationEffectors {
},
async completeTask(action) {
guard();
// The escalation task is completed by whoever claimed and fixed
// it in the normal case; the watchdog's recovery completion is
// best-effort (the task may already be claimed/done).
await run([
// Recovery resolution of the escalation task, best-effort at
// every step (task 20118 live demo): `complete` requires
// holding the claim, and the watchdog never claims — so for a
// task still sitting queued it always fails with "not claimed
// by repo-worker-code". That case is the common one (the
// source healed before any fixer picked the task up), and the
// honest terminal verb for it is `cancel` with the recovery
// reason — but cancel also cancels CLAIMED tasks, so it is
// only safe when the task is untouched: an unclaimed task has
// no timeline records. A task with records has an owner; leave
// it to them — the auto-closed incident issue is their signal,
// and their own completion closes the task out.
const completed = await run([
"fleet", "queue", "complete", action.queueTaskId, "--worker", "repo-worker-code",
"--result", JSON.stringify({ outcome: "already_done", note: "watchdog recovery: source healthy again (#6033)" }),
]).catch(() => "");
]).then(() => true).catch(() => false);
if (completed) return;
const timeline = await run(["fleet", "queue", "timeline", action.queueTaskId]).catch(() => "");
if (timeline.includes("no timeline records")) {
await run([
"fleet", "queue", "cancel", action.queueTaskId, "--worker", "repo-worker-code",
"--reason", "watchdog recovery: source healthy again; the repair this task commissioned is no longer needed (#6033)",
]).catch(() => "");
}
},
};
}
Expand Down
Loading