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

156 lines · 4.9 KB · TypeScript
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { buildTriageAgentPrompt } from './agent-prompt.ts'
import {
	buildFleetRateAgentPrompt,
	buildKodyPlatformAgentPrompt,
} from './kody-agent-prompt.ts'
import { emptyLoopGuardSnapshot } from './loop-guard.ts'
import { emptyPeerContext } from './peer-match.ts'
import type { FingerprintRecord } from './shared.ts'

function record(): FingerprintRecord {
	return {
		fingerprint: 'workflow:remote-to-mcp-codemod:timeout-90s',
		surface: 'workflow',
		owner: 'remote-to-mcp-codemod',
		package_id: 'pkg-1',
		kody_id: 'remote-to-mcp-codemod',
		error_family: 'timeout-90s',
		sample_message: 'Execution timed out after 90s',
		sample_run_id: 'run-1',
		activity_url: 'https://kody.codes/account/activity/run-1',
		sample_run_ids: ['run-1'],
		count: 3,
		first_seen: '2026-08-18T17:00:00.000Z',
		last_seen: '2026-08-18T17:30:00.000Z',
		status: 'queued',
		agent_id: null,
		agent_url: null,
		kody_agent_id: null,
		kody_agent_url: null,
		classification: null,
		discord_message_id: 'msg-1',
		spawned_at: null,
		completed_at: null,
		outcome: null,
		summary: null,
	}
}

describe('spawn prompts', () => {
	it('includes peer snapshot and a precomputed loop-guard snapshot', () => {
		const prompt = buildTriageAgentPrompt({
			record: record(),
			discordMessageId: 'msg-1',
			openSiblingCount: 3,
			peerContext: {
				...emptyPeerContext(),
				agents: [
					{
						id: 'bc-peer',
						name: 'kody-issue timeout-90s',
						status: 'IDLE',
						url: 'https://cursor.com/agents/bc-peer',
						createdAt: null,
						why: 'recent triage agent matching owner or error family',
					},
				],
			},
			loopGuard: {
				...emptyLoopGuardSnapshot(),
				gatheredAt: '2026-08-19T06:00:00.000Z',
				capRemaining: 2,
				spawnedThisHour: 1,
				alreadyEscalatedToKody: false,
				matchingOpenRunCount: 1,
				openRunsScanned: 50,
				matchingOpenRuns: [
					{
						id: 'run-open-1',
						surface: 'workflow',
						name: './fleet',
						kodyId: 'remote-to-mcp-codemod',
						startedAt: '2026-08-19T05:50:00.000Z',
						activityUrl: 'https://kody.codes/account/activity/run-open-1',
						errorMessage: 'Execution timed out after 90s',
					},
				],
			},
		})
		assert.match(prompt, /already gathered/)
		assert.match(prompt, /Do not start by repeating those calls/)
		assert.match(prompt, /capRemaining: 2/)
		assert.match(prompt, /run-open-1/)
		assert.equal(prompt.includes('do this first'), false)
		assert.match(prompt, /Peer work already in flight/)
		assert.match(prompt, /bc-peer/)
		assert.match(prompt, /kody:@kentcdodds\/kody-issue-triage\/peer-context/)
		assert.match(prompt, /getAgent/)
		assert.match(prompt, /getPrInfo/)
		assert.match(prompt, /LIVE peer already owns this family/)
		assert.match(prompt, /same coarse error family/)
		assert.match(prompt, /Never leave a fingerprint in `spawned`/)
		assert.match(prompt, /Do not page yourself/)
		assert.match(prompt, /placeholder ids/)
		assert.match(prompt, /smoke-test/)
		assert.match(prompt, /needs-Kent-decision/)
		assert.match(prompt, /title: 'short decision title'/)
		assert.match(prompt, /Choosable option A/)
		assert.match(prompt, /Every option needs an impact tag/)
	})

	it('seeds the platform prompt with related PRs', () => {
		const prompt = buildKodyPlatformAgentPrompt({
			record: record(),
			reason: 'workflow step budget',
			obvious: false,
			discordMessageId: 'msg-1',
			peerContext: {
				...emptyPeerContext(),
				pullRequests: [
					{
						repo: 'kentcdodds/kody',
						number: 44,
						title: 'Raise workflow timeout-90s budget',
						url: 'https://github.com/kentcdodds/kody/pull/44',
						draft: false,
						updatedAt: null,
						agentId: null,
						why: 'open PR on a Kody repo matching owner or error family',
					},
				],
			},
		})
		assert.match(prompt, /kentcdodds\/kody#44/)
		assert.match(prompt, /review that work instead of opening a second PR/)
		assert.match(prompt, /Do not start by calling/)
		assert.match(prompt, /needs-Kent-decision/)
		assert.match(prompt, /title: 'short decision title'/)
	})

	it('tells the fleet-rate agent to investigate Kody, not other users', () => {
		const prompt = buildFleetRateAgentPrompt({
			record: {
				...record(),
				fingerprint: 'rate:fleet:day:2026-08-22',
				surface: 'fleet-rate',
				owner: 'kody-platform',
				package_id: null,
				kody_id: null,
				error_family: 'package-error-rate-day',
				sample_message: 'Fleet user-package runtime error rate elevated (day, absolute_delta).',
				sample_run_id: null,
				activity_url: 'https://kody.codes/admin/insights',
				sample_run_ids: [],
			},
			discordMessageId: 'msg-1',
		})
		assert.match(prompt, /operator telemetry about \*\*Kody itself\*\*/)
		assert.match(prompt, /Do \*\*not\*\* read another user's Activity/)
		assert.match(prompt, /usage_aggregation/)
		assert.equal(prompt.includes('run_get'), false)
		assert.match(prompt, /needs-Kent-decision/)
		assert.match(prompt, /title: 'short decision title'/)
	})
})