← Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
/**
* Compare the factory's own GitHub webhook decisions with the
* `@kody/github.*` event path for the same deliveries.
*/
import { shadowStore, type ShadowRow } from './shadow-store.ts'
export type ShadowPair = { deliveryId: string; topic: string; webhook: string | null; event: string | null; at: string }
export type ShadowReport = {
deliveries: number
matched: number
mismatched: ShadowPair[]
/** Webhook decided, no event seen yet (older than graceMinutes). */
missingEvent: ShadowPair[]
/** Deliveries the event path ignores on purpose (issue comments). */
notMirrored: number
/** Events with no webhook row (deliveries that only reached @kody/github). */
eventOnly: number
/** Webhook rows whose forward to @kody/github failed (see `bridgeErrorSample`). */
bridgeErrors: number
bridgeErrorSample?: string
recent: ShadowPair[]
}
/** Webhook-side decisions that the event path does not mirror by design. */
export const NOT_MIRRORED_PREFIX = 'not-mirrored:'
function bridgeErrorOf(detail: string | undefined): string | null {
if (!detail) return null
try {
const error = (JSON.parse(detail) as { bridge?: { error?: unknown } | null })?.bridge?.error
return typeof error === 'string' && error ? error.slice(0, 300) : null
} catch {
return null
}
}
export function buildShadowReport(rows: ShadowRow[], now = Date.now(), graceMinutes = 10): ShadowReport {
const byId = new Map<string, { webhook?: ShadowRow; event?: ShadowRow }>()
for (const r of rows) {
const slot = byId.get(r.deliveryId) ?? {}
slot[r.side] = r
byId.set(r.deliveryId, slot)
}
const report: ShadowReport = { deliveries: byId.size, matched: 0, mismatched: [], missingEvent: [], notMirrored: 0, eventOnly: 0, bridgeErrors: 0, recent: [] }
for (const [deliveryId, { webhook, event }] of byId) {
const bridgeError = bridgeErrorOf(webhook?.detail)
if (bridgeError) {
report.bridgeErrors++
report.bridgeErrorSample ??= bridgeError
}
const pair: ShadowPair = { deliveryId, topic: event?.topic ?? webhook?.topic ?? '', webhook: webhook?.decision ?? null, event: event?.decision ?? null, at: (webhook ?? event)!.at }
if (report.recent.length < 20) report.recent.push(pair)
if (webhook?.decision.startsWith(NOT_MIRRORED_PREFIX)) {
report.notMirrored++
continue
}
if (webhook && event) {
if (webhook.decision === event.decision) report.matched++
else report.mismatched.push(pair)
} else if (webhook) {
if (now - Date.parse(webhook.at) > graceMinutes * 60_000) report.missingEvent.push(pair)
} else {
report.eventOnly++
}
}
return report
}
/**
* Shadow comparison of the GitHub webhook path and the GitHub event path.
*
* @param input - `{ limit? }` rows to read (default 400, max 2000)
* @returns `{ deliveries, matched, mismatched[], missingEvent[], notMirrored, eventOnly, bridgeErrors, bridgeErrorSample?, recent[] }`
*
* @example
* import report from 'kody:@kody/kody-factory/github-events/report'
* await report({})
* // => { deliveries: 12, matched: 11, mismatched: [], missingEvent: [], notMirrored: 1, eventOnly: 0, bridgeErrors: 0, recent: [...] }
*/
export default async function githubEventsReport(input: { limit?: number } = {}): Promise<ShadowReport> {
const limit = Math.min(Math.max(Math.floor(Number(input?.limit) || 400), 1), 2000)
return buildShadowReport(await shadowStore().recent(limit))
}