Files
PaperClipAI/tests/restart-chaos/wake-evidence.ts
DottaandClaude Opus 5.5 554e6d415c fix(restart-chaos): credit a follow-up only to a run that started a provider turn, and post a missed follow-up after the run ends (#21692)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The restart-chaos harness runs staging-mix waves across server swaps
and grades each task for duplicate side effects, extra turns and
follow-up delivery
> - In one wave, the grader reported two false failures
> - Task t005: the grader credited a follow-up to a run that failed
before its provider turn. It then counted the run that really served the
follow-up as a duplicate completion and an extra turn
> - Task t009: the agent finished before the follow-up window opened.
The harness never posted the follow-up, but the grader still required
the post
> - False failures hide real regressions, so the grader must match what
the product did
> - This pull request credits a follow-up only to a run that started a
provider turn, and posts a missed follow-up after the run ends
> - The benefit is that both false failures go away, and the t009 case
still tests the product: the product must answer the late follow-up

## Linked Issues or Issue Description

Refs #21598

**What happened?**
In wave `w-20261114T231451-s7110`, two tasks failed on harness defects,
not product defects.

- t005 (`T:completion_once`, a duplicate side effect and an extra turn):
run 76f30f70 was woken for follow-up comment 3f6a9eb9. It failed before
any provider turn, so the agent never received the comment. Recovery run
fdf1f793 served the comment. It added the FOLLOWUP line and did not post
DONE again (DONE comments = 1). The grader credited 76f30f70 with the
answer and flagged fdf1f793 as a second completion and an extra turn.
- t009 (`T:follow_up_posted`): the follow-up was due at 23:25:19. The
harness posts only during a swap overlap (opened 23:28:02.7) or at the
85% point (about 23:29:15). The agent finished at 23:27:56.0, so the
harness never posted. The grader still required the post.

**Expected behavior**
- t005 grades one completer (60bdd4dc) and no extra turn. fdf1f793 is
the run that answered the follow-up.
- t009 gets its follow-up after the run ends. The grade then requires
the product to answer it.

**Steps to reproduce**
1. Build wake evidence with three runs: a done run, a run woken by a
follow-up comment that has no provider turn, and a recovery run that
re-completes.
2. Grade with `gradeStagingMix`, `completedAtAnchor` and
`runsAfterDone`.
3. Drive `stagingMixStep` with the t009 timings: the run ends before the
overlap and before the 85% point.

## What Changed

- `tests/restart-chaos/wake-evidence.ts`: record `providerTurnStarted`
for each run. It is true when the run has a `turn.submitted`,
`turn.accepted` or `turn.started` run event.
- `tests/restart-chaos/follow-up-delivery.ts`: a run with
`providerTurnStarted: false` does not receive its comments. The comments
carry to the next run that starts a provider turn. That run is the
answering run. Add `undeliveredFollowUpRuns`. Evidence without the field
keeps the old behavior.
- `tests/restart-chaos/staging-mix-retry.ts`: `runsAfterDone` does not
list a run that failed before it could deliver a follow-up as an extra
turn.
- `tests/restart-chaos/staging-mix.ts`: when no run is active after the
hold or the 85% point, `stagingMixStep` posts the follow-up after the
run ended. It records the post as an after-end follow-up, so
`after_end_follow_ups_served` requires an agent reply and the ledger
line. The `follow_up_posted` detail says "after the run ended".
- `tests/restart-chaos/observer.ts`: a staging-mix task with an unposted
follow-up does not finish yet.
- Tests and `tests/restart-chaos/README.md`.

## Verification

- Red on master, green with the fix:
- `a follow-up whose run failed before any provider turn
(w-20261114T231451-s7110 t005) > credits the follow-up to the run that
served it, with one completer and no extra turn`
- `... > lets only the serving run re-complete, and still fails a later
recovery run that answers nothing`
- `staging-mix board step > posts a follow-up the run finished before as
an after-end follow-up the product must answer (w-20261114T231451-s7110
t009)`
- `staging-mix board step > does not post an after-end follow-up between
runs before the during-run window closes, or while a run is queued`
- `TaskObserver > does not finish a staging-mix task whose run ended
before its planned follow-up was posted`
- Guards that still grade as failures on master and with the fix:
- `... t005 > still fails a recovery re-completion when the run that
received the follow-up started its provider turn`
- `a done the server rejected as incomplete > keeps a run woken by
recovery after a real completion as an extra turn and a duplicate
completion (G5c t004)` (#21598)
- `completion_once truth table: a later completion that answers nothing`
- `wake evidence against real PostgreSQL` now checks
`providerTurnStarted` false, then true after a `turn.started` event.
- `npx vitest run --config tests/restart-chaos/vitest.config.ts
--maxWorkers=3`: 71 files, 823 tests pass.
- `tsc -p tests/restart-chaos/tsconfig.json --noEmit`: clean.
- Not run: a new wave. The wave's database is gone, so the real t005
evidence cannot get `providerTurnStarted` from the new query. The tests
use the real t005 and t009 values.

## Risks

- Low risk. The change affects only the restart-chaos harness.
- A run that failed after `turn.submitted` but before the provider read
the input counts as a delivery. This keeps the grader strict: it can
report a false duplicate, but it cannot hide a real one.
- A staging-mix task with a planned follow-up that cannot be posted now
waits until its deadline instead of resting. The grade still reports the
post errors.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code with
tool use and code execution.

## 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>
2026-11-14 21:12:52 -06:00

200 lines
14 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import type { ServerProcessWindow } from "./multi-server.js";
import { unshiftJson, unshiftMs, type AgingShift } from "./time-faults.js";
import { ISSUE_STATUSES } from "../../packages/shared/src/constants.js";
import type { CompletionEvidence } from "./staging-mix.js";
import { laterAnswers, rejectedDoneRuns, type DeliveryRun, type DeliveryWake } from "./follow-up-delivery.js";
import { AGENT_SLOTS_FILE, agentSlotsFromRows } from "./agent-slots.js";
import { agents, createDb, type Db } from "../../packages/db/src/index.js";
import { maxConcurrentRunsSql } from "../../server/src/services/heartbeat/timer-policy.js";
type Row = Record<string, unknown>;
export interface ControllerRow { bootId: string; epoch: number; processSpace: string; pid: number; startedAt: number; endedAt: number | null }
export interface ServingServer { pid: number; bootId: string | null; epoch: number | null; processSpace: string | null; harnessRole: string | null }
const ms = (value: unknown) => value === null || value === undefined ? null : Number(value);
const text = (value: unknown) => value === null || value === undefined ? null : String(value);
export const PROCESS_MATCH_SLACK_MS = 60_000;
const STATUSES: ReadonlySet<string> = new Set(ISSUE_STATUSES);
export function isIssueStatusTransition(row: Row): boolean {
return String(row.action ?? "").startsWith("issue.") && STATUSES.has(String(row.toStatus ?? "")) && (row.fromStatus == null || STATUSES.has(String(row.fromStatus)));
}
export function boundByProcessExit(controllers: readonly ControllerRow[], windows: readonly ServerProcessWindow[]): ControllerRow[] {
return controllers.map(controller => {
const window = windows.filter(candidate => candidate.pid === controller.pid && candidate.spawnedAt <= controller.startedAt + PROCESS_MATCH_SLACK_MS
&& (candidate.exitedAt === null || candidate.exitedAt >= controller.startedAt - PROCESS_MATCH_SLACK_MS))
.sort((a, b) => b.spawnedAt - a.spawnedAt)[0];
if (!window || window.exitedAt === null) return controller;
return { ...controller, endedAt: controller.endedAt === null ? window.exitedAt : Math.min(controller.endedAt, window.exitedAt) };
});
}
export function unshiftRows(rows: readonly Row[], timeKeys: readonly string[], jsonKeys: readonly string[], shifts: readonly AgingShift[]): Row[] {
if (!shifts.length) return [...rows];
return rows.map(row => {
const copy: Row = { ...row };
for (const key of timeKeys) if (copy[key] !== null && copy[key] !== undefined) copy[key] = unshiftMs(Number(copy[key]), shifts);
for (const key of jsonKeys) if (copy[key] !== null && copy[key] !== undefined) copy[key] = unshiftJson(copy[key], shifts);
return copy;
});
}
export function servingAt(at: number | null, controllers: readonly ControllerRow[], windows: readonly ServerProcessWindow[]): ServingServer[] {
if (at === null) return [];
const alive = controllers.filter(controller => controller.startedAt <= at && (controller.endedAt === null || controller.endedAt >= at));
const fromDb = alive.map(controller => ({ pid: controller.pid, bootId: controller.bootId, epoch: controller.epoch, processSpace: controller.processSpace,
harnessRole: windows.find(window => window.pid === controller.pid && window.spawnedAt <= at && (window.exitedAt === null || window.exitedAt >= at))?.role ?? null }));
const known = new Set(fromDb.map(server => server.pid));
const fromHarness = windows.filter(window => !known.has(window.pid) && window.spawnedAt <= at && (window.exitedAt === null || window.exitedAt >= at))
.map(window => ({ pid: window.pid, bootId: null, epoch: null, processSpace: null, harnessRole: window.role }));
return [...fromDb, ...fromHarness];
}
export interface WakeEvidenceInput {
runs: readonly Row[];
wakes: readonly Row[];
statusChanges: readonly Row[];
controllers: readonly ControllerRow[];
windows: readonly ServerProcessWindow[];
issues?: readonly Row[];
agents?: readonly Row[];
}
export function wakeEvidenceForIssue(issueId: string, input: WakeEvidenceInput) {
const runs = input.runs.filter(run => run.issueId === issueId);
const wakeIds = new Set(runs.map(run => text(run.wakeupRequestId)).filter(Boolean));
const wakes = input.wakes.filter(wake => wake.issueId === issueId || wakeIds.has(text(wake.id)));
const controllers = boundByProcessExit(input.controllers, input.windows);
const controllerByBoot = new Map(controllers.map(controller => [controller.bootId, controller]));
return {
issueId,
completedAt: ms(input.issues?.find(issue => issue.id === issueId)?.completedAt),
runs: runs.map(run => {
const owner = controllerByBoot.get(String(run.controllerBootId ?? ""));
return { runId: text(run.id), status: text(run.status), invocationSource: text(run.invocationSource), triggerDetail: text(run.triggerDetail),
wakeupRequestId: text(run.wakeupRequestId), createdAt: ms(run.createdAt), startedAt: ms(run.startedAt), finishedAt: ms(run.finishedAt),
acceptedDisposition: text(run.acceptedDisposition), acceptedAt: ms(run.acceptedAt), contextSnapshot: run.contextSnapshot ?? null,
providerTurnStarted: run.providerTurnStarted == null ? null : Boolean(run.providerTurnStarted), owner: owner ? { pid: owner.pid, bootId: owner.bootId, epoch: owner.epoch, processSpace: owner.processSpace } : null,
servingAtCreate: servingAt(ms(run.createdAt), controllers, input.windows) };
}),
wakes: wakes.map(wake => ({ id: text(wake.id), reason: text(wake.reason), source: text(wake.source), triggerDetail: text(wake.triggerDetail),
requestedByActorType: text(wake.requestedByActorType), requestedByActorId: text(wake.requestedByActorId), idempotencyKey: text(wake.idempotencyKey),
status: text(wake.status), runId: text(wake.runId), payload: wake.payload ?? null, error: text(wake.error),
createdAt: ms(wake.createdAt), requestedAt: ms(wake.requestedAt), claimedAt: ms(wake.claimedAt), finishedAt: ms(wake.finishedAt),
servingAtCreate: servingAt(ms(wake.createdAt), controllers, input.windows) })),
statusChanges: input.statusChanges.filter(change => change.issueId === issueId && isIssueStatusTransition(change)).map(change => ({ at: ms(change.createdAt), action: text(change.action),
actorType: text(change.actorType), actorId: text(change.actorId), runId: text(change.runId), from: text(change.fromStatus), to: text(change.toStatus) })),
};
}
async function readRows(db: Pick<Db, "$client" | "select">, companyId: string, shifts: readonly AgingShift[]): Promise<Omit<WakeEvidenceInput, "windows">> {
const client = db.$client;
const runs = await client.unsafe(
`select r.id::text as id, r.agent_id::text as "agentId", r.issue_id::text as "issueId", r.status, r.invocation_source as "invocationSource", r.trigger_detail as "triggerDetail",
r.wakeup_request_id::text as "wakeupRequestId", r.context_snapshot as "contextSnapshot", r.controller_boot_id::text as "controllerBootId",
floor(extract(epoch from r.created_at) * 1000) as "createdAt", floor(extract(epoch from r.started_at) * 1000) as "startedAt",
floor(extract(epoch from r.finished_at) * 1000) as "finishedAt", accepted.disposition as "acceptedDisposition",
floor(extract(epoch from accepted.accepted_at) * 1000) as "acceptedAt",
exists (select 1 from heartbeat_run_events t where t.company_id = r.company_id and t.run_id = r.id
and t.event_type in ('turn.submitted', 'turn.accepted', 'turn.started')) as "providerTurnStarted"
from heartbeat_runs r
left join lateral (select coalesce(e.payload->'prpEvent'->'payload'->'result'->>'reportedWorkDisposition', e.payload->'result'->>'reportedWorkDisposition') as disposition, e.created_at as accepted_at
from heartbeat_run_events e where e.run_id = r.id and e.event_type = 'run.result.accepted'
order by e.seq desc limit 1) accepted on true
where r.company_id::text = $1 order by r.created_at`, [companyId]) as Row[];
const issues = await client.unsafe(
`select id::text as id, floor(extract(epoch from completed_at) * 1000) as "completedAt" from issues where company_id::text = $1`, [companyId]) as Row[];
const wakes = await client.unsafe(
`select id::text as id, payload->>'issueId' as "issueId", source, trigger_detail as "triggerDetail", reason, payload, status,
requested_by_actor_type as "requestedByActorType", requested_by_actor_id as "requestedByActorId", idempotency_key as "idempotencyKey",
run_id::text as "runId", error, floor(extract(epoch from created_at) * 1000) as "createdAt",
floor(extract(epoch from requested_at) * 1000) as "requestedAt", floor(extract(epoch from claimed_at) * 1000) as "claimedAt",
floor(extract(epoch from finished_at) * 1000) as "finishedAt"
from agent_wakeup_requests where company_id::text = $1 order by created_at`, [companyId]) as Row[];
const statusChanges = await client.unsafe(
`select entity_id as "issueId", action, actor_type as "actorType", actor_id as "actorId", run_id::text as "runId",
coalesce(details->'_previous'->>'status', details->>'previousStatus') as "fromStatus", details->>'status' as "toStatus",
floor(extract(epoch from created_at) * 1000) as "createdAt"
from activity_log where company_id::text = $1 and entity_type = 'issue' and action like 'issue.%'
and details ? 'status' order by created_at`, [companyId]) as Row[];
const agentRows = (await db.select({ id: agents.id, companyId: agents.companyId, maxConcurrentRuns: maxConcurrentRunsSql() }).from(agents))
.filter(agent => agent.companyId === companyId) as Row[];
const controllers = (await client.unsafe(
`select boot_id as "bootId", epoch, process_space as "processSpace", pid, floor(extract(epoch from started_at) * 1000) as "startedAt",
floor(extract(epoch from coalesce(stopped_at, fenced_at)) * 1000) as "endedAt" from controller_instances order by epoch`) as Row[])
.map(row => ({ bootId: String(row.bootId), epoch: Number(row.epoch), processSpace: String(row.processSpace), pid: Number(row.pid),
startedAt: Number(row.startedAt), endedAt: ms(row.endedAt) }));
return {
runs: unshiftRows(runs, ["createdAt", "startedAt", "finishedAt", "acceptedAt"], ["contextSnapshot"], shifts),
issues: unshiftRows(issues, ["completedAt"], [], shifts),
agents: agentRows,
wakes: unshiftRows(wakes, ["createdAt", "requestedAt", "claimedAt", "finishedAt"], ["payload"], shifts),
statusChanges: unshiftRows(statusChanges, ["createdAt"], [], shifts),
controllers: controllers.map(controller => ({ ...controller, startedAt: unshiftMs(controller.startedAt, shifts),
endedAt: controller.endedAt === null ? null : unshiftMs(controller.endedAt, shifts) })),
};
}
export async function captureWakeEvidence(input: { db: Pick<Db, "$client" | "select">; companyId: string; waveDir: string;
observers: ReadonlyArray<EvidenceObserver> | null; windows: readonly ServerProcessWindow[];
shifts?: readonly AgingShift[]; redact: (text: string) => string }): Promise<number> {
const rows = await readRows(input.db, input.companyId, input.shifts ?? []);
const observers: readonly EvidenceObserver[] = input.observers ?? [...new Set(rows.runs.map(run => text(run.issueId)).filter((id): id is string => id !== null))]
.map(issueId => ({ plan: { taskId: `issue-${issueId}` }, record: { issueId } }));
const directory = path.join(input.waveDir, "wake-evidence");
await mkdir(directory, { recursive: true, mode: 0o700 });
let written = 0;
for (const observer of observers) {
if (!observer.record.issueId) continue;
const evidence = { schema: "paperclip.restart-chaos.wake-evidence/v1", taskId: observer.plan.taskId,
...wakeEvidenceForIssue(observer.record.issueId, { ...rows, windows: input.windows }) };
if (observer.plan.stagingMix) (observer.record.stagingMix ??= {}).completion = completionEvidence(evidence);
await writeFile(path.join(directory, `${observer.plan.taskId}.json`), input.redact(`${JSON.stringify(evidence, null, 2)}\n`), { mode: 0o600 });
written += 1;
}
await writeFile(path.join(directory, "servers.json"), `${JSON.stringify({ controllers: rows.controllers, harnessWindows: input.windows }, null, 2)}\n`, { mode: 0o600 });
await writeFile(path.join(directory, AGENT_SLOTS_FILE), `${JSON.stringify(agentSlotsFromRows(rows.agents ?? [], rows.runs), null, 2)}\n`, { mode: 0o600 });
return written;
}
export interface EvidenceObserver {
plan: { taskId: string; stagingMix?: unknown };
record: { issueId: string | null; stagingMix?: { completion?: CompletionEvidence } };
}
export function completionEvidence(evidence: { completedAt: number | null; wakes?: readonly DeliveryWake[];
runs: ReadonlyArray<DeliveryRun & { acceptedDisposition: string | null; acceptedAt: number | null }> }): CompletionEvidence {
const answers = laterAnswers(evidence);
const rejected = rejectedDoneRuns(evidence);
return { completedAt: evidence.completedAt, runs: evidence.runs.map(run => ({ runId: run.runId, startedAt: run.startedAt ?? null,
acceptedDisposition: run.acceptedDisposition, acceptedAt: run.acceptedAt, answersFollowUp: Boolean(run.runId && answers.has(run.runId)),
doneRejected: Boolean(run.runId && rejected.has(run.runId)) })) };
}
export function wakeEvidenceOnce(input: { databaseUrl: () => string; companyId: string; waveDir: string;
observers: () => ReadonlyArray<EvidenceObserver>; windows: readonly ServerProcessWindow[];
shifts: readonly AgingShift[]; redact: (text: string) => string; note: (kind: string, data: Record<string, unknown>) => void;
open?: (url: string) => Pick<Db, "$client" | "select"> }): () => Promise<void> {
let done: Promise<void> | null = null;
return () => done ??= (async () => {
let observers: ReturnType<typeof input.observers> | null = null;
try { observers = input.observers(); } catch { observers = null; }
const db = (input.open ?? createDb)(input.databaseUrl());
try {
const written = await captureWakeEvidence({ db, companyId: input.companyId, waveDir: input.waveDir, observers, windows: input.windows,
shifts: input.shifts, redact: input.redact });
input.note("wake_evidence_captured", { written, byTask: observers !== null });
} catch (error) {
input.note("wake_evidence_failed", { error: String(error) });
} finally {
await db.$client.end({ timeout: 5 }).catch(() => undefined);
}
})();
}