Skip to content
← 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/schedule-event-sweep.ts

38 lines · 1.1 KB · TypeScript
import { workflows } from 'kody:runtime'
import { hourBucket } from './shared.ts'

/**
 * After an event queues a fingerprint, schedule one guarded sweep so Patch
 * wakes without waiting for the hourly job. Idempotent per fingerprint/hour.
 */
export async function scheduleEventSweep(input: {
	fingerprint: string
	queued: boolean
}) {
	if (!input.queued || !input.fingerprint) {
		return { scheduled: false as const, reason: 'not-queued' as const }
	}
	const idempotencyKey = `kody-issue-triage:event-sweep:${input.fingerprint}:${hourBucket()}`
	try {
		const workflow = await workflows.create({
			exportName: './sweep',
			idempotencyKey,
			params: { limit: 1, reconcile: false },
		})
		return {
			scheduled: true as const,
			idempotencyKey,
			workflowId:
				(workflow as { id?: string; workflow_id?: string }).id ||
				(workflow as { workflow_id?: string }).workflow_id ||
				null,
		}
	} catch (error) {
		return {
			scheduled: false as const,
			reason: 'workflow-create-failed' as const,
			error: error instanceof Error ? error.message : String(error),
			idempotencyKey,
		}
	}
}