Files
t3code/apps/server/src/monitor/monitorDiff.test.ts
T
Theo BrowneandClaude Fable 5 e5ea2c65d2 Address review feedback from Bugbot and Macroscope
- Stop prepareMcpSession ending the active monitor on session start
  (session recycles no longer kill their own babysit)
- Fail closed at the wake send boundary: a failed fresh PR fetch discards
  the wake instead of falling back to the stale poll state
- Correlate provider.turn.start.failed releases by dispatch time and
  rewind wakeCount alongside the cursor
- Monotonic generation allocator; generation-conditional
  updateCursor/incrementWake/remove so stale polls can't mutate a
  newer monitor
- Dispatch thread.monitor.end before removing the registration in
  session teardown; end monitors on thread.archived
- Preserve wakeCount on idempotent monitor.start replay (breaker can't
  be reset)
- Key check-run cursors by head SHA + name + run id; detect
  resolved→unresolved thread reopens
- Human changes-requested reviews now block readiness
- Thread monitoringStartedAt from the registration so pre-existing
  unresolved threads don't block readiness forever
- Use the PR's base ref in the wake prompt instead of hard-coded main
- Boot reconcile: threads projecting "monitoring" with no registration
  are ended as session-ended

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

164 lines
5.6 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest";
import type { PullRequestMonitorSnapshot } from "../sourceControl/gitHubPullRequestMonitor.ts";
import { cursorFromSnapshot, diffPullRequestMonitorSnapshot } from "./monitorDiff.ts";
const snapshot = (
overrides: Partial<PullRequestMonitorSnapshot> = {},
): PullRequestMonitorSnapshot => ({
state: "open",
draft: false,
headSha: "head-1",
baseRefName: "main",
mergeability: "mergeable",
behindBaseBy: null,
requiredChecksKnown: true,
reviews: [],
reviewThreads: [],
issueComments: [],
checkRuns: [],
...overrides,
});
const thread = (updatedAt = "2026-01-01T00:00:00Z", resolved = false) => ({
id: "thread-1",
author: { login: "review-bot", type: "app" as const },
body: "Fix this",
path: "src/a.ts",
line: 3,
createdAt: "2026-01-01T00:00:00Z",
updatedAt,
resolved,
});
const check = (id: string, conclusion: "success" | "failure") => ({
id,
name: "CI",
status: "completed" as const,
conclusion,
startedAt: "2026-01-01T00:00:00Z",
headSha: "head-1",
});
describe("pull request monitor diff", () => {
it("baselines every existing event", () => {
const initial = snapshot({
reviewThreads: [thread()],
checkRuns: [check("run-1", "failure")],
});
assert.deepStrictEqual(
diffPullRequestMonitorSnapshot(cursorFromSnapshot(initial), initial).actionableEvents,
[],
);
});
it("detects a new comment and an edit by id plus updatedAt", () => {
const emptyCursor = cursorFromSnapshot(snapshot());
const withComment = snapshot({ reviewThreads: [thread()] });
const added = diffPullRequestMonitorSnapshot(emptyCursor, withComment);
assert.deepStrictEqual(added.actionableEvents, [
{ kind: "new-review-comment", threadId: "thread-1", edited: false },
]);
const edited = diffPullRequestMonitorSnapshot(
added.nextCursor,
snapshot({ reviewThreads: [thread("2026-01-02T00:00:00Z")] }),
);
assert.deepStrictEqual(edited.actionableEvents, [
{ kind: "new-review-comment", threadId: "thread-1", edited: true },
]);
});
it("detects new and edited bot issue comments", () => {
const issueComment = {
id: "comment-1",
author: { login: "review-bot", type: "app" as const },
body: "Please update this",
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "2026-01-01T00:00:00Z",
};
const added = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(snapshot()),
snapshot({ issueComments: [issueComment] }),
);
assert.deepStrictEqual(added.actionableEvents, [
{ kind: "new-review-comment", threadId: "comment-1", edited: false },
]);
const edited = diffPullRequestMonitorSnapshot(
added.nextCursor,
snapshot({
issueComments: [{ ...issueComment, updatedAt: "2026-01-02T00:00:00Z" }],
}),
);
assert.deepStrictEqual(edited.actionableEvents, [
{ kind: "new-review-comment", threadId: "comment-1", edited: true },
]);
});
it("records a resolved transition without waking", () => {
const initial = snapshot({ reviewThreads: [thread()] });
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ reviewThreads: [thread("2026-01-02T00:00:00Z", true)] }),
);
assert.deepStrictEqual(result.actionableEvents, []);
assert.strictEqual(result.nextCursor.threadVersions["thread-1"]?.resolved, true);
});
it("wakes when a resolved thread is reopened without an updatedAt change", () => {
const initial = snapshot({ reviewThreads: [thread(undefined, true)] });
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ reviewThreads: [thread()] }),
);
assert.deepStrictEqual(result.actionableEvents, [
{ kind: "new-review-comment", threadId: "thread-1", edited: true },
]);
});
it("wakes for a failed rerun with a new run id", () => {
const initial = snapshot({ checkRuns: [check("run-1", "failure")] });
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ checkRuns: [check("run-2", "failure")] }),
);
assert.deepStrictEqual(result.actionableEvents, [
{ kind: "check-failed", checkRunId: "run-2", checkName: "CI" },
]);
});
it("wakes for a new failure after a seen success", () => {
const initial = snapshot({ checkRuns: [check("run-1", "success")] });
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ checkRuns: [check("run-2", "failure")] }),
);
assert.deepStrictEqual(result.actionableEvents, [
{ kind: "check-failed", checkRunId: "run-2", checkName: "CI" },
]);
});
it("wakes for a same-name failure on a new head", () => {
const initial = snapshot({ checkRuns: [check("run-1", "failure")] });
const nextCheck = { ...check("run-2", "failure"), headSha: "head-2" };
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ headSha: "head-2", checkRuns: [nextCheck] }),
);
assert.deepStrictEqual(result.actionableEvents, [
{ kind: "check-failed", checkRunId: "run-2", checkName: "CI" },
]);
});
it("tracks concurrent same-name runs independently", () => {
const initial = snapshot({ checkRuns: [check("run-1", "success")] });
const result = diffPullRequestMonitorSnapshot(
cursorFromSnapshot(initial),
snapshot({ checkRuns: [check("run-1", "success"), check("run-2", "failure")] }),
);
assert.deepStrictEqual(result.actionableEvents, [
{ kind: "check-failed", checkRunId: "run-2", checkName: "CI" },
]);
});
});