From f59ba4adc0f530409e8665bb7e525fa903e99fe0 Mon Sep 17 00:00:00 2001 From: Mason Daugherty <61371264+mdrxy@users.noreply.github.com> Date: Thu, 20 Aug 2026 04:05:38 +0000 Subject: [PATCH 1/4] fix(code): clarify auto mode notice controls Co-authored-by: open-swe[bot] --- libs/code/deepagents_code/tui/widgets/auto_mode_notice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py b/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py index 2ece75b6cbb..58331350549 100644 --- a/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py +++ b/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py @@ -210,7 +210,7 @@ def compose(self) -> ComposeResult: open_links=False, ) yield Static( - "Enter to keep Auto · Esc for Manual", + "Enter switch to Auto · Esc keep Manual", classes="auto-mode-notice-help", markup=False, ) From 89ef8fea82ff3107630a3c34fa538713e1aed433 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Thu, 20 Aug 2026 12:01:24 -0400 Subject: [PATCH 2/4] fix(code): show Auto first-enable notice as pre-confirmation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The modal previously appeared after Auto was already active, asking the user to confirm something that had already happened. Enter "kept" Auto and Esc reverted to Manual — confusing semantics for a state transition that already occurred. Now the modal gates the switch: it appears before Auto activates. Enter confirms the switch and saves the notice; Esc cancels and stays in the current mode. This matches the existing YOLO acknowledgement pattern and makes the key labels accurate for the first time. The change applies to both entry points: Shift+Tab toggle and inline "approve all" escalation. Startup restore no longer needs the notice since an unsaved notice means Auto was never entered. --- libs/code/deepagents_code/app.py | 189 +++++++------- .../tui/widgets/auto_mode_notice.py | 19 +- libs/code/tests/unit_tests/test_app.py | 245 +++++++----------- 3 files changed, 203 insertions(+), 250 deletions(-) diff --git a/libs/code/deepagents_code/app.py b/libs/code/deepagents_code/app.py index e8bdaacc252..31534842bd9 100644 --- a/libs/code/deepagents_code/app.py +++ b/libs/code/deepagents_code/app.py @@ -4592,7 +4592,6 @@ async def on_mount(self) -> None: self._status_bar.set_approval_mode(self._approval_mode.value) if self._approval_mode.value == "auto": - self._notify_auto_mode_enabled_once() self._notify_auto_classifier_active() elif self._approval_mode.value == "yolo": self._warn_yolo_active(timeout=10) @@ -9502,112 +9501,59 @@ def _warn_yolo_active(self, *, timeout: float) -> None: markup=False, ) - def _notify_auto_mode_enabled_once(self) -> None: - """Show the Auto first-enable modal at most once per install. + async def _on_auto_approve_enabled(self) -> bool: + """Enable Auto only after the live Store acknowledges it. - Auto is already active when this runs. Enter keeps Auto and records the - notice; Esc (or a non-true dismiss) reverts to Manual without saving so - the notice can appear again. Save is best-effort on accept so a failed - write only re-shows on a later successful enable path. + Shows the first-enable confirmation modal before activating Auto when + the notice has not been shown yet. If the user cancels (Esc), returns + `False` so the inline approval loop continues asking. + + Returns: + `True` when Auto is active for subsequent actions. """ from deepagents_code.approval_mode import ( + ApprovalMode, has_auto_mode_notice, save_auto_mode_notice, ) from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen - if has_auto_mode_notice() or getattr(self, "_auto_mode_notice_pending", False): - return - - def handle_result(accepted: bool | None) -> None: - self._auto_mode_notice_pending = False - # Only an explicit continue persists "do not show again". Esc/None - # invert Auto back to Manual without recording the notice. - if accepted is True: - save_auto_mode_notice() - return - # `push_screen` callbacks are sync; schedule the Manual revert. - task = asyncio.create_task(self._revert_auto_mode_after_notice_cancel()) - task.add_done_callback(_log_task_exception) - - try: - self.push_screen( - AutoModeNoticeScreen( - model_label=self._auto_classifier_review_model_spec(), - distinct_from_main_model=self._auto_classifier_is_distinct(), - ), - handle_result, + if not self._auto_mode_eligible: + self._warn_live_approval_mode_unavailable( + "Auto is unavailable with a sandbox." ) - except Exception: - # Cosmetic notice must never break an already-applied Auto enable. - logger.warning("Could not show Auto first-enable notice", exc_info=True) - return - - # Mark pending only after the push succeeds so a failed push can't - # strand the guard True and suppress the notice for the whole session. - self._auto_mode_notice_pending = True - - async def _revert_auto_mode_after_notice_cancel(self) -> None: - """Leave Auto after the first-enable modal is cancelled with Esc. - - Best-effort live Store write mirrors the Manual toggle path: local TUI - state always returns to Manual; a failed live write warns and blocks new - runs when a live session was already writing approval mode. - """ - from deepagents_code.approval_mode import ApprovalMode - - # Already left Auto (e.g. a concurrent toggle) — nothing to undo. - if self._approval_mode is not ApprovalMode.AUTO: - return + return False - should_persist_live = ( - self._agent is not None and self._session_state is not None - ) - if should_persist_live and not await self._write_live_approval_mode( - ApprovalMode.MANUAL + if not has_auto_mode_notice() and not getattr( + self, "_auto_mode_notice_pending", False ): - self._approval_mode_blocked = True - self._warn_live_approval_mode_unavailable( - "Manual could not be persisted after declining Auto; active work " - "was cancelled and new runs are blocked." - ) - if self._agent_running: - self._force_interrupt_active_work() - # Still flip the UI so the user is not left trusting a declined Auto. - self._approval_mode = ApprovalMode.MANUAL - self._auto_approve = False - if self._session_state: - self._session_state.approval_mode = ApprovalMode.MANUAL - if self._status_bar: - self._status_bar.set_approval_mode(ApprovalMode.MANUAL.value) - return + # Pre-confirmation: show modal before activating Auto + loop = asyncio.get_running_loop() + future: asyncio.Future[bool] = loop.create_future() - self._approval_mode_blocked = False - self._approval_mode = ApprovalMode.MANUAL - self._auto_approve = False - if self._session_state: - self._session_state.approval_mode = ApprovalMode.MANUAL - if self._status_bar: - self._status_bar.set_approval_mode(ApprovalMode.MANUAL.value) - self.notify( - "Returned to Manual approval.", - severity="information", - markup=False, - ) + def handle_result(accepted: bool | None) -> None: + self._auto_mode_notice_pending = False + if not future.done(): + future.set_result(accepted is True) - async def _on_auto_approve_enabled(self) -> bool: - """Enable Auto only after the live Store acknowledges it. + try: + self.push_screen( + AutoModeNoticeScreen( + model_label=self._auto_classifier_review_model_spec(), + distinct_from_main_model=self._auto_classifier_is_distinct(), + ), + handle_result, + ) + except Exception: + logger.warning("Could not show Auto confirmation", exc_info=True) + return False + self._auto_mode_notice_pending = True - Returns: - `True` when Auto is active for subsequent actions. - """ - from deepagents_code.approval_mode import ApprovalMode + confirmed = await future + if not confirmed: + return False + save_auto_mode_notice() - if not self._auto_mode_eligible: - self._warn_live_approval_mode_unavailable( - "Auto is unavailable with a sandbox." - ) - return False if not await self._write_live_approval_mode(ApprovalMode.AUTO): self._warn_live_approval_mode_unavailable( "Auto could not be persisted; this approval remains pending in Manual." @@ -9619,7 +9565,6 @@ async def _on_auto_approve_enabled(self) -> bool: self._status_bar.set_approval_mode(ApprovalMode.AUTO.value) if self._session_state: self._session_state.approval_mode = ApprovalMode.AUTO - self._notify_auto_mode_enabled_once() self._notify_auto_classifier_active() await self._auto_accept_pending_goal_rubric() return True @@ -20844,6 +20789,7 @@ async def action_toggle_auto_approve(self) -> None: return from deepagents_code.approval_mode import ( ApprovalMode, + has_auto_mode_notice, has_yolo_acknowledgement, next_approval_mode, ) @@ -20865,6 +20811,11 @@ async def action_toggle_auto_approve(self) -> None: ) return + # Gate the first Auto switch on the education confirmation. + if target is ApprovalMode.AUTO and not has_auto_mode_notice(): + self._prompt_auto_mode_confirmation() + return + # Gate the first YOLO switcher entry on the same policy acknowledgement # as `--yolo`. Do not activate YOLO until the user continues. if target is ApprovalMode.YOLO and not has_yolo_acknowledgement(): @@ -20919,7 +20870,6 @@ async def _set_approval_mode(self, target: ApprovalMode) -> bool: if self._status_bar: self._status_bar.set_approval_mode(target.value) if target is ApprovalMode.AUTO: - self._notify_auto_mode_enabled_once() self._notify_auto_classifier_active() if should_persist_live: await self._auto_accept_pending_goal_rubric() @@ -21332,6 +21282,7 @@ async def _handle_approval_mode_command(self, target: ApprovalMode) -> None: """ from deepagents_code.approval_mode import ( ApprovalMode, + has_auto_mode_notice, has_yolo_acknowledgement, ) from deepagents_code.config import is_yolo_switcher_enabled @@ -21353,6 +21304,11 @@ async def _handle_approval_mode_command(self, target: ApprovalMode) -> None: ) return + # Gate the first Auto switch on the education confirmation. + if target is ApprovalMode.AUTO and not has_auto_mode_notice(): + self._prompt_auto_mode_confirmation() + return + if target is ApprovalMode.YOLO: if not is_yolo_switcher_enabled(): self._warn_live_approval_mode_unavailable( @@ -21431,6 +21387,51 @@ def handle_result(result: YoloModeNoticeResult | None) -> None: # suppress a later try for the whole session. self._yolo_mode_notice_pending = True + def _prompt_auto_mode_confirmation(self) -> None: + """Show the first-enable Auto confirmation before switching. + + Auto stays inactive until Enter persists the notice and the follow-up + mode write succeeds. Esc leaves the current mode untouched and does + not store the notice. + """ + from deepagents_code.approval_mode import ( + ApprovalMode, + save_auto_mode_notice, + ) + from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen + + if getattr(self, "_auto_mode_notice_pending", False): + return + + def handle_result(accepted: bool | None) -> None: + self._auto_mode_notice_pending = False + if accepted is not True: + return + if not save_auto_mode_notice(): + self._warn_live_approval_mode_unavailable( + "Auto notice could not be saved; staying in the current mode." + ) + return + task = asyncio.create_task(self._set_approval_mode(ApprovalMode.AUTO)) + task.add_done_callback(_log_task_exception) + + try: + self.push_screen( + AutoModeNoticeScreen( + model_label=self._auto_classifier_review_model_spec(), + distinct_from_main_model=self._auto_classifier_is_distinct(), + ), + handle_result, + ) + except Exception: + logger.warning("Could not show Auto confirmation", exc_info=True) + self._warn_live_approval_mode_unavailable( + "Could not open the Auto confirmation; approval mode unchanged." + ) + return + + self._auto_mode_notice_pending = True + def action_toggle_tool_output(self) -> None: """Toggle the most recent collapsible transcript unit.""" # Pending ask_user takes precedence so Ctrl+O toggles the question card. diff --git a/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py b/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py index 58331350549..a35e59e7c7b 100644 --- a/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py +++ b/libs/code/deepagents_code/tui/widgets/auto_mode_notice.py @@ -1,8 +1,8 @@ """First-enable confirmation modal for Auto mode. -Shown at most once per install (per notice version) after Auto successfully -becomes active. Enter keeps Auto and records the notice; Esc reverts to Manual -and leaves the notice unsaved so it can appear again next time. +Shown at most once per install (per notice version) before Auto activates. +Enter confirms the switch and records the notice; Esc cancels and leaves the +notice unsaved so it can appear again next time. """ from __future__ import annotations @@ -99,9 +99,10 @@ def build_auto_mode_notice_body( class AutoModeNoticeScreen(ModalScreen[bool]): """In-TUI first-run notice describing what Auto mode does. - Dismisses with `True` on Enter (keep Auto) and `False` on Esc (return to - Manual). Programmatic dismiss may yield `None`; callers treat that like - cancel so Auto is never left active without an explicit continue. + Dismisses with `True` on Enter (confirm switch to Auto) and `False` on + Esc (cancel, stay in current mode). Programmatic dismiss may yield `None`; + callers treat that like cancel so Auto is never activated without an + explicit confirm. """ BINDINGS: ClassVar[list[BindingType]] = [ @@ -210,7 +211,7 @@ def compose(self) -> ComposeResult: open_links=False, ) yield Static( - "Enter switch to Auto · Esc keep Manual", + "Enter switch to Auto · Esc cancel", classes="auto-mode-notice-help", markup=False, ) @@ -221,11 +222,11 @@ async def on_markdown_link_clicked(self, event: Markdown.LinkClicked) -> None: await open_checked_url_async(event.href, app=self.app, notify_on_success=True) def action_confirm(self) -> None: - """Keep Auto and mark the notice dismissed without re-showing.""" + """Confirm switch to Auto and mark the notice dismissed without re-showing.""" self.dismiss(True) def action_cancel(self) -> None: - """Return to Manual without persisting the notice. + """Cancel the Auto switch without persisting the notice. The method name must stay `cancel`: the app owns a priority `escape` binding that, for an active `ModalScreen`, dispatches to diff --git a/libs/code/tests/unit_tests/test_app.py b/libs/code/tests/unit_tests/test_app.py index 23b4efa4327..4205e638a5d 100644 --- a/libs/code/tests/unit_tests/test_app.py +++ b/libs/code/tests/unit_tests/test_app.py @@ -8767,6 +8767,10 @@ async def generate(request: dict[str, object]) -> None: new=AsyncMock(return_value=True), ), patch.object(app, "_continue_goal_work", handle), + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), ): await pilot.press("r") await pilot.pause() @@ -8926,6 +8930,10 @@ async def test_enabling_configured_auto_accepts_mounted_goal_review( new=AsyncMock(return_value=True), ), patch.object(app, "_continue_goal_work", handle), + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), ): await pilot.press("shift+tab") for _ in range(20): @@ -8983,6 +8991,10 @@ async def test_enabling_auto_honors_already_submitted_cancel( new=AsyncMock(return_value=True), ), patch.object(app, "_handle_user_message", handle), + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), ): await app._on_auto_approve_enabled() for _ in range(20): @@ -18949,9 +18961,13 @@ async def test_auto_command_sets_auto_when_eligible(self) -> None: app = DeepAgentsApp(agent=MagicMock()) app._auto_mode_eligible = True app._approval_mode = ApprovalMode.MANUAL - with patch.object( - app, "_set_approval_mode", new_callable=AsyncMock - ) as set_mode: + with ( + patch.object(app, "_set_approval_mode", new_callable=AsyncMock) as set_mode, + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), + ): await app._handle_command("/auto") set_mode.assert_awaited_once_with(ApprovalMode.AUTO) @@ -29564,6 +29580,10 @@ async def test_auto_approve_all_failed_write_warns(self) -> None: accept_goal, ), patch.object(app, "notify") as notify, + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), patch("deepagents_code.approval_mode.save_auto_mode_notice") as save_notice, ): await app._on_auto_approve_enabled() @@ -29578,7 +29598,7 @@ async def test_auto_approve_all_failed_write_warns(self) -> None: async def test_toggle_auto_first_enable_shows_modal_and_saves_on_dismiss( self, ) -> None: - """First successful Auto toggle shows the education modal once.""" + """First Auto toggle shows the confirmation modal before switching.""" from deepagents_code.approval_mode import ApprovalMode from deepagents_code.tui.widgets.auto_mode_notice import ( AutoModeNoticeScreen, @@ -29614,7 +29634,8 @@ async def test_toggle_auto_first_enable_shows_modal_and_saves_on_dismiss( ): await app.action_toggle_auto_approve() await pilot.pause() - assert app._approval_mode is ApprovalMode.AUTO + # Modal appears BEFORE mode changes — still MANUAL. + assert app._approval_mode is ApprovalMode.MANUAL assert isinstance(app.screen, AutoModeNoticeScreen) assert app.screen._body == build_auto_mode_notice_body( classifier, distinct_from_main_model=True @@ -30091,10 +30112,7 @@ async def test_set_approval_mode_reports_distinct_classifier_on_each_auto_entry( server_kwargs={"auto_classifier_model": classifier}, ) app._model_override = "openai:gpt-5.5" - with ( - patch.object(app, "_notify_auto_mode_enabled_once"), - patch.object(app, "notify") as notify, - ): + with patch.object(app, "notify") as notify: assert await app._set_approval_mode(ApprovalMode.AUTO) is True notify.assert_called_once_with( @@ -30115,10 +30133,7 @@ async def test_set_approval_mode_skips_classifier_toast_when_same_as_active( server_kwargs={"auto_classifier_model": classifier}, ) app._model_override = "openai:gpt-5.5" - with ( - patch.object(app, "_notify_auto_mode_enabled_once"), - patch.object(app, "notify") as notify, - ): + with patch.object(app, "notify") as notify: assert await app._set_approval_mode(ApprovalMode.AUTO) is True notify.assert_not_called() @@ -30193,16 +30208,20 @@ async def test_on_auto_approve_enabled_first_modal_and_save(self) -> None: return_value=True, ) as save_notice, ): - assert await app._on_auto_approve_enabled() is True + # The method awaits the modal result, so start it as a task + # and press Enter to confirm. + task = asyncio.create_task(app._on_auto_approve_enabled()) await pilot.pause() - assert app._approval_mode is ApprovalMode.AUTO + # Modal appears BEFORE activation. assert isinstance(app.screen, AutoModeNoticeScreen) assert app.screen._body == build_auto_mode_notice_body( classifier, distinct_from_main_model=True ) + assert app._approval_mode is not ApprovalMode.AUTO save_notice.assert_not_called() await pilot.press("enter") - await pilot.pause() + result = await task + assert result is True save_notice.assert_called_once_with() assert app._approval_mode is ApprovalMode.AUTO @@ -30243,58 +30262,6 @@ async def test_on_auto_approve_enabled_skips_modal_when_notice_shown(self) -> No assert not isinstance(app.screen, AutoModeNoticeScreen) save_notice.assert_not_called() - async def test_mount_auto_shows_modal_when_notice_unseen(self) -> None: - from deepagents_code.approval_mode import ApprovalMode - from deepagents_code.tui.widgets.auto_mode_notice import ( - AUTO_MODE_DOCS_URL, - AutoModeNoticeScreen, - build_auto_mode_notice_body, - ) - - classifier = "anthropic:claude-haiku-4-5" - with ( - patch( - "deepagents_code.approval_mode.has_auto_mode_notice", - return_value=False, - ), - patch( - "deepagents_code.approval_mode.save_auto_mode_notice", - return_value=True, - ) as save_notice, - ): - app = DeepAgentsApp(approval_mode=ApprovalMode.AUTO) - app._auto_classifier_model = classifier - async with app.run_test() as pilot: - await pilot.pause() - assert isinstance(app.screen, AutoModeNoticeScreen) - assert app.screen._body == build_auto_mode_notice_body( - classifier, distinct_from_main_model=True - ) - assert AUTO_MODE_DOCS_URL in app.screen._body - save_notice.assert_not_called() - await pilot.press("enter") - await pilot.pause() - save_notice.assert_called_once_with() - assert not isinstance(app.screen, AutoModeNoticeScreen) - assert app._approval_mode is ApprovalMode.AUTO - - async def test_mount_auto_skips_modal_when_notice_already_shown(self) -> None: - from deepagents_code.approval_mode import ApprovalMode - from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen - - with ( - patch( - "deepagents_code.approval_mode.has_auto_mode_notice", - return_value=True, - ), - patch("deepagents_code.approval_mode.save_auto_mode_notice") as save_notice, - ): - app = DeepAgentsApp(approval_mode=ApprovalMode.AUTO) - async with app.run_test() as pilot: - await pilot.pause() - assert not isinstance(app.screen, AutoModeNoticeScreen) - save_notice.assert_not_called() - async def test_mount_auto_reports_classifier_after_notice_was_shown(self) -> None: """Returning Auto users still see which model authorizes actions.""" from deepagents_code.approval_mode import ApprovalMode @@ -30318,88 +30285,68 @@ async def test_mount_auto_reports_classifier_after_notice_was_shown(self) -> Non markup=False, ) - async def test_mount_auto_notice_escape_reverts_to_manual_without_save( - self, - ) -> None: - """Esc cancels Auto and does not record the notice for this machine.""" + async def test_toggle_auto_first_enable_esc_cancels_switch(self) -> None: + """Esc on the pre-confirmation modal cancels the Auto switch.""" from deepagents_code.approval_mode import ApprovalMode from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen - with ( - patch( - "deepagents_code.approval_mode.has_auto_mode_notice", - return_value=False, - ), - patch( - "deepagents_code.approval_mode.save_auto_mode_notice", - return_value=True, - ) as save_notice, - ): - app = DeepAgentsApp(approval_mode=ApprovalMode.AUTO) - async with app.run_test() as pilot: - await pilot.pause() - assert isinstance(app.screen, AutoModeNoticeScreen) - assert app._approval_mode is ApprovalMode.AUTO - save_notice.assert_not_called() - with patch.object( + app = DeepAgentsApp() + app._auto_mode_eligible = True + async with app.run_test() as pilot: + await pilot.pause() + app._agent = object() + app._session_state = TextualSessionState( + thread_id="thread-1", + auto_approve=False, + ) + write_live = AsyncMock(return_value=True) + with ( + patch.object(app, "_write_live_approval_mode", new=write_live), + patch.object( app, - "_write_live_approval_mode", - new=AsyncMock(return_value=True), - ) as write_live: - await pilot.press("escape") - await pilot.pause() - save_notice.assert_not_called() - assert not isinstance(app.screen, AutoModeNoticeScreen) - assert app._approval_mode is ApprovalMode.MANUAL - # No live agent/session pair on mount-only setup, so no Store write. - write_live.assert_not_awaited() - - async def test_mount_auto_notice_action_cancel_reverts_to_manual(self) -> None: - """`action_cancel` matches Esc: Manual, notice not saved. - - Covers the app-interrupt path where the app's priority Escape handler - calls `action_cancel` on the modal rather than the screen's own binding. - """ - from deepagents_code.approval_mode import ApprovalMode - from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen - - with ( - patch( - "deepagents_code.approval_mode.has_auto_mode_notice", - return_value=False, - ), - patch( - "deepagents_code.approval_mode.save_auto_mode_notice", - return_value=True, - ) as save_notice, - ): - app = DeepAgentsApp(approval_mode=ApprovalMode.AUTO) - async with app.run_test() as pilot: + "_auto_accept_pending_goal_rubric", + new=AsyncMock(), + ), + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=False, + ), + patch( + "deepagents_code.approval_mode.save_auto_mode_notice", + ) as save_notice, + ): + await app.action_toggle_auto_approve() await pilot.pause() + # Modal appears, mode still MANUAL. assert isinstance(app.screen, AutoModeNoticeScreen) - app.screen.action_cancel() + assert app._approval_mode is ApprovalMode.MANUAL + await pilot.press("escape") await pilot.pause() + # Esc cancels: no save, no mode change, no live write. save_notice.assert_not_called() assert not isinstance(app.screen, AutoModeNoticeScreen) assert app._approval_mode is ApprovalMode.MANUAL + write_live.assert_not_awaited() - async def test_auto_notice_escape_writes_manual_when_session_live(self) -> None: - """Esc after an in-session Auto enable writes Manual to the live Store.""" + async def test_on_auto_approve_enabled_esc_returns_false(self) -> None: + """Esc on the inline Auto confirmation returns False without activating.""" from deepagents_code.approval_mode import ApprovalMode from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen - app = DeepAgentsApp() + app = DeepAgentsApp(auto_approve=False) app._auto_mode_eligible = True async with app.run_test() as pilot: await pilot.pause() - app._agent = object() app._session_state = TextualSessionState( thread_id="thread-1", auto_approve=False, ) - write_live = AsyncMock(return_value=True) with ( - patch.object(app, "_write_live_approval_mode", new=write_live), + patch.object( + app, + "_write_live_approval_mode", + new=AsyncMock(return_value=True), + ) as write_live, patch.object( app, "_auto_accept_pending_goal_rubric", @@ -30413,23 +30360,22 @@ async def test_auto_notice_escape_writes_manual_when_session_live(self) -> None: "deepagents_code.approval_mode.save_auto_mode_notice", ) as save_notice, ): - await app.action_toggle_auto_approve() + task = asyncio.create_task(app._on_auto_approve_enabled()) await pilot.pause() assert isinstance(app.screen, AutoModeNoticeScreen) - assert app._approval_mode is ApprovalMode.AUTO - write_live.assert_awaited_with(ApprovalMode.AUTO) - write_live.reset_mock() await pilot.press("escape") - await pilot.pause() + result = await task + assert result is False save_notice.assert_not_called() - assert app._approval_mode is ApprovalMode.MANUAL - write_live.assert_awaited_with(ApprovalMode.MANUAL) + assert app._approval_mode is not ApprovalMode.AUTO + write_live.assert_not_awaited() - async def test_notify_auto_mode_enabled_once_is_reentrant_safe(self) -> None: - """Two enables before dismiss push a single modal, saved once.""" + async def test_prompt_auto_mode_confirmation_is_reentrant_safe(self) -> None: + """Two calls before dismiss push a single modal.""" from deepagents_code.tui.widgets.auto_mode_notice import AutoModeNoticeScreen app = DeepAgentsApp() + app._auto_mode_eligible = True async with app.run_test() as pilot: await pilot.pause() with ( @@ -30440,10 +30386,10 @@ async def test_notify_auto_mode_enabled_once_is_reentrant_safe(self) -> None: patch( "deepagents_code.approval_mode.save_auto_mode_notice", return_value=True, - ) as save_notice, + ), ): - app._notify_auto_mode_enabled_once() - app._notify_auto_mode_enabled_once() + app._prompt_auto_mode_confirmation() + app._prompt_auto_mode_confirmation() await pilot.pause() notices = [ screen @@ -30451,13 +30397,13 @@ async def test_notify_auto_mode_enabled_once_is_reentrant_safe(self) -> None: if isinstance(screen, AutoModeNoticeScreen) ] assert len(notices) == 1 - await pilot.press("enter") - await pilot.pause() - save_notice.assert_called_once_with() - async def test_notify_auto_mode_enabled_once_survives_push_failure(self) -> None: - """A failed modal push never breaks the already-applied Auto enable.""" + async def test_prompt_auto_mode_confirmation_survives_push_failure(self) -> None: + """A failed modal push does not activate Auto.""" + from deepagents_code.approval_mode import ApprovalMode + app = DeepAgentsApp() + app._auto_mode_eligible = True async with app.run_test() as pilot: await pilot.pause() with ( @@ -30469,13 +30415,18 @@ async def test_notify_auto_mode_enabled_once_survives_push_failure(self) -> None "deepagents_code.approval_mode.save_auto_mode_notice" ) as save_notice, patch.object(app, "push_screen", side_effect=RuntimeError("boom")), + patch.object(app, "_warn_live_approval_mode_unavailable") as warn, ): # Must not raise despite the push failing. - app._notify_auto_mode_enabled_once() + app._prompt_auto_mode_confirmation() await pilot.pause() - # The guard is reset so a later enable can retry the notice. + # The guard is reset so a later attempt can retry. assert app._auto_mode_notice_pending is False save_notice.assert_not_called() + # Auto was never activated. + assert app._approval_mode is not ApprovalMode.AUTO + warn.assert_called_once() + assert "Could not open the Auto confirmation" in str(warn.call_args.args[0]) async def test_server_manual_fallback_updates_tui_mode_and_warns(self) -> None: from deepagents_code.approval_mode import ApprovalMode From f3d2046d9635fc89e4f43ae25d963e2d8a18b587 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Thu, 20 Aug 2026 12:28:30 -0400 Subject: [PATCH 3/4] fix(code): await pending Auto confirmation instead of bypassing it When two entry points race (e.g. Shift+Tab opens the confirmation modal while an inline "approve all" decision resumes), the second caller now awaits the shared future rather than falling through and activating Auto without the user's answer. --- libs/code/deepagents_code/app.py | 70 ++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/libs/code/deepagents_code/app.py b/libs/code/deepagents_code/app.py index 31534842bd9..9ba1952785a 100644 --- a/libs/code/deepagents_code/app.py +++ b/libs/code/deepagents_code/app.py @@ -9524,35 +9524,46 @@ async def _on_auto_approve_enabled(self) -> bool: ) return False - if not has_auto_mode_notice() and not getattr( - self, "_auto_mode_notice_pending", False - ): - # Pre-confirmation: show modal before activating Auto - loop = asyncio.get_running_loop() - future: asyncio.Future[bool] = loop.create_future() + if not has_auto_mode_notice(): + pending: asyncio.Future[bool] | None = getattr( + self, "_auto_mode_confirmation_future", None + ) + if pending is not None and not pending.done(): + # Another entry point already pushed the modal — await its result + # rather than bypassing the confirmation. + confirmed = await pending + if not confirmed: + return False + else: + # Pre-confirmation: show modal before activating Auto + loop = asyncio.get_running_loop() + future: asyncio.Future[bool] = loop.create_future() + self._auto_mode_confirmation_future = future - def handle_result(accepted: bool | None) -> None: - self._auto_mode_notice_pending = False - if not future.done(): - future.set_result(accepted is True) + def handle_result(accepted: bool | None) -> None: + self._auto_mode_notice_pending = False + if not future.done(): + future.set_result(accepted is True) - try: - self.push_screen( - AutoModeNoticeScreen( - model_label=self._auto_classifier_review_model_spec(), - distinct_from_main_model=self._auto_classifier_is_distinct(), - ), - handle_result, - ) - except Exception: - logger.warning("Could not show Auto confirmation", exc_info=True) - return False - self._auto_mode_notice_pending = True + try: + self.push_screen( + AutoModeNoticeScreen( + model_label=self._auto_classifier_review_model_spec(), + distinct_from_main_model=self._auto_classifier_is_distinct(), + ), + handle_result, + ) + except Exception: + logger.warning("Could not show Auto confirmation", exc_info=True) + self._auto_mode_confirmation_future = None + return False + self._auto_mode_notice_pending = True - confirmed = await future - if not confirmed: - return False - save_auto_mode_notice() + confirmed = await future + self._auto_mode_confirmation_future = None + if not confirmed: + return False + save_auto_mode_notice() if not await self._write_live_approval_mode(ApprovalMode.AUTO): self._warn_live_approval_mode_unavailable( @@ -21403,8 +21414,14 @@ def _prompt_auto_mode_confirmation(self) -> None: if getattr(self, "_auto_mode_notice_pending", False): return + loop = asyncio.get_running_loop() + future: asyncio.Future[bool] = loop.create_future() + self._auto_mode_confirmation_future = future + def handle_result(accepted: bool | None) -> None: self._auto_mode_notice_pending = False + if not future.done(): + future.set_result(accepted is True) if accepted is not True: return if not save_auto_mode_notice(): @@ -21425,6 +21442,7 @@ def handle_result(accepted: bool | None) -> None: ) except Exception: logger.warning("Could not show Auto confirmation", exc_info=True) + self._auto_mode_confirmation_future = None self._warn_live_approval_mode_unavailable( "Could not open the Auto confirmation; approval mode unchanged." ) From 0ae6272d09859ffd630e94090af4a948ff19bfad Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Thu, 20 Aug 2026 12:30:51 -0400 Subject: [PATCH 4/4] test(code): patch has_auto_mode_notice in failed-write toggle test The test exercises the write-failure path inside _set_approval_mode, so it needs to bypass the new pre-confirmation modal. --- libs/code/tests/unit_tests/test_app.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/code/tests/unit_tests/test_app.py b/libs/code/tests/unit_tests/test_app.py index 4205e638a5d..e9870213e53 100644 --- a/libs/code/tests/unit_tests/test_app.py +++ b/libs/code/tests/unit_tests/test_app.py @@ -29509,6 +29509,10 @@ async def test_toggle_on_failed_write_does_not_cancel_running_agent(self) -> Non ), patch.object(app, "_force_interrupt_active_work") as force, patch.object(app, "notify") as notify, + patch( + "deepagents_code.approval_mode.has_auto_mode_notice", + return_value=True, + ), patch( "deepagents_code.approval_mode.save_auto_mode_notice" ) as save_notice,