Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/ship-pr

Progressive ship-pr loop for agents: PR status, Bugbot, review sort, human review feedback, 7-day decisions, merge, deploy, wrap-up.

FollowFork

Files

actionable-model.test.ts

134 lines · 6.0 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { classifyActionable } from './actionable.ts'
import {
	MAX_BATCH,
	bodyHash,
	classifyUnsure,
	verdictKey,
	type ModelEvaluate,
	type UnsureComment,
	type VerdictCache,
} from './actionable-model.ts'

const praise =
	'Thanks so much for this, really appreciate the care you put into the migration and the docs, it makes the whole project nicer for everyone working on it every single day.'
const linkOnly = 'Nice! Related reading: https://example.com/notes'

function item(id: number, body: string): UnsureComment {
	return { key: verdictKey('comment', id, body), body, ruleReason: classifyActionable(body).reason }
}

function memoryCache(seed: Array<[string, { actionable: boolean; reason: string }]> = []) {
	const store = new Map(seed)
	const calls = { get: 0, put: 0 }
	const cache: VerdictCache = {
		async get(keys) {
			calls.get += 1
			return new Map(keys.filter((k) => store.has(k)).map((k) => [k, store.get(k)!]))
		},
		async put(rows) {
			calls.put += 1
			for (const r of rows) store.set(r.key, { actionable: r.actionable, reason: r.reason })
		},
	}
	return { cache, store, calls }
}

function model(answers: Record<string, unknown> | (() => Promise<unknown>)) {
	const calls: Array<Parameters<ModelEvaluate>[0]> = []
	const evaluate: ModelEvaluate = async (input) => {
		calls.push(input)
		if (typeof answers === 'function') return await answers()
		return { ok: true, answers, model: 'jev', usage: { input_tokens: 100 } }
	}
	return { evaluate, calls }
}

test('rules mark long praise and link-only comments unsure (and still actionable)', () => {
	for (const body of [praise, linkOnly]) {
		const r = classifyActionable(body)
		assert.equal(r.unsure, true, body)
		assert.equal(r.actionable, true, body)
	}
	assert.equal(classifyActionable('Thanks!').unsure, undefined)
	assert.equal(classifyActionable('Can you fix this?').unsure, undefined)
})

test('model says non-actionable: verdict is non-actionable, cached, one batched call', async () => {
	const { cache, store } = memoryCache()
	const m = model({ c0: { type: 'noul', noul: 0.04 }, c1: { type: 'noul', noul: 0.1 } })
	const r = await classifyUnsure([item(1, praise), item(2, linkOnly)], { evaluate: m.evaluate, cache })
	assert.equal(m.calls.length, 1)
	assert.deepEqual(Object.keys(m.calls[0].questions), ['c0', 'c1'])
	assert.equal((m.calls[0].state.comments as Record<string, string>).c0, praise)
	assert.equal(r.verdicts.get(item(1, praise).key)?.actionable, false)
	assert.equal(r.verdicts.get(item(1, praise).key)?.source, 'model')
	assert.match(r.verdicts.get(item(1, praise).key)!.reason, /p\(actionable\)=0\.04/)
	assert.equal(store.size, 2)
	assert.equal(r.stats.classified, 2)
})

test('model says actionable (or is not confident): verdict blocks', async () => {
	const m = model({ c0: { type: 'noul', noul: 0.91 }, c1: { type: 'noul', noul: 0.35 } })
	const r = await classifyUnsure([item(1, praise), item(2, linkOnly)], { evaluate: m.evaluate, cache: null })
	assert.equal(r.verdicts.get(item(1, praise).key)?.actionable, true)
	assert.equal(r.verdicts.get(item(2, linkOnly).key)?.actionable, true)
})

test('model error, timeout, and missing setup fail closed and are not cached', async () => {
	const cases: Array<() => Promise<unknown>> = [
		async () => {
			throw new Error('HTTP 402 Insufficient balance')
		},
		() => new Promise(() => {}),
		async () => ({ ok: false, error: 'cloudflareAccountId is not set' }),
	]
	for (const fn of cases) {
		const { cache, store } = memoryCache()
		const r = await classifyUnsure([item(1, praise)], { evaluate: model(fn).evaluate, cache, timeoutMs: 20 })
		const v = r.verdicts.get(item(1, praise).key)!
		assert.equal(v.actionable, true)
		assert.equal(v.source, 'fail-closed')
		assert.ok(r.stats.error)
		assert.equal(store.size, 0)
	}
	const none = await classifyUnsure([item(1, praise)], { evaluate: null, cache: null })
	assert.equal(none.verdicts.get(item(1, praise).key)?.source, 'fail-closed')
	const skipped = await classifyUnsure([item(1, praise)], { evaluate: model({}).evaluate, cache: null, skipReason: 'recent failure: x' })
	assert.match(skipped.verdicts.get(item(1, praise).key)!.reason, /recent failure/)
})

test('malformed model output fails closed', async () => {
	for (const answers of [{}, { c0: { type: 'noul', noul: 'no' } }, { c0: { type: 'noul', noul: 7 } }, { c0: null }]) {
		const r = await classifyUnsure([item(1, praise)], { evaluate: model(answers as Record<string, unknown>).evaluate, cache: null })
		const v = r.verdicts.get(item(1, praise).key)!
		assert.equal(v.actionable, true, JSON.stringify(answers))
		assert.equal(v.source, 'fail-closed', JSON.stringify(answers))
	}
	const garbage = await classifyUnsure([item(1, praise)], { evaluate: model(async () => 'not json').evaluate, cache: null })
	assert.equal(garbage.verdicts.get(item(1, praise).key)?.source, 'fail-closed')
	assert.equal(garbage.stats.error, 'unparseable model output')
})

test('cache hit skips the model; an edited body gets a new key', async () => {
	const key = item(1, praise).key
	const { cache } = memoryCache([[key, { actionable: false, reason: 'model: p(actionable)=0.03' }]])
	const m = model({})
	const r = await classifyUnsure([item(1, praise)], { evaluate: m.evaluate, cache })
	assert.equal(m.calls.length, 0)
	assert.equal(r.stats.cached, 1)
	assert.equal(r.verdicts.get(key)?.actionable, false)
	assert.notEqual(verdictKey('comment', 1, praise), verdictKey('comment', 1, praise + ' Edited.'))
	assert.equal(bodyHash('a<!-- marker -->'), bodyHash('a'))
})

test('batches at most MAX_BATCH comments per call; the rest fail closed', async () => {
	const items = Array.from({ length: MAX_BATCH + 3 }, (_, i) => item(i + 1, `${praise} ${i}`))
	const answers = Object.fromEntries(items.map((_, i) => [`c${i}`, { type: 'noul', noul: 0.01 }]))
	const m = model(answers)
	const r = await classifyUnsure(items, { evaluate: m.evaluate, cache: null })
	assert.equal(m.calls.length, 1)
	assert.equal(Object.keys(m.calls[0].questions).length, MAX_BATCH)
	assert.equal(r.stats.failedClosed, 3)
})