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/shared.test.ts

642 lines · 16.9 KB · TypeScript
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import {
	coarseErrorFamily,
	fingerprintFor,
	fleetRateFingerprintFor,
	isFleetRateFingerprint,
	matchFamiliesOverlap,
	isAgentExecuteOrigin,
	isUnsavedInlineWorkflow,
	nextFingerprintStatusAfterEvent,
	normalizeErrorFamily,
	retriageCooldownMs,
	shouldIgnoreAgentExecuteNoise,
	shouldIgnoreAgentExecuteTimeout,
	shouldIgnorePlatformWeather,
	skipReason,
	spawnSkipReasonForRecord,
	wrapperFamilyOf,
} from './shared.ts'

describe('normalizeErrorFamily', () => {
	it('collapses timeout durations and TypeError property names', () => {
		assert.equal(
			normalizeErrorFamily(
				'Execution timed out after 90s: Kody stopped observing the sandbox.',
			),
			'execution-timed-out',
		)
		assert.equal(
			normalizeErrorFamily('Execution timed out after 270s: Kody stopped observing the sandbox.'),
			'execution-timed-out',
		)
		assert.equal(
			normalizeErrorFamily(
				'Execution timed out after 2500ms: Kody stopped observing the sandbox. Nested work was asked to abort',
			),
			'execution-timed-out',
		)
		assert.equal(
			normalizeErrorFamily(
				'The platform interrupted this run before completion; outcome unknown.',
			),
			'platform-interrupted',
		)
		assert.equal(
			normalizeErrorFamily("Cannot read properties of undefined (reading 'sonos_list_groups')"),
			'cannot-read-properties-of-undefined',
		)
		assert.equal(
			normalizeErrorFamily("Cannot read properties of undefined (reading 'sonos_list_players')"),
			'cannot-read-properties-of-undefined',
		)
		assert.equal(
			normalizeErrorFamily("Cannot read properties of null (reading 'invoke')"),
			'cannot-read-properties-of-null',
		)
		assert.equal(
			normalizeErrorFamily(
				'Dynamic worker concurrency limit exceeded: each request may have up to 4 concurrent dynamic worker invocations.',
			),
			'dynamic-worker-concurrency-limit-exceeded',
		)
		assert.equal(
			normalizeErrorFamily(
				'Cal.com helper could not resolve that event type. Available event types: 587527 "30 Min Meeting" (30mins). Call list-event-types or summarize-booking-pages to pick a real id.',
			),
			'package-caller-validation',
		)
		assert.equal(
			normalizeErrorFamily(
				'get-available-slots requires eventTypeId (or eventTypeSlug + username/teamSlug). Available event types: 587527 "30 Min Meeting"',
			),
			'package-caller-validation',
		)
		assert.equal(
			normalizeErrorFamily(
				'Cal.com API request failed with 404 Not Found: Event type with id 999999999 not found Available event types: 587527 "30 Min Meeting"',
			),
			'package-caller-validation',
		)
		assert.equal(
			normalizeErrorFamily(
				'Shade workflow event failed: {"key":"hot-east-office-reopen","failed":[{"label":"laundryEast"',
			),
			'shade-workflow-event-failed',
		)
		assert.equal(
			normalizeErrorFamily(
				'Shade workflow event failed: {"key":"kitchen-door-day-open","failed":[{"label":"kitchenDoor","error":"Cannot read properties of undefined (reading \'bond_shade_set_position\')"}]}',
			),
			'cannot-read-properties-of-undefined',
		)
	})
})

describe('wrapperFamilyOf', () => {
	it('strips instance keys from shade workflow wrappers', () => {
		assert.equal(
			wrapperFamilyOf(
				'Shade workflow event failed: {"key":"hot-east-music-reopen","failed":[',
			),
			'shade-workflow-event-failed',
		)
		assert.equal(
			wrapperFamilyOf(
				'shade-workflow-event-failed-key-hot-east-office-reopen-failed-label',
			),
			'shade-workflow-event-failed',
		)
	})
})

describe('fingerprintFor shade wrappers', () => {
	it('does not create one fingerprint per shade event key', () => {
		const owner = { surface: 'workflow', kody_id: 'shade-automation' }
		assert.equal(
			fingerprintFor({
				...owner,
				error_message:
					'Shade workflow event failed: {"key":"hot-east-office-reopen","failed":[{"label":"laundryEast"',
			}),
			fingerprintFor({
				...owner,
				error_message:
					'Shade workflow event failed: {"key":"kitchen-door-day-open","failed":[{"label":"kitchenDoor"',
			}),
		)
	})
})

