Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

actionable.ts

177 lines · 7.9 KB · TypeScript
/**
 * Decide whether a human comment asks for anything. Pure and deterministic.
 *
 * Three outcomes:
 * - definitely actionable: a question, request or imperative wording, concern
 *   or negation wording, code (fence, inline code, suggestion block), or a
 *   task list.
 * - definitely non-actionable: short, carries an appreciation or approval
 *   signal (thanks, LGTM, praise words, emoji), and nothing else.
 * - unsure (`unsure: true`): none of the definite signals, but not clearly an
 *   acknowledgement either (a link, a long comment, extra content, or no
 *   appreciation signal). Unsure counts as actionable here; the caller may ask
 *   a model for a second opinion (see `./actionable-model.ts`).
 *
 * Formal review states are handled by the caller (CHANGES_REQUESTED always
 * needs an answer).
 */

export type Actionability = {
	actionable: boolean
	/** Short machine-friendly reason, for example `question` or `acknowledgement`. */
	reason: string
	/** The rules could not decide. `actionable` is true (fail closed) until a model verdict says otherwise. */
	unsure?: true
}

function unsure(reason: string): Actionability {
	return { actionable: true, reason, unsure: true }
}

/** Longer comments are assumed to carry substance, even when they open with thanks. */
export const MAX_ACK_WORDS = 25
/** Content words allowed beside the appreciation signal ("great work on the refactor"). */
export const MAX_EXTRA_WORDS = 3

