Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ describe('buildTitleDerivedAgentRows', () => {
).toEqual([['codex', 'working', 'Codex', '⠼ demo-repo']])
})

it('keeps explicit title identity over the launched agent', () => {
// Why (#13341): nested Codex title under Claude launch keeps the parent session icon.
it('keeps launch ownership over a nested child agent title', () => {
const launchAgent: TuiAgent = 'claude'
const rows = buildWorktreeAgentRows({
tabs: [makeTab('tab-1', { launchAgent })],
Expand All @@ -235,7 +236,7 @@ describe('buildTitleDerivedAgentRows', () => {
now: 2000
})

expect(rows.map((row) => [row.agentType, row.state])).toEqual([['codex', 'working']])
expect(rows.map((row) => [row.agentType, row.state])).toEqual([['claude', 'working']])
})

it('produces no row for a spinner-only title when the tab has no launch identity', () => {
Expand Down Expand Up @@ -360,8 +361,8 @@ describe('buildTitleDerivedAgentRows', () => {
})

expect(rowsFor('⠋ Claude Code').map((row) => row.agentType)).toEqual(['claude'])
// Pane reuse: the user exited OpenCode and ran claude in the same pane.
expect(rowsFor('✳ Claude Code', 'opencode').map((row) => row.agentType)).toEqual(['claude'])
// Why (#13341): Claude identity under an active OpenCode launch keeps the parent session.
expect(rowsFor('✳ Claude Code', 'opencode').map((row) => row.agentType)).toEqual(['opencode'])
// No owner to defend the pane: naming Claude stays the only available identity.
expect(rowsFor('⠋ use Claude Sonnet').map((row) => row.agentType)).toEqual(['claude'])
expect(rowsFor('zsh', 'opencode')).toHaveLength(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
resolveCompatibleAgentTypeForOwner
} from '../../../../shared/agent-title-owner'
import { resolvePaneAgentOwner } from '../../../../shared/pane-agent-owner'
import { isClaudeIdentityFrameTitle } from '../../../../shared/terminal-title-agent-type'

const EMPTY_RUNTIME_TITLES: Record<string, Record<number, string>> = {}
const EMPTY_LIVE_PTY_IDS: Record<string, string[]> = {}
Expand Down Expand Up @@ -202,22 +201,28 @@ export function resolveTitleDerivedAgentType(
ownerAgentType?: AgentType | null
): AgentType | null {
const agentType = TITLE_AGENT_LABEL_TO_TYPE[label] ?? 'unknown'
if (agentType !== 'claude') {
const owner = ownerAgentType && ownerAgentType !== 'unknown' ? ownerAgentType : null
if (agentType === 'claude') {
// Why: Claude's task-title spinner heuristic has no provider identity. In
// split panes it can match arbitrary terminal spinners, so sidebar rows only
// accept Claude when the title itself names Claude.
if (!CLAUDE_AGENT_TOKEN_RE.test(title)) {
return null
}
// Why (#8940 + #13341): Claude mentions never steal ownership, and a nested
// Claude identity frame under a durable non-Claude owner keeps the parent.
// Pane reuse is modeled by clearing launchAgent first (tab-agent path).
if (owner && owner !== 'claude') {
return null
}
return agentType
}
// Why: Claude's task-title spinner heuristic has no provider identity. In
// split panes it can match arbitrary terminal spinners, so sidebar rows only
// accept Claude when the title itself names Claude.
if (!CLAUDE_AGENT_TOKEN_RE.test(title)) {
return null
}
// Why: a "claude" word inside another agent's task text is a mention, not identity.
// Only a title that PRESENTS Claude may take a pane away from its known owner (#8940).
const owner = ownerAgentType && ownerAgentType !== 'unknown' ? ownerAgentType : null
if (owner && owner !== 'claude' && !isClaudeIdentityFrameTitle(title)) {
// Why (#13341): nested Codex/Gemini/... titles must not rebrand a pane whose
// launch owner is still a different agent — fall through to ownerAgentType.
if (owner && agentType !== 'unknown' && agentType !== owner) {
return null
}
return agentType
return agentType === 'unknown' ? null : agentType
}

function resolveTitleDerivedPaneOwner(
Expand Down
178 changes: 178 additions & 0 deletions src/renderer/src/lib/tab-agent-from-signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { isShellProcess } from '../../../shared/agent-detection'
import {
isClaudeIdentityFrameTitle,
resolveExplicitTerminalTitleAgentType
} from '../../../shared/terminal-title-agent-type'
import { resolveCompatibleAgentTypeForOwner } from '../../../shared/agent-title-owner'
import { isOpenCodeNativeTitle } from '../../../shared/opencode-terminal-title'
import { resolvePaneAgentOwner } from '../../../shared/pane-agent-owner'
import type { TuiAgent } from '../../../shared/types'

// A shell name or the tab's neutral default title (where inferred-interrupt reset parks it); blank titles are no evidence.
function titleShowsNoAgent(title: string, defaultTitle?: string): boolean {
const trimmed = title.trim()
return trimmed.length > 0 && (isShellProcess(trimmed) || trimmed === defaultTitle?.trim())
}

/**
* Resolves wrapper-compatible signal identity against the launch owner.
*/
function resolveSignalAgentForLaunchOwner(
signalAgent: TuiAgent | null | undefined,
launchAgent: TuiAgent | null
): TuiAgent | null {
if (!signalAgent) {
return null
}
return (resolveCompatibleAgentTypeForOwner(signalAgent, launchAgent) ?? signalAgent) as TuiAgent
}

/**
* Probe-free evidence a launched agent exited: title shows no agent, no live
* hook remains, and either the hook completed or observed activity vanished.
* Vanished-activity is local-only — remote rows also drop on transport blips.
*/
export function resolveLaunchedAgentExitEvidence(args: {
title: string
defaultTitle?: string
isRemote: boolean
hasObservedAgentSignal: boolean
hookAgent: TuiAgent | null
siblingHookAgent?: TuiAgent | null
hasCompletedHook: boolean
processAgent?: TuiAgent | null
processShellForeground?: boolean
}): boolean {
if (args.hookAgent || args.siblingHookAgent || args.processAgent) {
return false
}
// Why: OSC 133;D (foreground back at shell) is title-independent exit evidence; local-only — remote panes have no shell-foreground producer.
if (!args.isRemote && args.processShellForeground && args.hasObservedAgentSignal) {
return true
}
if (!titleShowsNoAgent(args.title, args.defaultTitle)) {
return false
}
return args.hasCompletedHook || (!args.isRemote && args.hasObservedAgentSignal)
}

/**
* Identity-first tab-agent resolution (hook > process > title > completed >
* sleeping > launch > sibling). Pure — the React hook gathers signals and calls this.
*/
export function resolveTabAgentFromSignals(args: {
hasObservedAgentSignal: boolean
isRemote: boolean
title: string
defaultTitle?: string
hookAgent: TuiAgent | null
siblingHookAgent?: TuiAgent | null
focusedCompletedHookAgent?: TuiAgent | null
siblingCompletedHookAgent?: TuiAgent | null
processAgent?: TuiAgent | null
processShellForeground?: boolean
sleepingSessionAgent?: TuiAgent | null
launchAgent?: TuiAgent
}): TuiAgent | null {
const launchAgent = args.launchAgent ?? null
// Durable focused-pane owner (launch intent → hook → session); focused-pane-scoped so a sibling can't re-own the focused title (would mislabel a Pi pane as OMP).
const owner = resolvePaneAgentOwner({
launchAgent,
hookAgent: args.hookAgent,
completedHookAgent: args.focusedCompletedHookAgent,
sleepingSessionAgent: args.sleepingSessionAgent
}) as TuiAgent | null

// The live/idle split governs title override; siblings normalize against launch intent only.
const liveFocusedIdentity = resolveSignalAgentForLaunchOwner(args.hookAgent, owner)
const liveSiblingIdentity = resolveSignalAgentForLaunchOwner(args.siblingHookAgent, launchAgent)
// Why: OSC 133;D proves this local pane returned to shell, so the idle identity is stale; remote titles lag runtime, so keep it there.
const processProvesShell = !args.isRemote && args.processShellForeground === true
const hasCompletedHook = (args.focusedCompletedHookAgent ?? null) !== null
const noAgentTitle = titleShowsNoAgent(args.title, args.defaultTitle)
const idleIdentitySuppressed =
!args.isRemote && (noAgentTitle || processProvesShell) && hasCompletedHook
const idleFocusedIdentity = idleIdentitySuppressed
? null
: resolveSignalAgentForLaunchOwner(args.focusedCompletedHookAgent, owner)
// Why: idleIdentitySuppressed is the FOCUSED pane's exit evidence, so it must not clear a sibling's idle identity.
const idleSiblingIdentity = resolveSignalAgentForLaunchOwner(
args.siblingCompletedHookAgent,
launchAgent
)
const sleepingSessionAgent = args.sleepingSessionAgent ?? null

const launchedAgentExited = resolveLaunchedAgentExitEvidence({
title: args.title,
defaultTitle: args.defaultTitle,
isRemote: args.isRemote,
hasObservedAgentSignal: args.hasObservedAgentSignal,
hookAgent: liveFocusedIdentity,
siblingHookAgent: liveSiblingIdentity,
hasCompletedHook,
processAgent: args.processAgent,
processShellForeground: args.processShellForeground
})
const activeLaunchAgent = launchedAgentExited ? null : launchAgent
// Why: re-own the foreground process within its title-identity group so OMP's nested pi (shell → omp → pi) can't flip an OMP-owned tab's icon.
const processAgentRaw = resolveSignalAgentForLaunchOwner(args.processAgent, owner)
// Why (#13341): Claude↔Codex nested CLIs become the local foreground process while the
// parent session still owns the pane. Do not let that child process rebrand the icon
// until launch ownership has exited (then process may name a genuine reuse).
const processAgent =
activeLaunchAgent && processAgentRaw && processAgentRaw !== activeLaunchAgent
? null
: processAgentRaw

// Title carries identity only as a reuse override (names a DIFFERENT-group agent) or a legacy standalone id when no hook — same-group titles say nothing (OMP wraps Pi), so the record wins.
const explicitTitleAgent = resolveSignalAgentForLaunchOwner(
resolveExplicitTerminalTitleAgentType(args.title),
owner
)
const priorIdentity = idleFocusedIdentity ?? activeLaunchAgent
const nativeOpenCodeTitle = explicitTitleAgent === 'opencode' && isOpenCodeNativeTitle(args.title)
// Why: a "claude" token in another agent's task text is a mention, not identity, so it must
// not take a pane from its known owner — only a title that PRESENTS Claude may (#8940).
const titleClaimsIdentity =
explicitTitleAgent !== 'claude' || isClaudeIdentityFrameTitle(args.title)
// Why: native OpenCode titles can reclaim stale launch intent before any observed hook signal.
// Why (#13341): once a launch owner is still active, a nested child's title is not pane reuse.
const titleReclaimsReusedPane =
priorIdentity !== null &&
explicitTitleAgent !== null &&
explicitTitleAgent !== priorIdentity &&
titleClaimsIdentity &&
!activeLaunchAgent &&
(args.hasObservedAgentSignal || hasCompletedHook || nativeOpenCodeTitle)
// Why: OpenCode's native `OC |` frame is provider identity at startup (before hooks), not a nested child.
const openCodeStartupTitle =
nativeOpenCodeTitle &&
launchAgent !== null &&
launchAgent !== 'opencode' &&
!args.hasObservedAgentSignal
// Why: native OpenCode titles lack a provider generation and cannot displace durable ownership.
const titleAgent =
processProvesShell ||
sleepingSessionAgent ||
(nativeOpenCodeTitle && idleFocusedIdentity !== null)
? null
: openCodeStartupTitle
? explicitTitleAgent
: titleReclaimsReusedPane
? explicitTitleAgent
: priorIdentity
? null
: explicitTitleAgent

// Identity-first precedence (see useTabAgent JSDoc): live hook > process > title > completed > sleeping > launch > sibling.
return (
liveFocusedIdentity ??
processAgent ??
titleAgent ??
idleFocusedIdentity ??
sleepingSessionAgent ??
activeLaunchAgent ??
liveSiblingIdentity ??
idleSiblingIdentity
)
}
105 changes: 105 additions & 0 deletions src/renderer/src/lib/use-tab-agent-nested-identity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { describe, expect, it } from 'vitest'
import { resolveTabAgentFromSignals } from './use-tab-agent'

// Why (#13341): nested Claude↔Codex (or other cross-group) invocations become the
// local foreground process / title while the parent session still owns the pane.
// Durable launch ownership must keep the parent icon until the parent exits.
describe('resolveTabAgentFromSignals — nested child identity (#13341)', () => {
it('keeps active Codex launch ownership when a nested Claude identity title appears', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✳ Claude Code',
hookAgent: null,
launchAgent: 'codex'
})
).toBe('codex')
})

it('lets a Claude identity title own the icon once launch ownership has cleared', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✳ Claude Code',
hookAgent: null,
launchAgent: undefined
})
).toBe('claude')
})

it('keeps OpenCode launch ownership when a nested Claude identity title appears', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✳ Claude Code',
hookAgent: null,
launchAgent: 'opencode'
})
).toBe('opencode')
})

