mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 14:35:17 +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 agent
tasks survive control-plane restarts
> - Staging takes a release about every 40 minutes as an overlap swap: a
new server starts beside the old one on the same database, both run for
8–9 minutes, then the old one gets SIGTERM
> - On 2026-11-12 staging failed or interrupted 577 of 1,000 runs across
these swaps, but the harness restarts one server at a time, grades each
task once and never repeats the swap
> - This pull request adds the release swap cell RS1: repeated overlap
swaps on one database, with the outgoing server starting new runs during
each overlap, and one end-of-wave grade that reads each task the way the
board shows it
> - The benefit is one number per wave, tasks completed without human
action over tasks started, that measures what staging users see
## Linked Issues or Issue Description
**Area**
Restart chaos harness (`tests/restart-chaos/`), test-only.
**Problem**
The harness did not reproduce the staging release shape. It had no
repeated swaps, no run started by the outgoing server during the
overlap, and no grade of every task at the end of a multi-swap wave.
**Proposed solution**
Add the `--release-swap` cell (RS1). It schedules K15 rolling redeploys
on a fixed clock, drives probe tasks through the outgoing server,
records per-swap evidence, and grades every task once after a settle
period.
**Why it matters**
The release cadence will not slow down. The harness must catch the
failures that the swap causes before staging does.
## What Changed
- `release-swap-schedule.ts`: the `--release-swap` options and the
`--release-swap-short` preset (3 swaps, 12 min apart, 510 s overlap)
(count 6, spacing 40 min, overlap 510 s, settle 15 min), their expansion
into a K15 overlap storm (adds `--k15-fresh-home --ephemeral-server` on
Daytona), the swap plan and the `ReleaseSwapRun` clock (swap k at k ×
spacing, never while a swap is unsettled; tasks launch until the last
swap settled).
- `release-swap-driver.ts`: the per-swap driver. It creates probe tasks
through the outgoing server's own port when the incoming server spawns,
when it enters startup recovery, and so that a run starts about 20 s
before the outgoing server's SIGTERM. It samples runs and their owning
controller PID every 2 s and records which server started each run. It
has a per-swap plug-in list; a plug-in can choose the checkout the
incoming server runs (for the later mixed-version variant).
- `release-swap-grade.ts`: the pure grade. One verdict per task (`pass`,
`needed_retry` with whether the click worked, `open_no_owner`,
`blocked_non_person`, `waiting_on_person`, `still_working`), the
per-swap numbers (failed runs, runs lost while the old server was alive,
time from SIGTERM to each surviving run's next event), duplicate runs
and duplicated side effects, the one-number summary, and one failure
label per failing task (`nothing pending`, `blocked for a non-person
reason`, `needed Retry` with the click result).
- `release-swap-finish.ts`: the end-of-wave collection. It waits for the
settle period, reads every task with a run from the API and the wave
database, clicks Retry where the board offers it (board UI when served,
else the same API call as the observer), waits again, and writes
`release-swap.json` and `release-swap-summary.md`.
- Small hooks in shared files: `supervisor.ts` calls `swapHook` at five
phases of the rolling redeploy and lets the hook choose the next server
root (the file stays at 1,000 lines); `config.ts`, `scheduler.ts`,
`types.ts` and `main.ts` wire the option, the plan, the clock, launches
during the overlap and the final grade.
- README section for the cell, its options and its grading.
- Unit tests for the scheduler, the option parsing, the grade
classifier, the per-swap numbers and the overlap evidence.
## Verification
- `pnpm exec vitest run --config tests/restart-chaos/vitest.config.ts`
(58 files, 704 tests pass, after merging master).
- `node scripts/typecheck-test-projects.mjs tests/restart-chaos` passes.
- Local smokes through the wave queue (`--env local --adapters opencode
--workloads A1,T1 --release-swap --release-swap-count 2
--release-swap-spacing-minutes 5 --release-swap-overlap-seconds 90
--release-swap-settle-minutes 2 --concurrency 2 --tasks 40`):
- `w-20261112T151735-s7101` (first head): 10 / 16 tasks completed
without human action; 26 min wall. It showed that native runs had no
owner in the sampler (fixed here).
- `w-20261112T154840-s7102` (owner fix): 8 / 13 tasks completed without
human action; 24 min wall. The late probe's run was started by the
outgoing server 20.0 s before its SIGTERM.
- Both smokes caught the same product failure: runs the outgoing server
hands off at shutdown (`restartSuspendedRunIds`) are classified
`awaitingEvidence` by the incoming server's startup recovery, even while
the outgoing server is still alive, and are never settled (`I2_settled`
reconciliation violations). They stay `running` with no process, and the
runs queued behind them are never dispatched. This PR does not fix it
(harness only).
- `w-20261112T163456-s7103` (probe agents, drain): 28 / 29 completed;
the late probe started on the outgoing server 20.0 s and 20.2 s before
SIGTERM in both swaps.
- `w-20261112T171115-s7104`, with 40 preparation probes per swap
overloaded the probe agent (late probe dispatched by the new server 30 s
after SIGTERM); the cap is now 6 per swap.
- CI notes on earlier heads: `ci / e2e shard (1/8)` failed once in
`task-loading-frame` (UI, outside this diff; re-run). `ci / General
tests (server (11/12))` failed in
`src/__tests__/storage-s3-multipart-throttle.test.ts` ("halves new
parts…", expected 3 to be 4), outside this diff; it is a known master
failure being fixed separately and is not touched here.
- Not run: the Daytona smoke. No published runner image matches this
checkout's content ID; an image build for master is queued.
## Risks
- Low risk: test-only harness code. No product code changes.
- Waves without `--release-swap` keep their plans and behavior. The only
shared-path change is the K15 overlap redeploy, which now calls an
optional hook that is unset in other waves.
- The probe tasks and the sampling add a few API calls and one database
query every 2 s during each swap.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits). Extended reasoning on.
## 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>
314 lines
17 KiB
TypeScript
314 lines
17 KiB
TypeScript
import { isProviderFault, withProviderFaultRules } from "./provider-faults.js";
|
||
import { isDiskFullFault } from "./disk-full-faults.js";
|
||
import { isSessionFault } from "./session-faults.js";
|
||
import { takesJourneyTriggers } from "./journeys.js";
|
||
import { workloadDefinitions } from "./workloads.js";
|
||
import { isTimeFault, timeFaultHoldMs } from "./time-faults-config.js";
|
||
import { isMultiServerFault, multiServerHoldMs } from "./multi-server.js";
|
||
import { isRepeatedRestartFault, planRestartSteps, type RestartStep } from "./repeated-restarts.js";
|
||
import { k15HoldMs } from "./redeploy.js";
|
||
import { planReleaseSwaps } from "./release-swap-schedule.js";
|
||
import { CR5_FIRE_WINDOW_MS, classRHoldMs, isClassRFault } from "./class-r.js";
|
||
import { type AdapterId, type FaultKind, type ScheduledFault, type StormFault, type StormPlan, type TaskFaultKind, type TaskPlan, type TriggerId, type WavePlan, type WaveOptions, type WorkloadId } from "./types.js";
|
||
import type { ReplayRef } from "./config.js";
|
||
import { createRng, k4HoldRange } from "./plan-primitives.js";
|
||
|
||
export { createRng, K4_HOLD_RANGE_MS, k4HoldRange } from "./plan-primitives.js";
|
||
|
||
export const TRIGGER_DELAY_RANGE_MS: Record<Exclude<TriggerId, "time">, readonly [number, number]> = {
|
||
comment: [0, 5_000],
|
||
retry: [0, 2_000],
|
||
queued: [0, 0],
|
||
tool: [15_000, 60_000],
|
||
result: [0, 3_000],
|
||
approval: [0, 3_000],
|
||
paused: [0, 3_000],
|
||
stop: [0, 2_000],
|
||
asked: [0, 3_000],
|
||
answered: [0, 3_000],
|
||
// Not a `--triggers` choice: `--provider-fault-restart-after-seconds` sets its delay.
|
||
provider: [0, 0],
|
||
};
|
||
|
||
export function triggerFitsWorkload(trigger: TriggerId, workload: WorkloadId): boolean {
|
||
if (trigger === "comment" || trigger === "result") return workload === "C1";
|
||
if (trigger === "tool") return workload === "B5";
|
||
if (trigger === "approval") return workload === "G1";
|
||
if (trigger === "paused") return workload === "H1";
|
||
if (trigger === "stop") return workload === "S1";
|
||
if (trigger === "asked" || trigger === "answered") return takesJourneyTriggers(workload);
|
||
return true;
|
||
}
|
||
|
||
/** The workload a trigger needs, for the error when none is selected. */
|
||
const TRIGGER_WORKLOAD: Partial<Record<TriggerId, string>> = { comment: "C1", result: "C1", tool: "B5", approval: "G1", paused: "H1", stop: "S1",
|
||
asked: "Q1, Q2 or Q3", answered: "Q1, Q2 or Q3" };
|
||
|
||
/** Faults a `result` task may fire inside a storm: a fast restart that lands on the boundary. */
|
||
const STORM_RESULT_FAULTS: readonly FaultKind[] = ["K1", "K2"];
|
||
|
||
/**
|
||
* A storm task has no fault of its own (`K13`): the wave's storm crosses it.
|
||
* A `result` task in a storm also fires its own K1 or K2 just after its run
|
||
* accepts a result, the window of RC-30, RC-33 and RC-41.
|
||
*/
|
||
function stormCellFaults(options: Pick<WaveOptions, "faults" | "stormMinutes" | "providerFaults" | "diskFullFaults" | "sessionFaults" | "baseline">, trigger: TriggerId): readonly TaskFaultKind[] {
|
||
if (!options.stormMinutes) return [...options.faults, ...(options.providerFaults ?? []), ...(options.diskFullFaults ?? []), ...(options.sessionFaults ?? []), ...(options.baseline ? ["K0" as const] : [])];
|
||
if (trigger === "time") return ["K13"];
|
||
if (trigger !== "result") throw new Error(`A K13 storm fires on its own clock; --triggers ${trigger} does not combine with it (use time or result)`);
|
||
const faults = options.faults.filter(fault => STORM_RESULT_FAULTS.includes(fault));
|
||
if (!faults.length) throw new Error("--triggers result in a storm needs K1 or K2 in --faults");
|
||
return faults;
|
||
}
|
||
|
||
/**
|
||
* Pairwise-friendly sampling: cycle a seeded shuffle of the full
|
||
* workload × adapter × fault product so small waves still spread across cells.
|
||
*/
|
||
function pinnedFireAfterMs(faultAfterMs: number | undefined, drawn: number): number {
|
||
return faultAfterMs ?? drawn;
|
||
}
|
||
|
||
export function planTasks(options: Pick<WaveOptions, "seed" | "tasks" | "workloads" | "adapters" | "faults" | "triggers" | "stormMinutes" | "k4HoldMaxMs" | "k15HoldMs" | "tm1HoldMs" | "faultAfterMs" | "providerFaults" | "diskFullFaults" | "sessionFaults" | "rrGapMs" | "baseline" | "cr1HoldMs" | "cr2HoldMs" | "cr3StallMs">): TaskPlan[] {
|
||
const rng = createRng(options.seed);
|
||
const triggers = options.triggers?.length ? options.triggers : ["time" as const];
|
||
for (const trigger of triggers) {
|
||
if (!options.workloads.some(workload => triggerFitsWorkload(trigger, workload))) {
|
||
throw new Error(`Trigger "${trigger}" needs workload ${TRIGGER_WORKLOAD[trigger] ?? "?"} in --workloads`);
|
||
}
|
||
}
|
||
if (options.faults.includes("CR5") && !triggers.includes("time")) {
|
||
throw new Error("CR5 stalls the plugin at launch on the time trigger; add time to --triggers");
|
||
}
|
||
const cells: Array<{ workload: WorkloadId; adapter: AdapterId; fault: TaskFaultKind; trigger: TriggerId }> = [];
|
||
// Same cell order as before storms took triggers, so a seed keeps its plan.
|
||
const allowed = new Map(triggers.map(trigger => [trigger, stormCellFaults(options, trigger)]));
|
||
const faults = [...new Set([...allowed.values()].flat())];
|
||
for (const workload of options.workloads) for (const adapter of options.adapters) for (const fault of faults) {
|
||
for (const trigger of triggers) {
|
||
if (fault === "CR5" && trigger !== "time") continue;
|
||
if (triggerFitsWorkload(trigger, workload) && allowed.get(trigger)!.includes(fault)) cells.push({ workload, adapter, fault, trigger });
|
||
}
|
||
}
|
||
const tasks: TaskPlan[] = [];
|
||
let order = rng.shuffle(cells);
|
||
for (let index = 0; index < options.tasks; index += 1) {
|
||
if (index > 0 && index % cells.length === 0) order = rng.shuffle(cells);
|
||
const cell = order[index % cells.length]!;
|
||
const definition = workloadDefinitions[cell.workload];
|
||
const [fireMin, fireMax] = definition.fireWindowMs;
|
||
const { trigger, ...cellRest } = cell;
|
||
tasks.push({
|
||
taskId: `t${String(index + 1).padStart(2, "0")}`,
|
||
index,
|
||
...cellRest,
|
||
...(trigger === "time" ? {} : { trigger, triggerDelayMs: rng.int(...TRIGGER_DELAY_RANGE_MS[trigger]) }),
|
||
fireAfterMs: cell.fault === "K13" || cell.fault === "K0" ? 0 : cell.fault === "CR5" ? rng.int(...CR5_FIRE_WINDOW_MS) : pinnedFireAfterMs(options.faultAfterMs, rng.int(fireMin, fireMax)),
|
||
holdMs: cell.fault === "K4" ? rng.int(...k4HoldRange(options.k4HoldMaxMs)) : isTimeFault(cell.fault) ? timeFaultHoldMs(cell.fault, rng.int, options.tm1HoldMs)
|
||
: isMultiServerFault(cell.fault) ? multiServerHoldMs(cell.fault, rng.int)
|
||
: cell.fault === "K15" ? k15HoldMs(options.k15HoldMs, rng.int)
|
||
: isClassRFault(cell.fault) ? classRHoldMs(cell.fault, options, rng.int) : 0,
|
||
// Drawn only for RR tasks, so the plans of existing seeds do not change.
|
||
...(isRepeatedRestartFault(cell.fault) ? { restartSteps: planRestartSteps(cell.fault, rng.int, options.rrGapMs) } : {}),
|
||
followUpAfterMs: definition.followUpWindowMs ? rng.int(definition.followUpWindowMs[0], definition.followUpWindowMs[1]) : null,
|
||
nonce: rng.hex(8),
|
||
});
|
||
}
|
||
return tasks;
|
||
}
|
||
|
||
function withoutStorm(options: WaveOptions): WaveOptions {
|
||
const { stormMinutes: _storm, ...rest } = options;
|
||
return rest;
|
||
}
|
||
|
||
/** A replay keeps the source wave's masks and may add more; it never drops one. */
|
||
function replayMasks(source: readonly string[] | undefined, added: readonly string[] | undefined) {
|
||
const masks = [...new Set([...(source ?? []), ...(added ?? [])])];
|
||
return masks.length ? { masks } : {};
|
||
}
|
||
|
||
export function planWave(waveId: string, options: WaveOptions, createdAt = new Date()): WavePlan {
|
||
return { schema: "paperclip.restart-chaos.wave-plan/v1", waveId, seed: options.seed,
|
||
createdAt: createdAt.toISOString(), options, tasks: withProviderFaultRules(planTasks(options), options)
|
||
// `--long-turn-minutes` is part of each task's plan, so a replay keeps it.
|
||
.map(task => options.longTurnMinutes ? { ...task, longTurnMinutes: options.longTurnMinutes } : task),
|
||
...(options.stormMinutes ? { storm: options.releaseSwap ? planReleaseSwaps(options.releaseSwap)
|
||
: planStorm(options.seed, options.stormMinutes, options.faults, options.k4HoldMaxMs, options.k15HoldMs) } : {}) };
|
||
}
|
||
|
||
/** K13: the gap between one storm fault settling and the next one firing. */
|
||
export const STORM_GAP_RANGE_MS: readonly [number, number] = [120_000, 240_000];
|
||
|
||
/**
|
||
* K13 restart storm: a seeded sequence of faults drawn from `faults`, each
|
||
* after a 2-4 minute gap, long enough to fill `minutes`. It uses its own RNG
|
||
* stream so adding a storm does not change the task cells for a seed.
|
||
*/
|
||
export function planStorm(seed: number, minutes: number, faults: readonly FaultKind[], k4HoldMaxMs?: number,
|
||
k15HoldRangeMs?: readonly [number, number]): StormPlan {
|
||
if (!faults.length) throw new Error("A K13 storm needs at least one fault kind");
|
||
const rng = createRng((seed ^ 0x6b313353) >>> 0);
|
||
const durationMs = minutes * 60_000;
|
||
const sequence: StormFault[] = [];
|
||
for (let planned = 0; planned < durationMs;) {
|
||
const gapMs = rng.int(...STORM_GAP_RANGE_MS);
|
||
const kind = faults[rng.int(0, faults.length - 1)]!;
|
||
sequence.push({ kind, gapMs, holdMs: kind === "K4" ? rng.int(...k4HoldRange(k4HoldMaxMs))
|
||
: kind === "K15" ? k15HoldMs(k15HoldRangeMs, rng.int) : 0 });
|
||
planned += gapMs;
|
||
}
|
||
return { durationMs, faults: sequence };
|
||
}
|
||
|
||
export interface StormState {
|
||
startedAt: number;
|
||
/** Storm faults fired so far. */
|
||
fired: number;
|
||
/** When the previous storm fault finished (server ready, or the fault failed); null before the first. */
|
||
lastSettledAt: number | null;
|
||
}
|
||
|
||
/** New tasks launch and storm faults fire only while the storm lasts; then it drains. */
|
||
export function stormActive(storm: StormPlan, state: StormState, now: number): boolean {
|
||
return now < state.startedAt + storm.durationMs;
|
||
}
|
||
|
||
/**
|
||
* The storm clock the wave loop consults. Every decision takes a fresh `now`,
|
||
* because the loop awaits slow work (server recovery, launches, pre-fault
|
||
* snapshots) between choosing an action and taking it.
|
||
*/
|
||
export class StormRun {
|
||
readonly state: StormState;
|
||
ended = false;
|
||
|
||
constructor(readonly plan: StormPlan, startedAt: number) {
|
||
this.state = { startedAt, fired: 0, lastSettledAt: null };
|
||
}
|
||
|
||
/** True once, on the transition to the drain. */
|
||
endIfDue(now: number): boolean {
|
||
if (this.ended || stormActive(this.plan, this.state, now)) return false;
|
||
this.ended = true;
|
||
return true;
|
||
}
|
||
|
||
/** Ask immediately before each launch. */
|
||
mayLaunch(now: number): boolean {
|
||
return !this.ended && stormActive(this.plan, this.state, now);
|
||
}
|
||
|
||
/** The next fault, never while the previous one is still unsettled: each gap runs from its settle. */
|
||
due(now: number): StormFault | null {
|
||
return this.ended || this.unsettled ? null : stormFaultDue(this.plan, this.state, now);
|
||
}
|
||
|
||
/** Claim a fault chosen before slow work. False when the storm ended meanwhile; nothing is consumed. */
|
||
commit(fault: StormFault, now: number): boolean {
|
||
if (this.due(now) !== fault) return false;
|
||
this.state.fired += 1;
|
||
this.unsettled = true;
|
||
return true;
|
||
}
|
||
|
||
settled(now: number) { this.state.lastSettledAt = now; this.unsettled = false; }
|
||
|
||
private unsettled = false;
|
||
|
||
/** The task pool ran dry with storm time left: the wave did not cover its storm. */
|
||
exhaustedEarly(now: number): boolean {
|
||
return !this.ended && stormActive(this.plan, this.state, now);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The next storm fault once its gap has passed. Gaps run from the previous
|
||
* fault settling, so a K4 hold never eats the next gap. The caller must not
|
||
* ask while a fault is still in progress.
|
||
*/
|
||
export function stormFaultDue(storm: StormPlan, state: StormState, now: number): StormFault | null {
|
||
if (!stormActive(storm, state, now)) return null;
|
||
const next = storm.faults[state.fired];
|
||
if (!next) return null;
|
||
return now >= (state.lastSettledAt ?? state.startedAt) + next.gapMs ? next : null;
|
||
}
|
||
|
||
/** Written after every fault: each task's launch time and the faults that crossed it so far. */
|
||
export const TASK_FAULT_LOG = "task-faults.json";
|
||
export type TaskFaultLog = Record<string, { createdAt: number | null; faultIds: string[]; serverBuildAtLaunch?: "upgrade_from" | "current" }>;
|
||
|
||
/**
|
||
* A replay of a K14 wave task that launched on the current build: it starts on the current build
|
||
* too (no `--upgrade-from`), and its recorded K14 restarts were current-build restarts (K2).
|
||
*/
|
||
export function replayOnCurrentBuild(plan: WavePlan): WavePlan {
|
||
const asK2 = <T extends { kind?: FaultKind; fault?: TaskFaultKind }>(item: T): T => ({ ...item,
|
||
...(item.kind === "K14" ? { kind: "K2" as const } : {}), ...(item.fault === "K14" ? { fault: "K2" as const } : {}) });
|
||
const { upgradeFrom: _upgradeFrom, ...options } = plan.options;
|
||
return { ...plan, options: { ...options, faults: options.faults.map(kind => kind === "K14" ? "K2" : kind) },
|
||
tasks: plan.tasks.map(task => ({ ...asK2(task), ...(task.replayFaults ? { replayFaults: task.replayFaults.map(asK2) } : {}) })) };
|
||
}
|
||
|
||
/** The crossing record for a replay: the graded record, else the live fault log (interrupted or wedged waves). */
|
||
export function replayRecordFor(taskId: string, graded: { createdAt: number | null; faultIds: string[] } | null,
|
||
logged: TaskFaultLog | null): { createdAt: number | null; faultIds: string[] } | null {
|
||
if (graded?.faultIds.length) return graded;
|
||
return logged?.[taskId] ?? graded;
|
||
}
|
||
|
||
/** Faults that actually crossed a task, as offsets from its launch. */
|
||
export function observedFaultSchedule(createdAt: number | null, faultIds: readonly string[],
|
||
faults: ReadonlyArray<{ faultId: string; kind: FaultKind; holdMs: number; startedAt: number; requestedBy?: string;
|
||
trigger?: TriggerId; triggerDelayMs?: number; repeatedRestart?: { planned: RestartStep[] } }>, taskId?: string): ScheduledFault[] {
|
||
if (createdAt === null) return [];
|
||
return faults.filter(fault => faultIds.includes(fault.faultId))
|
||
.map((fault): ScheduledFault => ({ kind: fault.kind, holdMs: fault.holdMs, atMs: Math.max(0, fault.startedAt - createdAt),
|
||
// RR: the replay restarts again after the same gaps.
|
||
...(fault.repeatedRestart ? { restartSteps: fault.repeatedRestart.planned.map(step => ({ ...step })) } : {}),
|
||
...(fault.trigger && fault.trigger !== "time" && fault.requestedBy === taskId
|
||
? { trigger: fault.trigger, delayMs: fault.triggerDelayMs ?? 0 } : {}) }))
|
||
.sort((a, b) => a.atMs - b.atMs);
|
||
}
|
||
|
||
/** The faults a task schedules: its replayed sequence, or its one planned fault. */
|
||
export function taskFaultSchedule(task: TaskPlan): ScheduledFault[] {
|
||
if (task.replayFaults?.length) return task.replayFaults;
|
||
// A storm crosses K13 tasks; a provider fault (PV*), a disk-full fault (DF1) and a matrix baseline (K0) restart nothing, unless
|
||
// `--provider-fault-restart` asked for a K1 once the provider fault applied (the `provider` trigger).
|
||
const restart = task.providerFault?.restart;
|
||
if (restart) return [{ kind: restart.kind, holdMs: restart.holdMs ?? 0, atMs: 0, trigger: "provider", delayMs: restart.afterAppliedMs }];
|
||
if (task.fault === "K13" || task.fault === "K0" || isProviderFault(task.fault) || isDiskFullFault(task.fault) || isSessionFault(task.fault)) return [];
|
||
const planned: ScheduledFault = { kind: task.fault, holdMs: task.holdMs, atMs: task.fireAfterMs,
|
||
...(task.restartSteps ? { restartSteps: task.restartSteps } : {}) };
|
||
const trigger = task.trigger ?? "time";
|
||
if (trigger === "time") return [planned];
|
||
const delayMs = task.triggerDelayMs ?? 0;
|
||
if (trigger === "retry") return [planned, { kind: "K1", holdMs: 0, atMs: 0, trigger, delayMs }];
|
||
return [{ ...planned, trigger, delayMs }];
|
||
}
|
||
|
||
/**
|
||
* A replay keeps the saved cell, hold and nonce and runs alone. When the source
|
||
* wave recorded which faults crossed the task (including other tasks' faults),
|
||
* the replay fires that same sequence at the same offsets from launch.
|
||
*/
|
||
export function replayWave(source: WavePlan, ref: ReplayRef, now = Date.now(),
|
||
overrides: Partial<Pick<WaveOptions, "pollMs" | "budgetCents" | "masks">> = {}, observed: ScheduledFault[] = []): WavePlan {
|
||
if (source.schema !== "paperclip.restart-chaos.wave-plan/v1") throw new Error("Unsupported wave plan schema");
|
||
if (source.waveId !== ref.waveId) throw new Error(`Plan is for ${source.waveId}, not ${ref.waveId}`);
|
||
const task = source.tasks.find(candidate => candidate.taskId === ref.taskId);
|
||
if (!task) throw new Error(`Wave ${ref.waveId} has no task ${ref.taskId}`);
|
||
const base = source.waveId.replace(/-replay-t[0-9]{2,3}-[0-9]+$/, "");
|
||
return {
|
||
schema: source.schema,
|
||
waveId: `${base}-replay-${task.taskId}-${now}`,
|
||
seed: source.seed,
|
||
createdAt: new Date(now).toISOString(),
|
||
// A replay fires only the faults that crossed the task, never a new storm.
|
||
options: { ...withoutStorm(source.options), ...overrides, tasks: 1, concurrency: 1, ...replayMasks(source.options.masks, overrides.masks) },
|
||
tasks: [{ ...task, ...(observed.length ? { replayFaults: observed.map(fault => ({ ...fault })) } : {}) }],
|
||
replayOf: { ...ref },
|
||
};
|
||
}
|