Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/coding-agent/src/utils/title-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,21 @@ function getFallbackTerminalTitle(cwd: string | undefined): string | undefined {
return sanitizeTerminalTitlePart(baseName);
}

function getProjectTerminalTitleSuffix(cwd: string | undefined): string | undefined {
// Kept separate from the no-session fallback above: when a session title
// exists the project directory is appended (`label - project`) so tabs on
// different repositories stay identifiable (issue #12600).
return getFallbackTerminalTitle(cwd);
}

export function formatSessionTerminalTitle(sessionName: string | undefined, cwd?: string): string {
const label = sanitizeTerminalTitlePart(sessionName) ?? getFallbackTerminalTitle(cwd);
return label ? `${DEFAULT_TERMINAL_TITLE}: ${label}` : DEFAULT_TERMINAL_TITLE;
const project =
label !== undefined && sanitizeTerminalTitlePart(sessionName) !== undefined
? getProjectTerminalTitleSuffix(cwd)
: undefined;
const fullLabel = project !== undefined && project !== label ? `${label} - ${project}` : label;
return fullLabel ? `${DEFAULT_TERMINAL_TITLE}: ${fullLabel}` : DEFAULT_TERMINAL_TITLE;
}

/**
Expand Down Expand Up @@ -653,7 +665,12 @@ export function setSessionTerminalTitle(sessionName: string | undefined, cwd?: s
// write into the parent shell's tab. Only `initTerminalTitleState()`, the
// explicit terminal-ownership path, releases the latch.
terminalTitleRuntime.extensionOverride = undefined;
terminalTitleRuntime.label = sanitizeTerminalTitlePart(sessionName) ?? getFallbackTerminalTitle(cwd);
const sessionLabel = sanitizeTerminalTitlePart(sessionName);
const project = sessionLabel !== undefined ? getProjectTerminalTitleSuffix(cwd) : undefined;
terminalTitleRuntime.label =
sessionLabel !== undefined && project !== undefined && project !== sessionLabel
? `${sessionLabel} - ${project}`
: (sessionLabel ?? getFallbackTerminalTitle(cwd));
emitTerminalTitle();
}

Expand Down
57 changes: 57 additions & 0 deletions packages/coding-agent/test/terminal-title-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,63 @@ describe("buildTerminalTitleWithState", () => {
expect(buildTerminalTitleWithState(undefined, "working", 1, true, "linux", "line", wslEnv)).toBe(`${BRAND} :`);
});
});
describe("setSessionTerminalTitle project suffix (issue #12600)", () => {
let writes: string[] = [];
let stdoutSpy: { mockRestore(): void } | undefined;
let prevHeadless = false;
let ttyDescriptor: PropertyDescriptor | undefined;
let windowsTitleMock: WindowsConsoleTitleMock | undefined;

beforeEach(() => {
prevHeadless = setTerminalHeadless(false);
ttyDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "isTTY");
Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true });
windowsTitleMock = mockWindowsConsoleTitle();
windowsTitleMock.succeeds = true;
writes = [];
stdoutSpy = spyOn(process.stdout, "write").mockImplementation((chunk: unknown) => {
writes.push(typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk as Uint8Array));
return true;
});
initTerminalTitleState();
resetObserved(writes, windowsTitleMock);
});

afterEach(() => {
disposeTerminalTitleState();
stdoutSpy?.mockRestore();
stdoutSpy = undefined;
windowsTitleMock?.restore();
windowsTitleMock = undefined;
if (ttyDescriptor) Object.defineProperty(process.stdout, "isTTY", ttyDescriptor);
else Reflect.deleteProperty(process.stdout, "isTTY");
setTerminalHeadless(prevHeadless);
});
it("appends the project directory to the session title", () => {
setTerminalTitleState("idle");
resetObserved(writes, windowsTitleMock);
setSessionTerminalTitle("Fix authentication flow", "/repo/oh-my-pi");
const titles = observedTitles(writes, windowsTitleMock);
expect(titles[titles.length - 1]).toBe(`${BRAND} > Fix authentication flow - oh-my-pi`);
});

it("omits the suffix when the session title already is the project name", () => {
setTerminalTitleState("idle");
resetObserved(writes, windowsTitleMock);
setSessionTerminalTitle("oh-my-pi", "/repo/oh-my-pi");
const titles = observedTitles(writes, windowsTitleMock);
expect(titles[titles.length - 1]).toBe(`${BRAND} > oh-my-pi`);
});

it("keeps the no-session fallback as the bare project name", () => {
setSessionTerminalTitle("Temporary probe title", "/repo/oh-my-pi");
setTerminalTitleState("idle");
resetObserved(writes, windowsTitleMock);
setSessionTerminalTitle(undefined, "/repo/other-project");
const titles = observedTitles(writes, windowsTitleMock);
expect(titles[titles.length - 1]).toBe(`${BRAND} > other-project`);
});
});

// Regression coverage for the shutdown-leak bug (PR #4451): the run-state
// `working` spinner arms a periodic `setInterval` that, on every tick, re-emits
Expand Down