fix(cli): resolve duplicate footer on tool cancel via ESC (#21743) (#21781)

This commit is contained in:
ruomeng
2026-03-18 16:27:38 -04:00
committed by GitHub
parent fd44718bfe
commit 94e6bf8591
2 changed files with 83 additions and 7 deletions
@@ -5,7 +5,7 @@
*/
import type React from 'react';
import { useMemo, useCallback, useState } from 'react';
import { useEffect, useMemo, useCallback, useState } from 'react';
import { Box, Text } from 'ink';
import { DiffRenderer } from './DiffRenderer.js';
import { RenderInline } from '../../utils/InlineMarkdownRenderer.js';
@@ -79,6 +79,7 @@ export const ToolConfirmationMessage: React.FC<
callId,
expanded: false,
});
const [isCancelling, setIsCancelling] = useState(false);
const isMcpToolDetailsExpanded =
mcpDetailsExpansionState.callId === callId
? mcpDetailsExpansionState.expanded
@@ -183,7 +184,7 @@ export const ToolConfirmationMessage: React.FC<
return true;
}
if (keyMatchers[Command.ESCAPE](key)) {
handleConfirm(ToolConfirmationOutcome.Cancel);
setIsCancelling(true);
return true;
}
if (keyMatchers[Command.QUIT](key)) {
@@ -196,6 +197,20 @@ export const ToolConfirmationMessage: React.FC<
{ isActive: isFocused, priority: true },
);
// TODO(#23009): Remove this hack once we migrate to the new renderer.
// Why useEffect is used here instead of calling handleConfirm directly:
// There is a race condition where calling handleConfirm immediately upon
// keypress removes the tool UI component while the UI is in an expanded state.
// This simultaneously triggers setConstrainHeight, causing render two footers.
// By bridging the cancel action through state (isCancelling) and this useEffect,
// we delay handleConfirm until the next render cycle, ensuring setConstrainHeight
// resolves properly first.
useEffect(() => {
if (isCancelling) {
handleConfirm(ToolConfirmationOutcome.Cancel);
}
}, [isCancelling, handleConfirm]);
const handleSelect = useCallback(
(item: ToolConfirmationOutcome) => handleConfirm(item),
[handleConfirm],