Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/main/runtime/mobile-session-terminal-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function buildHeadlessMobileSessionTerminalTabs(
parentTabId: tab.id,
leafId,
title,
customTitle: tab.customTitle?.trim() || null,
...(ptyId ? { ptyId } : {}),
...(tab.startupCwd ? { startupCwd: tab.startupCwd } : {}),
...(tab.launchAgent ? { launchAgent: tab.launchAgent } : {}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from '../orca-runtime-test-mocks.spec'
import {
HEADLESS_LEAF_ID,
TEST_WORKTREE_ID,
makeRuntimeStoreWithWorkspaceSession,
makeWorkspaceSessionWithHeadlessTerminal
} from '../orca-runtime-test-fixtures.spec'

// #29975: a paired desktop ranks its own saved-session title over the host's live title, so a
// CLI rename on a headless host only reaches it when the host says the name is manual.
function headlessRuntime() {
const { runtimeStore, getSession } = makeRuntimeStoreWithWorkspaceSession(
makeWorkspaceSessionWithHeadlessTerminal()
)
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the fixture store implements every method these headless paths call; it is the shared runtime spec fixture.
return { runtime: new OrcaRuntimeService(runtimeStore as never), getSession }
}

describe('headless terminal rename publication', () => {
async function renamedRuntime() {
const { runtime } = headlessRuntime()
runtime.setPtyController({
spawn: vi.fn(async () => ({ id: 'rename-pty' })),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
const created = await runtime.createTerminal(`id:${TEST_WORKTREE_ID}`, {
tabId: 'host-tab',
leafId: HEADLESS_LEAF_ID,
activate: true
})
return { runtime, handle: created.handle }
}

async function publishedSurface(runtime: OrcaRuntimeService) {
const listed = await runtime.listMobileSessionTabs(`id:${TEST_WORKTREE_ID}`)
const surface = listed.tabs.find(
(tab) => tab.type === 'terminal' && tab.parentTabId === 'host-tab'
)
return surface?.type === 'terminal' ? surface : undefined
}

it('publishes the manual name on the live surface without a cold rehydrate', async () => {
const { runtime, handle } = await renamedRuntime()

await runtime.renameTerminal(handle, 'Codex · 绑定实现')

expect(await publishedSurface(runtime)).toMatchObject({
title: 'Codex · 绑定实现',
customTitle: 'Codex · 绑定实现'
})
})

it('names a tab with no live pane by its tab id, and persists it', async () => {
const { runtime, getSession } = headlessRuntime()
runtime['hydrateHeadlessMobileSessionTabsFromWorkspaceSession'](TEST_WORKTREE_ID)

await runtime.setMobileSessionTabProps(`id:${TEST_WORKTREE_ID}`, {
tabId: 'host-tab',
customTitle: ' Sleeping '
})

// `title` too: phones and older desktops read only that.
expect(await publishedSurface(runtime)).toMatchObject({
title: 'Sleeping',
customTitle: 'Sleeping'
})
expect(
getSession().tabsByWorktree[TEST_WORKTREE_ID]!.find((tab) => tab.id === 'host-tab')
?.customTitle
).toBe('Sleeping')
})

it('restores the saved title when a tab-id rename is cleared', async () => {
const { runtime } = headlessRuntime()
runtime['hydrateHeadlessMobileSessionTabsFromWorkspaceSession'](TEST_WORKTREE_ID)
const before = (await publishedSurface(runtime))?.title
const worktree = `id:${TEST_WORKTREE_ID}`
await runtime.setMobileSessionTabProps(worktree, { tabId: 'host-tab', customTitle: 'X' })

await runtime.setMobileSessionTabProps(worktree, { tabId: 'host-tab', customTitle: null })

expect(await publishedSurface(runtime)).toMatchObject({ title: before, customTitle: null })
})

it('names and clears a live pane through its tab id as terminal.rename does', async () => {
const { runtime, handle } = await renamedRuntime()
const worktree = `id:${TEST_WORKTREE_ID}`
await runtime.renameTerminal(handle, 'By handle')

await runtime.setMobileSessionTabProps(worktree, { tabId: 'host-tab', customTitle: 'By tab' })
expect(await publishedSurface(runtime)).toMatchObject({
title: 'By tab',
customTitle: 'By tab'
})

await runtime.setMobileSessionTabProps(worktree, { tabId: 'host-tab', customTitle: null })
const cleared = await publishedSurface(runtime)
expect(cleared?.customTitle).toBeNull()
expect(cleared?.title).not.toBe('By tab')
})

it('drops a name set while the pane slept when its handle is reset', async () => {
const { runtime } = headlessRuntime()
runtime['hydrateHeadlessMobileSessionTabsFromWorkspaceSession'](TEST_WORKTREE_ID)
await runtime.setMobileSessionTabProps(`id:${TEST_WORKTREE_ID}`, {
tabId: 'host-tab',
customTitle: 'Asleep name'
})
runtime.setPtyController({
spawn: vi.fn(async () => ({ id: 'wake-pty' })),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
const woken = await runtime.createTerminal(`id:${TEST_WORKTREE_ID}`, {
tabId: 'host-tab',
leafId: HEADLESS_LEAF_ID,
activate: true
})

await runtime.renameTerminal(woken.handle, null)

const surface = await publishedSurface(runtime)
expect(surface?.customTitle).toBeNull()
expect(surface?.title).not.toBe('Asleep name')
})

it('publishes a cleared name as null so clients drop theirs', async () => {
const { runtime, handle } = await renamedRuntime()
await runtime.renameTerminal(handle, 'Pinned')

await runtime.renameTerminal(handle, null)

expect(await publishedSurface(runtime)).toMatchObject({ customTitle: null })
})
})
1 change: 1 addition & 0 deletions src/main/runtime/orca-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ await import('./orca-runtime-tests/mobile-session-tabs-part-03.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-04.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-05.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-06.spec')
await import('./orca-runtime-tests/headless-terminal-rename-publication.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-07.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-08.spec')
await import('./orca-runtime-tests/mobile-session-tabs-part-09.spec')
Expand Down
3 changes: 2 additions & 1 deletion src/main/runtime/rpc/methods/session-tab-mutation-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export const SESSION_TAB_MUTATION_METHODS = [
tabId: params.tabId,
...(params.color !== undefined ? { color: params.color } : {}),
...(params.isPinned !== undefined ? { isPinned: params.isPinned } : {}),
...(params.viewMode !== undefined ? { viewMode: params.viewMode } : {})
...(params.viewMode !== undefined ? { viewMode: params.viewMode } : {}),
...(params.customTitle !== undefined ? { customTitle: params.customTitle } : {})
})
}
})
Expand Down
1 change: 1 addition & 0 deletions src/main/runtime/runtime-mobile-session-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export function projectRuntimeMobileSessionTabs(
parentTabId: tab.parentTabId,
leafId: tab.leafId,
title,
...(tab.customTitle !== undefined ? { customTitle: tab.customTitle } : {}),
...(tab.ptyId ? { ptyId: tab.ptyId } : {}),
// Bind identity to the handle's live owner, never a stale persisted surface.
...(terminalPty?.incarnationId ? { incarnationId: terminalPty.incarnationId } : {}),
Expand Down
4 changes: 4 additions & 0 deletions src/main/runtime/session-tabs/headless-tab-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export abstract class RuntimeHeadlessTabProperties extends OrcaRuntimeWithCloseH
tabId: string,
props: {
title?: string
customTitle?: string | null
color?: string | null
isPinned?: boolean
viewMode?: 'terminal' | 'chat'
Expand All @@ -109,6 +110,9 @@ export abstract class RuntimeHeadlessTabProperties extends OrcaRuntimeWithCloseH
return {
...tab,
...(props.title !== undefined ? { title: props.title } : {}),
...(props.customTitle !== undefined && tab.type === 'terminal'
? { customTitle: props.customTitle }
: {}),
...(props.color !== undefined ? { color: props.color } : {}),
...(props.isPinned !== undefined ? { isPinned: props.isPinned } : {}),
...(props.viewMode !== undefined ? { viewMode: props.viewMode } : {})
Expand Down
57 changes: 53 additions & 4 deletions src/main/runtime/session-tabs/headless-terminal-tab-close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { buildHeadlessMobileSessionTabGroups } from '../mobile-session-layout-pr
import { appendRetiredTerminalSurfaceProofs } from '../mobile-session-terminal-retirement-proof'
import type { RuntimePtyWorktreeRecord } from '../runtime-terminal-state-records'
import type { TerminalPaneLayoutNode } from '../../../shared/terminal-tab-types'
import type { RuntimeTerminalRename } from '../../../shared/runtime-terminal-contracts'
import type { RuntimeSessionTabCloseReason } from '../../../shared/runtime-session-contracts'

export abstract class OrcaRuntimeWithCloseHeadlessMobileTerminalTab extends OrcaRuntimeWithCloseStructuredAgentSessionTab {
Expand Down Expand Up @@ -40,6 +41,14 @@ export abstract class OrcaRuntimeWithCloseHeadlessMobileTerminalTab extends Orca
...args: Parameters<HeadlessTabPropertiesMethods['persistHeadlessTerminalPaneLayout']>
): ReturnType<HeadlessTabPropertiesMethods['persistHeadlessTerminalPaneLayout']>

protected abstract getPtyIdsForExplicitTabClose(worktreeId: string, tabId: string): string[]

abstract renameTerminal(handle: string, title: string | null): Promise<RuntimeTerminalRename>

protected abstract persistHeadlessTerminalTitle(
...args: Parameters<PersistHeadlessTerminalTitleMethods['persistHeadlessTerminalTitle']>
): ReturnType<PersistHeadlessTerminalTitleMethods['persistHeadlessTerminalTitle']>

protected abstract resolveMobileSessionHostTabId(
...args: Parameters<PersistHeadlessTerminalTitleMethods['resolveMobileSessionHostTabId']>
): ReturnType<PersistHeadlessTerminalTitleMethods['resolveMobileSessionHostTabId']>
Expand Down Expand Up @@ -250,29 +259,69 @@ export abstract class OrcaRuntimeWithCloseHeadlessMobileTerminalTab extends Orca
// Why: tab color/pin are host-authoritative for remote-server tabs but had no
// push path, so pinning or coloring a tab reverted on the next snapshot and
// was never persisted. Persist to the workspace session + live snapshot.
/** What `title` reads once a manual name is cleared; readers that predate `customTitle` show it. */
protected headlessTerminalFallbackTitle(worktreeId: string, tabId: string): string {
const tab = this.getWorkspaceSessionForWorktree(worktreeId)?.tabsByWorktree[worktreeId]?.find(
(candidate) => candidate.id === tabId
)
return (
tab?.generatedTitle?.trim() || tab?.title?.trim() || tab?.defaultTitle?.trim() || 'Terminal'
)
}

async setMobileSessionTabProps(
worktreeSelector: string,
args: {
tabId: string
color?: string | null
isPinned?: boolean
viewMode?: 'terminal' | 'chat'
customTitle?: string | null
}
): Promise<{ updated: true }> {
const explicitWorktreeId = this.getExplicitSessionWorktreeIdSelector(worktreeSelector)
const worktreeId =
explicitWorktreeId ?? (await this.resolveWorktreeSelector(worktreeSelector)).id
// Why: a renderer-authoritative host owns + republishes tab props, so a
// headless write would be overwritten. Persist only when headless.
if (this.getAvailableAuthoritativeWindow()) {
return { updated: true }
}
const snapshot = this.mobileSessionTabsByWorktree.get(worktreeId)
const hostTabId = snapshot
? (this.resolveMobileSessionHostTabId(snapshot, args.tabId) ?? args.tabId)
: args.tabId
// Why by tab: a sleeping or starting pane has no handle for terminal.rename to name (#29975).
const customTitle =
args.customTitle === undefined ? undefined : args.customTitle?.trim() || null
if (this.getAvailableAuthoritativeWindow()) {
if (customTitle !== undefined) {
this.notifier?.renameTerminal(hostTabId, customTitle)
}
return { updated: true }
}
this.persistHeadlessSessionTabProps(worktreeId, hostTabId, args)
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, hostTabId, args)
// Why: a live pane's own title outranks the snapshot's, so name it the way terminal.rename does.
const liveHandles =
customTitle === undefined
? []
: this.getPtyIdsForExplicitTabClose(worktreeId, hostTabId).flatMap((ptyId) => {
const handle = this.handleByPtyId.get(ptyId)
return handle ? [handle] : []
})
for (const handle of liveHandles) {
await this.renameTerminal(handle, customTitle ?? null)
}
if (customTitle !== undefined && liveHandles.length === 0) {
this.persistHeadlessTerminalTitle(worktreeId, hostTabId, customTitle)
}
// A live pane's rename above already set its title; only a paneless tab needs `title` here.
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, hostTabId, {
...args,
...(customTitle !== undefined && liveHandles.length === 0
? {
customTitle,
title: customTitle ?? this.headlessTerminalFallbackTitle(worktreeId, hostTabId)
}
: {})
})
Comment on lines +316 to +324

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Publish the normalized customTitle on the live-pane path, not the raw args.customTitle.

Line 293 normalizes the value: it trims it, and a blank value becomes null. The live-pane path does not use the normalized value.

  • When liveHandles.length > 0, the conditional override on lines 318-323 does not apply.
  • ...args then writes the untrimmed args.customTitle into the snapshot. This overwrites the trimmed value that renameTerminal published.
  • With input " foo ", the host publishes customTitle: " foo ".
  • With a whitespace-only clear such as " ", the host publishes " " instead of null. Clients then never see the name as cleared.

When a notifier with renameTerminal exists, renameTerminal skips its own snapshot write. This spread is then the only thing that publishes customTitle, so the raw value always reaches clients.

Always spread the normalized value. Keep the title override for paneless tabs only.

🐛 Proposed fix
--- "a/src/main/runtime/session-tabs/headless-terminal-tab-close.ts"
+++ "b/src/main/runtime/session-tabs/headless-terminal-tab-close.ts"
@@ -313,15 +313,13 @@
       this.persistHeadlessTerminalTitle(worktreeId, hostTabId, customTitle)
     }
     // A live pane's rename above already set its title; only a paneless tab needs `title` here.
     this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, hostTabId, {
       ...args,
-      ...(customTitle !== undefined && liveHandles.length === 0
-        ? {
-            customTitle,
-            title: customTitle ?? this.headlessTerminalFallbackTitle(worktreeId, hostTabId)
-          }
-        : {})
+      ...(customTitle !== undefined ? { customTitle } : {}),
+      ...(customTitle !== undefined && liveHandles.length === 0
+        ? { title: customTitle ?? this.headlessTerminalFallbackTitle(worktreeId, hostTabId) }
+        : {})
     })
     return { updated: true }
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, hostTabId, {
...args,
...(customTitle !== undefined && liveHandles.length === 0
? {
customTitle,
title: customTitle ?? this.headlessTerminalFallbackTitle(worktreeId, hostTabId)
}
: {})
})
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, hostTabId, {
...args,
...(customTitle !== undefined ? { customTitle } : {}),
...(customTitle !== undefined && liveHandles.length === 0
? { title: customTitle ?? this.headlessTerminalFallbackTitle(worktreeId, hostTabId) }
: {})
})

