Skip to content

Generate thread titles for scripts that do not use spaces - #3990

Merged
ymichael merged 1 commit into
mainfrom
bb/session-title-summary-is-invalid-for-cjk-charact-thr_f95s7km2rk
Sep 21, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/session-title-summary-is-invalid-for-cjk-charact-thr_f95s7km2rk

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Thread title generation measured text in whitespace-delimited tokens and UTF-16 code units, neither of which means anything in scripts that do not separate words with spaces. shouldGenerateThreadTitle gated inference on text.split(/\s+/u).length >= 5, so a Chinese or Japanese prompt is one token at any length and the model was never called — the thread kept the raw prompt prefix as its label forever. The same mistake appears twice more: sanitizeGeneratedTitle capped titles at five space-delimited words, which is a no-op for Chinese and Japanese (any length passed through) and destructive for Korean, and deriveTitleFallback sliced at 77 UTF-16 code units, which splits a surrogate pair on astral characters such as 𠮷 and emits a lone surrogate. Reported on Discord (#bugs, laogui6132, 2026-09-21, "Session title summary is invalid for cjk characters"); no GitHub issue was filed. The sidebar is not involved — .bb-thread-title truncates with CSS ellipsis, which is width-based and script-agnostic.

What changed

New apps/server/src/services/lib/text-measure.ts gives all three call sites one measure: Intl.Segmenter for word and grapheme boundaries, plus a display width that charges wide characters two columns. In apps/server/src/services/threads/title-generation.ts, eligibility now counts word-like segments, the generated title breaks at the last word boundary within a 48-column budget, and the fallback elides on grapheme boundaries within an 80-column budget. packages/templates/src/templates/generate-thread-metadata.md no longer states its limit in English words.

Latin behaviour is unchanged by construction: display width equals length for ASCII, so deriveTitleFallback is byte-identical on English input and the pre-existing assertion "Investigate Extremely Long Generated Thread Title Output" → "Investigate Extremely Long Generated Thread" still holds untouched. No wire, CLI, or configuration surface changed, so no HOST_DAEMON_PROTOCOL_VERSION bump.

How you verified

Reproduced on a running instance from this worktree (pnpm start:worktree) before changing anything, creating threads via bb thread spawn whose first message was CJK text of varying lengths. Before the fix, the Chinese (64 chars) and Japanese (57 chars) threads recorded reason: "too-short" with durationMs: 0 — proving the model was never called — and stayed untitled, while a 123-character English prompt produced Investigate sidebar environment labels. The sidebar DOM for those rows contained the entire prompt. Korean reached inference but was cut to the dangling 포크 후 사이드바 스레드 행의, losing its verb.

Rebuilt and re-ran the same inputs after the fix:

Input Before After
Chinese, 64 chars (untitled, too-short) 调查分叉线程环境标记错误
Japanese, 57 chars (untitled, too-short) フォーク後の環境バッジ誤表示を調査
Korean, 65 chars 포크 후 스레드 환경 배지 포크 후 사이드바 환경 배지 오류 조사
Mixed zh+en 修复分叉后侧边栏徽章 修复分叉后 sidebar badge 错误
English, 123 chars Investigate sidebar environment labels Fix sidebar thread environment labels
Chinese, 6 chars (untitled, too-short) (untitled, too-short) — correctly skipped

23 new or changed test cases in apps/server/test/services/text-measure.test.ts and apps/server/test/threads/title-generation.test.ts cover Chinese, Japanese, Korean, mixed and English, surrogate pairs, emoji ZWJ clusters, and the hard-cut path when a single word exceeds the budget. Each fails on the parent commit and passes here.

pnpm exec turbo run test --filter=@bb/server --filter=@bb/templates — 3032 server tests and 47 template tests pass. pnpm exec turbo run typecheck lint --filter='...[origin/main]' — 67 tasks, clean.

AGENT GENERATED

🤖 Generated with Claude Code

Thread title generation measured text in whitespace-delimited tokens and
UTF-16 code units, which is meaningless for Chinese, Japanese and other
scripts that do not separate words with spaces.

Root cause, in apps/server/src/services/threads/title-generation.ts:

- shouldGenerateThreadTitle required `text.split(/\s+/u).length >= 5`.
  A Chinese or Japanese prompt of any length is one token, so inference
  was never attempted and the thread kept the raw prompt prefix as its
  label. Reproduced on a running instance: a 64-character Chinese prompt
  recorded `reason: "too-short"` with `durationMs: 0`, while a
  123-character English prompt produced "Investigate sidebar environment
  labels".
- sanitizeGeneratedTitle capped titles at five space-delimited words.
  That is a no-op for Chinese and Japanese (any length passed through)
  and destructive for Korean, which cut "포크 후 사이드바 스레드 행의
  잘못된 환경 배지 조사" down to the dangling "포크 후 사이드바 스레드 행의".
- deriveTitleFallback sliced at 77 UTF-16 code units, which splits a
  surrogate pair for astral characters such as 𠮷 and emits a lone
  surrogate, and spends the 80-unit budget on text that renders twice
  as wide.

All three now share one measure in services/lib/text-measure.ts:
Intl.Segmenter for word and grapheme boundaries, plus a display width
that charges wide characters two columns. Eligibility counts word-like
segments, the generated title breaks at the last word boundary within a
48-column budget, and the fallback elides on grapheme boundaries within
an 80-column budget. The prompt template no longer states the limit in
English words.

Latin behaviour is unchanged: display width equals length for ASCII, and
"Investigate Extremely Long Generated Thread Title Output" still yields
"Investigate Extremely Long Generated Thread".

Verified on a running instance from this worktree. Before the fix,
Chinese and Japanese threads were untitled and the sidebar showed the
full prompt; after it they are titled "调查分叉线程环境标记错误" and
"フォーク後の環境バッジ誤表示を調査", Korean gained "포크 후 사이드바 환경
배지 오류 조사", English still yields "Fix sidebar thread environment
labels", and the short "修复这个错误" is still correctly skipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
@ymichael
ymichael force-pushed the bb/session-title-summary-is-invalid-for-cjk-charact-thr_f95s7km2rk branch from e8784bd to da6065d Compare September 21, 2026 16:45
@ymichael
ymichael merged commit bb8fc9e into main Sep 21, 2026
16 checks passed
@ymichael
ymichael deleted the bb/session-title-summary-is-invalid-for-cjk-charact-thr_f95s7km2rk branch September 21, 2026 17:03
ymichael added a commit that referenced this pull request Sep 21, 2026
## Human comments

## What was wrong

The Unicode-aware text measurement introduced in #3990 was local to the
server title generator, while other compact user-facing labels still
treated JavaScript UTF-16 code-unit length as display width. Wide-script
text could therefore occupy roughly twice its intended terminal or UI
width. Compact-label truncation could split grapheme clusters such as
joined emoji, while hard `slice()` boundaries could split astral
characters at surrogate-pair boundaries. The repository's generic HTML
escaping and UTF-16 slicing helpers also lived in `@bb/domain` despite
having no domain semantics.

## What changed

Added a dependency-free internal `@bb/text-utils` package and moved text
measurement, UTF-16-safe slicing, and HTML escaping out of `@bb/domain`.
Core server, database, desktop, client, CLI, Connect, host-daemon,
thread-view, provider-bridge, and Plugin Guide consumers import those
primitives from the dedicated package. The Tasks and Workflows plugins
keep equivalent narrowly scoped helpers inside their own packages rather
than depending on a private `@bb/*` utility package.

CLI table widths and truncation, queued-message previews, terminal
command titles, timeline separators, workflow fallback labels, and Tasks
delegated-thread titles use grapheme-safe display-width measurement. New
`sliceUtf16Head` and `sliceUtf16Tail` helpers preserve the existing
code-unit limits while preventing hard caps from splitting surrogate
pairs in blocker and conversation summaries, queued-message reasons,
provider presentation text, todo snapshots, terminal titles, daemon
payload previews, and provider stderr tails. Connect and Plugin Guide
HTML generation now use the shared escaper. Emoji width detection uses
Unicode emoji-presentation properties and recognizes variation-selector
and keycap sequences. Flags, hearts, airplanes, keycaps, and watches
occupy two columns, preventing CLI table rendering from unexpectedly
truncating titles or emitting lone surrogates. Text-presentation symbols
remain narrow. The same detection is used in both plugin-local helpers.
Added focused coverage for HTML escaping, surrogate boundaries, CJK
text, astral ideographs, joined emoji, and rendered emoji tables. There
are no server/daemon wire changes and no `HOST_DAEMON_PROTOCOL_VERSION`
change.

The queued-message preview adds 99 raw bytes to the measured app boot
payload. After rebasing, the raw boot allowance increases by 512 bytes
relative to current main, from 1,826,261 to 1,826,773 bytes; the Brotli
cap remains unchanged. The current production bundle is 1,698.3 KiB raw
/ 417.3 KiB Brotli and passes the bundle budget.

## How you verified

Added regression tests in `@bb/text-utils`, `@bb/cli`,
`@bb/client-core`, `bb-plugin-workflows`, and `bb-plugin-tasks` that
fail under code-unit truncation and pass with display-width truncation.
Existing database and desktop coverage verifies the moved UTF-16 and
HTML helpers at their consumers.

Ran:

`pnpm exec turbo run test typecheck --filter=@bb/text-utils
--filter=@bb/domain --filter=@bb/db --filter=@bb/server --filter=@bb/cli
--filter=@bb/client-core --filter=bb-plugin-workflows
--filter=bb-plugin-tasks`

All 24 original Turbo tasks passed. The run included 3,037 server tests,
826 CLI tests, 590 database tests, 394 Tasks tests, 301 client-core
tests, 248 Workflows tests, 213 domain tests, and 11 initial text-utils
tests. After localizing the plugin helpers, both plugin typechecks and
all 642 plugin tests passed, and the Tasks bundled-plugin build passed.
Desktop typecheck and the 22 directly affected local-view, log-viewer,
and existing-server-dialog tests passed.

For the expanded core sweep, all 12 affected typecheck tasks passed,
along with 5,088 app tests, 3,037 server tests, 411 thread-view tests,
284 provider-bridge protocol tests, 121 Connect tests, 76 Plugin API map
tests, and 13 text-utils tests. The host-daemon suite passed 726 of 727
tests on its broad concurrent run; its unrelated Git credential-prompt
timing test passed immediately when rerun in isolation, as did the
directly affected 35 terminal-manager tests. A fresh production app
build and `node apps/app/scripts/check-bundle-budget.mjs` passed.

The emoji-width regression tests failed before the fix (all five
rendered-table cases) and passed afterward. The focused verification
passed all 38 tests: 19 text-utils tests, 7 CLI table tests, 10 Tasks
delegation tests, and 2 Workflows UI-view tests. Text-utils typecheck
and `git diff --check` also passed. Commands:

```sh
pnpm exec turbo run test typecheck --filter=@bb/text-utils
pnpm exec turbo run test --filter=@bb/cli -- src/__tests__/table.test.ts
pnpm exec turbo run test --filter=bb-plugin-tasks -- delegate/delegate.test.ts
pnpm exec turbo run test --filter=bb-plugin-workflows -- src/ui-view.test.ts
```

> AGENT GENERATED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant