@@ -3,6 +3,14 @@ import {
33 summarizeExactReviewHandoff ,
44 summarizeExactReviewPressure ,
55} from "./exact-review-health.ts" ;
6+ import {
7+ REVIEW_TELEMETRY_DEGRADED_MS ,
8+ REVIEW_TELEMETRY_ORPHAN_MS ,
9+ REVIEW_TELEMETRY_RETENTION_MS ,
10+ type DurableReviewTelemetry ,
11+ type ReviewTelemetryHealth ,
12+ normalizeReviewTelemetry ,
13+ } from "./review-telemetry.ts" ;
614
715type GithubAppJsonOptions = { method ?: string ; body ?: BodyInit ; errorLabel ?: string } ;
816const GITHUB_TIMEOUT_MS = 4500 ;
@@ -263,6 +271,7 @@ const EXACT_REVIEW_QUEUE_DELIVERY_TABLE = "exact_review_queue_deliveries";
263271const EXACT_REVIEW_QUEUE_METRICS_TABLE = "exact_review_queue_metrics" ;
264272const EXACT_REVIEW_QUEUE_METRIC_BUCKET_TABLE = "exact_review_queue_metric_buckets" ;
265273const EXACT_REVIEW_QUEUE_DEAD_LETTER_TABLE = "exact_review_queue_dead_letters" ;
274+ const EXACT_REVIEW_REVIEW_TELEMETRY_TABLE = "exact_review_review_telemetry" ;
266275const EXACT_REVIEW_QUEUE_DEAD_LETTER_LIMIT = 5_000 ;
267276const EXACT_REVIEW_QUEUE_DEAD_LETTER_RESOLVED_TTL_MS = 30 * 24 * 60 * 60 * 1000 ;
268277const EXACT_REVIEW_QUEUE_METRIC_BUCKET_MS = 5 * 60 * 1000 ;
@@ -847,6 +856,14 @@ export class ExactReviewQueue {
847856 return this . supersedePublicationCandidates ( await request . json ( ) . catch ( ( ) => null ) ) ;
848857 }
849858
859+ if ( request . method === "POST" && url . pathname === "/review-telemetry" ) {
860+ return this . recordReviewTelemetry ( await request . json ( ) . catch ( ( ) => null ) ) ;
861+ }
862+
863+ if ( request . method === "GET" && url . pathname === "/review-telemetry" ) {
864+ return this . listReviewTelemetry ( url . searchParams ) ;
865+ }
866+
850867 if ( request . method === "GET" && url . pathname === "/item-status" ) {
851868 const targetRepo = String ( url . searchParams . get ( "target_repo" ) || "" ) . trim ( ) ;
852869 const itemNumber = Number ( url . searchParams . get ( "item_number" ) ) ;
@@ -956,6 +973,7 @@ export class ExactReviewQueue {
956973 const snapshot = this . storage . transactionSync ( ( ) => {
957974 this . pruneDeliveryReceiptsSync ( now ) ;
958975 this . pruneQueueTelemetrySync ( now ) ;
976+ this . pruneReviewTelemetrySync ( now ) ;
959977 const current = this . readStateSync ( ) ;
960978 // Dashboard reads are also the operational heartbeat. Reclaim leases and
961979 // restore the alarm here so a deploy or lost alarm cannot strand backlog.
@@ -973,9 +991,11 @@ export class ExactReviewQueue {
973991 reviewFlow : this . reviewFlowSummarySync ( now ) ,
974992 publicationFlow : this . publicationFlowSummarySync ( now ) ,
975993 deadLetters : this . deadLetterStatsSync ( ) ,
994+ reviewTelemetryHealth : this . reviewTelemetryHealthSync ( now ) ,
976995 } ;
977996 } ) ;
978- const { state, metrics, reviewFlow, publicationFlow, deadLetters } = snapshot ;
997+ const { state, metrics, reviewFlow, publicationFlow, deadLetters, reviewTelemetryHealth } =
998+ snapshot ;
979999 const publicationControl = this . refreshPublicationControlSync ( state , now ) ;
9801000 await this . scheduleNext ( state , now ) ;
9811001 const stats = exactReviewQueueStats (
@@ -1026,6 +1046,7 @@ export class ExactReviewQueue {
10261046 } ,
10271047 } ,
10281048 delivery_receipts : this . deliveryReceiptCountSync ( ) ,
1049+ review_telemetry_health : reviewTelemetryHealth ,
10291050 storage_schema_version : EXACT_REVIEW_QUEUE_STORAGE_SCHEMA_VERSION ,
10301051 legacy_rollback_available :
10311052 ! this . legacyMirrorDisabled &&
@@ -1543,6 +1564,159 @@ export class ExactReviewQueue {
15431564 } ) ;
15441565 }
15451566
1567+ private recordReviewTelemetry ( value : unknown ) {
1568+ const record = normalizeReviewTelemetry ( value ) ;
1569+ if ( ! record ) return json ( { error : "invalid_review_telemetry" } , 400 ) ;
1570+ const updatedAt = Date . parse ( record . updated_at ) ;
1571+ this . storage . transactionSync ( ( ) => {
1572+ this . pruneReviewTelemetrySync ( Date . now ( ) ) ;
1573+ // Terminal truth is first-writer immutable. Retries may replay the same
1574+ // payload, but neither a heartbeat nor a conflicting terminal delivery
1575+ // may make durable observations depend on arrival order.
1576+ this . storage . sql . exec (
1577+ `INSERT INTO ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1578+ (repo, item_number, run_id, run_attempt, status, updated_at, lease_expires_at,
1579+ record_json)
1580+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1581+ ON CONFLICT(repo, item_number, run_id, run_attempt) DO UPDATE SET
1582+ status = excluded.status,
1583+ updated_at = excluded.updated_at,
1584+ lease_expires_at = excluded.lease_expires_at,
1585+ record_json = excluded.record_json
1586+ WHERE ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE } .status != 'completed'
1587+ AND (excluded.status = 'completed'
1588+ OR excluded.updated_at >= ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE } .updated_at)` ,
1589+ record . repo ,
1590+ record . item_number ,
1591+ record . run_id ,
1592+ record . run_attempt ,
1593+ record . status ,
1594+ updatedAt ,
1595+ record . lease_expires_at === null ? null : Date . parse ( record . lease_expires_at ) ,
1596+ JSON . stringify ( record ) ,
1597+ ) ;
1598+ } ) ;
1599+ return json ( { ok : true } ) ;
1600+ }
1601+
1602+ private listReviewTelemetry ( search : URLSearchParams ) {
1603+ const repo = String ( search . get ( "repo" ) || "" ) . trim ( ) ;
1604+ const itemNumber = Number ( search . get ( "item_number" ) ) ;
1605+ const limit = search . has ( "limit" ) ? Number ( search . get ( "limit" ) ) : 20 ;
1606+ if (
1607+ ! / ^ [ A - Z a - z 0 - 9 _ . - ] + \/ [ A - Z a - z 0 - 9 _ . - ] + $ / . test ( repo ) ||
1608+ ! Number . isInteger ( itemNumber ) ||
1609+ itemNumber < 1 ||
1610+ ! Number . isInteger ( limit ) ||
1611+ limit < 1 ||
1612+ limit > 100
1613+ ) {
1614+ return json ( { error : "invalid_review_telemetry_query" } , 400 ) ;
1615+ }
1616+ this . pruneReviewTelemetrySync ( Date . now ( ) ) ;
1617+ return json ( {
1618+ ok : true ,
1619+ repo,
1620+ item_number : itemNumber ,
1621+ reviews : this . reviewTelemetryRowsSync ( { repo, itemNumber, limit } ) ,
1622+ } ) ;
1623+ }
1624+
1625+ private reviewTelemetryRowsSync ( options : {
1626+ repo ?: string ;
1627+ itemNumber ?: number ;
1628+ status ?: DurableReviewTelemetry [ "status" ] ;
1629+ limit ?: number ;
1630+ } ) {
1631+ const predicates : string [ ] = [ ] ;
1632+ const bindings : unknown [ ] = [ ] ;
1633+ if ( options . repo !== undefined ) {
1634+ predicates . push ( "repo = ?" ) ;
1635+ bindings . push ( options . repo ) ;
1636+ }
1637+ if ( options . itemNumber !== undefined ) {
1638+ predicates . push ( "item_number = ?" ) ;
1639+ bindings . push ( options . itemNumber ) ;
1640+ }
1641+ if ( options . status !== undefined ) {
1642+ predicates . push ( "status = ?" ) ;
1643+ bindings . push ( options . status ) ;
1644+ }
1645+ const rows = this . storage . sql . exec (
1646+ `SELECT record_json
1647+ FROM ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1648+ ${ predicates . length ? `WHERE ${ predicates . join ( " AND " ) } ` : "" }
1649+ ORDER BY updated_at DESC, repo, item_number, run_id, run_attempt
1650+ LIMIT ?` ,
1651+ ...bindings ,
1652+ options . limit ?? 100 ,
1653+ ) as Iterable < { record_json ?: unknown } > ;
1654+ return Array . from ( rows )
1655+ . map ( ( row ) => normalizeReviewTelemetry ( JSON . parse ( String ( row . record_json || "null" ) ) ) )
1656+ . filter ( ( record ) : record is DurableReviewTelemetry => record !== null ) ;
1657+ }
1658+
1659+ private reviewTelemetryHealthSync ( now : number ) : ReviewTelemetryHealth {
1660+ const counts = Array . from (
1661+ this . storage . sql . exec (
1662+ `SELECT COUNT(*) AS refreshing,
1663+ COALESCE(SUM(CASE WHEN updated_at <= ? THEN 1 ELSE 0 END), 0)
1664+ AS slow_refreshing,
1665+ COALESCE(SUM(CASE
1666+ WHEN updated_at <= ?
1667+ AND (lease_expires_at IS NULL OR lease_expires_at <= ?)
1668+ THEN 1 ELSE 0 END), 0) AS orphan_refreshing
1669+ FROM ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1670+ WHERE status = 'refreshing'` ,
1671+ now - REVIEW_TELEMETRY_DEGRADED_MS ,
1672+ now - REVIEW_TELEMETRY_ORPHAN_MS ,
1673+ now ,
1674+ ) ,
1675+ ) [ 0 ] as Record < string , unknown > | undefined ;
1676+ const orphanCount = Number ( counts ?. orphan_refreshing || 0 ) ;
1677+ const orphanRows = this . storage . sql . exec (
1678+ `SELECT record_json
1679+ FROM ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1680+ WHERE status = 'refreshing'
1681+ AND updated_at <= ?
1682+ AND (lease_expires_at IS NULL OR lease_expires_at <= ?)
1683+ ORDER BY updated_at, repo, item_number, run_id, run_attempt
1684+ LIMIT 20` ,
1685+ now - REVIEW_TELEMETRY_ORPHAN_MS ,
1686+ now ,
1687+ ) as Iterable < { record_json ?: unknown } > ;
1688+ const orphans = Array . from ( orphanRows )
1689+ . map ( ( row ) => normalizeReviewTelemetry ( JSON . parse ( String ( row . record_json || "null" ) ) , now ) )
1690+ . filter ( ( record ) : record is DurableReviewTelemetry => record !== null )
1691+ . map ( ( record ) => ( {
1692+ repo : record . repo ,
1693+ item_number : record . item_number ,
1694+ run_id : record . run_id ,
1695+ run_attempt : record . run_attempt ,
1696+ updated_at : record . updated_at ,
1697+ age_seconds : Math . floor ( Math . max ( 0 , now - Date . parse ( record . updated_at ) ) / 1000 ) ,
1698+ lease_expires_at : record . lease_expires_at ,
1699+ } ) ) ;
1700+ const slowCount = Number ( counts ?. slow_refreshing || 0 ) ;
1701+ return {
1702+ status : orphanCount ? "critical" : slowCount ? "degraded" : "healthy" ,
1703+ refreshing : Number ( counts ?. refreshing || 0 ) ,
1704+ slow_refreshing : slowCount ,
1705+ orphan_refreshing : orphanCount ,
1706+ degraded_after_seconds : REVIEW_TELEMETRY_DEGRADED_MS / 1000 ,
1707+ orphan_after_seconds : REVIEW_TELEMETRY_ORPHAN_MS / 1000 ,
1708+ orphans,
1709+ } ;
1710+ }
1711+
1712+ private pruneReviewTelemetrySync ( now : number ) {
1713+ this . storage . sql . exec (
1714+ `DELETE FROM ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1715+ WHERE status = 'completed' AND updated_at <= ?` ,
1716+ now - REVIEW_TELEMETRY_RETENTION_MS ,
1717+ ) ;
1718+ }
1719+
15461720 private async supersedePublicationCandidates ( value : unknown ) {
15471721 const body = objectValue ( value ) ;
15481722 const candidates = exactReviewPublicationCandidates ( body . items ) ;
@@ -1790,6 +1964,38 @@ export class ExactReviewQueue {
17901964 ON ${ EXACT_REVIEW_QUEUE_DEAD_LETTER_TABLE }
17911965 (status, last_failed_at, dead_letter_id)` ,
17921966 ) ;
1967+ // Review telemetry is intentionally additive to the v1 queue protocol.
1968+ // PR 674 can populate generation and operation_id inside record_json
1969+ // without coupling this observation schema to its queue tuple rollout.
1970+ this . storage . sql . exec (
1971+ `CREATE TABLE IF NOT EXISTS ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE } (
1972+ repo TEXT NOT NULL,
1973+ item_number INTEGER NOT NULL CHECK (item_number >= 1),
1974+ run_id TEXT NOT NULL,
1975+ run_attempt INTEGER NOT NULL CHECK (run_attempt >= 1),
1976+ status TEXT NOT NULL CHECK (status IN ('refreshing', 'completed')),
1977+ updated_at INTEGER NOT NULL,
1978+ lease_expires_at INTEGER,
1979+ record_json TEXT NOT NULL,
1980+ PRIMARY KEY (repo, item_number, run_id, run_attempt)
1981+ ) STRICT` ,
1982+ ) ;
1983+ const hasLeaseExpiry = Array . from (
1984+ this . storage . sql . exec (
1985+ `SELECT name FROM pragma_table_info('${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE } ')
1986+ WHERE name = 'lease_expires_at'` ,
1987+ ) ,
1988+ ) . length ;
1989+ if ( ! hasLeaseExpiry ) {
1990+ this . storage . sql . exec (
1991+ `ALTER TABLE ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE }
1992+ ADD COLUMN lease_expires_at INTEGER` ,
1993+ ) ;
1994+ }
1995+ this . storage . sql . exec (
1996+ `CREATE INDEX IF NOT EXISTS exact_review_review_telemetry_status
1997+ ON ${ EXACT_REVIEW_REVIEW_TELEMETRY_TABLE } (status, updated_at)` ,
1998+ ) ;
17931999 }
17942000
17952001 private readStorageMetaSync ( ) {
0 commit comments