Fix browser annotation sends to active Windows agents (#8309)

* fix: authorize guarded sends to active ConPTY agents

* Fix stale-handle rejection using controller-less status checks and contr

- Distinguish `terminal_handle_stale` (PTY rebind mid-check) from `terminal_exited`
  so callers get an accurate reason instead of a generic `terminal_not_writable`.
- Move the PTY binding assertion to always run after the fresh foreground read,
  even when no controller is present, closing a gap where a rebind during a
  controller-less check went undetected.
- Call `confirmForegroundProcess` through its owning controller instance so the
  method keeps its `this` receiver instead of being invoked unbound.
- Update reliability gates and design doc to reflect the new test coverage and
  corrected error semantics.

* rm design doc
This commit is contained in:
Jinjing
2026-07-11 14:32:55 -07:00
committed by GitHub
parent 800967278e
commit 2802f0419d
7 changed files with 652 additions and 24 deletions
+69 -19
View File
@@ -1205,6 +1205,7 @@ type RuntimePtyController = {
stopAndWait?(ptyId: string, opts?: { keepHistory?: boolean }): Promise<boolean>
getCwd?(ptyId: string): Promise<string | null>
getForegroundProcess(ptyId: string): Promise<string | null>
confirmForegroundProcess?(ptyId: string): Promise<string | null>
hasChildProcesses?(ptyId: string): Promise<boolean>
clearBuffer?(ptyId: string): Promise<void>
resize?(ptyId: string, cols: number, rows: number): boolean
@@ -10226,7 +10227,8 @@ export class OrcaRuntimeService {
}
async getTerminalAgentStatus(handle: string): Promise<RuntimeTerminalAgentStatus> {
const terminal = this.getTerminalAgentStatusSnapshot(handle)
const ptyId = this.getTerminalAgentStatusPtyId(handle)
const terminal = this.getTerminalAgentStatusSnapshot(handle, ptyId)
const explicitStatus = this.getFreshExplicitAgentStatusForHandle(handle)
const blockedByWaitText = detectTerminalWaitBlockedReason(terminal.waitText)
const liveTitleClearsBlockedText =
@@ -10250,7 +10252,8 @@ export class OrcaRuntimeService {
// Fresh hook state is tighter, but current shell/management evidence wins.
const isRunningAgent =
!terminalTitleBlocksExplicitAgentStatus(terminal.title) &&
!(await this.terminalHasShellForegroundProcess(handle))
!(await this.terminalHasShellForegroundProcess(handle, ptyId))
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
return {
handle,
isRunningAgent,
@@ -10261,14 +10264,42 @@ export class OrcaRuntimeService {
return { handle, isRunningAgent: true, status: terminal.titleStatus }
}
return {
handle,
isRunningAgent: await this.isTerminalRunningAgent(handle),
status: null
}
const isRunningAgent = await this.isTerminalRunningAgent(handle)
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
return { handle, isRunningAgent, status: null }
}
private getTerminalAgentStatusSnapshot(handle: string): {
private getTerminalAgentStatusPtyId(handle: string): string {
const pty = this.getLivePtyForHandle(handle)
if (pty) {
if (!pty.pty.connected) {
throw new Error('terminal_gone')
}
return pty.pty.ptyId
}
const { leaf } = this.getLiveLeafForHandle(handle)
if (getTerminalState(leaf) !== 'running') {
throw new Error('terminal_exited')
}
if (!leaf.ptyId) {
throw new Error('terminal_gone')
}
return leaf.ptyId
}
private assertTerminalAgentStatusPtyBinding(handle: string, expectedPtyId: string): void {
if (this.getTerminalAgentStatusPtyId(handle) === expectedPtyId) {
return
}
// Why: delayed process evidence belongs only to the PTY that started the
// read, while callers still rely on the established stale-handle contract.
throw new Error('terminal_handle_stale')
}
private getTerminalAgentStatusSnapshot(
handle: string,
expectedPtyId: string
): {
waitText: string
waitBlockedAt: number | null
title: string | null
@@ -10277,8 +10308,8 @@ export class OrcaRuntimeService {
} {
const pty = this.getLivePtyForHandle(handle)
if (pty) {
if (!pty.pty.connected) {
throw new Error('terminal_gone')
if (!pty.pty.connected || pty.pty.ptyId !== expectedPtyId) {
throw new Error('terminal_not_writable')
}
const leaf = this.getPrimaryLeafForPty(pty.pty.ptyId)
const leafTitle = leaf
@@ -10316,6 +10347,9 @@ export class OrcaRuntimeService {
if (!leaf.ptyId) {
throw new Error('terminal_gone')
}
if (leaf.ptyId !== expectedPtyId) {
throw new Error('terminal_not_writable')
}
const title = getLatestAgentCandidateTitleInfo(
{ title: leaf.paneTitle, updatedAt: leaf.paneTitleUpdatedAt },
{ title: leaf.lastOscTitle, updatedAt: leaf.lastOscTitleAt },
@@ -10330,21 +10364,36 @@ export class OrcaRuntimeService {
}
}
private async terminalHasShellForegroundProcess(handle: string): Promise<boolean> {
private async terminalHasShellForegroundProcess(handle: string, ptyId: string): Promise<boolean> {
if (!this.ptyController) {
return false
}
let foregroundProcess: string | null
try {
const pty = this.getLivePtyForHandle(handle)
const ptyId = pty?.pty.ptyId ?? this.getLiveLeafForHandle(handle).leaf.ptyId
if (!ptyId) {
return false
}
const foregroundProcess = await this.ptyController.getForegroundProcess(ptyId)
return foregroundProcess !== null && isShellProcess(foregroundProcess)
foregroundProcess = await this.ptyController.getForegroundProcess(ptyId)
} catch {
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
return false
}
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
if (!foregroundProcess || !isShellProcess(foregroundProcess)) {
return false
}
const confirmationController = this.ptyController
if (!confirmationController?.confirmForegroundProcess) {
return true
}
let confirmedProcess: string | null
try {
confirmedProcess = await confirmationController.confirmForegroundProcess(ptyId)
} catch {
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
return true
}
this.assertTerminalAgentStatusPtyBinding(handle, ptyId)
// Why: hook identity is generic; strong provider evidence only needs to
// prove that some recognized agent still owns this exact PTY.
return recognizeAgentProcess(confirmedProcess) === null
}
private shouldDelayPtyBackedMobileSnapshotForForegroundAgent(
@@ -20876,7 +20925,8 @@ export class OrcaRuntimeService {
// status without throwing on stale handles, so this returns null on any error.
getAgentStatusForHandle(handle: string): string | null {
try {
return this.getTerminalAgentStatusSnapshot(handle).titleStatus
const ptyId = this.getTerminalAgentStatusPtyId(handle)
return this.getTerminalAgentStatusSnapshot(handle, ptyId).titleStatus
} catch {
return null
}