@@ -64,6 +64,23 @@ const DIVIDER_FRAME_MS = 60;
6464 /** Width of the bright pulse segment in the animated divider. */
6565 const PULSE_WIDTH = 5;
6666
67+/** Number of trailing words shown in the idle terminal-title preview. */
68+const TERMINAL_TITLE_WORD_COUNT = 5;
69+
70+/** Divider ticks spent on each animated terminal-title scanner frame. */
71+const TERMINAL_TITLE_TICKS_PER_FRAME = 4;
72+
73+/** Frames used for the active terminal-title glow-scanner animation. */
74+const TERMINAL_TITLE_FRAMES = [
75+ "[=o---]",
76+ "[-=o--]",
77+ "[--=o-]",
78+ "[---=o]",
79+ "[--o=-]",
80+ "[-o=--]",
81+ "[o=---]",
82+] as const;
83+
6784 /** Maximum number of committed messages rendered before older history is chunked. */
6885 const CONVERSATION_CHUNK_MESSAGES = 50;
6986
@@ -109,6 +126,15 @@ let dividerTimer: ReturnType<typeof setInterval> | null = null;
109126 /** Whether stdin was already in raw mode before the TUI initialized. */
110127 let stdinWasRaw = false;
111128
129+/** Latest application state associated with the active terminal UI. */
130+let titleState: AppState | null = null;
131+
132+/** Whether a cel viewport has rendered for the current UI session. */
133+let titleViewportActive = false;
134+
135+/** Last terminal title written during the current UI session. */
136+let lastTerminalTitle: string | null = null;
137+
112138 // ---------------------------------------------------------------------------
113139 // Overlay state
114140 // ---------------------------------------------------------------------------
@@ -150,6 +176,148 @@ export function resetUiState(): void {
150176 resetConversationRenderCache();
151177 activeOverlay = null;
152178 stdinWasRaw = false;
179+ titleState = null;
180+ titleViewportActive = false;
181+ lastTerminalTitle = null;
182+}
183+
184+// ---------------------------------------------------------------------------
185+// Terminal title
186+// ---------------------------------------------------------------------------
187+
188+function collapseTerminalTitleText(text: string): string | null {
189+ const collapsed = text.replace(/\s+/g, " ").trim();
190+ return collapsed.length > 0 ? collapsed : null;
191+}
192+
193+function getUserTerminalTitleText(
194+ content: Extract<AppState["messages"][number], { role: "user" }>["content"],
195+): string | null {
196+ if (typeof content === "string") {
197+ return collapseTerminalTitleText(content);
198+ }
199+
200+ const text = content
201+ .filter(
202+ (block): block is Extract<(typeof content)[number], { type: "text" }> => {
203+ return block.type === "text";
204+ },
205+ )
206+ .map((block) => block.text)
207+ .join(" ");
208+
209+ return collapseTerminalTitleText(text);
210+}
211+
212+function getAssistantTerminalTitleText(
213+ content: Extract<
214+ AppState["messages"][number],
215+ { role: "assistant" }
216+ >["content"],
217+): string | null {
218+ const text = content
219+ .filter(
220+ (
221+ block,
222+ ): block is Extract<
223+ Extract<
224+ AppState["messages"][number],
225+ { role: "assistant" }
226+ >["content"][number],
227+ { type: "text" }
228+ > => {
229+ return block.type === "text";
230+ },
231+ )
232+ .map((block) => block.text)
233+ .join(" ");
234+
235+ return collapseTerminalTitleText(text);
236+}
237+
238+function truncateTerminalTitleTail(text: string): string {
239+ const words = text.split(" ");
240+ if (words.length <= TERMINAL_TITLE_WORD_COUNT) {
241+ return text;
242+ }
243+ return `...${words.slice(-TERMINAL_TITLE_WORD_COUNT).join(" ")}`;
244+}
245+
246+function buildIdleTerminalTitle(state: Pick<AppState, "messages">): string {
247+ for (let index = state.messages.length - 1; index >= 0; index -= 1) {
248+ const message = state.messages[index];
249+ if (!message) {
250+ continue;
251+ }
252+
253+ let text: string | null = null;
254+ switch (message.role) {
255+ case "user":
256+ text = getUserTerminalTitleText(message.content);
257+ break;
258+ case "assistant":
259+ text = getAssistantTerminalTitleText(message.content);
260+ break;
261+ case "toolResult":
262+ case "ui":
263+ break;
264+ }
265+
266+ if (text) {
267+ return `mc - ${truncateTerminalTitleTail(text)}`;
268+ }
269+ }
270+
271+ return "mc";
272+}
273+
274+/**
275+ * Build the current terminal title from UI state.
276+ *
277+ * Idle titles show a short tail preview from the latest conversational text
278+ * message. Active turns show a stable-width glow scanner.
279+ *
280+ * @param state - Application state needed to derive the title.
281+ * @param animationTick - Divider animation tick used to pick the scanner frame.
282+ * @returns The terminal title text to write via cel-tui.
283+ */
284+export function buildTerminalTitle(
285+ state: Pick<AppState, "messages" | "running">,
286+ animationTick = dividerTick,
287+): string {
288+ if (!state.running) {
289+ return buildIdleTerminalTitle(state);
290+ }
291+
292+ const frameIndex =
293+ Math.floor(animationTick / TERMINAL_TITLE_TICKS_PER_FRAME) %
294+ TERMINAL_TITLE_FRAMES.length;
295+ return `mc - ${TERMINAL_TITLE_FRAMES[frameIndex]}`;
296+}
297+
298+function syncTerminalTitle(
299+ state: Pick<AppState, "messages" | "running"> | null,
300+): void {
301+ if (!state || !titleViewportActive) {
302+ return;
303+ }
304+
305+ const title = buildTerminalTitle(state);
306+ if (title === lastTerminalTitle) {
307+ return;
308+ }
309+
310+ cel.setTitle(title);
311+ lastTerminalTitle = title;
312+}
313+
314+function invalidateTerminalTitleCache(): void {
315+ lastTerminalTitle = null;
316+}
317+
318+function requestRender(): void {
319+ syncTerminalTitle(titleState);
320+ cel.render();
153321 }
154322
155323 // ---------------------------------------------------------------------------
@@ -162,7 +330,7 @@ function startDividerAnimation(): void {
162330 dividerTick = 0;
163331 dividerTimer = setInterval(() => {
164332 dividerTick++;
165- cel.render();
333+ requestRender();
166334 }, DIVIDER_FRAME_MS);
167335 }
168336
@@ -286,14 +454,14 @@ function prependConversationChunk(state: AppState, width: number): void {
286454 function openOverlay(overlay: ActiveOverlay): void {
287455 activeOverlay = overlay;
288456 inputFocused = false;
289- cel.render();
457+ requestRender();
290458 }
291459
292460 /** Dismiss the active overlay and return focus to the input. */
293461 function dismissOverlay(): void {
294462 activeOverlay = null;
295463 inputFocused = true;
296- cel.render();
464+ requestRender();
297465 }
298466
299467 function openPathAutocompleteOverlay(state: AppState): void {
@@ -331,7 +499,7 @@ function handleTabKeyPress(state: AppState): void {
331499 const completedInput = autocompleteInputPath(inputValue, state.cwd);
332500 if (completedInput) {
333501 inputValue = completedInput;
334- cel.render();
502+ requestRender();
335503 return;
336504 }
337505
@@ -366,18 +534,20 @@ export function renderActiveOverlay(state: AppState): Node | null {
366534 * @returns Stable callbacks for the controlled TextInput.
367535 */
368536 export function createInputController(state: AppState): InputController {
537+ titleState = state;
538+
369539 return {
370540 onChange: (value) => {
371541 inputValue = value;
372- cel.render();
542+ requestRender();
373543 },
374544 onFocus: () => {
375545 inputFocused = true;
376- cel.render();
546+ requestRender();
377547 },
378548 onBlur: () => {
379549 inputFocused = false;
380- cel.render();
550+ requestRender();
381551 },
382552 onKeyPress: (key) => {
383553 if (key === "enter") {
@@ -385,13 +555,13 @@ export function createInputController(state: AppState): InputController {
385555
386556 if (isQuitInput(raw)) {
387557 inputValue = "";
388- cel.render();
558+ requestRender();
389559 requestGracefulExit(state);
390560 return false;
391561 }
392562
393563 inputValue = "";
394- cel.render();
564+ requestRender();
395565 handleInput(raw, state);
396566 return false;
397567 }
@@ -479,7 +649,7 @@ function appendUiMessage(
479649 }
480650 state.messages.push(message);
481651 scrollConversationToBottom();
482- cel.render();
652+ requestRender();
483653 }
484654
485655 /**
@@ -521,9 +691,7 @@ const commandController = createCommandController({
521691 appendInfoMessage,
522692 appendTodoMessage,
523693 scrollConversationToBottom,
524- render: () => {
525- cel.render();
526- },
694+ render: requestRender,
527695 reloadPromptContext,
528696 openInBrowser,
529697 });
@@ -537,9 +705,7 @@ const agentController = createUiAgentController({
537705 appendInfoMessage,
538706 handleCommand: (command, state) =>
539707 commandController.handleCommand(command, state),
540- render: () => {
541- cel.render();
542- },
708+ render: requestRender,
543709 scrollConversationToBottom,
544710 startDividerAnimation,
545711 stopDividerAnimation,
@@ -547,6 +713,7 @@ const agentController = createUiAgentController({
547713
548714 /** Route raw user input through parseInput and dispatch accordingly. */
549715 export function handleInput(raw: string, state: AppState): void {
716+ titleState = state;
550717 agentController.handleInput(raw, state);
551718 }
552719
@@ -618,6 +785,7 @@ export function suspendToBackground(
618785 stop();
619786 onResume(() => {
620787 clearInterval(keepAlive);
788+ invalidateTerminalTitleCache();
621789 resumeUi();
622790 });
623791
@@ -657,7 +825,7 @@ function renderConversationLog(state: AppState, width: number): Node {
657825 prependConversationChunk(state, width);
658826 }
659827
660- cel.render();
828+ requestRender();
661829 },
662830 },
663831 buildConversationLog(state, width),
@@ -681,6 +849,12 @@ export function renderBaseLayout(
681849 inputController: InputController,
682850 onSuspend?: () => void,
683851 ): Node {
852+ titleState = state;
853+ titleViewportActive = true;
854+ if (lastTerminalTitle === null) {
855+ syncTerminalTitle(state);
856+ }
857+
684858 return VStack(
685859 {
686860 height: "100%",
@@ -736,6 +910,7 @@ export function renderBaseLayout(
736910 export function startUI(state: AppState): void {
737911 resetUiState();
738912 stdinWasRaw = process.stdin.isRaw || false;
913+ titleState = state;
739914 const terminal = new ProcessTerminal();
740915 const inputController = createInputController(state);
741916 cel.init(terminal);
@@ -749,7 +924,7 @@ export function startUI(state: AppState): void {
749924 if (state.running) {
750925 startDividerAnimation();
751926 }
752- cel.render();
927+ requestRender();
753928 });
754929 });
755930 const overlay = renderActiveOverlay(state);
@@ -759,6 +934,7 @@ export function startUI(state: AppState): void {
759934 }
760935 return base;
761936 });
937+ syncTerminalTitle(state);
762938
763939 if (state.running) {
764940 startDividerAnimation();