it('keeps Claude launch ownership when a nested Codex process is foreground', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✦ Codex',
hookAgent: null,
processAgent: 'codex',
launchAgent: 'claude'
})
).toBe('claude')
})

it('keeps Codex launch ownership when a nested Claude process is foreground', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✳ Claude Code',
hookAgent: null,
processAgent: 'claude',
launchAgent: 'codex'
})
).toBe('codex')
})

it('keeps parent ownership when fenced parent hooks coexist with nested process', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✦ Codex',
hookAgent: 'claude',
processAgent: 'codex',
launchAgent: 'claude'
})
).toBe('claude')
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: '✳ Claude Code',
hookAgent: 'codex',
processAgent: 'claude',
launchAgent: 'codex'
})
).toBe('codex')
})

it('still lets a nested process identity surface once launch ownership has exited', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: 'zsh',
hookAgent: null,
processAgent: 'codex',
launchAgent: undefined
})
).toBe('codex')
})
})
19 changes: 15 additions & 4 deletions src/renderer/src/lib/use-tab-agent-pi-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ describe('resolveTabAgentFromSignals — Pi/OMP identity', () => {
).toBe('omp')
})

it('still lets a genuine cross-group foreground process reclaim a reused OMP pane', () => {
// Scope guard: a different-group process (Codex is not Pi-compatible) is
// real-time proof the pane was reused, so it overrides the OMP launch owner
// instead of collapsing onto it.
it('keeps OMP launch ownership when a nested cross-group process is foreground (#13341)', () => {
// Why: nested Codex under OMP is not pane reuse while launch ownership remains.
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
Expand All @@ -187,6 +185,19 @@ describe('resolveTabAgentFromSignals — Pi/OMP identity', () => {
processAgent: 'codex',
launchAgent: 'omp'
})
).toBe('omp')
})

it('lets a cross-group process own the icon once launch ownership has cleared', () => {
expect(
resolveTabAgentFromSignals({
hasObservedAgentSignal: true,
isRemote: false,
title: 'zsh',
hookAgent: null,
processAgent: 'codex',
launchAgent: undefined
})
).toBe('codex')
})

Expand Down
Loading
Loading