fix(host-service): fall back to prompt slug when LLM replies conversationally

Closed
AN
3mo ago
+201-2

Fixes #5288

Problem

When the workspace prompt contains a URL (e.g. a Jira task link), the branch-naming model ignores the "return ONLY the branch name" instruction and answers conversationally:

I'd be happy to help you implement that task, but I don't have access to...

Two consequences:

  1. The old sanitizer let that whole sentence through as a kebab-case branch name (i-d-be-happy-to-help-you-implement-that-task-...).
  2. Because the model returns the identical reply regardless of the URL, distinct prompts produced identical names — and the deduplicator then suffixed them into near-identical branches, which is why de-duplication appeared broken.

Fix

In ai-branch-name.ts:

  • isPlausibleBranchName(candidate) — rejects sanitized replies with more than 8 kebab segments (a real branch name is 2–4 words; a conversational reply is a whole sentence).
  • slugifyPrompt(prompt) — derives a deterministic branch name from the prompt itself: strips URLs, drops stop words, keeps the first 4 meaningful words. "Implement this jira task: https://jira.example.com/TASK-123" → jira-task.
  • generateBranchNameFromPrompt falls back to the prompt slug when the model's reply is implausible; the result still runs through the existing deduplicateBranchName.

Validation

ai-branch-name.test.ts — 7 unit tests: plausibility gate (accepts feat-auth-flow, rejects the conversational reply), slugifier (URL stripping + empty fallback), conversational fallback (exact #5288 repro), and dedup of the fallback slug (jira-task → jira-task-2).

tsc --noEmit: 0 errors · Biome: clean.


Summary by cubic

Prevents conversational LLM replies from becoming branch names by rejecting implausible outputs and falling back to a prompt-derived slug. Fixes #5288; names are deterministic, deduplicated, and capped at 100 characters.

  • Plausibility gate: rejects sanitized candidates with more than 8 kebab segments.
  • Prompt slug fallback: strips URLs, drops stop words, keeps the first 4 meaningful words; sanitizes and enforces the 100‑char limit; defaults to "workspace".
  • Apply fallback when the generated string is implausible or sanitizes to empty; then pass through existing de-duplication.
  • Tests cover null-model, generation timeout/error, long-slug truncation, and dedup on both fallback and plausible names; mocks reset after each test.

Summary by CodeRabbit

  • New Features

    • Improved automatic branch-name generation with clearer, more concise names.
    • Added fallback names based on the request when generated names are too long, conversational, or empty.
    • Removes URLs and common filler words from fallback names.
    • Limits fallback names to a practical length and uses “workspace” when no suitable name is available.
    • Prevents duplicate branch names by checking existing branches.
  • Bug Fixes

    • Rejects implausible generated branch names and applies a reliable fallback.