From 4faf0e941814eccf497f32673c47cd54941e6fd9 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Fri, 24 Jul 2026 04:48:22 -0700 Subject: [PATCH] Make sweep respect merged-PR auto-settle like the sidebar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Real-data testing surfaced the gap the plan had flagged: the sweep passed changeRequestState: null, so threads the sidebar auto-settles (merged/closed PR + idle) counted as unsettled — 18 candidates vs ~7 sidebar cards. SidebarV2 now mirrors its per-row PR-state map into the sweep store, isSweepCandidate consults it, and the pre-run summary subscribes to a version counter so its count tracks PR states as they stream in. Co-Authored-By: Claude Fable 5 --- apps/web/src/components/SidebarV2.tsx | 6 ++++ .../reviewSweep/ReviewSweepView.tsx | 5 ++- apps/web/src/reviewSweepStore.ts | 35 ++++++++++++++++--- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/SidebarV2.tsx b/apps/web/src/components/SidebarV2.tsx index 82b412539e..22725af1fe 100644 --- a/apps/web/src/components/SidebarV2.tsx +++ b/apps/web/src/components/SidebarV2.tsx @@ -81,6 +81,7 @@ import { type SidebarProjectGroupMember, type SidebarProjectSnapshot, } from "../sidebarProjectGrouping"; +import { publishSweepChangeRequestStates } from "../reviewSweepStore"; import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"; import { useThreadSelectionStore } from "../threadSelectionStore"; import { useThreadActions } from "../hooks/useThreadActions"; @@ -1163,6 +1164,11 @@ export default function SidebarV2() { }, [], ); + // Mirror PR states to the review-sweep store so its unsettled-candidate + // predicate matches this sidebar's partition (merged-PR auto-settle). + useEffect(() => { + publishSweepChangeRequestStates(changeRequestStateByKey); + }, [changeRequestStateByKey]); // Project scope: one menu above the list. Scoping filters the list without // making the header width depend on the number or length of project names. diff --git a/apps/web/src/components/reviewSweep/ReviewSweepView.tsx b/apps/web/src/components/reviewSweep/ReviewSweepView.tsx index 95f9b92b0f..ace5aa1f62 100644 --- a/apps/web/src/components/reviewSweep/ReviewSweepView.tsx +++ b/apps/web/src/components/reviewSweep/ReviewSweepView.tsx @@ -159,6 +159,9 @@ function SweepPreRunSummary() { const shells = useThreadShells(); const serverConfigs = useServerConfigs(); const autoSettleAfterDays = useClientSettings((settings) => settings.sidebarAutoSettleAfterDays); + // Recompute when the sidebar publishes fresh PR states — merged-PR + // auto-settle changes which threads count as unsettled. + const candidateVersion = useReviewSweepStore((state) => state.candidateVersion); const { candidateCount, reviewedCount, modelsByEnvironment } = useMemo(() => { const now = new Date().toISOString(); @@ -198,7 +201,7 @@ function SweepPreRunSummary() { reviewedCount: reviewed.length, modelsByEnvironment: [...models.values()], }; - }, [autoSettleAfterDays, serverConfigs, shells]); + }, [autoSettleAfterDays, candidateVersion, serverConfigs, shells]); if (candidateCount === 0) { return ( diff --git a/apps/web/src/reviewSweepStore.ts b/apps/web/src/reviewSweepStore.ts index e3e095d3d4..9a9b17e6bb 100644 --- a/apps/web/src/reviewSweepStore.ts +++ b/apps/web/src/reviewSweepStore.ts @@ -23,6 +23,22 @@ import { reviewEnvironment } from "./state/review"; import { environmentServerConfigsAtom } from "./state/server"; import { threadEnvironment } from "./state/threads"; +/** PR states mirrored from SidebarV2's per-row VCS subscriptions (keyed by + scoped thread key). The sidebar auto-settles merged/closed+idle threads + using this data; the sweep must see the same states or it will count + those threads as unsettled. Module-level because the sidebar is mounted + for the app's lifetime and owns the subscriptions. */ +let changeRequestStateByKey: ReadonlyMap = new Map(); + +export function publishSweepChangeRequestStates( + states: ReadonlyMap, +): void { + changeRequestStateByKey = states; + // Bump the store version so the pre-run summary recomputes its candidate + // count as PR states stream in from the sidebar rows. + useReviewSweepStore.getState().bumpCandidateVersion(); +} + const SWEEP_CONCURRENCY = 3; /** Soft cap so a giant backlog doesn't spawn an unbounded number of LLM generations in one click. */ @@ -52,6 +68,9 @@ interface ReviewSweepState { items: Readonly>; /** Count of candidates dropped by SWEEP_MAX_THREADS (0 = full coverage). */ truncatedCount: number; + /** Bumped when sidebar-owned PR states change; the pre-run summary + subscribes so its candidate count tracks the sidebar's partition. */ + candidateVersion: number; startRun: (input: { runId: number; items: ReadonlyArray; @@ -59,6 +78,7 @@ interface ReviewSweepState { }) => void; patchItem: (key: string, patch: Partial) => void; finishRun: (runId: number) => void; + bumpCandidateVersion: () => void; reset: () => void; } @@ -68,6 +88,7 @@ export const useReviewSweepStore = create((set) => ({ order: [], items: {}, truncatedCount: 0, + candidateVersion: 0, startRun: ({ runId, items, truncatedCount }) => set({ phase: "running", @@ -83,6 +104,7 @@ export const useReviewSweepStore = create((set) => ({ return { items: { ...state.items, [key]: { ...existing, ...patch } } }; }), finishRun: (runId) => set((state) => (state.runId === runId ? { phase: "complete" } : state)), + bumpCandidateVersion: () => set((state) => ({ candidateVersion: state.candidateVersion + 1 })), reset: () => set({ phase: "idle", order: [], items: {}, truncatedCount: 0 }), })); @@ -101,13 +123,18 @@ export function isSweepCandidate( ): boolean { if (shell.archivedAt !== null) return false; if (capabilities?.threadSettlement !== true || capabilities.reviewSweep !== true) return false; - // PR state is owned by per-row VCS subscriptions in the sidebar and isn't - // readable here; passing null may include a few threads the sidebar shows - // as settled (merged PR + idle). Reviewing them is harmless. + // PR state comes from SidebarV2's per-row subscriptions via + // publishSweepChangeRequestStates, so merged-PR+idle threads classify as + // settled here exactly like they do in the sidebar. Rows that haven't + // reported yet fall back to null (treated as unsettled) — same as a + // freshly mounted sidebar. + const changeRequestState = + changeRequestStateByKey.get(scopedThreadKey(scopeThreadRef(shell.environmentId, shell.id))) ?? + null; return !effectiveSettled(shell, { now: options.now, autoSettleAfterDays: options.autoSettleAfterDays, - changeRequestState: null, + changeRequestState, }); }