describe('coarseErrorFamily', () => {
	it('maps stored variants onto one family', () => {
		assert.equal(coarseErrorFamily('timeout-90s'), 'execution-timed-out')
		assert.equal(
			coarseErrorFamily(
				'execution-timed-out-after-ns-kody-stopped-observing-the-sandbox-nested-work-wa',
			),
			'execution-timed-out',
		)
		assert.equal(
			coarseErrorFamily('cannot-read-properties-of-undefined-reading-sonos-list-groups'),
			'cannot-read-properties-of-undefined',
		)
		assert.equal(
			coarseErrorFamily('cannot-read-properties-of-undefined-reading-sonos-list-players'),
			'cannot-read-properties-of-undefined',
		)
		assert.equal(
			coarseErrorFamily(
				'shade-workflow-event-failed-key-hot-east-front-awning-reopen-failed-l',
			),
			'shade-workflow-event-failed',
		)
		assert.equal(
			coarseErrorFamily(
				'cal-com-helper-could-not-resolve-that-event-type-available-event-types-n-n-mi',
			),
			'package-caller-validation',
		)
		assert.equal(
			coarseErrorFamily(
				'get-available-slots-requires-eventtypeid-or-eventtypeslug-username-teamslug',
			),
			'package-caller-validation',
		)
		assert.equal(
			matchFamiliesOverlap(
				{
					error_family:
						'cal-com-helper-could-not-resolve-that-event-type-available-event-types-n-n-mi',
					sample_message:
						'Cal.com helper could not resolve that event type. Available event types: 587527',
				},
				{
					error_family:
						'get-available-slots-requires-eventtypeid-or-eventtypeslug-username-teamslug',
					sample_message:
						'get-available-slots requires eventTypeId (or eventTypeSlug + username/teamSlug).',
				},
			),
			true,
		)
		assert.equal(
			matchFamiliesOverlap(
				{
					error_family:
						'shade-workflow-event-failed-key-hot-east-office-reopen-failed-label',
					sample_message:
						'Shade workflow event failed: {"key":"hot-east-office-reopen","failed":[{"error":"Cannot read properties of undefined (reading \'bond_shade_set_position\')"}]}',
				},
				{
					error_family: 'cannot-read-properties-of-undefined',
					sample_message:
						'Shade workflow event failed: {"key":"hot-east-close","error":"Cannot read properties of undefined"}',
				},
			),
			true,
		)
	})
})