const suggestionRe = /```\s*suggestion\b/i
const codeFenceRe = /```|~~~/
const inlineCodeRe = /`[^`\n]+`/
const linkRe = /\bhttps?:\/\/|\bwww\./i
const questionRe = /[?\uFF1F\u00BF]/
const taskListRe = /^\s*[-*]\s*\[[ xX]\]/m

/** Requests and imperatives. Matched as whole words, case-insensitive. */
const requestWords = [
	'please', 'pls', 'plz', 'kindly',
	'can', 'could', 'would', 'should', 'shall', 'must', 'might', 'may',
	'need', 'needs', 'needed', 'require', 'requires', 'required',
	"let's", 'lets', 'let us',
	'want', 'wants', 'wish', 'hope', 'expect',
	'change', 'changes', 'fix', 'fixes', 'add', 'adds', 'remove', 'delete', 'drop',
	'rename', 'update', 'move', 'revert', 'rewrite', 'refactor', 'extract', 'split',
	'replace', 'use', 'avoid', 'consider', 'make', 'try', 'check', 'test', 'tests',
	'handle', 'ensure', 'verify', 'document', 'clean', 'simplify', 'rebase',
	'squash', 'wait', 'hold', 'block', 'unblock', 'run', 'rerun', 'address',
	'suggest', 'suggestion', 'propose', 'prefer', 'recommend', 'todo', 'nit', 'nitpick',
	'why', 'how', 'what', 'where', 'when', 'who', 'which', 'wonder', 'wondering',
	'maybe', 'perhaps', 'instead', 'rather', 'also', 'still',
	'follow-up', 'followup', 'later',
]

/** Concerns, defects, negation and contrast: "thanks, but this breaks X". */
const concernWords = [
	'but', 'however', 'though', 'although', 'except', 'unless', 'yet',
	'not', 'no', 'nope', 'never', 'without',
	'wrong', 'bug', 'bugs', 'buggy', 'broken', 'break', 'breaks', 'broke', 'breaking',
	'fail', 'fails', 'failed', 'failing', 'failure', 'error', 'errors', 'crash', 'crashes',
	'leak', 'leaks', 'issue', 'issues', 'problem', 'problems', 'regression', 'regress',
	'missing', 'typo', 'concern', 'concerned', 'worry', 'worried', 'unsure', 'confused',
	'confusing', 'unclear', 'incorrect', 'bad', 'ugly', 'hacky', 'risky', 'dangerous',
	'blocker', 'blocking', 'revisit',
]

/** Appreciation and approval signals (phrases first so they are consumed whole). */
const appreciationPhrases = [
	'thank you', 'thanks a lot', 'thanks so much', 'many thanks', 'looks good to me', 'looks good',
	'looks great', 'well done', 'good job', 'great job', 'good work', 'great work', 'nice work',
	'nice job', 'ship it', 'nicely done', 'love it', 'love this', 'much appreciated',
]
const appreciationWords = [
	'thanks', 'thank', 'thx', 'ty', 'tysm', 'cheers', 'lgtm', 'nice', 'great', 'awesome',
	'amazing', 'excellent', 'perfect', 'love', 'loved', 'cool', 'sweet', 'beautiful', 'brilliant',
	'fantastic', 'wonderful', 'neat', 'superb', 'stellar', 'kudos', 'bravo', 'congrats',
	'congratulations', 'yay', 'woo', 'woohoo', 'appreciate', 'appreciated', 'approved', 'approve',
	'nailed', 'solid', 'elegant', 'impressive', 'good', 'ok', 'okay', 'yes', 'yep',
	'ack', 'acknowledged', 'agreed', 'agree', 'done', 'merged',
]

/** Function words and polite filler that carry no request on their own. */
const fillerWords = [
	'a', 'an', 'the', 'this', 'that', 'these', 'those', 'it', "it's", 'its', 'is', 'are', 'was',
	'were', 'be', 'been', 'for', 'of', 'on', 'in', 'to', 'with', 'and', 'so', 'very', 'really',
	'much', 'lot', 'lots', 'all', 'you', 'your', "you're", 'u', 'i', "i'm", 'me', 'my', 'we', 'our',
	'us', 'everyone', 'folks', 'team', 'again', 'here', 'there', 'just', 'too', 'as', 'always',
	'super', 'totally', 'indeed', 'overall', 'looks', 'look', 'pr', 'pull', 'request', 'work',
	'job', 'one', 'stuff', 'thing', 'idea', 'oh', 'wow', 'hey', 'hi', 'haha', 'lol', 'man',
	'dude', 'buddy', 'friend', 'sir', 'mate', 'such', 'quick', 'fast', 'speedy', 'now',
]

function wordSet(words: Array<string>) {
	return new Set(words.map((w) => w.toLowerCase()))
}

const requestSet = wordSet(requestWords)
const concernSet = wordSet(concernWords)
const appreciationSet = wordSet(appreciationWords)
const fillerSet = wordSet(fillerWords)

const emojiRe = /\p{Extended_Pictographic}/u
const shortcodeRe = /:[a-z0-9_+-]+:/gi
const plusOneRe = /(?:^|\s)\+1\b/
const mentionRe = /(?:^|\s)@[a-z0-9-]+(?:\/[a-z0-9._-]+)?/gi
const contractionRe = /n't\b/i

function stripNoise(text: string): string {
	return String(text ?? '')
		.replace(/<!--[\s\S]*?-->/g, ' ')
		.replace(/^\s*>.*$/gm, ' ') // quoted text is someone else's words
		.trim()
}

/** Classify one human comment body. Empty bodies are non-actionable. */
export function classifyActionable(body: unknown): Actionability {
	const text = stripNoise(String(body ?? ''))
	if (!text) return { actionable: false, reason: 'empty' }
	if (suggestionRe.test(text)) return { actionable: true, reason: 'suggestion block' }
	if (codeFenceRe.test(text) || inlineCodeRe.test(text)) return { actionable: true, reason: 'code' }
	if (questionRe.test(text)) return { actionable: true, reason: 'question' }
	if (taskListRe.test(text)) return { actionable: true, reason: 'task list' }
	if (contractionRe.test(text)) return { actionable: true, reason: 'negation' }

	const hasEmoji = emojiRe.test(text) || /:[a-z0-9_+-]+:/i.test(text) || plusOneRe.test(text)
	let rest = text
		.replace(/\bhttps?:\/\/\S+|\bwww\.\S+/gi, ' ')
		.replace(shortcodeRe, ' ')
		.replace(mentionRe, ' ')
		.replace(/\p{Extended_Pictographic}|\p{Emoji_Modifier}|\u200d|\ufe0f/gu, ' ')
		.replace(/(?:^|\s)\+1\b/g, ' ')
		.toLowerCase()
		.replace(/[\u2018\u2019]/g, "'")

	const words = rest.match(/[\p{L}\p{N}][\p{L}\p{N}'-]*/gu) ?? []
	for (const phrase of ["let's", 'let us', 'follow-up']) {
		if (rest.includes(phrase)) return { actionable: true, reason: 'request' }
	}
	for (const w of words) {
		if (requestSet.has(w)) return { actionable: true, reason: 'request' }
		if (concernSet.has(w)) return { actionable: true, reason: 'concern' }
	}
	if (linkRe.test(text)) return unsure('link')
	if (!words.length) {
		// Only punctuation, emoji, shortcodes, or mentions.
		return hasEmoji ? { actionable: false, reason: 'emoji only' } : unsure('unclassified')
	}
	if (words.length > MAX_ACK_WORDS) return unsure('long comment')

	let appreciation = hasEmoji
	for (const phrase of appreciationPhrases) {
		if (rest.includes(phrase)) {
			appreciation = true
			rest = rest.split(phrase).join(' ')
		}
	}
	const remaining = (rest.match(/[\p{L}\p{N}][\p{L}\p{N}'-]*/gu) ?? []).filter((w) => {
		if (appreciationSet.has(w)) {
			appreciation = true
			return false
		}
		return !fillerSet.has(w)
	})
	if (!appreciation) return unsure('no acknowledgement signal')
	if (remaining.length > MAX_EXTRA_WORDS) return unsure('content beyond acknowledgement')
	return { actionable: false, reason: 'acknowledgement' }
}

export function isActionable(body: unknown): boolean {
	return classifyActionable(body).actionable
}