Skip to content
← Public packages

@kentcdodds/sentry-triage

Sentry triage wakes Cole (Grok Bot) per issue: one active wake per repo (lease + queue), loop-safe Discord. Cole may spawn Cursor for isolated repo work.

src/triage-report.ts

64 lines · 1.9 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import type { TriageIssueRecord, TriageReportResult } from './types.ts'
import { listStorageEntries, recordFromListEntry } from './list-storage.ts'

/**
 * Maintenance/insight: dump all triage issue records plus hourly counters,
 * and a snapshot of per-repo leases / queue depths when those tables exist.
 * Invoked cross-package via a static `kody:@kentcdodds/sentry-triage` import.
 */
export default async function triageReport(): Promise<TriageReportResult> {
	const storage = packageStorage()
	const rawEntries = await listStorageEntries(storage, { prefix: 'issue:' })
	const records: TriageIssueRecord[] = []
	for (const entry of rawEntries) {
		const record = (await recordFromListEntry(storage, entry)) as
			| TriageIssueRecord
			| null
		if (record) records.push(record)
	}
	const debugShape =
		records.length === 0
			? JSON.stringify(await storage.list({ prefix: 'issue:' }))?.slice(0, 500)
			: undefined
	records.sort((left, right) =>
		String(right.seenAt ?? right.spawnedAt ?? '').localeCompare(
			String(left.seenAt ?? left.spawnedAt ?? ''),
		),
	)
	const counterEntries = await listStorageEntries(storage, {
		prefix: 'spawned:',
	})
	const counters = {
		entries: counterEntries,
		count: counterEntries.length,
	}

	let repoLeases = null
	let repoQueue = null
	try {
		const leaseResult = await storage.sql(
			`SELECT project_slug, holder, claimed_at FROM repo_leases`,
		)
		repoLeases = leaseResult?.results ?? leaseResult?.rows ?? []
	} catch {
		// Table may not exist yet.
	}
	try {
		const queueResult = await storage.sql(
			`SELECT project_slug, COUNT(*) AS n FROM repo_queue GROUP BY project_slug`,
		)
		repoQueue = queueResult?.results ?? queueResult?.rows ?? []
	} catch {
		// Table may not exist yet.
	}

	return {
		count: records.length,
		records,
		counters,
		debugShape,
		repoLeases,
		repoQueue,
	}
}