Add PR monitoring: server-driven babysit mode for agent threads

Threads can now formally monitor a PR after creating it. The agent calls a
new monitor_start MCP tool; a dedicated PullRequestMonitor service polls
GitHub every 30s, diffs review comments / reviews / check runs against a
per-thread cursor, and wakes the thread with a self-contained prompt when
there is something to address. The server — never the agent — declares the
PR ready from head-SHA-anchored GitHub state.

- GitHub monitor snapshot fetch (reviews, threads, check runs, mergeability)
  with pure cursor-diff and readiness modules
- thread.monitor.start/update/end domain events through decider/projector,
  monitor state on the thread shell, migration 034 (monitor_json)
- Wake dispatch protocol: claim → fresh terminal revalidation at the send
  boundary → dispatch → cursor ack only after accepted start; user turns
  always win and rewind in-flight wake cursors so events re-diff; 10-wake
  circuit breaker ends as needs-attention
- Monitoring holds the thread unsettled; user settle ends the monitor;
  session teardown ends it as session-ended
- UI: steady cyan Monitoring pill (never masks approval/input/failed),
  blockers sub-line, header MonitorStrip with Stop, emerald Ready-to-merge
  state with confirm dialog; clients can only end a monitor as "stopped"

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-26 19:39:42 -07:00
1 parent a17cbc3b40
commit 63e07db893
41 files changed
+3178 -21

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
import type {
PullRequestMonitorCheckRun,
PullRequestMonitorSnapshot,
} from "../sourceControl/gitHubPullRequestMonitor.ts";
export interface PullRequestMonitorCursor {
readonly headSha: string;
readonly reviewStates: Readonly<Record<string, string>>;
readonly threadVersions: Readonly<
Record<string, { readonly updatedAt: string; readonly resolved: boolean }>
>;
readonly issueCommentVersions: Readonly<Record<string, string>>;
readonly checkRuns: Readonly<
Record<string, { readonly runId: string; readonly outcome: "success" | "failure" | "pending" }>
>;
readonly behindBase: boolean;
}
export type PullRequestMonitorActionableEvent =
| { readonly kind: "new-review-comment"; readonly threadId: string; readonly edited: boolean }
| { readonly kind: "changes-requested-review"; readonly reviewId: string }
| { readonly kind: "check-failed"; readonly checkRunId: string; readonly checkName: string }
| { readonly kind: "behind-base" };
const checkOutcome = (check: PullRequestMonitorCheckRun): "success" | "failure" | "pending" => {
if (check.status !== "completed") return "pending";
if (
check.conclusion === "success" ||
check.conclusion === "neutral" ||
check.conclusion === "skipped"
) {
return "success";
}
return "failure";
};
export function cursorFromSnapshot(snapshot: PullRequestMonitorSnapshot): PullRequestMonitorCursor {
return {
headSha: snapshot.headSha,
reviewStates: Object.fromEntries(snapshot.reviews.map((review) => [review.id, review.state])),
threadVersions: Object.fromEntries(
snapshot.reviewThreads.map((thread) => [
thread.id,
{ updatedAt: thread.updatedAt, resolved: thread.resolved },
]),
),
issueCommentVersions: Object.fromEntries(
snapshot.issueComments.map((comment) => [comment.id, comment.updatedAt]),
),
checkRuns: Object.fromEntries(
snapshot.checkRuns
.filter((check) => check.headSha === snapshot.headSha)
.map((check) => [check.name, { runId: check.id, outcome: checkOutcome(check) }]),
),
behindBase: (snapshot.behindBaseBy ?? 0) > 0,
};
}
export function diffPullRequestMonitorSnapshot(
previousCursor: PullRequestMonitorCursor,
snapshot: PullRequestMonitorSnapshot,
): {
readonly actionableEvents: ReadonlyArray<PullRequestMonitorActionableEvent>;
readonly nextCursor: PullRequestMonitorCursor;
} {
const actionableEvents: PullRequestMonitorActionableEvent[] = [];
for (const thread of snapshot.reviewThreads) {
const previous = previousCursor.threadVersions[thread.id];
if (!thread.resolved && (!previous || previous.updatedAt !== thread.updatedAt)) {
actionableEvents.push({
kind: "new-review-comment",
threadId: thread.id,
edited: previous !== undefined,
});
}
}
for (const comment of snapshot.issueComments) {
const previousUpdatedAt = previousCursor.issueCommentVersions[comment.id];
if (!previousUpdatedAt || previousUpdatedAt !== comment.updatedAt) {
actionableEvents.push({
kind: "new-review-comment",
threadId: comment.id,
edited: previousUpdatedAt !== undefined,
});
}
}
for (const review of snapshot.reviews) {
if (
review.commitSha === snapshot.headSha &&
review.state === "changes-requested" &&
previousCursor.reviewStates[review.id] !== "changes-requested"
) {
actionableEvents.push({ kind: "changes-requested-review", reviewId: review.id });
}
}
for (const check of snapshot.checkRuns.filter((item) => item.headSha === snapshot.headSha)) {
const outcome = checkOutcome(check);
const previous = previousCursor.checkRuns[check.name];
// A rerun preserves the check-name outcome history; only success -> failure wakes again.
if (outcome === "failure" && previous?.outcome !== "failure") {
actionableEvents.push({
kind: "check-failed",
checkRunId: check.id,
checkName: check.name,
});
}
}
if ((snapshot.behindBaseBy ?? 0) > 0 && !previousCursor.behindBase) {
actionableEvents.push({ kind: "behind-base" });
}
return { actionableEvents, nextCursor: cursorFromSnapshot(snapshot) };
}