return { updated: true }
}
}
11 changes: 11 additions & 0 deletions src/main/runtime/session-tabs/pty-backed-session-publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export abstract class OrcaRuntimeWithPublishPtyBackedMobileSessionTerminal exten
candidate.parentTabId === args.tabId &&
candidate.viewMode !== undefined
)?.viewMode
// Why: republishing a surface must not drop the tab's manual name; a new split leaf inherits it.
const customTitle = (
existingTab ??
existing?.tabs.find(
(candidate): candidate is RuntimeMobileSessionTerminalTab =>
candidate.type === 'terminal' &&
candidate.parentTabId === args.tabId &&
candidate.customTitle !== undefined
)
)?.customTitle
const tab: RuntimeMobileSessionTerminalTab = {
type: 'terminal',
id: `${args.tabId}::${args.leafId}`,
Expand All @@ -103,6 +113,7 @@ export abstract class OrcaRuntimeWithPublishPtyBackedMobileSessionTerminal exten
ptyId: pty.ptyId,
incarnationId: pty.incarnationId,
title,
...(customTitle !== undefined ? { customTitle } : {}),
...(pty.launchAgent ? { launchAgent: pty.launchAgent } : {}),
...(args.startupCwd ? { startupCwd: args.startupCwd } : {}),
...(viewMode ? { viewMode } : {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export abstract class OrcaRuntimeWithGetWorktreeTerminalProvisioningHost extends
this.notifier?.renameTerminal(tabId, title, { recordInteraction: false })
if (!this.getAvailableAuthoritativeWindow()) {
this.persistHeadlessTerminalTitle(worktreeId, tabId, title)
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, tabId, { title })
this.applyHeadlessSessionTabPropsToSnapshot(worktreeId, tabId, {
title,
customTitle: title.trim() || null
})
}
},
setTabColor: async (handle, color) => {
Expand Down
12 changes: 12 additions & 0 deletions src/main/runtime/workspaces/workspace-removal-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ export abstract class OrcaRuntimeWithResolveWorktreeRemovalTarget extends OrcaRu
// lost on restart. Persist customTitle so a headless rebuild keeps it.
if (!this.notifier?.renameTerminal && pty.pty.tabId) {
this.persistHeadlessTerminalTitle(pty.pty.worktreeId, pty.pty.tabId, title)
// Why: paired desktops rank their own saved-session title above the live one, so the
// name has to be published as manual or a resumed session hides it (#29975).
const customTitle = title?.trim() || null
this.applyHeadlessSessionTabPropsToSnapshot(pty.pty.worktreeId, pty.pty.tabId, {
customTitle,
// Why: a name set while the pane slept lives in the snapshot title; a reset must drop it.
...(customTitle === null
? {
title: this.headlessTerminalFallbackTitle(pty.pty.worktreeId, pty.pty.tabId)
}
: {})
})
}
for (const leaf of this.leaves.values()) {
if (leaf.ptyId === pty.pty.ptyId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export function buildMobileTerminalSurfaceTabs(
type: 'terminal' as const,
id: mobileTerminalSurfaceId(terminal.id, leafId),
title,
// Why every surface: the name is tab-wide, and a paired desktop reads its active surface.
customTitle: terminal.customTitle?.trim() || null,
...(tabWideFallbackSafe && terminal.quickCommandLabel?.trim()
? { quickCommandLabel: terminal.quickCommandLabel.trim() }
: {}),
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/runtime/web-runtime-terminal-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export function setWebRuntimeTabProps(args: {
color?: string | null
isPinned?: boolean
viewMode?: 'terminal' | 'chat'
customTitle?: string | null
}): boolean {
const environmentId =
getRuntimeEnvironmentIdForWorktree(useAppStore.getState(), args.worktreeId) ?? null
Expand All @@ -266,7 +267,8 @@ export function setWebRuntimeTabProps(args: {
tabId: hostTabId,
...(args.color !== undefined ? { color: args.color } : {}),
...(args.isPinned !== undefined ? { isPinned: args.isPinned } : {}),
...(args.viewMode !== undefined ? { viewMode: args.viewMode } : {})
...(args.viewMode !== undefined ? { viewMode: args.viewMode } : {}),
...(args.customTitle !== undefined ? { customTitle: args.customTitle } : {})
},
timeoutMs: 15_000
})
Expand Down
Loading
Loading