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
158 changes: 158 additions & 0 deletions src/main/runtime/orca-runtime-codex-idle-title-chat-address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// #31136: on a headless `orca serve` host, Codex's own idle title is its project name (no agent
// marker), captured from Codex 0.163. Only a shell title may retire the hook's transcript address.
import { describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from './orca-runtime'
import type { AgentStatusIpcPayload } from '../../shared/agent-status-types'
import { makePaneKey } from '../../shared/stable-pane-id'

vi.mock('electron', () => ({
BrowserWindow: { fromId: vi.fn(() => null) },
webContents: { fromId: vi.fn(() => null) },
ipcMain: { on: vi.fn(), removeListener: vi.fn() },
app: { getPath: vi.fn(() => '/tmp') }
}))

const LEAF_ID = '11111111-1111-4111-8111-111111111111'
const TAB_ID = 'codex-tab'
const WORKTREE_ID = 'wt-1'
const PANE_KEY = makePaneKey(TAB_ID, LEAF_ID)
const PTY_ID = 'pty-codex'
const PROVIDER_SESSION = {
key: 'session_id' as const,
id: '01a15d4e-5a65-7c20-9442-73a471f43654',
transcriptPath:
'/home/u/.codex/sessions/2026/10/21/rollout-2026-10-21T00-12-58-01a15d4e-5a65-7c20-9442-73a471f43654.jsonl'
}

function codexRow(overrides: Partial<AgentStatusIpcPayload>): AgentStatusIpcPayload {
const now = Date.now()
return {
paneKey: PANE_KEY,
state: 'done',
prompt: '',
agentType: 'codex',
connectionId: null,
receivedAt: now,
stateStartedAt: now,
tabId: TAB_ID,
worktreeId: WORKTREE_ID,
providerSession: PROVIDER_SESSION,
...overrides
}
}

// Codex 0.163 frames: its window title, and the OSC 7501 program state it paints beside it.
const title = (text: string): string => `\x1b]0;${text}\x07`
const programState = (state: 'working' | 'idle'): string =>
`\x1b]7501;state=${state}:app=codex\x1b\\`

async function codexPaneAfterOutput(
rows: AgentStatusIpcPayload[],
frames: string[],
/** The host's verdict when a title drops the agent marker, as the hook server reports it. */
presence: 'live' | 'exited' = 'live'
): Promise<unknown> {
const runtime = new OrcaRuntimeService(null, undefined, {
getAgentStatusSnapshot: () => rows,
getAgentProviderSessionRowsForPane: () => rows,
checkHookAgentPresence: async () => presence
})
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: OrcaRuntimeService defines this protected method; the spy only stubs workspace resolution.
const internals = runtime as unknown as {
resolveTerminalWorkspaceLaunchScope: (selector: string) => Promise<unknown>
}
vi.spyOn(internals, 'resolveTerminalWorkspaceLaunchScope').mockResolvedValue({
id: WORKTREE_ID,
path: '/repo/app',
connectionId: null,
repo: null,
folderWorkspace: null
})
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: PTY_ID }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
await runtime.createTerminal(`id:${WORKTREE_ID}`, {
tabId: TAB_ID,
leafId: LEAF_ID,
launchAgent: 'codex',
title: 'Terminal'
})
for (const frame of frames) {
runtime.onPtyData(PTY_ID, frame, Date.now())
// The presence recheck a dropped marker starts settles before the next frame.
await new Promise((resolve) => setImmediate(resolve))
}
const { tabs } = await runtime.listMobileSessionTabs(`id:${WORKTREE_ID}`)
const tab = tabs[0]
return tab?.type === 'terminal' ? tab.agentStatus : undefined
}