describe('skipReason', () => {
	it('skips smoke tests and sibling triage record-outcome throws', () => {
		assert.equal(
			skipReason({
				surface: 'export',
				kody_id: 'platform-feedback-triage',
				name: './record-outcome',
				error_message: 'unknown fingerprint: smoke-test-nonexistent-fingerprint',
			}),
			'smoke-test',
		)
		assert.equal(
			skipReason({
				surface: 'export',
				kody_id: 'platform-feedback-triage',
				name: './record-outcome',
				error_message: 'shipped requires prUrl https://github.com/kentcdodds/kody/pull/n',
			}),
			'sibling-triage-export',
		)
		assert.equal(
			skipReason({
				surface: 'workflow',
				kody_id: 'sentry-triage',
				name: './process-sentry-webhook',
				error_message: 'agent spawn failed: The operation was aborted due to timeout',
			}),
			null,
		)
	})

	it('skips every agent execute origin, not only ad-hoc execute', () => {
		assert.equal(
			skipReason({
				surface: 'execute',
				error_message: 'packages.invoke requires exportName',
			}),
			'agent-execute',
		)
		assert.equal(
			skipReason({
				surface: 'execute',
				kody_id: 'notion',
				package_id: 'pkg-notion',
				error_message: 'Cursor API 409',
			}),
			'agent-execute',
		)
		assert.equal(
			skipReason({
				surface: 'export',
				kody_id: 'notion',
				name: './smoke-test',
				error_message: 'Package was not found for secret access.',
				metadata: { source: 'execute', exportName: './smoke-test' },
			}),
			'agent-execute',
		)
		assert.equal(
			skipReason({
				surface: 'export',
				kody_id: 'morning-briefing',
				name: './run-morning-briefing',
				error_message: 'Missing morningBriefingDiscordChannelId value',
			}),
			null,
		)
		assert.equal(
			skipReason({
				surface: 'workflow',
				kody_id: 'sonos-house',
				name: './automations/sitting-room-piano/stop',
				error_message: "Cannot read properties of undefined (reading 'sonos_list_players')",
				metadata: { sourceType: 'package', exportName: './automations/sitting-room-piano/stop' },
			}),
			null,
		)
		assert.equal(
			skipReason({
				surface: 'subscription',
				kody_id: 'event-notifier',
				error_message:
					'Dynamic worker concurrency limit exceeded: each request may have up to 4 concurrent dynamic worker invocations.',
			}),
			null,
		)
		assert.equal(
			shouldIgnoreAgentExecuteNoise({
				surface: 'execute',
				error_message: 'Tool "email_send" not found',
			}),
			true,
		)
		assert.equal(
			shouldIgnoreAgentExecuteNoise({
				surface: 'subscription',
				kody_id: 'event-notifier',
				error_message:
					'Dynamic worker concurrency limit exceeded: each request may have up to 4 concurrent dynamic worker invocations.',
			}),
			false,
		)
		assert.equal(
			shouldIgnoreAgentExecuteNoise({
				surface: 'export',
				kody_id: 'bluesky',
				name: './create-post',
				metadata: { source: 'execute' },
				error_message: 'params.text is required',
			}),
			true,
		)
		assert.equal(
			shouldIgnoreAgentExecuteNoise({
				surface: 'export',
				kody_id: 'bluesky',
				name: './create-post',
				error_message: 'params.text is required',
			}),
			false,
		)
		assert.equal(
			isAgentExecuteOrigin({
				surface: 'export',
				metadata: { source: 'execute' },
			}),
			true,
		)
		assert.equal(
			isAgentExecuteOrigin({
				surface: 'workflow',
				metadata: { sourceType: 'package' },
			}),
			false,
		)
		assert.equal(
			isUnsavedInlineWorkflow({
				surface: 'workflow',
				name: 'inline-code',
				metadata: { sourceType: 'inline' },
				error_message: 'Execution timed out after 270s: Kody stopped observing the sandbox.',
			}),
			true,
		)
		assert.equal(
			skipReason({
				surface: 'workflow',
				name: 'inline-code',
				metadata: { sourceType: 'inline' },
				error_message: 'Execution timed out after 270s: Kody stopped observing the sandbox.',
			}),
			'agent-execute',
		)
		assert.equal(
			shouldIgnoreAgentExecuteTimeout({
				surface: 'workflow',
				name: 'inline-code',
				metadata: { sourceType: 'inline' },
				error_message: 'Execution timed out after 270s: Kody stopped observing the sandbox.',
			}),
			true,
		)
		assert.equal(
			shouldIgnoreAgentExecuteTimeout({
				surface: 'execute',
				error_message: 'Execution timed out after 90s: Kody stopped observing the sandbox.',
			}),
			true,
		)
		assert.equal(
			shouldIgnoreAgentExecuteTimeout({
				surface: 'execute',
				error_message: 'packages.invoke requires exportName',
			}),
			false,
		)
		assert.equal(
			shouldIgnoreAgentExecuteTimeout({
				surface: 'workflow',
				kody_id: 'personal-history',
				name: './daily-prompt',
				error_message: 'Execution timed out after 270s: Kody stopped observing the sandbox.',
			}),
			false,
		)
		assert.equal(
			skipReason({
				surface: 'workflow',
				name: 'package_publish_external_push',
				metadata: { sourceType: 'inline' },
				error_message: 'Execution timed out after 270s: Kody stopped observing the sandbox.',
			}),
			null,
		)
		assert.equal(
			isUnsavedInlineWorkflow({
				surface: 'workflow',
				kody_id: 'sonos-house',
				name: 'inline-code',
				metadata: { sourceType: 'package' },
			}),
			false,
		)
	})
})

describe('spawnSkipReasonForRecord', () => {
	it('skips execute-surface fingerprints and execute-sourced samples', () => {
		assert.equal(
			spawnSkipReasonForRecord({
				fingerprint: 'execute:ad-hoc:cursor-api-n',
				surface: 'execute',
				owner: 'ad-hoc',
				package_id: null,
				kody_id: null,
				sample_run_id: 'run-1',
			}),
			'agent-execute',
		)
		assert.equal(
			spawnSkipReasonForRecord(
				{
					fingerprint: 'export:notion:package-id-was-not-found-for-secret-access',
					surface: 'export',
					owner: 'notion',
					package_id: 'pkg-notion',
					kody_id: 'notion',
					sample_run_id: 'run-2',
				},
				{
					id: 'run-2',
					surface: 'export',
					kody_id: 'notion',
					metadata: { source: 'execute' },
				},
			),
			'agent-execute',
		)
		assert.equal(
			spawnSkipReasonForRecord(
				{
					fingerprint: 'workflow:sonos-house:cannot-read-properties-of-undefined',
					surface: 'workflow',
					owner: 'sonos-house',
					package_id: 'pkg-sonos',
					kody_id: 'sonos-house',
					sample_run_id: 'run-3',
				},
				{
					id: 'run-3',
					surface: 'workflow',
					kody_id: 'sonos-house',
					metadata: { sourceType: 'package' },
				},
			),
			null,
		)
		assert.equal(
			spawnSkipReasonForRecord({
				fingerprint: 'workflow:inline-code:execution-timed-out',
				surface: 'workflow',
				owner: 'inline-code',
				package_id: null,
				kody_id: null,
				sample_run_id: 'run-4',
			}),
			'agent-execute',
		)
		assert.equal(
			spawnSkipReasonForRecord({
				fingerprint: 'rate:fleet:hour:2026-08-23',
				surface: 'fleet-rate',
				owner: 'fleet',
				package_id: null,
				kody_id: null,
				sample_run_id: null,
			}),
			null,
		)
	})
})

