← Public packages
@kentcdodds/kody-issue-triage
Loop-safe triage for Kody run errors and fleet package-runtime error-rate elevations. Wakes Cole (Grok Bot) to decide; Cursor agents only when Cole escalates.
src/spawn-snapshot.ts
67 lines · 1.8 KB · TypeScriptimport { kody } from 'kody:runtime'
import {
MATCHING_OPEN_RUN_LIMIT,
runMatchesRecord,
slimMatchingRun,
type LoopGuardSnapshot,
type MatchingOpenRun,
} from './loop-guard.ts'
import { hourBucket, maxAgentsPerHour, type FingerprintRecord, type RunSnippet } from './shared.ts'
import { getCounter, getLease } from './storage.ts'
export type { LoopGuardSnapshot, MatchingOpenRun } from './loop-guard.ts'
export {
emptyLoopGuardSnapshot,
formatLoopGuardForPrompt,
runMatchesRecord,
} from './loop-guard.ts'
export async function gatherLoopGuardSnapshot(
record: FingerprintRecord,
): Promise<LoopGuardSnapshot> {
const bucket = hourBucket()
const [spawnedThisHour, fingerprintsThisHour, eventsThisHour, lease] =
await Promise.all([
getCounter(`spawned:${bucket}`),
getCounter(`fingerprints:${bucket}`),
getCounter(`events:${bucket}`),
getLease(),
])
let matchingOpenRuns: MatchingOpenRun[] = []
let matchingOpenRunCount = 0
let openRunsScanned = 0
try {
const page = await kody.runList({
status: 'error',
error_triage: 'open',
limit: 50,
})
const runs = (page.runs || []) as RunSnippet[]
openRunsScanned = runs.length
const matched = runs.filter((run) => runMatchesRecord(run, record))
matchingOpenRunCount = matched.length
matchingOpenRuns = matched.slice(0, MATCHING_OPEN_RUN_LIMIT).map(slimMatchingRun)
} catch {
matchingOpenRuns = []
}
return {
gatheredAt: new Date().toISOString(),
alreadyEscalatedToKody: Boolean(record.kody_agent_id),
spawnedThisHour,
fingerprintsThisHour,
eventsThisHour,
capRemaining: Math.max(0, maxAgentsPerHour - spawnedThisHour),
lease: lease
? {
holder: lease.holder,
fingerprint: lease.fingerprint,
acquiredAt: lease.acquired_at,
}
: null,
matchingOpenRuns,
matchingOpenRunCount,
openRunsScanned,
}
}