Files
orca/src/shared/branch-name-from-work.ts
T
Brennan Benson ce910e5d52 fix(naming): keep the built-in branch-name prompt general (#9088)
* fix(naming): keep the built-in branch-name prompt general

The shipped auto-rename prompt no longer hard-codes style rules (word
count, kebab-case, no prefixes). Users can already override naming via
Source Control AI instructions and the branch-name command template; a
prescriptive default fought those overrides. Git-safe sanitization still
runs after generation.

* fix(naming): preserve branch prompt overrides

* chore(skills): refresh rc.2 manifest version
2026-07-16 18:33:34 -07:00

131 lines
4.7 KiB
TypeScript

import { MARINE_CREATURES } from './marine-creatures'
// Why: post-generation sanitization still bounds the leaf so a long model dump
// cannot become an unreadable branch. The *prompt* stays general — users can
// override naming style via Source Control AI instructions / templates.
export const MAX_BRANCH_NAME_WORDS = 4
const CREATURE_NAMES_LOWER = new Set(MARINE_CREATURES.map((name) => name.toLowerCase()))
function normalizeName(name: string): string {
return name.trim().toLowerCase()
}
/**
* True when `branchLeaf` (the segment after any `prefix/`) is an Orca
* auto-generated creature name, e.g. `Nautilus` or `Octopus-2`. Used as the
* eligibility gate so auto-rename only ever overwrites a name Orca itself
* picked — never a branch the user named deliberately.
*/
export function isAutoGeneratedCreatureBranchName(branchLeaf: string): boolean {
const normalized = normalizeName(branchLeaf)
if (!normalized) {
return false
}
// Collision suffixes (`-2`, `-3`, …) are appended at creation; strip one
// before checking membership so numbered creatures still qualify.
const base = normalized.replace(/-\d+$/, '')
return CREATURE_NAMES_LOWER.has(base)
}
/**
* Turn raw model output into a safe, short kebab-case branch leaf. Returns ''
* when nothing usable remains, which callers treat as "skip the rename".
*/
export function sanitizeBranchSlug(raw: string, maxWords = MAX_BRANCH_NAME_WORDS): string {
const words = raw
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.split('-')
.filter(Boolean)
if (words.length === 0) {
return ''
}
return words.slice(0, maxWords).join('-')
}
/**
* Drop a leading prefix segment the generation model sometimes prepends (e.g.
* with a `tmchow/` branch prefix it emits `tmchow/worktree-spinner`, which
* `sanitizeBranchSlug` folds to `tmchow-worktree-spinner`). Left alone, that
* leaks the prefix into both the branch leaf (yielding a doubled
* `tmchow/tmchow-...`) and the humanized display name. Strips only when the
* leading segment matches the *configured* prefix, so a work-derived name that
* merely starts with a real word survives. Prefix-only output (the model
* echoed just `tmchow`) yields an empty slug so the caller skips the rename —
* otherwise it would double-prefix to `tmchow/tmchow`.
*/
export function stripConfiguredBranchPrefix(
slug: string,
prefix: string | null | undefined
): string {
if (!prefix) {
return slug
}
const prefixSlug = prefix
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
if (!prefixSlug) {
return slug
}
if (slug === prefixSlug) {
return ''
}
return slug.startsWith(`${prefixSlug}-`) ? slug.slice(prefixSlug.length + 1) : slug
}
/**
* Turn a branch slug into a readable workspace display label, e.g.
* `supported-models-list` → `Supported models list`. Used to update the
* sidebar name alongside the branch so the two stay in sync.
*/
export function humanizeBranchSlug(slug: string): string {
const joined = slug.split('-').filter(Boolean).join(' ')
if (!joined) {
return ''
}
return joined.charAt(0).toUpperCase() + joined.slice(1)
}
export type BranchNameWorkContext = {
/** The user's first prompt to the agent in this workspace. */
firstPrompt: string
/** The agent's first response, when it has already arrived. Optional because
* the rename fires as soon as work begins, before a reply may exist. */
assistantMessage?: string
}
/**
* Build the text-generation prompt that asks the configured agent to name the
* work. Kept in shared so the prompt is identical across local and SSH targets.
*
* Why: the shipped default stays general. Naming style is left open so users
* who override via Source Control AI instructions / the branch-name command
* template are not fighting a rigid built-in rule list. Git-safety (kebab,
* length) is enforced after generation by sanitizeBranchSlug.
*/
export function buildBranchNamePrompt(context: BranchNameWorkContext, customPrompt = ''): string {
const sections: string[] = []
const prompt = customPrompt.trim()
// Why: when the user supplies naming guidance, lead with it so their
// override owns style rather than sitting under a prescriptive default.
if (prompt) {
sections.push(prompt, '')
}
sections.push(
prompt
? 'Generate a git branch name that summarizes the coding task described below.'
: 'Generate a short git branch name that summarizes the coding task described below.',
'Output ONLY the branch name on a single line, nothing else.',
''
)
sections.push('User request:', context.firstPrompt.trim())
const assistant = context.assistantMessage?.trim()
if (assistant) {
sections.push('', "Agent's initial response:", assistant)
}
return sections.join('\n')
}