From 5280a3b165c8cb65bfd4e30a2fc3111253f69fab Mon Sep 17 00:00:00 2001 From: weishu Date: Mon, 14 Sep 2026 14:29:47 +0800 Subject: [PATCH] fix(chat): dismiss Codex plan actions when continuing planning --- android/README.md | 4 +- .../companion/feature/chat/ChatViewModel.kt | 5 +++ .../chat/ChatViewModelInteractionTest.kt | 13 +++++- .../HapiClient/Chat/ChatInteractor.swift | 6 ++- .../Chat/ChatInteractorTests.swift | 12 +++++- ios/README.md | 4 +- web/README.md | 4 ++ web/src/components/AssistantChat/context.tsx | 21 ++++++++-- .../ToolCard/CodexPlanFooter.test.tsx | 42 +++++++++++++++++-- .../components/ToolCard/CodexPlanFooter.tsx | 4 +- 10 files changed, 102 insertions(+), 13 deletions(-) diff --git a/android/README.md b/android/README.md index 62f69551..a871412b 100644 --- a/android/README.md +++ b/android/README.md @@ -310,7 +310,9 @@ input/result remains under Source. Shared Codex proposals also show **Implement plan** and **Continue planning** when the active session's `agentState.codexPlanProposalId` matches the tool-call id. Implementation uses the dedicated plan endpoint, not permission approval; -continue only focuses the composer, preserving its draft. The menu stays visible +continue hides that proposal’s action menu locally and focuses the composer, +preserving its draft and plan mode without sending a message. The plan document +remains readable, and a new proposal gets a fresh menu. The menu stays visible when the document is folded; pending/error state survives row recycling. Withdrawn, historical and child proposals remain read-only (an outstanding operation/error can still be shown). diff --git a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt index 376822af..f7072071 100644 --- a/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt +++ b/android/app/src/main/kotlin/app/hapi/companion/feature/chat/ChatViewModel.kt @@ -415,6 +415,7 @@ class ChatViewModel( private data class CodexPlanOperations( val pendingPlanId: String? = null, val implementedPlanIds: Set = emptySet(), + val continuedPlanIds: Set = emptySet(), val errors: Map = emptyMap(), ) private val codexPlanOperations = MutableStateFlow(CodexPlanOperations()) @@ -1469,6 +1470,7 @@ class ChatViewModel( detail.active && detail.metadata?.flavor == "codex" && detail.metadata?.capabilities?.concurrentClients == true && it !in operations.implementedPlanIds + && it !in operations.continuedPlanIds }, pendingPlanId = operations.pendingPlanId, disabled = busy || detail?.thinking == true, @@ -1520,6 +1522,9 @@ class ChatViewModel( fun continueCodexPlan(planId: String) { if (!currentCodexPlanActions().forPlan(planId).canAct) return + codexPlanOperations.update { + it.copy(continuedPlanIds = it.continuedPlanIds + planId, errors = it.errors - planId) + } composerFocusRequest.update { it + 1 } } diff --git a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt index 2bd14186..53559e4a 100644 --- a/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt +++ b/android/app/src/test/kotlin/app/hapi/companion/feature/chat/ChatViewModelInteractionTest.kt @@ -483,7 +483,7 @@ class ChatViewModelInteractionTest { assertEquals(0L, vm.composer.value.focusRequest) } - @Test fun `continue planning preserves the draft and mode without a request`() = runTest { + @Test fun `continue planning dismisses actions and preserves the draft and mode without a request`() = runTest { val harness = InteractionHarness(this, planDetail()) harness.viewModel.setComposerText("Refine step two") harness.viewModel.continueCodexPlan("plan-1") @@ -495,6 +495,17 @@ class ChatViewModelInteractionTest { assertTrue(harness.api.sendCalls.value.isEmpty()) assertTrue(harness.api.approveCalls.value.isEmpty()) assertTrue(harness.api.configCalls.value.isEmpty()) + assertFalse(harness.viewModel.codexPlanActions.value.forPlan("plan-1").isVisible) + harness.viewModel.continueCodexPlan("plan-1") + harness.viewModel.implementCodexPlan("plan-1") + harness.sessionStore.setDetail(planDetail()) + testScheduler.runCurrent() + assertEquals(1L, harness.viewModel.composer.value.focusRequest) + assertTrue(harness.api.planCalls.value.isEmpty()) + assertFalse(harness.viewModel.codexPlanActions.value.forPlan("plan-1").isVisible) + harness.sessionStore.setDetail(planDetail().copy(agentState = AgentState(codexPlanProposalId = "plan-2"))) + testScheduler.runCurrent() + assertTrue(harness.viewModel.codexPlanActions.value.forPlan("plan-2").canAct) } @Test fun `plan implementation is single flight and acceptance survives stale refresh`() = runTest { diff --git a/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatInteractor.swift b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatInteractor.swift index 7e9e9157..947d9b7b 100644 --- a/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatInteractor.swift +++ b/ios/Packages/HapiKit/Sources/HapiClient/Chat/ChatInteractor.swift @@ -88,6 +88,7 @@ public final class ChatInteractor { // Screen-owned, not row-local: recycled plan cards retain operation state. private var pendingCodexPlanId: String? private var implementedCodexPlanIds: Set = [] + private var continuedCodexPlanIds: Set = [] private var codexPlanErrors: [String: String] = [:] private var queuedOpPending = false @@ -835,6 +836,7 @@ public final class ChatInteractor { && detail?.metadata?.capabilities?.concurrentClients == true && detail?.agentState?.codexPlanProposalId == planId && !implementedCodexPlanIds.contains(planId) + && !continuedCodexPlanIds.contains(planId) return CodexPlanActionState( available: available, pending: pendingCodexPlanId == planId, @@ -870,10 +872,12 @@ public final class ChatInteractor { } } - /// Continue planning only focuses the existing composer; it neither sends + /// Continue planning dismisses this proposal’s actions and focuses the composer; it neither sends /// text nor resolves an approval nor changes the collaboration mode. public func continueCodexPlan(planId: String) { guard codexPlanActions(planId: planId).canAct else { return } + continuedCodexPlanIds.insert(planId) + codexPlanErrors[planId] = nil composerFocusRequest += 1 } diff --git a/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatInteractorTests.swift b/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatInteractorTests.swift index 257889c5..1800e0eb 100644 --- a/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatInteractorTests.swift +++ b/ios/Packages/HapiKit/Tests/HapiClientTests/Chat/ChatInteractorTests.swift @@ -998,7 +998,7 @@ struct ChatInteractorTests { #expect(await harness.performer.count("POST", pathSuffix: "/codex/plan/implement") == 0) } - @Test func continuePlanningOnlyFocusesAndPreservesDraftAndMode() async throws { + @Test func continuePlanningDismissesActionsAndPreservesDraftAndMode() async throws { let harness = try await ChatInteractionHarness(detail: planSession(), activate: false) harness.interactor.setComposerText("Please refine step two") harness.interactor.continueCodexPlan(planId: "plan-1") @@ -1006,6 +1006,16 @@ struct ChatInteractorTests { #expect(harness.interactor.composerText == "Please refine step two") #expect(harness.store.detail(for: chatSessionID)?.collaborationMode == .plan) #expect(await harness.performer.exchanges.filter { $0.method == "POST" }.isEmpty) + #expect(!harness.interactor.codexPlanActions(planId: "plan-1").isVisible) + harness.interactor.continueCodexPlan(planId: "plan-1") + harness.interactor.implementCodexPlan(planId: "plan-1") + #expect(harness.interactor.composerFocusRequest == 1) + harness.store.updateDetailLocal(chatSessionID) { $0 = planSession() } + #expect(!harness.interactor.codexPlanActions(planId: "plan-1").isVisible) + harness.store.updateDetailLocal(chatSessionID) { + $0.agentState?.codexPlanProposalId = "plan-2" + } + #expect(harness.interactor.codexPlanActions(planId: "plan-2").canAct) } @Test func implementingPlanIsSingleFlightAndSurvivesWithdrawalAndStaleRefresh() async throws { diff --git a/ios/README.md b/ios/README.md index b15e873b..eabbdbc3 100644 --- a/ios/README.md +++ b/ios/README.md @@ -381,7 +381,9 @@ inspector; null output does not show a misleading "No output" placeholder. Shared Codex proposals expose **Implement plan** and **Continue planning** only when the active session's `agentState.codexPlanProposalId` matches the tool-call id. Implementation uses the dedicated plan endpoint, not permission approval; -continue only focuses the composer, preserving its draft. Pending/error state +continue hides that proposal’s action menu locally and focuses the composer, +preserving its draft and plan mode without sending a message. The plan document +remains readable, and a new proposal gets a fresh menu. Pending/error state survives row recycling. Withdrawn, historical and child proposals stay read-only (an outstanding operation/error can still be shown). Plans are prewarmed in the chat Markdown cache and never use the ordinary diff --git a/web/README.md b/web/README.md index 78c6fca2..4b65b18c 100644 --- a/web/README.md +++ b/web/README.md @@ -66,6 +66,10 @@ See `src/router.tsx` for route definitions. - Composer for sending messages. - Permission mode and model selection for supported agents. - Session abort and handoff controls. +- Codex **Continue planning** hides the current proposal's action menu locally + and focuses the composer without sending a message or replacing its draft. + The plan document remains readable; recycled cards stay dismissed and new + proposals get fresh action menus. - Context size display. - Per-session scratchlist (`src/components/AssistantChat/ScratchlistPanel.tsx`) - Workbench panel for held notes/drafts; **distinct from the queue**. diff --git a/web/src/components/AssistantChat/context.tsx b/web/src/components/AssistantChat/context.tsx index be69c0b3..d55c373e 100644 --- a/web/src/components/AssistantChat/context.tsx +++ b/web/src/components/AssistantChat/context.tsx @@ -1,5 +1,5 @@ import type { ReactNode } from 'react' -import { createContext, useContext } from 'react' +import { createContext, useContext, useState } from 'react' import type { ApiClient } from '@/api/client' import type { TerminalToolDisplayMode } from '@/hooks/useTerminalToolDisplayMode' import type { SessionMetadataSummary } from '@/types/api' @@ -16,7 +16,8 @@ export type HappyChatContextValue = { disabled: boolean onRefresh: () => void codexPlanProposalId?: string | null - onContinuePlan?: () => void + onContinuePlan?: (planId: string) => void + continuedPlanIds?: ReadonlySet onRetryMessage?: (localId: string) => void historyActionPending?: boolean onForkConversation?: (messageLocalId?: string) => Promise @@ -37,8 +38,22 @@ export type HappyChatContextValue = { const HappyChatContext = createContext(null) export function HappyChatProvider(props: { value: HappyChatContextValue; children: ReactNode }) { + // Keep dismissal outside virtualized cards so recycling cannot revive a menu. + const [continued, setContinued] = useState<{ sessionId: string; ids: ReadonlySet }>({ + sessionId: props.value.sessionId, ids: new Set() + }) + const ids = continued.sessionId === props.value.sessionId ? continued.ids : new Set() + const value: HappyChatContextValue = { + ...props.value, + continuedPlanIds: ids, + onContinuePlan: props.value.onContinuePlan ? (planId) => { + if (props.value.disabled || props.value.codexPlanProposalId !== planId || ids.has(planId)) return + setContinued({ sessionId: props.value.sessionId, ids: new Set([...ids, planId]) }) + props.value.onContinuePlan?.(planId) + } : undefined + } return ( - + {props.children} ) diff --git a/web/src/components/ToolCard/CodexPlanFooter.test.tsx b/web/src/components/ToolCard/CodexPlanFooter.test.tsx index 647f9666..2360935e 100644 --- a/web/src/components/ToolCard/CodexPlanFooter.test.tsx +++ b/web/src/components/ToolCard/CodexPlanFooter.test.tsx @@ -26,22 +26,48 @@ function fixture(overrides: Partial = {}) { tool: { id: 'plan', name: 'ExitPlanMode', input: { plan: '# Durable proposal' }, state: 'completed', createdAt: 1, startedAt: 1, completedAt: 2, execStartedAt: null, execCompletedAt: null, description: null, result: null } } + let cardMount = 0 const ui = () => - const view = render(ui()) - return { ctx, implement, continued, refresh, rerender: () => view.rerender(ui()) } + return { + ctx, block, implement, continued, refresh, + remount: () => { cardMount++; view.rerender(ui()) }, + rerender: () => view.rerender(ui()) + } } describe('shared Codex plan card', () => { - it('offers execution and composer focus without creating an approval request', () => { + it('dismisses actions and focuses the composer without creating an approval request', () => { const f = fixture() expect(screen.getByText('# Durable proposal')).toBeInTheDocument() expect(screen.queryByText('tool.waitingForApproval')).not.toBeInTheDocument() fireEvent.click(screen.getByRole('button', { name: 'tool.plan.continue' })) expect(f.continued).toHaveBeenCalledOnce() expect(f.implement).not.toHaveBeenCalled() + expect(screen.queryByRole('button', { name: 'tool.plan.continue' })).not.toBeInTheDocument() + expect(screen.queryByRole('button', { name: 'tool.plan.implement' })).not.toBeInTheDocument() + expect(screen.getByText('# Durable proposal')).toBeInTheDocument() + // Recycling the card and a stale live proposal must not revive the menu. + f.remount() + f.ctx.codexPlanProposalId = null + f.rerender() + f.ctx.codexPlanProposalId = 'plan' + f.rerender() + expect(screen.queryByRole('button', { name: 'tool.plan.continue' })).not.toBeInTheDocument() + }) + + it('offers actions for a new proposal after continuing the previous one', () => { + const f = fixture() + fireEvent.click(screen.getByRole('button', { name: 'tool.plan.continue' })) + f.ctx.codexPlanProposalId = 'new-plan' + f.block.id = 'new-plan' + f.block.tool.id = 'new-plan' + f.rerender() + expect(screen.getByRole('button', { name: 'tool.plan.continue' })).toBeEnabled() + expect(screen.getByRole('button', { name: 'tool.plan.implement' })).toBeEnabled() }) it('submits once and keeps pending feedback while the native action removes the available id', async () => { @@ -71,6 +97,16 @@ describe('shared Codex plan card', () => { expect(f.implement).toHaveBeenCalledOnce() }) + it('dismisses an earlier implementation error along with the actions', async () => { + const f = fixture() + f.implement.mockRejectedValue(new Error('Try again')) + fireEvent.click(screen.getByRole('button', { name: 'tool.plan.implement' })) + await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Try again')) + fireEvent.click(screen.getByRole('button', { name: 'tool.plan.continue' })) + expect(screen.queryByRole('alert')).not.toBeInTheDocument() + expect(screen.queryByRole('button', { name: 'tool.plan.implement' })).not.toBeInTheDocument() + }) + it.each([null, 'child-plan', 'newer-plan'])('keeps historical plans read-only when the current id is %s', codexPlanProposalId => { fixture({ codexPlanProposalId }) expect(screen.getByText('# Durable proposal')).toBeInTheDocument() diff --git a/web/src/components/ToolCard/CodexPlanFooter.tsx b/web/src/components/ToolCard/CodexPlanFooter.tsx index 3518b16c..eda8a88d 100644 --- a/web/src/components/ToolCard/CodexPlanFooter.tsx +++ b/web/src/components/ToolCard/CodexPlanFooter.tsx @@ -13,7 +13,7 @@ export function CodexPlanFooter(props: { planId: string }) { const [error, setError] = useState(null) const available = ctx?.metadata?.flavor === 'codex' && ctx.codexPlanProposalId === props.planId - if (!ctx || (!available && !pending && !error)) return null + if (!ctx || ctx.continuedPlanIds?.has(props.planId) || (!available && !pending && !error)) return null const implement = async () => { if (!available || ctx.disabled || inFlight.current) return @@ -41,7 +41,7 @@ export function CodexPlanFooter(props: { planId: string }) { {t('tool.plan.implement')} {ctx.onContinuePlan ? ( - ) : null}