|
| 1 | +/** |
| 2 | + * What a running review tells anyone watching it. |
| 3 | + * |
| 4 | + * The bus carries no state a restart would need. Every durable fact, the |
| 5 | + * status, the stage rows, the run notes, the usage, is written to the database |
| 6 | + * by the service and the checkpointing runner before it is announced here, so |
| 7 | + * a listener that joins late or reconnects after a restart can read the whole |
| 8 | + * truth from the snapshot and lose nothing. That ordering is the point: an |
| 9 | + * event that arrived before the row it describes would let the UI show a stage |
| 10 | + * as finished while the database still says it is running, and reloading the |
| 11 | + * page would appear to undo it. |
| 12 | + * |
| 13 | + * Listeners are untrusted with respect to each other. One that throws must not |
| 14 | + * stop the others from being told, and must not fail the review it is watching. |
| 15 | + */ |
| 16 | + |
| 17 | +import type { ReviewStage, StageErrorClass } from "@/lib/domain/enums"; |
| 18 | +import type { ReviewStatus } from "@/lib/domain/state-machines"; |
| 19 | +import type { RunNote } from "../db/repositories/reviews"; |
| 20 | + |
| 21 | +export interface StageUsage { |
| 22 | + inputTokens: number; |
| 23 | + outputTokens: number; |
| 24 | + costEquivalentUsd: number; |
| 25 | +} |
| 26 | + |
| 27 | +export type ReviewEvent = |
| 28 | + | { kind: "status"; status: ReviewStatus; pausedReason?: string | undefined } |
| 29 | + | { |
| 30 | + kind: "stage"; |
| 31 | + stage: ReviewStage; |
| 32 | + /** `replayed` costs nothing and spawns nothing; it is not a live call. */ |
| 33 | + phase: "started" | "replayed" | "finished" | "failed"; |
| 34 | + attempt?: number | undefined; |
| 35 | + usage?: StageUsage | undefined; |
| 36 | + errorClass?: StageErrorClass | undefined; |
| 37 | + } |
| 38 | + | { kind: "engine"; stage: ReviewStage; event: "tool-use" | "text"; detail: string } |
| 39 | + | { kind: "rate-limit"; status: string; resetsAt?: number | undefined } |
| 40 | + | { kind: "note"; note: RunNote } |
| 41 | + | { |
| 42 | + kind: "done"; |
| 43 | + outcome: "completed" | "paused" | "cancelled" | "failed"; |
| 44 | + reason?: string | undefined; |
| 45 | + }; |
| 46 | + |
| 47 | +export type ReviewListener = (event: ReviewEvent) => void; |
| 48 | + |
| 49 | +export interface ReviewBus { |
| 50 | + emit: (reviewId: string, event: ReviewEvent) => void; |
| 51 | + subscribe: (reviewId: string, listener: ReviewListener) => () => void; |
| 52 | + /** How many listeners a review has, so a test can prove unsubscribe worked. */ |
| 53 | + listenerCount: (reviewId: string) => number; |
| 54 | +} |
| 55 | + |
| 56 | +export function createReviewBus(onListenerError: (error: unknown) => void = () => {}): ReviewBus { |
| 57 | + const listeners = new Map<string, Set<ReviewListener>>(); |
| 58 | + |
| 59 | + return { |
| 60 | + emit(reviewId, event) { |
| 61 | + // Copied before iterating: a listener may unsubscribe itself in response |
| 62 | + // to a done event, and mutating the set mid-iteration would skip whoever |
| 63 | + // came after it. |
| 64 | + for (const listener of [...(listeners.get(reviewId) ?? [])]) { |
| 65 | + try { |
| 66 | + listener(event); |
| 67 | + } catch (error) { |
| 68 | + // A browser that disconnected mid-write must not fail the review. |
| 69 | + onListenerError(error); |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + |
| 74 | + subscribe(reviewId, listener) { |
| 75 | + const forReview = listeners.get(reviewId) ?? new Set<ReviewListener>(); |
| 76 | + forReview.add(listener); |
| 77 | + listeners.set(reviewId, forReview); |
| 78 | + |
| 79 | + return () => { |
| 80 | + const current = listeners.get(reviewId); |
| 81 | + if (!current) return; |
| 82 | + current.delete(listener); |
| 83 | + if (current.size === 0) listeners.delete(reviewId); |
| 84 | + }; |
| 85 | + }, |
| 86 | + |
| 87 | + listenerCount(reviewId) { |
| 88 | + return listeners.get(reviewId)?.size ?? 0; |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
0 commit comments