Files
t3code/apps/server/src/monitor/wakePrompt.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

89 lines
3.7 KiB
TypeScript

import type { PullRequestMonitorSnapshot } from "../sourceControl/gitHubPullRequestMonitor.ts";
import type { PullRequestMonitorActionableEvent } from "./monitorDiff.ts";
import type { PullRequestMonitorReadiness } from "./readiness.ts";
const excerpt = (body: string) => body.replace(/\s+/g, " ").trim().slice(0, 280);
export function formatBlockersSummary(readiness: PullRequestMonitorReadiness): string {
if (readiness.blockers.length === 0)
return readiness.label === "ready-to-merge" ? "Ready to merge" : "No known blockers";
return readiness.blockers
.map((blocker) => {
switch (blocker.kind) {
case "terminal":
return `PR is ${blocker.state}`;
case "draft":
return "PR is a draft";
case "mergeability":
return `Mergeability: ${blocker.state}`;
case "checks-missing":
return "No check results are available";
case "check-pending":
return `${blocker.checkName}: pending`;
case "check-failed":
return `${blocker.checkName}: failed`;
case "changes-requested":
return `${blocker.reviewer}: changes requested`;
case "unresolved-thread":
return `Review thread ${blocker.threadId}: unresolved`;
}
})
.join("\n");
}
function formatEvent(
event: PullRequestMonitorActionableEvent,
snapshot: PullRequestMonitorSnapshot,
): string {
switch (event.kind) {
case "new-review-comment": {
const thread = snapshot.reviewThreads.find((item) => item.id === event.threadId);
const comment = snapshot.issueComments.find((item) => item.id === event.threadId);
if (thread) {
const location =
thread.path === null
? ""
: `, ${thread.path}${thread.line === null ? "" : `:${thread.line}`}`;
return `- Comment from ${thread.author.login}${location}: ${excerpt(thread.body)}`;
}
return comment
? `- Comment from ${comment.author.login}: ${excerpt(comment.body)}`
: `- ${event.edited ? "Updated" : "New"} review comment (${event.threadId})`;
}
case "changes-requested-review": {
const review = snapshot.reviews.find((item) => item.id === event.reviewId);
const detail = excerpt(event.body);
return `- ${review?.author.login ?? "Reviewer"} requested changes${detail ? `: ${detail}` : ""}`;
}
case "check-failed": {
const check = snapshot.checkRuns.find((item) => item.id === event.checkRunId);
return `- Check ${event.checkName}: ${check?.conclusion ?? "failure"}`;
}
case "behind-base":
return `- PR is behind ${snapshot.baseRefName}${snapshot.behindBaseBy === null ? "" : ` by ${snapshot.behindBaseBy} commit(s)`}`;
}
}
export function buildWakePrompt(input: {
readonly prNumber: number;
readonly wakeCount: number;
readonly events: ReadonlyArray<PullRequestMonitorActionableEvent>;
readonly snapshot: PullRequestMonitorSnapshot;
readonly readiness: PullRequestMonitorReadiness;
}): string {
return `New activity on PR #${input.prNumber} (monitoring, wake ${input.wakeCount}/10).
${input.events.map((event) => formatEvent(event, input.snapshot)).join("\n")}
Status: ${formatBlockersSummary(input.readiness)}
Head: ${input.snapshot.headSha} (approvals/checks are evaluated against this commit)
Policy:
- Verify bot claims against the source before acting.
- Fix legitimate findings and push.
- Dismiss false positives with a brief reply — never silently ignore or comply.
- For CI failures: compare against ${input.snapshot.baseRefName}; re-run suspected flakes; if the same real failure repeats, ask the user via a question rather than guessing.
- Rebase if the PR is behind ${input.snapshot.baseRefName}.
- If the goal has become impossible, say so and ask the user.`;
}