Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 21 additions & 8 deletions libs/code/deepagents_code/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class _ConfigWriteResult:
from deepagents_code.tui.widgets.approval import ApprovalMenu
from deepagents_code.tui.widgets.ask_user import AskUserMenu
from deepagents_code.tui.widgets.auth import AuthManagerScreen
from deepagents_code.tui.widgets.cwd_switch import CwdSwitchAbortMode
from deepagents_code.tui.widgets.goal_review import GoalReviewMenu, GoalReviewResult
from deepagents_code.tui.widgets.model_selector import ModelSelectorScreen
from deepagents_code.tui.widgets.notification_center import (
Expand Down Expand Up @@ -16638,6 +16639,7 @@ async def _offer_thread_cwd_switch(
*,
restart_server: bool,
allow_abort: bool = False,
abort_mode: CwdSwitchAbortMode = "resume",
) -> Literal["continue", "abort"]:
"""Offer to switch to a resumed thread's cwd when it differs.

Expand All @@ -16647,16 +16649,19 @@ async def _offer_thread_cwd_switch(
switch replaces the app-owned server so the backend runs in the
new cwd. When False (launch-time resume), the server has not
started yet, so only the process cwd is changed.
allow_abort: When True (launch-time `-r` resume), the prompt offers a
third "abort" option that declines the resume entirely.
allow_abort: When True, the prompt offers a third "abort" option that
declines the resume/switch entirely. Set for both the launch-time
`-r` resume and the in-session `/threads` switcher.
abort_mode: Selects the abort wording — "resume" (launch-time, abort
starts a new session) or "switch" (in-session `/threads`, abort
cancels and keeps the current thread).

Returns:
`"continue"` when resume may proceed, or `"abort"` when the user
declined the resume or a requested switch was accepted but
failed (the caller should stop the resume). The two abort
sources are mode-exclusive: the user-declined abort fires only
when `allow_abort` is True, and the switch-failed abort only
when `restart_server` is True.
declined the resume/switch or a requested switch was accepted but
failed (the caller should stop the resume). The user-declined
abort fires only when `allow_abort` is True, and the switch-failed
abort only when `restart_server` is True.
"""
target = await self._thread_cwd_mismatch(thread_id)
if target is None:
Expand All @@ -16673,6 +16678,7 @@ async def _offer_thread_cwd_switch(
thread_cwd=str(target),
project_settings_change_detected=project_settings_change_detected,
allow_abort=allow_abort,
abort_mode=abort_mode,
)
)
if choice == "abort":
Expand Down Expand Up @@ -16776,6 +16782,8 @@ async def _resume_thread(self, thread_id: str) -> None:
cwd_choice = await self._offer_thread_cwd_switch(
thread_id,
restart_server=True,
allow_abort=True,
abort_mode="switch",
)
if cwd_choice == "abort":
return
Expand All @@ -16796,7 +16804,12 @@ async def _resume_thread(self, thread_id: str) -> None:
prev_session_thread = self._session_state.thread_id
prev_cwd = Path(self._cwd)

cwd_choice = await self._offer_thread_cwd_switch(thread_id, restart_server=True)
cwd_choice = await self._offer_thread_cwd_switch(
thread_id,
restart_server=True,
allow_abort=True,
abort_mode="switch",
)
if cwd_choice == "abort":
return

Expand Down
40 changes: 27 additions & 13 deletions libs/code/deepagents_code/tui/widgets/cwd_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@
CwdSwitchChoice = Literal["switch", "stay", "abort"]
"""Outcome of the cwd switch prompt.

`"abort"` is only offered when the prompt is opened with `allow_abort=True`
(launch-time `-r` resume) and means "don't resume; start a new session".
`"abort"` is only offered when the prompt is opened with `allow_abort=True`.
Its meaning depends on `abort_mode`: at launch-time `-r` resume (`"resume"`) it
means "don't resume; start a new session", and in the in-session `/threads`
switcher (`"switch"`) it means "cancel; keep the current thread".
"""

CwdSwitchAbortMode = Literal["resume", "switch"]
"""Which flow opened the abort-capable prompt, selecting the abort wording.

`"resume"` is the launch-time `-r` resume (abort starts a new session);
`"switch"` is the in-session `/threads` switcher (abort cancels the switch).
"""


Expand Down Expand Up @@ -87,13 +96,15 @@ def __init__(
thread_cwd: str,
project_settings_change_detected: bool = False,
allow_abort: bool = False,
abort_mode: CwdSwitchAbortMode = "resume",
) -> None:
"""Initialize the prompt."""
super().__init__()
self._current_cwd = current_cwd
self._thread_cwd = thread_cwd
self._project_settings_change_detected = project_settings_change_detected
self._allow_abort = allow_abort
self._abort_mode: CwdSwitchAbortMode = abort_mode

