Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 95 additions & 94 deletions libs/code/deepagents_code/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Pending modal lets Auto bypass confirmation

_auto_mode_notice_pending means another entry point is still waiting for the user's answer, not that confirmation has happened. For example, an inline “approve all” decision can resume just after Shift+Tab or /auto opens this modal; this condition then skips the await future block and proceeds directly to _write_live_approval_mode(AUTO). Auto can therefore activate while the confirmation is still visible, and remains active even if the user presses Esc. A pending confirmation needs to be awaited or treated as False, rather than as permission to fall through.

(Refers to lines 9527-9529)


Your feedback helps Open SWE learn. React with 👍 or 👎 to tell us if this review comment was useful.

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."
Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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():
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 10 additions & 9 deletions libs/code/deepagents_code/tui/widgets/auto_mode_notice.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]] = [
Expand Down Expand Up @@ -210,7 +211,7 @@ def compose(self) -> ComposeResult:
open_links=False,
)
yield Static(
"Enter to keep Auto · Esc for Manual",
"Enter switch to Auto · Esc cancel",
classes="auto-mode-notice-help",
markup=False,
)
Expand All @@ -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
Expand Down
Loading
Loading