Files
t3code/apps/server/src/monitor/readiness.test.ts
T
Theo BrowneandClaude Fable 5 0c1de35f5a Harden PR monitoring after adversarial review
Findings from a multi-agent review panel (23 confirmed after two-lens
adversarial verification), grouped by root cause:

Lifecycle identity
- Thread a monitor generation through monitor commands, events, and the
  projected monitor state; reject updates/ends whose generation is not
  the active one, and make every registry removal generation-conditional
  so a delayed lifecycle event cannot delete a newer monitor
- Re-validate registration, projected monitor status, archive state,
  generation, and claim at the wake send boundary (after the fresh fetch,
  not before it)

Event loss
- Diff before the readiness exit: actionable activity now wins over
  ending as ready, so a bot comment landing in the same poll as green CI
  is never discarded
- Persist non-actionable cursor transitions so resolved→reopened is
  still detectable
- Acknowledge the cursor only if the wake claim still exists, so a
  user-turn release is not overwritten

Readiness
- Collapse reviews to each reviewer's latest opinionated review, so an
  approval supersedes that reviewer's earlier changes-requested
- Re-include pre-monitor review threads once they are edited or reopened

Snapshot integrity
- Final terminal/head revalidation after pagination, with one bounded
  refetch on head change
- Fetch the last 10 comments per review thread and track per-thread
  updatedAt so edits to non-final comments are visible
- Reopened threads wake regardless of self-authorship
- Include the changes-requested review body in the wake prompt

Lifecycle events
- Archive atomically emits thread.monitor-ended before thread.archived,
  mirroring settle
- Treat a queued-but-unadopted user turn as busy at the send boundary
- Boot reconciliation isolates per-thread failures (Bugbot)

UI
- Gate ready-state PR actions on the checkout PR matching the monitored
  PR number
- Wake snoozed threads on monitor ready/needs-attention/session-ended
- Legacy sidebar renders the ready payoff and a needs-attention state;
  session failure outranks Monitoring
- Reset merge confirmation when PR or monitor status changes
- endThreadMonitor honors a caller-provided endedAt

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-26 19:39:43 -07:00

142 lines
4.2 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest";
import type { PullRequestMonitorSnapshot } from "../sourceControl/gitHubPullRequestMonitor.ts";
import { computeReadiness } from "./readiness.ts";
const snapshot = (
overrides: Partial<PullRequestMonitorSnapshot> = {},
): PullRequestMonitorSnapshot => ({
state: "open",
draft: false,
headSha: "head-2",
baseRefName: "main",
mergeability: "mergeable",
behindBaseBy: null,
requiredChecksKnown: true,
reviews: [],
reviewThreads: [],
issueComments: [],
checkRuns: [
{
id: "run-1",
name: "CI",
status: "completed",
conclusion: "success",
startedAt: null,
headSha: "head-2",
},
],
...overrides,
});
describe("pull request monitor readiness", () => {
it("keeps blocking on a changes-requested review even after a push", () => {
// A stale change request must hold the gate until the bot re-reviews;
// otherwise every fix push would flash green before re-review lands.
const result = computeReadiness(
snapshot({
reviews: [
{
id: "review-1",
author: { login: "review-bot", type: "app" },
state: "changes-requested",
submittedAt: "2026-01-01T00:00:00Z",
commitSha: "head-1",
body: "",
},
],
}),
);
assert.strictEqual(result.ready, false);
assert.deepStrictEqual(result.blockers, [
{ kind: "changes-requested", reviewer: "review-bot" },
]);
});
it("blocks on a human changes-requested review", () => {
const result = computeReadiness(
snapshot({
reviews: [
{
id: "review-human",
author: { login: "human-reviewer", type: "user" },
state: "changes-requested",
submittedAt: "2026-01-01T00:00:00Z",
commitSha: "head-2",
body: "",
},
],
}),
);
assert.deepStrictEqual(result.blockers, [
{ kind: "changes-requested", reviewer: "human-reviewer" },
]);
});
it("ignores stale approvals after a push", () => {
const result = computeReadiness(
snapshot({
reviews: [
{
id: "review-1",
author: { login: "review-bot", type: "app" },
state: "approved",
submittedAt: "2026-01-01T00:00:00Z",
commitSha: "head-1",
body: "",
},
],
}),
);
// A stale approval neither blocks nor counts as fresh green evidence.
assert.strictEqual(result.ready, true);
});
it("unblocks a changes request superseded by the same reviewer's approval", () => {
const result = computeReadiness(
snapshot({
reviews: [
{
id: "changes",
author: { login: "reviewer", type: "user" },
state: "changes-requested",
submittedAt: "2026-01-01T00:00:00Z",
commitSha: "head-1",
body: "Please fix this",
},
{
id: "approval",
author: { login: "reviewer", type: "user" },
state: "approved",
submittedAt: "2026-01-02T00:00:00Z",
commitSha: "head-2",
body: "",
},
],
}),
);
assert.strictEqual(result.ready, true);
});
it("does not report ready when no check runs exist", () => {
const result = computeReadiness(snapshot({ checkRuns: [] }));
assert.strictEqual(result.ready, false);
assert.strictEqual(result.label, "no-known-blockers");
assert.deepStrictEqual(result.blockers, [{ kind: "checks-missing" }]);
});
it("returns terminal blockers for merged and closed snapshots", () => {
for (const state of ["merged", "closed"] as const) {
const result = computeReadiness(snapshot({ state }));
assert.strictEqual(result.ready, false);
assert.deepStrictEqual(result.blockers[0], { kind: "terminal", state });
}
});
it("uses the honest fallback label when required-check evidence is unavailable", () => {
const result = computeReadiness(snapshot({ requiredChecksKnown: false }));
assert.strictEqual(result.ready, true);
assert.strictEqual(result.label, "no-known-blockers");
});
});