# Branch naming

A branch name follows its current conversation endpoint; an explicit fork uses another endpoint. Rewind preserves the current branch identity and name, even when its endpoint temporarily rests on a shared ancestor of an existing sibling. The trunk has no synthesized `main` name and follows the same rules.

## Who wins

| Tier | Source | Locks | Automatic naming may overwrite |
| --- | --- | --- | --- |
| Highest | User rename (`rename_branch`, Branches panel, `/branch <name>`) or user-requested LLM name (`auto_name_branch`, `/branch`, TUI `/branch rename`) | Sets `name_locked` | No |
| Spawn | Label of a spawned task, recorded at admission on the reply the run will write (`<job>_user_reply`), so the agent list and Activity name the branch while it runs; written again at the end only if the branch has no name | No | Only if the write-back passes its checks |
| Automatic | Background LLM label at thresholds | No | Yes |
| Fallback | First 8 hex digits of `head_msg_id` | No | Replaced by any stored name |

Whether a name is protected depends on whether the user asked for it, not on whether an LLM wrote it.

## One turn on a branch

1. **`finalize_turn`** Skipped for background turns; their branches are hidden when they end.
2. **`carry_branch_entry`** A recorded branch's entry has already moved with each message the turn wrote (`follow_line` for the selected branch, `carry_branch` for another), merged with any entry written at the new tip, so a rename mid-turn keeps the counter. For a line without a branch record, this moves the entry from the tip this turn extended to the new head, so name, lock and counter follow the branch. An ordinary fork point stays put; the fork starts fresh. Continuing after Rewind carries the current entry even when a sibling exists, preserving its name, lock and counter.
3. **`maybe_auto_name_branch`** Stops if archived. Otherwise `turns` + 1, locked or not; a locked branch gets no automatic label.
4. **`turns` in 1, 6, 16, 40** Background thread: last 6 messages, at most 2000 characters, 2 to 6 words, capped at 40 characters.
5. **Write back** Rereads the entry and discards the label if it is now locked or archived (the lock is checked again on the record inside the write lock); otherwise stores it and refreshes `branches_list`.

## State

```
branches: {
  <head_msg_id>: {
    name, created_at, updated_at,
    auto_named: bool,
    name_locked: bool,
    name_gen_count: int,
    turns: int,
  }
}
```

In session `meta.json`, written by `set_branch_name`, which merges so a name-only write keeps the lock and counters. Every writer merges on disk under the session write lock ([branch records](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/session/storage-consistency.html#branch-records)). A write addressed to a tip the branch has since moved past (the previous reply, or this turn's own message) goes to the branch's record at its tip now.

| Display | Shows |
| --- | --- |
| Branches panel | Name, else the short id (`graph/branches.py`) |
| DAG badge | Name, else the text the user asked on that branch, else the short id (`dag/render/labels/badges.ts`) |

The transcript goes inside `<branch>` tags with an instruction to treat it as data, so a message cannot rename the branch.

**Compared with session titles**

|  | Session | Branch |
| --- | --- | --- |
| Placeholder | First line, 50 characters (`_title_from_text`) | Short id: a branch is another possibility at the same position |
| Prompt | `_TITLE_SYSTEM_PROMPT`, 3 to 7 words | `build_branch_name_prompt`, 2 to 6 words; separate semantics, so separate prompts |
| Thresholds | `_RETITLE_AT_TURNS` | Same constant |
| Turn count | Assistant messages across the whole session | The branch's own `turns`: unaffected by other branches, no walk to the fork point |
| Lock | `_user_titled`, top level of `meta.json` | `name_locked`, in the branch entry |
| Broadcast | `session_updated` | `branches_list` refresh |
| Background thread | `_bg()` in `titles.py` | Own thread; write-back targets differ, so nothing is shared |

**Where it lives**

| Concern | Source |
| --- | --- |
| Entry, carry, counter | `openprogram/state/store/session/session_store/branches.py` |
| Thresholds, prompt, background naming | `openprogram/runtime/conversation/turn_runtime/titles.py` |
| Turn-end trigger | `openprogram/runtime/conversation/turn_runtime/finalize.py` |
| Rename and on-demand actions | `openprogram/interfaces/server/ws_actions/branch.py`. Both default to the session's `head_id`. On-demand naming is synchronous and uses the chat runtime. |
| Composer command | `apps/web/components/chat/composer/slash/slash-commands.ts` |

## Boundaries

- Labels remain in session metadata and branch references; no separate naming registry.
- Branch naming changes neither the session title nor the DAG parent edges.

## Implementation status

Implemented. Worth testing when this changes: manual rename against a concurrent automatic result, unnamed trunk, fork progression, and the WebSocket update after background naming.