describe('headless Codex chat address under Codex idle titles (#31136)', () => {
it('keeps the transcript address once a turn with no tool call ends', async () => {
const agentStatus = await codexPaneAfterOutput(
[codexRow({ state: 'done' })],
[
programState('working'),
title('⠋ repo'),
title('⠸ Reply with hi | repo'),
title('Reply with hi | repo'),
programState('idle')
]
)

expect(agentStatus).toMatchObject({
agentType: 'codex',
state: 'done',
providerSession: PROVIDER_SESSION
})
})

it('keeps the transcript address while a new turn starts under the idle title', async () => {
const agentStatus = await codexPaneAfterOutput(
[codexRow({ state: 'working', prompt: 'explain the parser' })],
[title('Reply with hi | repo'), programState('idle')]
)

// The newer idle title outranks the older hook state, as under any other title.
expect(agentStatus).toMatchObject({
agentType: 'codex',
state: 'done',
providerSession: PROVIDER_SESSION
})
})

it('follows OSC 7501 working under a title with no agent marker', async () => {
const agentStatus = await codexPaneAfterOutput(
[codexRow({ state: 'working', prompt: 'explain the parser' })],
[title('⠋ repo'), title('Reply with hi | repo'), programState('working')]
)

expect(agentStatus).toMatchObject({
agentType: 'codex',
state: 'working',
providerSession: PROVIDER_SESSION
})
})

it('keeps an identity-only session address under the idle title', async () => {
const agentStatus = await codexPaneAfterOutput(
[codexRow({ state: 'done', providerSessionOnly: true })],
[title('⠋ repo'), title('Reply with hi | repo'), programState('idle')]
)

expect(agentStatus).toMatchObject({ state: 'done', providerSession: PROVIDER_SESSION })
})

it('still retires the pane when the shell reclaims its title', async () => {
const agentStatus = await codexPaneAfterOutput(
[codexRow({ state: 'done' })],
[title('⠋ repo'), title('Reply with hi | repo'), programState('idle'), title('zsh')],
'exited'
)

expect(agentStatus).toBeUndefined()
})
})
13 changes: 7 additions & 6 deletions src/main/runtime/runtime-mobile-agent-status-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ export function buildRuntimeMobileAgentStatus(
// Why: a pending question is never inherited across hook events (unlike
// `toolName`), so it proves the agent is parked on a selector right now.
hookRow.live?.payload.interactivePrompt != null ||
// Why: headless serve has no renderer to retain an OSC row, so a fresh hook
// agentType is the only live signal a hook-only pane can offer — and an agent
// that reports over HTTP need never set a title this gate would recognize.
// Scoped to panes with no PTY status at all, so it cannot revive a spinner:
// this branch publishes `done`. It only keeps the transcript addressable.
(!pty?.lastAgentStatus && (hookRow.agentType != null || hookRow.providerSession != null))
// Why: headless serve has no renderer to retain an OSC row, so hook evidence is the
// only signal a hook-only pane can offer, and an agent that reports over HTTP need
// never set a title this gate would recognize. With PTY status, only a shell or
// management title shows the pane was released: Codex's idle title is just its
// project name (#31136). The status below still follows the PTY title's state.
((!pty?.lastAgentStatus || !terminalTitleBlocksExplicitAgentStatus(ptyTitle)) &&
(hookRow.agentType != null || hookRow.providerSession != null))
if (!hasLiveHookSignal) {
return {}
}
Comment on lines +88 to 97

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '45,115p' src/main/runtime/runtime-mobile-agent-status-builder.ts
sed -n '160,225p' src/main/runtime/runtime-mobile-agent-status-builder.ts
sed -n '175,210p' src/main/runtime/runtime-mobile-session-projection.ts

Repository: stablyai/orca

Length of output: 7815


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- hook projection and row fields ---'
rg -n -F --glob '*.ts' -e 'providerSessionAgentType' -e 'providerSessionReceivedAt' -e 'selectRuntimeHookAgentRowForPane' -e 'resolveRuntimeHookLiveAgentRow' src/main/runtime
printf '%s\n' '--- relevant hook projection ---'
sed -n '110,195p' src/main/runtime/runtime-mobile-agent-status-projection.ts
printf '%s\n' '--- builder complete logical path ---'
sed -n '1,125p' src/main/runtime/runtime-mobile-agent-status-builder.ts
printf '%s\n' '--- renderer identity and provider-session projection ---'
sed -n '175,215p' src/main/runtime/runtime-mobile-session-projection.ts
printf '%s\n' '--- Chat/provider-session consumers ---'
rg -n -F --glob '*.{ts,tsx}' -e 'providerSession' -e 'agentStatus' src/main src/renderer src 2>/dev/null | rg -i 'chat|conversation|address|providerSessionAgentType|native' | head -160

Repository: stablyai/orca

Length of output: 34459


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- ownership resolver and tests ---'
rg -n -F --glob '*.ts' -e 'function resolvePaneAgentOwnerRecord' -e 'resolvePaneAgentOwnerRecord' -e 'foregroundAgent' src/shared src/main/runtime | head -120
sed -n '1,220p' src/shared/pane-agent-owner.ts
rg -n -F --glob '*.test.ts' -e 'foregroundAgent' -e 'providerSessionOnly' -e 'stale' src/main/runtime src/shared | rg 'owner|agent-status|mobile|pane' | head -120
printf '%s\n' '--- mobile Chat address consumer ---'
sed -n '1,95p' src/main/native-chat/native-chat-file-provenance.ts
sed -n '45,80p' src/renderer/src/store/slices/agent-status-provider-session.test.ts
rg -n -F --glob '*.ts' -e 'agentStatus.providerSession' -e 'providerSession.id' -e 'providerSession?.id' src/renderer/src src/main | head -120
printf '%s\n' '--- changed gate in comparison ---'
git diff --unified=12 7f21cc91b8f6cda56c24572fdbe9932bde9e8717 68483e4fcfde57b80623f7d046457af987ed04cc -- src/main/runtime/runtime-mobile-agent-status-builder.ts

Repository: stablyai/orca

Length of output: 43364


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- native Chat pane resolution ---'
sed -n '1,85p' src/renderer/src/components/native-chat/native-chat-pane-resolution.ts
printf '%s\n' '--- foreground agent update ---'
sed -n '105,155p' src/main/runtime/runtime-pty-foreground-agent.ts
printf '%s\n' '--- hook row retention contract ---'
sed -n '112,190p' src/main/runtime/runtime-mobile-agent-status-projection.ts

Repository: stablyai/orca

Length of output: 7444


Match the hook session agent before using neutral-title evidence.

selectRuntimeHookAgentRowForPane keeps providerSession after the hook agent becomes stale. The builder can then emit pty.foregroundAgent as agentType while attaching the stale Codex providerSession. Native Chat uses both fields and providerSession.id to resolve the conversation, so Chat can open the Codex conversation for a different foreground agent. Compare the known hook session agent with the effective owner before accepting and emitting that provider session. This still accepts same-agent Codex idle-title rows.

Suggested fix
   if (!pty?.lastAgentStatus && !retained && !hookRow.agentType && !hookRow.providerSession) {
     return {}
   }
-  const providerSession = hookRow.providerSession
-    ? { providerSession: hookRow.providerSession }
+  const ownerRecord = resolvePaneAgentOwnerRecord({
+    launchAgent: tab.launchAgent ?? pty?.launchAgent ?? null,
+    hookAgent: retained?.payload.agentType ?? hookRow.agentType
+  })
+  const ownerAgent = ownerRecord?.agent ?? pty?.foregroundAgent ?? null
+  const hookProviderSessionMatchesOwner =
+    hookRow.providerSessionAgentType == null ||
+    ownerAgent == null ||
+    hookRow.providerSessionAgentType === ownerAgent
+  const providerSession =
+    hookRow.providerSession && hookProviderSessionMatchesOwner
+      ? { providerSession: hookRow.providerSession }
       : retained?.providerSession
         ? { providerSession: retained.providerSession }
         : {}
@@
       ((!pty?.lastAgentStatus || !terminalTitleBlocksExplicitAgentStatus(ptyTitle)) &&
-        (hookRow.agentType != null || hookRow.providerSession != null))
+        (hookRow.agentType != null ||
+          (hookRow.providerSession != null && hookProviderSessionMatchesOwner)))
@@
-  const ownerRecord = resolvePaneAgentOwnerRecord({
-    launchAgent: tab.launchAgent ?? pty?.launchAgent ?? null,
-    hookAgent: retained?.payload.agentType ?? hookRow.agentType
-  })
-  const ownerAgent = ownerRecord?.agent ?? pty?.foregroundAgent ?? null
   const ownerOptions = { ownerIsLaunch: ownerRecord?.ownerIsLaunch === true }

Expand Down
Loading