mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 17:14:54 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The restart-chaos harness (`tests/restart-chaos/`) proves that a release swap (two servers that overlap, then the old one stops) does not break running agent work > - The staging twin runs that swap with two local servers and real Daytona sandboxes, and it is the gate before any hosted proof > - A loaded twin wave (48 tasks, 20 runs plus 20 queued, 2 swaps) reported 31 per-task failures and 2 extra turns, but the product did the correct thing in almost all of them > - Three harness defects made the noise: the graders called a required follow-up run an extra turn, called a healthy queued run an orphan, and called a parent that waited for its subtasks a stall. Cleanup also left archived Daytona sandboxes in the shared org on every wave > - This pull request lands the twin harness work and fixes the three grader defects and the cleanup gap, each with a red/green harness test > - The benefit is a stop-rule grade that shows only real failures (here: 1 Retry banner) and makes the follow-up wait visible, and a wave that leaves no sandbox behind ## Linked Issues or Issue Description Refs #21175 and #21227 (the server's comment and child-result delivery rules; this harness takes their outcome from the server's wake record and does not re-check them). **What happened?** Wave `w-20261113T052703-s7301` (loaded twin L1, on Daytona) graded 31 tasks FAIL and listed 2 extra turns. The release-swap grade was 82 / 82 tasks completed without human action. - t007 and t010 (OpenCode): a person posted a follow-up comment during the first run (05:44:56 and 05:49:21). OpenCode takes no input mid-turn. The first run finished `done` at 05:53 and said "No board comments arrived". The deferred wake was promoted, and the second run answered the comment after 16 to 17 minutes in the 20-deep queue. The graders counted the second run as an extra turn and as a `completion_once` duplicate. - t021 to t048: each waited about 483 s queued for a slot in the deliberate 20-deep queue. U1 allows 5 minutes queued plus 3 minutes without an owner, so all 28 failed `no_owner_no_action`. - t001: a parent ended its turn `yielded` to wait for its two subtasks. The observer saw no run for 3 minutes, called it a stall, and rested it `in_progress`. The parent completed later through its `issue_children_completed` wake. R1, R3, R5 and U1 failed. - Cleanup: every Daytona wave left 1 to 8 archived `paperclip-create-*` setup sandboxes and reported `remainingSandboxes: []`. Daytona does not delete archived sandboxes. **Expected behavior** - A run that starts with a person's comment that no earlier run received is required. Only a run that starts with no undelivered input is an extra turn. - A run queued while every slot of its agent is taken is healthy for the declared queue time. A run queued while its agent has a free slot still fails. - A parent that waits for open subtasks is in a named wait, not a stall. - Cleanup deletes the sandboxes that this wave created and fails when one remains. **Steps to reproduce** 1. Run the L1 loaded twin (command in the README section "Staging twin cells"). 2. Read `staging-mix-summary.md` and `SUMMARY.md`. ## What Changed - Cherry-picked the 8 twin harness commits from `fastloop/staging-twin-loaded`: staging-mix under the release-swap driver on Daytona, `--server-from` on Daytona, probe agents verified while the first tasks run, image matched to the `--server-from` checkout, and the knobs `RESTART_CHAOS_K15_KILL_OLD_AFTER_SECONDS` / `_KILL_OLD_SWAPS`, `RESTART_CHAOS_K15_FREEZE_NEW_SECONDS` / `_FREEZE_SWAPS`, `RESTART_CHAOS_MIX_BANNER_REST_SECONDS` and `RESTART_CHAOS_MIX_FIRST_SLOT_SECONDS`. A misplaced doc block in `supervisor.ts` is moved back to its function. No code comments are added. - Cleanup (`daytona-sweep.ts`): after the environment delete, the harness deletes every sandbox that carries both of this wave's UUID labels (company and environment), archived ones included. `assertHarnessMayMutate` still refuses any sandbox without both labels and any `fleet-sbx-*` snapshot. The `paperclip-create-*` name refusal is removed, because every plugin sandbox (the fleet's included) has that name, so the labels are the real ownership proof. A sandbox left after deletion fails the wave (`undeletedSandboxes`, warm-shutdown verdict, `ttft-baseline`). - Follow-ups (`follow-up-delivery.ts`, `wake-evidence.ts`, `staging-mix-retry.ts`, `staging-mix.ts`): delivery comes from the server's own record. A comment that the server judged delivered never becomes a run. A run that the server started from a wake carrying a person's comment posted after the first run started (a retry included) is required, not an extra turn. `completion_once` keeps the first completion as the baseline and does not count a later completion by such a run (truth-table test). The grader does not re-check the server's delivery rule; the server's tests cover it. The summary adds `extra turns: N` and `follow-up answered in a later run: n, queued wait p50/max, posted to answered p50/max`. - Declared queue (`universal.ts`, `agent-slots.ts`, `wave-grade-inputs.ts`): with `--mix-queue` above 0, U1 allows ceil(queue / runs) × (longest turn + 5 min) queued (27 minutes for L1). Only queued time while the run's agent has a free slot counts against the 5-minute bound. Slot use comes from a new `wake-evidence/agent-slots.json` (run start/finish and each agent's `maxConcurrentRuns`). - Subtask wait (`child-wait.ts`, `observer.ts`, `grader.ts`): an idle parent after a `yielded` or `blocked` turn, until its `issue_children_completed` wake, is a named wait (`waitingOnChildren`) for R5 and U1. The observer no longer gives up on a parent while a subtask is open. An older record that rested during that wait takes the completion from the wake evidence. - Review follow-ups: the first staging-mix slot moves earlier without adding a swap; each sandbox delete pass has a deadline; a frozen incoming server is always resumed (`server-freeze.ts`); the first completion is always the baseline for `completion_once` (truth-table test); a follow-up run that failed before its turn keeps the answer owed for its retry; `agent-slots.json` saves the server's effective run cap (`maxConcurrentRunsSql`); the wave-end cleanup flow moved to `daytona-cleanup.ts` (`main.ts` stays under 1,000 lines). - README: one-command entries for the twin cell (G, G2, G3) and L1, the knobs, the stop-rule grading, the new summary lines, the U1 queue rule, the subtask wait and the cleanup rule. ## Verification - Red/green tests (red = new tests against the previous source): - Cleanup, `daytona-sweep.test.ts`: red 4 failed (for example "expected [] to deeply equal [ 'archived', 'archiving', 'started' ]": nothing was deleted). Green: pass. - Follow-ups, declared queue, subtask wait (`staging-mix-retry.test.ts`, `universal.test.ts`, `child-wait.test.ts`, `wake-evidence.test.ts`): red 6 failed (for example the t010 run listed as an extra turn, U1 fail for the t030 queue shape, t001 "rested_not_done"). Green: pass. The tests use the real timings of t001, t010 and t030. - Full harness suite: `npx vitest run --config tests/restart-chaos/vitest.config.ts`: 71 files, 811 tests pass. `npx tsc -p tests/restart-chaos/tsconfig.json --noEmit`: clean. - `pnpm verify` exits 2 (the harness README is a global CI input). Required CI supplies the broad coverage. - One-off offline analysis (a scratch script, not part of this PR): the saved wave `w-20261113T052703-s7301` regraded with these graders, from a copy of its results: | | Before | After | | --- | --- | --- | | Per-task FAIL | 31 (t001 R1, t007 and t010 `completion_once`, t021 to t048 U1) | 0 | | Extra turns | 2 (t007, t010) | 0 | | Retry banner shown | 1 (t020, 3 s, completed without a click) | 1 (t020) | | Follow-up answered in a later run | not shown | 4: t001, t007, t010, t012. Queued wait p50 983 s, max 1739 s. Posted to answered p50 1420 s, max 2255 s | t001 and t012 started their follow-up run before completion, so they were never extra turns. They are listed because the wait is real. This wave predates `agent-slots.json`, so the regrade used only the declared queue bound for U1. - No Daytona wave ran for this PR. There is no live proof here that the cleanup deletes archived sandboxes in the real org or that the new evidence file is written on a live wave. The delete path uses the same `DELETE /sandbox/<id>` call as the Daytona SDK's `deleteSandbox`. ## Risks - The harness now deletes Daytona sandboxes in an org that the Cloud fleet shares. The guard is the wave's two freshly minted UUID labels, filtered on the server and checked again on every row, plus the `fleet-sbx-*` refusal. A sandbox without both labels is never deleted. - When the server wrongly re-delivers a comment that a finished run already received, the harness counts that run as a required follow-up, not as an extra turn. The server's own delivery tests catch that class, not this grader. `completion_once` still fails when that run also reports completion and is not the first. - The declared queue bound is generous (27 minutes for L1). A stuck queue with full slots for longer than that still fails. A queue with a free slot fails after 5 minutes when slot evidence exists. Without slot evidence only the declared bound applies. - Test harness only. No product code changes. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with tool use (shell, file edits, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
343 lines
21 KiB
TypeScript
343 lines
21 KiB
TypeScript
/**
|
||
* Universal checks U1–U5: scenario-independent rules graded on every task of
|
||
* every wave, whatever its workload. Pure functions over observer samples (no
|
||
* I/O), so they also run over saved evidence (`universal-audit.ts`).
|
||
*
|
||
* U1 liveness every non-terminal task has a live owner or a visible, actionable item
|
||
* U2 retry loop the same failure does not repeat without the task changing state
|
||
* U3 diagnosable every failed run names a specific cause (its code, or a recorded failureCause)
|
||
* U4 stale notices no failure or Retry notice outlives later successful work
|
||
*
|
||
* Each rule is either `enforced` (a violation fails the task) or a warning
|
||
* (reported in the grade and ledger, never fails the task). The split comes
|
||
* from the false-positive audit over saved waves; see README "Universal checks".
|
||
*/
|
||
import type { RunSample, TaskRecord, TaskSample } from "./types.js";
|
||
|
||
export type UniversalId = "U1" | "U2" | "U3" | "U4" | "U5";
|
||
export interface UniversalResult { verdict: "pass" | "fail" | "n/a"; detail: string; enforced: boolean;
|
||
/** Measurements behind the verdict (milliseconds or counts), for bound tuning. */
|
||
metrics?: Record<string, number> }
|
||
export type UniversalGrade = Record<UniversalId, UniversalResult>;
|
||
|
||
export interface UniversalBounds {
|
||
/** U1: longest ready-server time a non-terminal task may spend with no live owner and no actionable item. */
|
||
orphanMs: number;
|
||
/** U1: longest ready-server time a run may stay queued without starting. */
|
||
queuedMs: number;
|
||
/** U1: a `running` run with no event, activity or start evidence for this long is not a live owner. */
|
||
silentRunMs: number;
|
||
/** U2: most consecutive failed runs with one failure code while the issue status stays the same. */
|
||
sameFailureRuns: number;
|
||
/** U4: longest a failure or Retry notice may remain after a later run succeeded. */
|
||
staleNoticeMs: number;
|
||
maxRuns: number;
|
||
declaredQueuedMs?: number;
|
||
}
|
||
|
||
export type SlotFree = (runId: string, at: number) => boolean | null;
|
||
|
||
export function declaredQueueBoundMs(mix: { runs: number; queue: number; turnMinutes: readonly [number, number] } | undefined): number | undefined {
|
||
if (!mix || mix.queue <= 0) return undefined;
|
||
return Math.ceil(mix.queue / Math.max(1, mix.runs)) * (mix.turnMinutes[1] + 5) * 60_000;
|
||
}
|
||
|
||
/**
|
||
* Defaults from the 2026-11-09 audit of 79 saved waves (1,620 tasks; README
|
||
* "Universal checks"). Override with RESTART_CHAOS_U1_ORPHAN_SECONDS,
|
||
* RESTART_CHAOS_U1_QUEUED_SECONDS, RESTART_CHAOS_U2_SAME_FAILURE_RUNS and
|
||
* RESTART_CHAOS_U4_STALE_SECONDS.
|
||
*/
|
||
export const DEFAULT_UNIVERSAL_BOUNDS: UniversalBounds = {
|
||
orphanMs: 3 * 60_000,
|
||
queuedMs: 5 * 60_000,
|
||
silentRunMs: 3 * 60_000,
|
||
sameFailureRuns: 3,
|
||
staleNoticeMs: 60_000,
|
||
maxRuns: 10,
|
||
};
|
||
|
||
/** Rules that fail a task. The rest are warnings until the audit shows no false positive on known-good waves. */
|
||
export const ENFORCED_UNIVERSAL: ReadonlySet<UniversalId> = new Set<UniversalId>(["U1", "U2", "U4", "U5"]);
|
||
|
||
export function universalBounds(env: NodeJS.ProcessEnv = process.env): UniversalBounds {
|
||
const seconds = (name: string, fallback: number) => {
|
||
const raw = env[name];
|
||
if (raw === undefined || raw === "") return fallback;
|
||
const value = Number(raw);
|
||
if (!Number.isFinite(value) || value <= 0) throw new Error(`${name} must be a positive number`);
|
||
return value;
|
||
};
|
||
return {
|
||
orphanMs: seconds("RESTART_CHAOS_U1_ORPHAN_SECONDS", DEFAULT_UNIVERSAL_BOUNDS.orphanMs / 1000) * 1000,
|
||
queuedMs: seconds("RESTART_CHAOS_U1_QUEUED_SECONDS", DEFAULT_UNIVERSAL_BOUNDS.queuedMs / 1000) * 1000,
|
||
silentRunMs: DEFAULT_UNIVERSAL_BOUNDS.silentRunMs,
|
||
sameFailureRuns: Math.round(seconds("RESTART_CHAOS_U2_SAME_FAILURE_RUNS", DEFAULT_UNIVERSAL_BOUNDS.sameFailureRuns)),
|
||
staleNoticeMs: seconds("RESTART_CHAOS_U4_STALE_SECONDS", DEFAULT_UNIVERSAL_BOUNDS.staleNoticeMs / 1000) * 1000,
|
||
maxRuns: Math.round(seconds("RESTART_CHAOS_U5_MAX_RUNS", DEFAULT_UNIVERSAL_BOUNDS.maxRuns)),
|
||
};
|
||
}
|
||
|
||
const TERMINAL_ISSUE = new Set(["done", "cancelled"]);
|
||
const ms = (iso: string | null | undefined) => (iso ? Date.parse(iso) : NaN);
|
||
const seconds = (value: number) => `${Math.round(value / 1000)}s`;
|
||
const ready = (sample: TaskSample) => sample.reachable && sample.serverReady;
|
||
|
||
/**
|
||
* Codes that name no cause a person can act on. `adapter_failed` is the
|
||
* catch-all an adapter returns when it did not classify its failure; the board
|
||
* shows it as "adapter failed".
|
||
*/
|
||
export const GENERIC_FAILURE_CODES: ReadonlySet<string> = new Set(["", "error", "unknown", "unknown_error", "failed", "adapter_failed", "internal_error"]);
|
||
/** Server fallbacks that record only that the adapter gave no code: they name nothing specific. */
|
||
const VAGUE_FAILURE_CAUSES: ReadonlySet<string> = new Set(["adapter_reported_error", "object", "string", "undefined"]);
|
||
/** A recorded failureCause that names something: not empty, a bare class, `unknown_error` or a no-code fallback. */
|
||
export function diagnosableFailureCause(cause: string | null | undefined): boolean {
|
||
const text = (cause ?? "").trim();
|
||
return text !== "" && !GENERIC_FAILURE_CODES.has(text.toLowerCase()) && !VAGUE_FAILURE_CAUSES.has(text)
|
||
&& !bareCauseMessage(text);
|
||
}
|
||
const FAILED_RUN = new Set(["failed", "timed_out"]);
|
||
|
||
/** A message whose only cause is a bare exception class or "unknown" (the #20385 "last cause: Error" class). */
|
||
export function bareCauseMessage(message: string | null | undefined): boolean {
|
||
const text = (message ?? "").trim();
|
||
if (!text) return true;
|
||
if (/^(error|unknown|unknown_error|internal error)\.?$/i.test(text)) return true;
|
||
return /\b(?:last cause|cause|reason)\s*[:=]\s*\(?\s*(?:Error|unknown_error|unknown|undefined|null)\s*\)?(?:\s|[;,.)]|$)/.test(text)
|
||
|| /^Error at source seq \d+$/.test(text) || /:\s*Error at source seq \d+/.test(text);
|
||
}
|
||
|
||
/** Latest view of each run across the samples. */
|
||
function finalRuns(record: TaskRecord): Map<string, RunSample & { firstSeenAt: number; finishedSeenAt: number | null; statusWhenFinished: string | null }> {
|
||
const runs = new Map<string, RunSample & { firstSeenAt: number; finishedSeenAt: number | null; statusWhenFinished: string | null }>();
|
||
for (const sample of record.samples) {
|
||
if (!sample.reachable) continue;
|
||
for (const run of sample.runs ?? []) {
|
||
const prior = runs.get(run.id);
|
||
const finished = !["queued", "running", "scheduled_retry"].includes(run.status);
|
||
runs.set(run.id, { ...run, firstSeenAt: prior?.firstSeenAt ?? sample.at,
|
||
finishedSeenAt: prior?.finishedSeenAt ?? (finished ? sample.at : null),
|
||
statusWhenFinished: prior?.statusWhenFinished ?? (finished ? sample.issueStatus ?? null : null) });
|
||
}
|
||
}
|
||
return runs;
|
||
}
|
||
|
||
/** U1: what keeps this task moving at this sample, or null when nothing does. */
|
||
/** A retry timer or an accepted Retry that waits on the system, with no run dispatched yet. */
|
||
export function retryTimer(sample: TaskSample): boolean {
|
||
const dispatched = (sample.runs ?? []).some(run => ["queued", "running", "scheduled_retry"].includes(run.status));
|
||
return !dispatched && Boolean(sample.scheduledRetry || sample.interruption?.retryWait || sample.interruption?.state === "retry_scheduled");
|
||
}
|
||
|
||
/** `timerFor`: ready-server time an accepted Retry has waited on the system with no run dispatched. */
|
||
export function liveOwner(sample: TaskSample, queuedFor: ReadonlyMap<string, number>, bounds: UniversalBounds, timerFor = 0): string | null {
|
||
for (const run of sample.runs ?? []) {
|
||
if (run.status === "running") {
|
||
if (run.toolInFlight || run.processAlive) return `run ${run.id} running (tool or process alive)`;
|
||
const evidence = Math.max(ms(run.lastEvidenceAt) || 0, ms(run.startedAt) || 0);
|
||
if (!evidence || sample.at - evidence <= bounds.silentRunMs) return `run ${run.id} running`;
|
||
}
|
||
if (run.status === "queued" || run.status === "scheduled_retry") {
|
||
if ((queuedFor.get(run.id) ?? 0) <= bounds.queuedMs) return `run ${run.id} ${run.status}`;
|
||
}
|
||
}
|
||
// A retry row stays on the issue after its run is dispatched; once a run exists the run's own bounds above decide.
|
||
// A scheduled retry is a timer (samples carry no due time, so it is not bounded here). An accepted Retry
|
||
// that waits on the system (`retryWait`) is an owner only within the queue bound.
|
||
if (retryTimer(sample) && (!sample.interruption?.retryWait || timerFor <= bounds.queuedMs)) return "retry timer";
|
||
if (sample.activeRecoveryAction && sample.recoveryOwner !== "board") return "system recovery action";
|
||
if (sample.waitingOnChildren) return "open subtasks";
|
||
return null;
|
||
}
|
||
|
||
/** U1: an item a person can see and act on (Retry, a question, an approval, a board-owned recovery, a named blocker). */
|
||
export function actionableItem(sample: TaskSample): string | null {
|
||
const interruption = sample.interruption;
|
||
// A named user action (for example `confirm_run_stopped`) is shown even while an accepted Retry waits.
|
||
if (interruption?.userAction) return `user action ${interruption.userAction}`;
|
||
// Otherwise an accepted Retry that waits on the system ("Retry is waiting for Paperclip to ...") leaves nothing
|
||
// for the person to do: the Retry button, the failed notice and the blocker describe work they already asked for.
|
||
if (interruption?.retryWait) return null;
|
||
if (interruption?.canRetry) return "Retry";
|
||
if (interruption && (interruption.state === "waiting_for_user" || interruption.state === "failed")) return `interruption ${interruption.state}`;
|
||
if (sample.activeRecoveryAction && sample.recoveryOwner === "board") return "board recovery action";
|
||
if (sample.executionBlocker) return "execution blocker";
|
||
// Blocked and in-review issues wait on another issue, an approval or a person by definition.
|
||
if (sample.issueStatus === "blocked" || sample.issueStatus === "in_review") return `status ${sample.issueStatus}`;
|
||
return null;
|
||
}
|
||
|
||
export function gradeU1(record: TaskRecord, bounds: UniversalBounds, slotFree: SlotFree = () => null): Omit<UniversalResult, "enforced"> {
|
||
if (!record.samples.some(ready)) return { verdict: "n/a", detail: "no ready sample" };
|
||
// Ready-server time each run has spent queued (outages do not count).
|
||
const totalQueued = new Map<string, number>();
|
||
const freeQueued = new Map<string, number>();
|
||
const queuedFor = new Map<string, number>();
|
||
let orphanMs = 0, worstOrphan = 0, orphanFrom: number | null = null, lastAt: number | null = null;
|
||
let worstQueued = 0, worstQueuedRun = "";
|
||
let timerFor = 0, worstTimer = 0;
|
||
for (const sample of record.samples) {
|
||
// Outages pause the clock: a down server is the fault's time, not the task's.
|
||
if (!ready(sample)) { lastAt = null; continue; }
|
||
for (const run of sample.runs ?? []) {
|
||
if (run.status === "queued" || run.status === "scheduled_retry") {
|
||
const step = lastAt !== null && totalQueued.has(run.id) ? sample.at - lastAt : 0;
|
||
const waited = (totalQueued.get(run.id) ?? 0) + step;
|
||
totalQueued.set(run.id, waited);
|
||
if (waited > worstQueued) { worstQueued = waited; worstQueuedRun = run.id; }
|
||
if (bounds.declaredQueuedMs === undefined || run.status !== "queued") { queuedFor.set(run.id, waited); continue; }
|
||
const free = (freeQueued.get(run.id) ?? 0) + (slotFree(run.id, sample.at) === true ? step : 0);
|
||
freeQueued.set(run.id, free);
|
||
queuedFor.set(run.id, waited > bounds.declaredQueuedMs ? Number.POSITIVE_INFINITY : free);
|
||
}
|
||
}
|
||
timerFor = retryTimer(sample) && sample.interruption?.retryWait ? timerFor + (lastAt !== null ? sample.at - lastAt : 0) : 0;
|
||
worstTimer = Math.max(worstTimer, timerFor);
|
||
if (TERMINAL_ISSUE.has(sample.issueStatus ?? "")) { orphanMs = 0; orphanFrom = null; lastAt = sample.at; continue; }
|
||
const owned = liveOwner(sample, queuedFor, bounds, timerFor) ?? actionableItem(sample);
|
||
if (owned) { orphanMs = 0; orphanFrom = null; }
|
||
else {
|
||
if (orphanFrom === null) orphanFrom = sample.at;
|
||
else if (lastAt !== null) orphanMs += sample.at - lastAt;
|
||
worstOrphan = Math.max(worstOrphan, orphanMs);
|
||
if (orphanMs > bounds.orphanMs) {
|
||
const queued = (sample.runs ?? []).filter(run => run.status === "queued" || run.status === "scheduled_retry");
|
||
return { verdict: "fail", metrics: { ownerlessMs: orphanMs, queuedMs: worstQueued, retryTimerMs: worstTimer }, detail: `no_owner_no_action ${seconds(orphanMs)} in ${sample.issueStatus ?? "unknown"}`
|
||
+ (queued.length ? ` with ${queued.map(run => `${run.id} queued ${seconds(totalQueued.get(run.id) ?? 0)}`
|
||
+ `${freeQueued.has(run.id) ? ` (${seconds(freeQueued.get(run.id)!)} with a free slot, declared queue bound ${seconds(bounds.declaredQueuedMs!)})` : ""}`).join(",")}` : "")
|
||
+ (timerFor ? ` after a retry wait of ${seconds(timerFor)}${sample.interruption?.retryWait ? " (accepted Retry waiting on the system)" : ""}` : "")
|
||
+ ` (interruption ${sample.interruption?.state ?? "none"})` };
|
||
}
|
||
}
|
||
lastAt = sample.at;
|
||
}
|
||
return { verdict: "pass", metrics: { ownerlessMs: worstOrphan, queuedMs: worstQueued, retryTimerMs: worstTimer }, detail: `longest ownerless ${seconds(worstOrphan)}; longest queued ${seconds(worstQueued)}${worstQueuedRun ? ` (${worstQueuedRun})` : ""}` };
|
||
}
|
||
|
||
/** The issue status seen between two failures (both finish polls included) was not one value throughout. */
|
||
function issueStatusChanged(record: TaskRecord, before: { finishedSeenAt: number | null; statusWhenFinished: string | null },
|
||
after: { finishedSeenAt: number | null; statusWhenFinished: string | null }): boolean {
|
||
if (before.statusWhenFinished !== after.statusWhenFinished) return true;
|
||
const [from, to] = [before.finishedSeenAt ?? 0, after.finishedSeenAt ?? Number.POSITIVE_INFINITY];
|
||
return record.samples.some(sample => sample.reachable && sample.at >= from && sample.at <= to && sample.issueStatus !== before.statusWhenFinished);
|
||
}
|
||
|
||
export function gradeU2(record: TaskRecord, bounds: UniversalBounds): Omit<UniversalResult, "enforced"> {
|
||
// Every finished run in creation order: a succeeded run between two failures is progress and ends the streak.
|
||
const finished = [...finalRuns(record).values()].filter(run => run.finishedSeenAt !== null)
|
||
.sort((a, b) => (ms(a.createdAt) || a.firstSeenAt) - (ms(b.createdAt) || b.firstSeenAt));
|
||
const isFailure = (run: RunSample) => FAILED_RUN.has(run.status) || run.status === "interrupted";
|
||
if (!finished.some(isFailure)) return { verdict: "n/a", detail: "no failed run" };
|
||
let streak = 0, worst = 0, worstCode = "";
|
||
let previous: (typeof finished)[number] | null = null;
|
||
for (const current of finished) {
|
||
if (!isFailure(current)) {
|
||
// A cancellation (for example a superseded continuation) is neither progress nor a repeat.
|
||
if (current.status !== "cancelled") { streak = 0; previous = null; }
|
||
continue;
|
||
}
|
||
const same = previous !== null && `${previous.status}:${previous.errorCode}` === `${current.status}:${current.errorCode}`
|
||
&& !issueStatusChanged(record, previous, current);
|
||
streak = same ? streak + 1 : 1;
|
||
previous = current;
|
||
if (streak > worst) { worst = streak; worstCode = `${current.status}:${current.errorCode ?? "none"}`; }
|
||
}
|
||
return worst > bounds.sameFailureRuns
|
||
? { verdict: "fail", metrics: { sameFailureRuns: worst }, detail: `same_failure_repeated ${worst}x ${worstCode} without a task state change` }
|
||
: { verdict: "pass", metrics: { sameFailureRuns: worst }, detail: `most repeats of one failure ${worst} (${worstCode})` };
|
||
}
|
||
|
||
/** What the wave database adds for U3: each run's error text and the task's recovery actions. */
|
||
export interface UniversalDbEvidence {
|
||
runMessages: ReadonlyMap<string, string | null>;
|
||
recoveryActions: ReadonlyArray<{ id: string; kind: string; cause: string | null; nextAction: string | null }>;
|
||
}
|
||
|
||
/** U3: failure codes from the samples; with database evidence, also run error text and recovery-action causes. */
|
||
export function gradeU3(record: TaskRecord, db?: UniversalDbEvidence): Omit<UniversalResult, "enforced"> {
|
||
const failed = [...finalRuns(record).values()].filter(run => FAILED_RUN.has(run.status));
|
||
const actions = db?.recoveryActions ?? [];
|
||
if (!failed.length && !actions.length) return { verdict: "n/a", detail: "no failed run or recovery action" };
|
||
const messages = db?.runMessages;
|
||
const vague: string[] = [];
|
||
for (const action of actions) {
|
||
if (bareCauseMessage(action.cause) || bareCauseMessage(action.nextAction)) {
|
||
vague.push(`recovery ${action.id}(${action.kind}) cause=${JSON.stringify((action.cause ?? "").slice(0, 80))} next=${JSON.stringify((action.nextAction ?? "").slice(0, 80))}`);
|
||
}
|
||
}
|
||
for (const run of failed) {
|
||
const code = run.errorCode ?? "";
|
||
// A generic code with a recorded cause (for example `adapter_failed` + `40001`) names what failed.
|
||
if (diagnosableFailureCause(run.failureCause)) continue;
|
||
if (GENERIC_FAILURE_CODES.has(code.trim().toLowerCase())) {
|
||
vague.push(`${run.id}:code=${code || "none"}${run.failureCause ? ` cause=${run.failureCause.slice(0, 80)}` : ""}`);
|
||
} else if (messages?.has(run.id) && bareCauseMessage(messages.get(run.id))) {
|
||
vague.push(`${run.id}:${code} message=${JSON.stringify((messages.get(run.id) ?? "").slice(0, 120))}`);
|
||
}
|
||
}
|
||
return vague.length
|
||
? { verdict: "fail", detail: `undiagnosable_failure ${vague.slice(0, 4).join("; ")}${vague.length > 4 ? ` (+${vague.length - 4})` : ""}` }
|
||
: { verdict: "pass", detail: `${failed.length} failed run(s) name a specific cause${messages ? " and message" : ""}; ${actions.length} recovery action(s)` };
|
||
}
|
||
|
||
/** A failure or Retry notice the board shows for this sample. */
|
||
function failureNotice(sample: TaskSample): string | null {
|
||
const interruption = sample.interruption;
|
||
if (!interruption) return null;
|
||
if (interruption.canRetry) return "Retry offered";
|
||
if (interruption.state === "failed") return "failed notice";
|
||
return null;
|
||
}
|
||
|
||
export function gradeU4(record: TaskRecord, bounds: UniversalBounds): Omit<UniversalResult, "enforced"> {
|
||
let staleFrom: number | null = null, worst = 0, what = "";
|
||
const latestFinish = new Map<string, RunSample>();
|
||
for (const sample of record.samples) {
|
||
if (!ready(sample)) continue;
|
||
for (const run of sample.runs ?? []) latestFinish.set(run.id, run);
|
||
const runs = [...latestFinish.values()].sort((a, b) => (ms(a.createdAt) || 0) - (ms(b.createdAt) || 0));
|
||
const latest = runs[runs.length - 1];
|
||
const active = runs.some(run => ["queued", "running", "scheduled_retry"].includes(run.status));
|
||
const succeededLast = latest?.status === "succeeded" && !active;
|
||
const notice = succeededLast ? failureNotice(sample) : null;
|
||
if (!notice) { staleFrom = null; continue; }
|
||
staleFrom ??= sample.at;
|
||
if (sample.at - staleFrom > worst) { worst = sample.at - staleFrom; what = `${notice} after ${latest!.id} succeeded (status ${sample.issueStatus ?? "unknown"})`; }
|
||
}
|
||
// A notice still shown at rest is stale however briefly it was observed.
|
||
const last = [...record.samples].reverse().find(ready);
|
||
const atRest = record.restAt !== null && staleFrom !== null && last !== undefined && failureNotice(last) !== null;
|
||
if (worst > bounds.staleNoticeMs || atRest) return { verdict: "fail", metrics: { staleNoticeMs: worst }, detail: `stale_notice ${what} for ${seconds(worst)}${atRest ? ", still shown at rest" : ""}` };
|
||
return { verdict: "pass", metrics: { staleNoticeMs: worst }, detail: worst ? `notice cleared ${seconds(worst)} after success` : "no notice after success" };
|
||
}
|
||
|
||
export function gradeU5(record: TaskRecord, bounds: UniversalBounds): Omit<UniversalResult, "enforced"> {
|
||
const runs = finalRuns(record).size;
|
||
return runs > bounds.maxRuns
|
||
? { verdict: "fail", metrics: { runs }, detail: `run_budget_exceeded ${runs} runs > ${bounds.maxRuns}` }
|
||
: { verdict: "pass", metrics: { runs }, detail: `${runs} runs` };
|
||
}
|
||
|
||
export function gradeUniversal(record: TaskRecord, options: { bounds?: UniversalBounds; db?: UniversalDbEvidence;
|
||
enforced?: ReadonlySet<UniversalId>; slotFree?: SlotFree } = {}): UniversalGrade {
|
||
const bounds = options.bounds ?? universalBounds();
|
||
const enforced = options.enforced ?? ENFORCED_UNIVERSAL;
|
||
const results = {
|
||
U1: gradeU1(record, bounds, options.slotFree),
|
||
U2: gradeU2(record, bounds),
|
||
U3: gradeU3(record, options.db),
|
||
U4: gradeU4(record, bounds),
|
||
U5: gradeU5(record, bounds),
|
||
};
|
||
return Object.fromEntries(Object.entries(results).map(([id, result]) => [id, { ...result, enforced: enforced.has(id as UniversalId) }])) as UniversalGrade;
|
||
}
|
||
|
||
/** Violations that fail the task (enforced rules only). */
|
||
export const enforcedUniversalFailures = (grade: UniversalGrade) =>
|
||
(Object.entries(grade) as Array<[UniversalId, UniversalResult]>).filter(([, result]) => result.enforced && result.verdict === "fail");
|
||
/** Violations reported as warnings. */
|
||
export const universalWarnings = (grade: UniversalGrade) =>
|
||
(Object.entries(grade) as Array<[UniversalId, UniversalResult]>).filter(([, result]) => !result.enforced && result.verdict === "fail");
|