Keep hook completion notifications ahead of bells
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
committed by
Neil
co-authored by
Orca
parent
492a474fae
commit
dd37ec90bf
@@ -2,6 +2,11 @@ import type { ParsedAgentStatusPayload } from '../../../../shared/agent-status-t
|
||||
import type { GlobalSettings } from '../../../../shared/types'
|
||||
import type { RuntimeTerminalProcessInspection } from '@/runtime/runtime-terminal-inspection'
|
||||
|
||||
export type AgentCompletionDispatchMeta = {
|
||||
source: 'hook' | 'title' | 'process-exit'
|
||||
quietedHookDone: boolean
|
||||
}
|
||||
|
||||
export type AgentCompletionCoordinatorOptions = {
|
||||
paneKey: string
|
||||
getPtyId: () => string | null
|
||||
@@ -10,7 +15,7 @@ export type AgentCompletionCoordinatorOptions = {
|
||||
settings: Pick<GlobalSettings, 'activeRuntimeEnvironmentId'> | null | undefined,
|
||||
ptyId: string
|
||||
) => Promise<RuntimeTerminalProcessInspection>
|
||||
dispatchCompletion: (title: string) => void
|
||||
dispatchCompletion: (title: string, meta?: AgentCompletionDispatchMeta) => void
|
||||
isLive: () => boolean
|
||||
shouldPollProcessCadence?: () => boolean
|
||||
}
|
||||
@@ -21,6 +26,7 @@ export type AgentCompletionCoordinator = {
|
||||
observeTitleWorking: () => void
|
||||
observeHookStatus: (payload: ParsedAgentStatusPayload) => void
|
||||
startProcessTracking: () => void
|
||||
hasPendingHookDoneCompletion: () => boolean
|
||||
resetCompletionState: (options?: { requireFreshWorking?: boolean }) => void
|
||||
dispose: () => void
|
||||
}
|
||||
|
||||
@@ -355,7 +355,10 @@ describe('agent completion coordinator', () => {
|
||||
vi.advanceTimersByTime(HOOK_DONE_QUIET_MS)
|
||||
|
||||
expect(dispatchCompletion).toHaveBeenCalledTimes(1)
|
||||
expect(dispatchCompletion).toHaveBeenCalledWith('codex')
|
||||
expect(dispatchCompletion).toHaveBeenCalledWith('codex', {
|
||||
source: 'hook',
|
||||
quietedHookDone: true
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores stale working title state after a hook completion already notified', () => {
|
||||
@@ -616,6 +619,7 @@ describe('agent completion coordinator', () => {
|
||||
prompt: 'run the goal',
|
||||
agentType: 'codex'
|
||||
})
|
||||
expect(coordinator.hasPendingHookDoneCompletion()).toBe(true)
|
||||
vi.advanceTimersByTime(HOOK_DONE_QUIET_MS - 1)
|
||||
expect(dispatchCompletion).not.toHaveBeenCalled()
|
||||
|
||||
@@ -624,6 +628,7 @@ describe('agent completion coordinator', () => {
|
||||
prompt: 'run the goal',
|
||||
agentType: 'codex'
|
||||
})
|
||||
expect(coordinator.hasPendingHookDoneCompletion()).toBe(false)
|
||||
vi.advanceTimersByTime(HOOK_DONE_QUIET_MS)
|
||||
expect(dispatchCompletion).not.toHaveBeenCalled()
|
||||
|
||||
@@ -635,7 +640,10 @@ describe('agent completion coordinator', () => {
|
||||
vi.advanceTimersByTime(HOOK_DONE_QUIET_MS)
|
||||
|
||||
expect(dispatchCompletion).toHaveBeenCalledTimes(1)
|
||||
expect(dispatchCompletion).toHaveBeenCalledWith('codex')
|
||||
expect(dispatchCompletion).toHaveBeenCalledWith('codex', {
|
||||
source: 'hook',
|
||||
quietedHookDone: true
|
||||
})
|
||||
})
|
||||
|
||||
it.each([
|
||||
|
||||
@@ -114,7 +114,11 @@ export function createAgentCompletionCoordinator(
|
||||
return `${source}:${currentTurn}:${processSession}`
|
||||
}
|
||||
|
||||
function dispatchCompletion(source: CompletionSource, title: string): void {
|
||||
function dispatchCompletion(
|
||||
source: CompletionSource,
|
||||
title: string,
|
||||
optionsOverride: { quietedHookDone?: boolean } = {}
|
||||
): void {
|
||||
if (source !== 'hook' && pendingHookDoneTimer !== null) {
|
||||
return
|
||||
}
|
||||
@@ -134,7 +138,14 @@ export function createAgentCompletionCoordinator(
|
||||
lastCompletedTurn = currentTurn
|
||||
lastCompletionSource = source
|
||||
workingStatusObserved = false
|
||||
options.dispatchCompletion(title)
|
||||
if (optionsOverride.quietedHookDone === true) {
|
||||
options.dispatchCompletion(title, {
|
||||
source,
|
||||
quietedHookDone: true
|
||||
})
|
||||
} else {
|
||||
options.dispatchCompletion(title)
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleHookDoneCompletion(title: string): void {
|
||||
@@ -149,7 +160,7 @@ export function createAgentCompletionCoordinator(
|
||||
const pendingTitle = pendingHookDoneTitle
|
||||
pendingHookDoneTitle = null
|
||||
if (pendingTitle) {
|
||||
dispatchCompletion('hook', pendingTitle)
|
||||
dispatchCompletion('hook', pendingTitle, { quietedHookDone: true })
|
||||
}
|
||||
}, HOOK_DONE_QUIET_MS)
|
||||
}
|
||||
@@ -461,6 +472,10 @@ export function createAgentCompletionCoordinator(
|
||||
scheduleNextPoll()
|
||||
}
|
||||
|
||||
function hasPendingHookDoneCompletion(): boolean {
|
||||
return pendingHookDoneTimer !== null
|
||||
}
|
||||
|
||||
function resetCompletionState(options: { requireFreshWorking?: boolean } = {}): void {
|
||||
clearPendingHookDone()
|
||||
dropPendingTitle()
|
||||
@@ -490,6 +505,7 @@ export function createAgentCompletionCoordinator(
|
||||
observeTitleWorking,
|
||||
observeHookStatus,
|
||||
startProcessTracking,
|
||||
hasPendingHookDoneCompletion,
|
||||
resetCompletionState,
|
||||
dispose
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ async function flushAsyncTicks(count = 6): Promise<void> {
|
||||
const toastInfo = vi.fn()
|
||||
const LEAF_1 = '11111111-1111-4111-8111-111111111111' as const
|
||||
const LEAF_2 = '22222222-2222-4222-8222-222222222222' as const
|
||||
const AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS = 250
|
||||
const AGENT_TASK_COMPLETE_NOTIFICATION_MAX_WAIT_MS = 1_500
|
||||
|
||||
function leafIdForPane(paneId: number): string {
|
||||
@@ -4447,6 +4448,123 @@ describe('connectPanePty', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('lets delayed hook completion notifications win over concurrent terminal bells', async () => {
|
||||
const { connectPanePty } = await import('./pty-connection')
|
||||
const transport = createMockTransport('pty-hook')
|
||||
transportFactoryQueue.push(transport)
|
||||
|
||||
vi.useFakeTimers()
|
||||
const pane = createPane(1)
|
||||
const manager = createManager(1)
|
||||
const deps = createDeps()
|
||||
|
||||
connectPanePty(pane as never, manager as never, deps as never)
|
||||
|
||||
const statusHandler = createdTransportOptions[0]?.onAgentStatus as
|
||||
| ((payload: {
|
||||
state: 'working' | 'done'
|
||||
prompt: string
|
||||
agentType: 'codex'
|
||||
lastAssistantMessage?: string
|
||||
}) => void)
|
||||
| undefined
|
||||
const bellHandler = createdTransportOptions[0]?.onBell as (() => void) | undefined
|
||||
if (!statusHandler || !bellHandler) {
|
||||
throw new Error('Expected hook status and bell handlers to be registered')
|
||||
}
|
||||
|
||||
statusHandler({
|
||||
state: 'working',
|
||||
prompt: 'finish the implementation',
|
||||
agentType: 'codex'
|
||||
})
|
||||
statusHandler({
|
||||
state: 'done',
|
||||
prompt: 'finish the implementation',
|
||||
agentType: 'codex',
|
||||
lastAssistantMessage: 'Done.'
|
||||
})
|
||||
bellHandler()
|
||||
|
||||
vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS)
|
||||
expect(deps.dispatchNotification).not.toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'terminal-bell' })
|
||||
)
|
||||
|
||||
vi.advanceTimersByTime(
|
||||
AGENT_TASK_COMPLETE_NOTIFICATION_MAX_WAIT_MS - AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS
|
||||
)
|
||||
expect(deps.dispatchNotification).not.toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'terminal-bell' })
|
||||
)
|
||||
vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS)
|
||||
expect(deps.dispatchNotification).toHaveBeenCalledWith({
|
||||
source: 'agent-task-complete',
|
||||
terminalTitle: 'codex',
|
||||
paneKey: makePaneKey('tab-1', LEAF_1)
|
||||
})
|
||||
expect(deps.dispatchNotification).not.toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'terminal-bell' })
|
||||
)
|
||||
})
|
||||
|
||||
it('restores a suppressed terminal bell when a delayed hook completion resumes work', async () => {
|
||||
const { connectPanePty } = await import('./pty-connection')
|
||||
const transport = createMockTransport('pty-hook')
|
||||
transportFactoryQueue.push(transport)
|
||||
|
||||
vi.useFakeTimers()
|
||||
const pane = createPane(1)
|
||||
const manager = createManager(1)
|
||||
const deps = createDeps()
|
||||
|
||||
connectPanePty(pane as never, manager as never, deps as never)
|
||||
|
||||
const statusHandler = createdTransportOptions[0]?.onAgentStatus as
|
||||
| ((payload: {
|
||||
state: 'working' | 'done'
|
||||
prompt: string
|
||||
agentType: 'codex'
|
||||
lastAssistantMessage?: string
|
||||
}) => void)
|
||||
| undefined
|
||||
const bellHandler = createdTransportOptions[0]?.onBell as (() => void) | undefined
|
||||
if (!statusHandler || !bellHandler) {
|
||||
throw new Error('Expected hook status and bell handlers to be registered')
|
||||
}
|
||||
|
||||
statusHandler({
|
||||
state: 'working',
|
||||
prompt: 'finish the implementation',
|
||||
agentType: 'codex'
|
||||
})
|
||||
statusHandler({
|
||||
state: 'done',
|
||||
prompt: 'finish the implementation',
|
||||
agentType: 'codex',
|
||||
lastAssistantMessage: 'Done.'
|
||||
})
|
||||
bellHandler()
|
||||
vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS)
|
||||
expect(deps.dispatchNotification).not.toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'terminal-bell' })
|
||||
)
|
||||
|
||||
statusHandler({
|
||||
state: 'working',
|
||||
prompt: 'finish the implementation',
|
||||
agentType: 'codex'
|
||||
})
|
||||
vi.advanceTimersByTime(AGENT_TASK_COMPLETE_NOTIFICATION_GRACE_MS)
|
||||
|
||||
expect(deps.dispatchNotification).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'terminal-bell' })
|
||||
)
|
||||
expect(deps.dispatchNotification).not.toHaveBeenCalledWith(
|
||||
expect.objectContaining({ source: 'agent-task-complete' })
|
||||
)
|
||||
})
|
||||
|
||||
it('restores a suppressed terminal bell when the pending agent completion is canceled', async () => {
|
||||
const { connectPanePty } = await import('./pty-connection')
|
||||
const transport = createMockTransport()
|
||||
|
||||
@@ -187,11 +187,17 @@ function hasAgentNotificationDetail(entry: AgentStatusEntry | undefined): boolea
|
||||
)
|
||||
}
|
||||
|
||||
function canDispatchAgentNotificationAfterGrace(entry: AgentStatusEntry | undefined): boolean {
|
||||
function canDispatchAgentNotificationAfterGrace(
|
||||
entry: AgentStatusEntry | undefined,
|
||||
options: { allowDoneDetailAfterGrace?: boolean } = {}
|
||||
): boolean {
|
||||
// Why: hook-backed goal/mission loops can report `done` between milestones.
|
||||
// User-input states may notify as soon as detail arrives, but `done` waits
|
||||
// for the max quiet window so resumed work can cancel the pending banner.
|
||||
return hasAgentNotificationDetail(entry) && entry?.state !== 'done'
|
||||
return (
|
||||
hasAgentNotificationDetail(entry) &&
|
||||
(entry?.state !== 'done' || options.allowDoneDetailAfterGrace === true)
|
||||
)
|
||||
}
|
||||
|
||||
function recordPtyConnectDiagnostic(message: string): void {
|
||||
@@ -665,7 +671,10 @@ export function connectPanePty(
|
||||
getPtyId: () => transport.getPtyId(),
|
||||
getSettings: () => useAppStore.getState().settings,
|
||||
inspectProcess: inspectRuntimeTerminalProcess,
|
||||
dispatchCompletion: (title) => scheduleAgentTaskCompleteNotification(title),
|
||||
dispatchCompletion: (title, meta) =>
|
||||
scheduleAgentTaskCompleteNotification(title, {
|
||||
allowDoneDetailAfterGrace: meta?.quietedHookDone
|
||||
}),
|
||||
shouldPollProcessCadence: () =>
|
||||
isAgentTaskCompleteNotificationEnabled() && deps.isVisibleRef.current,
|
||||
isLive: () => {
|
||||
@@ -913,7 +922,8 @@ export function connectPanePty(
|
||||
|
||||
const hasPendingAgentTaskCompleteNotification = (): boolean =>
|
||||
isAgentTaskCompleteNotificationEnabled() &&
|
||||
(agentTaskCompleteNotificationGraceTimer !== null ||
|
||||
(agentCompletionCoordinator.hasPendingHookDoneCompletion() ||
|
||||
agentTaskCompleteNotificationGraceTimer !== null ||
|
||||
agentTaskCompleteNotificationMaxTimer !== null ||
|
||||
agentTaskCompleteStatusUnsubscribe !== null)
|
||||
|
||||
@@ -953,7 +963,10 @@ export function connectPanePty(
|
||||
return enabled
|
||||
}
|
||||
|
||||
const scheduleAgentTaskCompleteNotification = (title: string): void => {
|
||||
const scheduleAgentTaskCompleteNotification = (
|
||||
title: string,
|
||||
options: { allowDoneDetailAfterGrace?: boolean } = {}
|
||||
): void => {
|
||||
if (!syncAgentTaskCompleteNotificationEnabled()) {
|
||||
return
|
||||
}
|
||||
@@ -986,7 +999,7 @@ export function connectPanePty(
|
||||
return
|
||||
}
|
||||
const entry = useAppStore.getState().agentStatusByPaneKey[cacheKey]
|
||||
if (canDispatchAgentNotificationAfterGrace(entry)) {
|
||||
if (canDispatchAgentNotificationAfterGrace(entry, options)) {
|
||||
dispatch()
|
||||
}
|
||||
}
|
||||
@@ -1143,6 +1156,9 @@ export function connectPanePty(
|
||||
if (syncAgentTaskCompleteNotificationEnabled()) {
|
||||
agentCompletionCoordinator.observeHookStatus(payload)
|
||||
}
|
||||
if (payload.state === 'working' && pendingTerminalBellNotification) {
|
||||
scheduleTerminalBellNotification()
|
||||
}
|
||||
}
|
||||
}
|
||||
const transport = runtimeEnvironmentId
|
||||
|
||||
Reference in New Issue
Block a user