describe('shouldIgnorePlatformWeather', () => {
	it('closes skills retriever isolate weather and system-email status interrupts', () => {
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'retriever',
				kody_id: 'skills',
				name: 'skills',
				error_name: 'platform_interrupted',
				error_message: 'The platform interrupted this run before completion; outcome unknown.',
			}),
			true,
		)
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'retriever',
				kody_id: 'skills',
				name: 'skills',
				error_message:
					'Execution timed out after 2500ms: Kody stopped observing the sandbox. Nested work was asked to abort',
			}),
			true,
		)
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'retriever',
				kody_id: 'skills',
				name: 'skills',
				error_message:
					'Dynamic worker concurrency limit exceeded: each request may have up to 10 concurrent dynamic worker invocations.',
			}),
			true,
		)
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'app_fetch',
				kody_id: 'system-email',
				name: '/api/status',
				error_name: 'platform_interrupted',
				error_message: 'The platform interrupted this run before completion; outcome unknown.',
			}),
			true,
		)
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'subscription',
				kody_id: 'event-notifier',
				error_message:
					'Dynamic worker concurrency limit exceeded: each request may have up to 4 concurrent dynamic worker invocations.',
			}),
			false,
		)
		assert.equal(
			shouldIgnorePlatformWeather({
				surface: 'workflow',
				kody_id: 'kody-issue-triage',
				error_message: 'Execution timed out after 90s: Kody stopped observing the sandbox.',
			}),
			false,
		)
	})
})

describe('nextFingerprintStatusAfterEvent', () => {
	const completed = '2026-08-18T00:00:00.000Z'
	const later = Date.parse(completed) + retriageCooldownMs + 60_000

	it('keeps in-flight rows and already-escalated kody issues closed', () => {
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'spawned',
				completedAt: null,
				kodyAgentId: null,
			}),
			'spawned',
		)
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'recommendation',
				completedAt: completed,
				kodyAgentId: 'bc-kody',
				runStartedAt: '2026-08-19T00:00:00.000Z',
				now: later,
			}),
			'recommendation',
		)
	})

	it('never re-queues ignored or resolved_noise standing fingerprints', () => {
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'resolved_noise',
				completedAt: completed,
				kodyAgentId: null,
				runStartedAt: '2026-08-19T01:00:00.000Z',
				now: later,
			}),
			'resolved_noise',
		)
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'ignored',
				completedAt: completed,
				kodyAgentId: null,
				runStartedAt: '2026-08-19T01:00:00.000Z',
				now: later,
			}),
			'ignored',
		)
	})

	it('re-queues skipped execute fingerprints on the next event', () => {
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'skipped_execute',
				completedAt: completed,
				kodyAgentId: null,
				runStartedAt: '2026-08-17T23:00:00.000Z',
				now: Date.parse(completed) + 1_000,
			}),
			'queued',
		)
	})

	it('does not re-queue leftover runs that started before the outcome', () => {
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'fixed',
				completedAt: completed,
				kodyAgentId: null,
				runStartedAt: '2026-08-17T23:00:00.000Z',
				now: later,
			}),
			'fixed',
		)
		assert.equal(
			nextFingerprintStatusAfterEvent({
				status: 'fixed',
				completedAt: completed,
				kodyAgentId: null,
				runStartedAt: '2026-08-19T01:00:00.000Z',
				now: later,
			}),
			'queued',
		)
	})
})

describe('fleet rate fingerprints', () => {
	it('uses rate:fleet:{window}:{day} and rejects user-bearing shapes', () => {
		assert.equal(
			fleetRateFingerprintFor({ window: 'day', day: '2026-08-22' }),
			'rate:fleet:day:2026-08-22',
		)
		assert.equal(isFleetRateFingerprint('rate:fleet:hour:2026-08-22'), true)
		assert.equal(isFleetRateFingerprint('workflow:shade-automation:timeout'), false)
		assert.equal(isFleetRateFingerprint('rate:fleet:day:user-1'), false)
	})
})