Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function TerminalPaneNativeChatPortal({
} = controller
const chatPaneSessionId = useAppStore((state) =>
effectiveChatViewMode && chatPane
? resolvePaneAgentSessionId(state, makePaneKey(tabId, chatPane.leafId))
? resolvePaneAgentSessionId(state, makePaneKey(tabId, chatPane.leafId), 'retained')
: null
)
if (!effectiveChatViewMode || !chatPane?.container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ describe('resolvePaneAgentSessionId', () => {
).toBe('confirmed-session')
})

it('keeps the retained ID copyable after exit while live-only consumers see no session', () => {
const exited = state(undefined, sleeping('saved-session'), true)
expect(resolvePaneAgentSessionId(exited, PANE_KEY, 'retained')).toBe('saved-session')
expect(resolvePaneAgentSessionId(exited, PANE_KEY)).toBeNull()
})

describe('liveness', () => {
it('is absent once the pane is proven back at the shell', () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ export type PaneAgentSessionIdState = {
paneForegroundAgentByPaneKey: Record<string, PaneForegroundAgentEntry | undefined>
}

/** Resolves the provider session owned by one exact terminal pane, while its agent is still live. */
/** Resolve exact-pane provider identity for live actions or copying the retained session ID. */
export function resolvePaneAgentSessionId(
state: PaneAgentSessionIdState,
paneKey: string
paneKey: string,
policy: 'live' | 'retained' = 'live'
): string | null {
// Gate durable resume records on shell return too; they intentionally outlive the agent for cold restore.
if (state.paneForegroundAgentByPaneKey[paneKey]?.shellForeground === true) {
// The common pattern keeps ended-session identity copyable without claiming that its interface is live.
if (policy === 'live' && state.paneForegroundAgentByPaneKey[paneKey]?.shellForeground === true) {

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.

Minor · Functional Correctness · Quick win

Preserve a copyable ID through normal exit cleanup.

When an agent exits normally, pane-agent-identity.ts clears its sleeping record, and interrupt-input-intent.ts drops its live status. The retained lookup then returns null, so users cannot copy the session ID after exit. Preserve a copy-only ID through that cleanup and resolve it here. (raw.githubusercontent.com)

return null
}
const live = state.agentStatusByPaneKey[paneKey]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RecognizedAgentProcess } from '../../../../shared/agent-process-re
import type { PaneForegroundAgentEntry } from '@/store/slices/pane-foreground-agent'
import type { RuntimeTerminalProcessInspection } from '@/runtime/runtime-terminal-inspection'

export type ForegroundReadReason = 'command' | 'visible-pty' | 'command-finished'
export type ForegroundReadReason = 'command' | 'visible-pty' | 'command-finished' | 'accepted-input'

export type PaneForegroundAgentTrackerDeps = {
getPtyId: () => string | null
Expand Down Expand Up @@ -32,3 +32,18 @@ export type PaneForegroundAgentTrackerDeps = {
onCommandFinishedUnavailable?: () => void
onVisibleForegroundSettled?: (outcome: 'agent' | 'shell' | 'inconclusive') => void
}

export type PaneForegroundAgentTracker = {
/** True while any read is scheduled or running, whatever its authority. */
hasReadInFlight: () => boolean
onVisiblePtyBound: (expectsAgent?: boolean) => boolean
onCommandStarted: (requireConfirmation?: boolean) => void
/** Accepted input requests evidence without asserting a shell lifecycle boundary. */
onAcceptedCommandBoundary: () => void
onAgentStartupObserved: () => void
/** True when pane identity must remain visible until an async shell confirmation. */
onCommandFinished: () => boolean
/** The process monitor confirmed this agent exited (no agent, no children, settled). */
onProcessExitConfirmed: (process: RecognizedAgentProcess) => void
dispose: () => void
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,84 @@ describe('createPaneForegroundAgentTracker', () => {
})
})

it('uses typed-agent text only to await process confirmation', async () => {
it('keeps accepted input confirm-only when no real command marker arrives', async () => {
readForegroundProcess.mockResolvedValue('zsh')
const tracker = makeTracker()
tracker.onAcceptedCommandBoundary()
await flushSettleRead(COMMAND_SETTLE_MS + WRAPPER_RESOLVE_RETRY_MS + SECOND_WRAPPER_RETRY_MS)
expect(confirmForegroundProcess).toHaveBeenCalledTimes(3)
expect(publish).not.toHaveBeenCalled()
expect(onConfirmedShellForeground).not.toHaveBeenCalled()
})

it.each([false, true])(
'coalesces accepted submit with OSC start (read in flight=%s)',
async (inFlight) => {
readForegroundProcess.mockResolvedValue(null)
let finishFirstRead: (value: string | null) => void = () => {}
readForegroundProcess.mockImplementationOnce(
() =>
new Promise((resolve) => {
finishFirstRead = resolve
})
)
const tracker = makeTracker()
tracker.onAcceptedCommandBoundary()
tracker.onAcceptedCommandBoundary()
expect(publish).not.toHaveBeenCalled()
if (inFlight) {
await flushSettleRead(COMMAND_SETTLE_MS)
}
tracker.onCommandStarted(true)
tracker.onVisiblePtyBound()
expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: null, shellForeground: false })
if (!inFlight) {
await flushSettleRead(COMMAND_SETTLE_MS)
}
finishFirstRead(null)
await flushSettleRead(0)
await flushSettleRead(WRAPPER_RESOLVE_RETRY_MS + SECOND_WRAPPER_RETRY_MS)
expect(confirmForegroundProcess).toHaveBeenCalledTimes(3)
expect(publish).toHaveBeenCalledTimes(2)
expect(onConfirmedShellForeground).not.toHaveBeenCalled()
expect(tracker.hasReadInFlight()).toBe(false)
}
)

it('discovers an alias result only from confirmed process data and fences a replaced PTY', async () => {
readForegroundProcess.mockResolvedValue('droid')
const tracker = makeTracker()
tracker.onAcceptedCommandBoundary()
await flushSettleRead(COMMAND_SETTLE_MS)
expect(publish).toHaveBeenLastCalledWith({
agent: 'droid',
agentEvidence: 'process-read',
routingTrusted: true,
shellForeground: false
})
publish.mockClear()
let finish: (process: string) => void = () => {}
readForegroundProcess.mockImplementationOnce(
() =>
new Promise((resolve) => {
finish = resolve
})
)
tracker.onAcceptedCommandBoundary()
await flushSettleRead(COMMAND_SETTLE_MS)
ptyId = 'pty-2'
finish('droid')
await Promise.resolve()
await Promise.resolve()
expect(publish).not.toHaveBeenCalled()
tracker.dispose()
})

it('awaits provider confirmation when a boundary requires routing evidence', async () => {
readForegroundProcess.mockResolvedValueOnce('powershell.exe').mockResolvedValueOnce('droid')
const tracker = makeTracker()

tracker.onCommandStarted('droid')
tracker.onCommandStarted(true)
expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: null, shellForeground: false })

await flushSettleRead(COMMAND_SETTLE_MS)
Expand All @@ -239,11 +312,11 @@ describe('createPaneForegroundAgentTracker', () => {
})
})

it('never publishes typed Droid when command-start reads stay unavailable', async () => {
it('never invents an agent when confirmation reads stay unavailable', async () => {
readForegroundProcess.mockResolvedValue(null)
const tracker = makeTracker()

tracker.onCommandStarted('droid')
tracker.onCommandStarted(true)
await flushSettleRead(COMMAND_SETTLE_MS)
expect(publish).toHaveBeenLastCalledWith({ agent: null, shellForeground: false })

Expand All @@ -258,10 +331,10 @@ describe('createPaneForegroundAgentTracker', () => {
expect(publish).toHaveBeenLastCalledWith({ agent: null, shellForeground: false })
})

it('does not replace known foreground identity with typed command inference', () => {
it('revokes known foreground routing at an actual command start', () => {
const tracker = makeTracker(() => true)

tracker.onCommandStarted('droid')
tracker.onCommandStarted(true)

expect(publish).toHaveBeenCalledExactlyOnceWith({ agent: null, shellForeground: false })
})
Expand Down
Loading
Loading