def _body_text(self) -> str:
"""Return the prompt body text."""
Expand All @@ -105,11 +116,13 @@ def _body_text(self) -> str:
if self._project_settings_change_detected
else ""
)
abort_note = (
"\n\nOr abort to start a new session instead of resuming."
if self._allow_abort
else ""
)
abort_note = ""
if self._allow_abort:
abort_note = (
"\n\nOr abort to start a new session instead of resuming."
if self._abort_mode == "resume"
else "\n\nOr abort to cancel and keep your current thread."
)
return (
"This thread was last used from:\n"
f" {target}\n\n"
Expand Down Expand Up @@ -138,11 +151,12 @@ def compose(self) -> ComposeResult:
classes="cwd-switch-body",
markup=False,
)
help_text = (
"Enter: switch · Esc: stay here · A: don't resume"
if self._allow_abort
else "Enter: switch · Esc: stay here"
)
help_text = "Enter: switch · Esc: stay here"
if self._allow_abort:
abort_help = (
"A: don't resume" if self._abort_mode == "resume" else "A: cancel"
)
help_text = f"{help_text} · {abort_help}"
yield Static(
help_text,
classes="cwd-switch-help",
Expand Down Expand Up @@ -181,7 +195,7 @@ def action_stay(self) -> None:
self.dismiss("stay")

def action_abort(self) -> None:
"""Dismiss with `abort` to skip the resume, when the prompt allows it."""
"""Dismiss with `abort` to skip the resume/switch, when the prompt allows it."""
if not self._allow_abort:
return
self.dismiss("abort")
Expand Down
50 changes: 50 additions & 0 deletions libs/code/tests/unit_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21622,6 +21622,56 @@ def replace_server(cwd: Path) -> str:
set_spinner.assert_has_awaits([call("Loading thread"), call(None)])
load_thread_history.assert_not_awaited()

async def test_threads_switch_offers_abort_and_cancels(
self,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The `/threads` switcher offers abort; aborting keeps the current thread."""
monkeypatch.chdir(tmp_path)
app = DeepAgentsApp(thread_id="old-thread", cwd=tmp_path)
app._agent = MagicMock()
app._session_state = TextualSessionState(thread_id="old-thread")
app._lc_thread_id = "old-thread"
app._mount_message = AsyncMock() # ty: ignore[invalid-assignment]
fetch = AsyncMock()
app._fetch_thread_history_data = fetch # ty: ignore[invalid-assignment]
offer = AsyncMock(return_value="abort")
app._offer_thread_cwd_switch = offer # ty: ignore[invalid-assignment]

await app._resume_thread("new-thread")

assert offer.await_args is not None
assert offer.await_args.kwargs["allow_abort"] is True
assert offer.await_args.kwargs["abort_mode"] == "switch"
# Aborting must not switch threads or load history.
assert app._session_state.thread_id == "old-thread"
assert app._lc_thread_id == "old-thread"
fetch.assert_not_awaited()

async def test_threads_reselect_offers_abort(
self,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Reselecting the current thread also offers abort and cancels silently."""
monkeypatch.chdir(tmp_path)
app = DeepAgentsApp(thread_id="thread-1", cwd=tmp_path)
app._agent = MagicMock()
app._session_state = TextualSessionState(thread_id="thread-1")
app._lc_thread_id = "thread-1"
mount = AsyncMock()
app._mount_message = mount # ty: ignore[invalid-assignment]
offer = AsyncMock(return_value="abort")
app._offer_thread_cwd_switch = offer # ty: ignore[invalid-assignment]

await app._resume_thread("thread-1")

assert offer.await_args is not None
assert offer.await_args.kwargs["allow_abort"] is True
assert offer.await_args.kwargs["abort_mode"] == "switch"
mount.assert_not_awaited()

# --- _resolve_thread_cwd_mismatch (pure staticmethod) ---

def test_resolve_relative_path_is_unavailable(self) -> None:
Expand Down
14 changes: 14 additions & 0 deletions libs/code/tests/unit_tests/tui/widgets/test_cwd_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ def test_help_and_body_mention_abort_only_when_allowed(self) -> None:
assert "new session" not in without._body_text()
assert "new session" in with_abort._body_text()

def test_switch_mode_uses_cancel_wording(self) -> None:
"""The in-session `/threads` abort describes cancelling, not a new session."""
switch = CwdSwitchPromptScreen(
current_cwd="/a",
thread_cwd="/b",
allow_abort=True,
abort_mode="switch",
)

body = switch._body_text()
assert "new session" not in body
assert "cancel" in body
assert "keep your current thread" in body

def test_action_abort_dismisses_abort_when_allowed(self) -> None:
"""Abort resolves the prompt to `abort` when offered."""
screen = CwdSwitchPromptScreen(
Expand Down
Loading