deep-agent-cli 0.1.9__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/PKG-INFO +6 -3
  2. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/README.md +5 -2
  3. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/__init__.py +1 -1
  4. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/app.py +220 -134
  5. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/commands.py +2 -2
  6. deep_agent_cli-0.2.0/agent/cli/input_actions.py +115 -0
  7. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/interactions.py +21 -11
  8. deep_agent_cli-0.2.0/agent/cli/lifecycle.py +210 -0
  9. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/main.py +2 -0
  10. deep_agent_cli-0.2.0/agent/cli/mcp_controller.py +65 -0
  11. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/mcp_ui.py +45 -51
  12. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/previews.py +16 -12
  13. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/rendering.py +140 -63
  14. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/session_controller.py +23 -18
  15. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/state.py +12 -8
  16. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/config.example.yaml +16 -15
  17. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/config.py +115 -20
  18. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/environment.py +7 -4
  19. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/factory.py +36 -16
  20. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/llm.py +12 -8
  21. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/config.py +1 -6
  22. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/manager.py +10 -7
  23. deep_agent_cli-0.2.0/agent/mcp/middleware.py +21 -0
  24. deep_agent_cli-0.2.0/agent/mcp/tools.py +65 -0
  25. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/recovery.py +8 -8
  26. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/performance.py +3 -2
  27. deep_agent_cli-0.2.0/agent/run_events.py +384 -0
  28. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/runner.py +123 -359
  29. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/sandbox.py +95 -42
  30. deep_agent_cli-0.2.0/agent/sandbox_mounts.py +112 -0
  31. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/sandbox_pool.py +11 -3
  32. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/sandbox_worker.py +2 -2
  33. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/session.py +48 -8
  34. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/_grep_worker.py +7 -1
  35. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/examples.py +1 -1
  36. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/execute.py +20 -31
  37. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/grep.py +22 -33
  38. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/human_input.py +2 -1
  39. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/web_search.py +12 -20
  40. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/pyproject.toml +1 -1
  41. deep_agent_cli-0.2.0//347/273/210/347/253/257.png +0 -0
  42. deep_agent_cli-0.1.9/agent/mcp/middleware.py +0 -28
  43. deep_agent_cli-0.1.9/agent/mcp/tools.py +0 -63
  44. deep_agent_cli-0.1.9//347/273/210/347/253/257.png +0 -0
  45. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/.gitignore +0 -0
  46. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/LICENSE +0 -0
  47. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/__init__.py +0 -0
  48. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/attachments.py +0 -0
  49. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/bootstrap.py +0 -0
  50. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cancel.py +0 -0
  51. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/clipboard.py +0 -0
  52. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/gitinfo.py +0 -0
  53. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/input.py +0 -0
  54. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/pasted_content.py +0 -0
  55. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/skills.py +0 -0
  56. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/working_messages.py +0 -0
  57. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/cli/working_messages.yaml +0 -0
  58. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/control.py +0 -0
  59. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/file_mutation.py +0 -0
  60. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/http_environment.py +0 -0
  61. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/llm_stream.py +0 -0
  62. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/__init__.py +0 -0
  63. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/adapter.py +0 -0
  64. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/bridge.py +0 -0
  65. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/content.py +0 -0
  66. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/logging.py +0 -0
  67. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/names.py +0 -0
  68. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/oauth.py +0 -0
  69. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/mcp/oauth_storage.py +0 -0
  70. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/__init__.py +0 -0
  71. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/attachments.py +0 -0
  72. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/cancel_tools.py +0 -0
  73. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/pause.py +0 -0
  74. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/runtime_config.py +0 -0
  75. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/steering.py +0 -0
  76. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/tool_arg_hints.py +0 -0
  77. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/usage.py +0 -0
  78. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/workspace_filesystem.py +0 -0
  79. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/middleware/write_operation.py +0 -0
  80. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/network.py +0 -0
  81. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/permission.py +0 -0
  82. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/session_lock.py +0 -0
  83. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/session_runtime.py +0 -0
  84. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/stream.py +0 -0
  85. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/__init__.py +0 -0
  86. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/tools/human_interaction.py +0 -0
  87. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/agent/version.py +0 -0
  88. {deep_agent_cli-0.1.9 → deep_agent_cli-0.2.0}/assets/readme/runtime-map.svg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deep-agent-cli
3
- Version: 0.1.9
3
+ Version: 0.2.0
4
4
  Summary: A sandboxed coding agent CLI for Linux and WSL2
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -50,7 +50,7 @@ OpenAI 兼容接口 · Bubblewrap
50
50
 
51
51
  在项目目录运行 `deep-agent` 进入交互式终端,用 `deep-agent resume` 恢复已有会话。要在其他 Python 程序里复用同一套运行核心,使用 [Python API](docs/library.md)。
52
52
 
53
- > 当前版本 **0.1.9**,处于早期开发阶段。稳定版前,配置、会话存储格式和 Python API 可能出现不兼容变更。
53
+ > 当前版本 **0.2.0**,处于早期开发阶段。稳定版前,配置、会话存储格式和 Python API 可能出现不兼容变更。
54
54
 
55
55
  ## 能做什么
56
56
 
@@ -92,7 +92,7 @@ deep-agent
92
92
 
93
93
  配置、Skills 和按键配置目录必须位于工作区之外,**不要将家目录或其上层目录作为工作区**。
94
94
 
95
- 默认模板提供可选的 Node、Cargo / Rustup、Chrome、X11 和浏览器缓存挂载。NVM 挂载整个版本目录,使用哪个版本由用户在沙箱 `PATH` 中指定;程序不自动发现工具安装位置或选择版本。已有配置需手动对照 [默认挂载说明](docs/configuration.md#默认工具挂载) 合并,升级不会覆盖用户配置。
95
+ 默认模板是 `mode: restricted`,不预置工具挂载;需要 Node、Cargo / Rustup、Chrome、X11 或浏览器缓存时,按 [默认工具挂载](docs/configuration.md#默认工具挂载) 中的示例手动添加,可选源不存在时跳过。NVM 挂载整个版本目录后,使用哪个版本由用户在沙箱 `PATH` 中指定;程序不自动发现工具安装位置或选择版本。已有配置需手动对照模板合并,升级不会覆盖用户配置。
96
96
 
97
97
  默认模板使用 `token-plan/qwen3.8-flash`。在启动 Agent 的同一个 Bash 终端中输入对应密钥:
98
98
 
@@ -112,11 +112,14 @@ printf '\n'
112
112
  ```yaml
113
113
  llm:
114
114
  default: token-plan/qwen3.8-flash
115
+ timeout: 60
115
116
  models:
116
117
  token-plan:
117
118
  base_url: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
118
119
  api_key: ${TOKEN_PLAN_API_KEY}
119
120
  api: chat_completions
121
+ max_retries: 5
122
+ stream_chunk_timeout: 120
120
123
  reasoning_efforts: [none, low, medium, xhigh]
121
124
  models:
122
125
  qwen3.8-flash:
@@ -19,7 +19,7 @@ OpenAI 兼容接口 · Bubblewrap
19
19
 
20
20
  在项目目录运行 `deep-agent` 进入交互式终端,用 `deep-agent resume` 恢复已有会话。要在其他 Python 程序里复用同一套运行核心,使用 [Python API](docs/library.md)。
21
21
 
22
- > 当前版本 **0.1.9**,处于早期开发阶段。稳定版前,配置、会话存储格式和 Python API 可能出现不兼容变更。
22
+ > 当前版本 **0.2.0**,处于早期开发阶段。稳定版前,配置、会话存储格式和 Python API 可能出现不兼容变更。
23
23
 
24
24
  ## 能做什么
25
25
 
@@ -61,7 +61,7 @@ deep-agent
61
61
 
62
62
  配置、Skills 和按键配置目录必须位于工作区之外,**不要将家目录或其上层目录作为工作区**。
63
63
 
64
- 默认模板提供可选的 Node、Cargo / Rustup、Chrome、X11 和浏览器缓存挂载。NVM 挂载整个版本目录,使用哪个版本由用户在沙箱 `PATH` 中指定;程序不自动发现工具安装位置或选择版本。已有配置需手动对照 [默认挂载说明](docs/configuration.md#默认工具挂载) 合并,升级不会覆盖用户配置。
64
+ 默认模板是 `mode: restricted`,不预置工具挂载;需要 Node、Cargo / Rustup、Chrome、X11 或浏览器缓存时,按 [默认工具挂载](docs/configuration.md#默认工具挂载) 中的示例手动添加,可选源不存在时跳过。NVM 挂载整个版本目录后,使用哪个版本由用户在沙箱 `PATH` 中指定;程序不自动发现工具安装位置或选择版本。已有配置需手动对照模板合并,升级不会覆盖用户配置。
65
65
 
66
66
  默认模板使用 `token-plan/qwen3.8-flash`。在启动 Agent 的同一个 Bash 终端中输入对应密钥:
67
67
 
@@ -81,11 +81,14 @@ printf '\n'
81
81
  ```yaml
82
82
  llm:
83
83
  default: token-plan/qwen3.8-flash
84
+ timeout: 60
84
85
  models:
85
86
  token-plan:
86
87
  base_url: https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
87
88
  api_key: ${TOKEN_PLAN_API_KEY}
88
89
  api: chat_completions
90
+ max_retries: 5
91
+ stream_chunk_timeout: 120
89
92
  reasoning_efforts: [none, low, medium, xhigh]
90
93
  models:
91
94
  qwen3.8-flash:
@@ -1,4 +1,4 @@
1
- """Terminal presentation layer for the DeepAgent template."""
1
+ """Terminal presentation layer for the Deep Agent CLI."""
2
2
 
3
3
  from agent.cli.app import CliApplication
4
4
 
@@ -63,6 +63,7 @@ from agent.cli.rendering import (
63
63
  RenderedUnit,
64
64
  TranscriptDocument,
65
65
  TranscriptRenderer,
66
+ render_interaction_viewport,
66
67
  render_interaction,
67
68
  render_queued_messages,
68
69
  render_review,
@@ -81,6 +82,14 @@ from agent.permission import (
81
82
  allow_mode_unavailable_reason,
82
83
  parse_permission_mode,
83
84
  )
85
+ from agent.cli.lifecycle import AppLifecycle, TaskStop
86
+ from agent.cli.mcp_controller import MCPController
87
+ from agent.cli.input_actions import (
88
+ EXIT_WINDOW_SECONDS as CTRL_C_EXIT_WINDOW_SECONDS,
89
+ InputAction,
90
+ InputActionRouter,
91
+ InputView,
92
+ )
84
93
  from agent.performance import model_metric_groups
85
94
  from agent.mcp.logging import MCPLogHandler
86
95
  from agent.runner import AgentRunner, InterruptKind, RunEvent, RunResult, UnknownInterruptError
@@ -88,7 +97,6 @@ from agent.runner import AgentRunner, InterruptKind, RunEvent, RunResult, Unknow
88
97
  # Workspace/model and execution settings on the left; git/context on the right.
89
98
  FOOTER_LINES = 2
90
99
  INPUT_CLEARED_HINT = "Input cleared · press Ctrl+C again to exit"
91
- CTRL_C_EXIT_WINDOW_SECONDS = 1.0
92
100
  STATUS_NOTICE_SECONDS = 1.0
93
101
 
94
102
  _RUN_EPOCH: ContextVar[int | None] = ContextVar("deep_agent_tui_run_epoch", default=None)
@@ -269,6 +277,28 @@ class _ScrollableTextControl(FormattedTextControl):
269
277
  return None
270
278
 
271
279
 
280
+ class _InteractionControl(_ScrollableTextControl):
281
+ """A fixed heading and footer around a selection-following option list."""
282
+
283
+ def __init__(self, *args: Any, controller: Any, **kwargs: Any) -> None:
284
+ self._controller = controller
285
+ self._option_offset = 0
286
+ self._selection_context: Any = None
287
+ super().__init__(*args, **kwargs)
288
+
289
+ def create_content(self, width: int, height: int | None = None) -> UIContent:
290
+ controller = self._controller()
291
+ context = (controller, controller.index if controller else None, width)
292
+ if context != self._selection_context:
293
+ self._option_offset = 0
294
+ self._selection_context = context
295
+ rendered, self._option_offset = render_interaction_viewport(
296
+ controller, width, height or 1, self._option_offset,
297
+ )
298
+ self.text = to_formatted_text(ANSI(rendered))
299
+ return super().create_content(width, height)
300
+
301
+
272
302
  class _BackToBottomControl(_ScrollableTextControl):
273
303
  """Make the whole return-to-bottom hint clickable without stealing wheel events."""
274
304
 
@@ -431,11 +461,13 @@ class CliApplication:
431
461
  self._run_epoch = 0
432
462
  self._cancel_sent = False
433
463
  self._compacting = False
434
- self._last_ctrl_c: float | None = None
464
+ self._input_actions = InputActionRouter()
435
465
  self._status_notice_handle: asyncio.TimerHandle | None = None
436
466
  self._status_before_notice = "Ready"
437
467
  self._status_notice: str | None = None
438
- self._exiting = False
468
+ self.lifecycle = AppLifecycle(self)
469
+ self.mcp_controller = MCPController()
470
+ self._mcp_draft: dict[str, str] = {}
439
471
  self._transcript_line_count = 1
440
472
  # None = stick to bottom (follow new output); int = pinned scroll row.
441
473
  self._transcript_anchor: int | None = None
@@ -478,8 +510,9 @@ class CliApplication:
478
510
  on_select=self._select_transcript,
479
511
  selected_line=self._selected_transcript_line,
480
512
  )
481
- self.interaction_control = _ScrollableTextControl(
513
+ self.interaction_control = _InteractionControl(
482
514
  text=self._interaction_text, focusable=False, on_scroll=self.scroll_transcript,
515
+ controller=lambda: self.interaction,
483
516
  on_select_outside=self._select_below_transcript,
484
517
  )
485
518
  self.attachment_control = _ScrollableTextControl(
@@ -702,12 +735,7 @@ class CliApplication:
702
735
  await self.sessions.start()
703
736
  await self.application.run_async(pre_run=self._start_mcp_connections)
704
737
  finally:
705
- manager = getattr(self.runner, "mcp", None)
706
- if manager is not None:
707
- manager.cancel_pending_connections()
708
- self.sessions.notify_exit()
709
- await self._stop_git_watch()
710
- self._io_executor.shutdown(wait=False, cancel_futures=True)
738
+ await self.lifecycle.shutdown()
711
739
 
712
740
  def _start_mcp_connections(self) -> None:
713
741
  manager = getattr(self.runner, "mcp", None)
@@ -782,7 +810,7 @@ class CliApplication:
782
810
  def _start_git_watch(self) -> None:
783
811
  if self._git_task is not None:
784
812
  return
785
- self._git_task = asyncio.get_running_loop().create_task(self._watch_git())
813
+ self._git_task = self.lifecycle.spawn(self._watch_git(), stop=TaskStop.CANCEL, name="git-watch")
786
814
 
787
815
  async def _watch_git(self) -> None:
788
816
  """Keep the footer's git summary fresh without ever blocking a render pass."""
@@ -872,7 +900,8 @@ class CliApplication:
872
900
  interrupt = self.runner.current_interrupt()
873
901
  if not self.state.running and interrupt is None:
874
902
  events: list[RunEvent] = []
875
- self.runner._apply_pending_runtime_config(events.append)
903
+ if not self.runner.apply_pending_config_if_idle(events.append):
904
+ events = []
876
905
  for event in events:
877
906
  self._apply_event(event, announce=False)
878
907
  if any(event.type == "runtime_config_failed" for event in events):
@@ -891,12 +920,8 @@ class CliApplication:
891
920
  before = self.runner.permission_mode()
892
921
  self.runner.request_permission_change(mode)
893
922
  interrupt = self.runner.current_interrupt()
894
- if not self.state.running and interrupt is None and self.runner._operation_lock.acquire(blocking=False):
895
- events: list[RunEvent] = []
896
- try:
897
- self.runner._apply_pending_runtime_config(events.append)
898
- finally:
899
- self.runner._operation_lock.release()
923
+ events: list[RunEvent] = []
924
+ if not self.state.running and interrupt is None and self.runner.apply_pending_config_if_idle(events.append):
900
925
  for event in events:
901
926
  self._apply_event(event, announce=False)
902
927
  if any(event.type == "runtime_config_failed" for event in events):
@@ -919,12 +944,8 @@ class CliApplication:
919
944
  self.state.add_system(str(exc), error=True)
920
945
  return
921
946
  interrupt = self.runner.current_interrupt()
922
- if not self.state.running and interrupt is None and self.runner._operation_lock.acquire(blocking=False):
923
- events: list[RunEvent] = []
924
- try:
925
- self.runner._apply_pending_runtime_config(events.append)
926
- finally:
927
- self.runner._operation_lock.release()
947
+ events: list[RunEvent] = []
948
+ if not self.state.running and interrupt is None and self.runner.apply_pending_config_if_idle(events.append):
928
949
  for event in events:
929
950
  self._apply_event(event, announce=False)
930
951
  if any(event.type == "runtime_config_failed" for event in events):
@@ -943,6 +964,7 @@ class CliApplication:
943
964
  for item in self.commands
944
965
  )
945
966
  self.state.add_system(
967
+ "/help\n"
946
968
  "Keyboard\n"
947
969
  "Enter submit · Ctrl+J newline · Alt+Enter follow-up · Esc cancel+restore · "
948
970
  "PgUp/PgDn scroll · Ctrl+P next model · Alt+P prev model · Alt+Up restore queue · "
@@ -970,6 +992,7 @@ class CliApplication:
970
992
  if pending_effort is not None:
971
993
  model_line += f"Reasoning: {self.runner.reasoning_effort()} → {pending_effort} (pending)\n"
972
994
  self.state.add_system(
995
+ "/status\n"
973
996
  f"Status: {'running' if self.state.running else 'idle'}\n"
974
997
  f"{model_line}"
975
998
  f"{tool_line}\n"
@@ -1007,6 +1030,7 @@ class CliApplication:
1007
1030
  )
1008
1031
  if store is None:
1009
1032
  self.state.add_system(
1033
+ "/session\n"
1010
1034
  f"Session: {self.runner.thread_id}\n"
1011
1035
  f"{model_line}"
1012
1036
  "Checkpointer: in-memory\nPersistent /resume is not configured."
@@ -1019,6 +1043,7 @@ class CliApplication:
1019
1043
  created_at = info.created_at.astimezone().isoformat(sep=" ", timespec="seconds") if info else "unknown"
1020
1044
  updated_at = info.updated_at.astimezone().isoformat(sep=" ", timespec="seconds") if info else "unknown"
1021
1045
  self.state.add_system(
1046
+ "/session\n"
1022
1047
  f"Session: {self.runner.thread_id}\n"
1023
1048
  f"Title: {title}\n"
1024
1049
  f"Created: {created_at}\n"
@@ -1036,6 +1061,16 @@ class CliApplication:
1036
1061
  async def resume_session(self, arg: str = "") -> None:
1037
1062
  await self.sessions.resume(arg)
1038
1063
 
1064
+ def reset_session_view(self) -> None:
1065
+ """Drop editor chrome that belonged to the previous session."""
1066
+ self._clear_skill_draft()
1067
+ self.clear_transcript_selection()
1068
+ self.interaction = None
1069
+ self._deferred_config_interaction = None
1070
+ self._reviewing = False
1071
+ self._transcript_anchor = None
1072
+ self._renderer.clear()
1073
+
1039
1074
  def _apply_session_snapshot(self, snapshot: Any) -> None:
1040
1075
  self.sessions.apply_snapshot(snapshot)
1041
1076
 
@@ -1292,6 +1327,21 @@ class CliApplication:
1292
1327
  self.set_status("Select permission mode · Enter confirm · Esc cancel", transient=False)
1293
1328
  self.application.invalidate()
1294
1329
 
1330
+ def _show_sandbox_mounts(self) -> None:
1331
+ from agent.sandbox_mounts import render_launch_manifest
1332
+
1333
+ backend = getattr(self.runner.prepared, "backend", None)
1334
+ executor = getattr(backend, "executor", None)
1335
+ config = getattr(executor, "config", None) or self.runner._sandbox_config
1336
+ if config is None:
1337
+ self.state.add_system("Sandbox mount policy is not available for this backend.", error=True)
1338
+ return
1339
+ skills = getattr(backend, "skills_dir", None)
1340
+ self.state.add_system(render_launch_manifest(
1341
+ config, skills_dir=skills, pending_mode=self.runner.pending_sandbox_mode(),
1342
+ ))
1343
+ self.application.invalidate()
1344
+
1295
1345
  async def select_sandbox(self, arg: str = "") -> None:
1296
1346
  if self.sessions.waiting:
1297
1347
  self.set_status(self.sessions.wait_status(), transient=False)
@@ -1300,11 +1350,14 @@ class CliApplication:
1300
1350
  self.set_status("Finish the current interaction before changing sandbox mode")
1301
1351
  return
1302
1352
  raw = arg.strip()
1353
+ if raw == "mounts":
1354
+ self._show_sandbox_mounts()
1355
+ return
1303
1356
  if raw:
1304
1357
  mode = parse_sandbox_mode(raw)
1305
1358
  if mode is None:
1306
1359
  self.state.add_system(
1307
- "Unknown sandbox mode. Use /sandbox restricted or /sandbox full-access",
1360
+ "Unknown sandbox mode. Use /sandbox restricted, /sandbox full-access, or /sandbox mounts",
1308
1361
  error=True,
1309
1362
  )
1310
1363
  return
@@ -1457,45 +1510,120 @@ class CliApplication:
1457
1510
  buffer.cursor_position = len(text)
1458
1511
  return restored_parts
1459
1512
 
1460
- def _apply_session_snapshot(self, snapshot: Any) -> None:
1461
- self._clear_skill_draft()
1462
- self.clear_transcript_selection()
1463
- self.state.load_transcript(snapshot.transcript)
1464
- self.state.load_model_metrics(snapshot.model_metrics_history)
1465
- self.interaction = None
1466
- self._reviewing = False
1467
- self.state.add_system(
1468
- f"Resumed session {snapshot.info.id} (last run: {snapshot.info.last_run_status.value})"
1513
+ @property
1514
+ def _exiting(self) -> bool:
1515
+ return self.lifecycle.exiting
1516
+
1517
+ @property
1518
+ def _last_ctrl_c(self) -> float | None:
1519
+ """Double-press clock. Owned by InputActionRouter; exposed for existing checks."""
1520
+ return self._input_actions.last_ctrl_c
1521
+
1522
+ def _input_view(self, buffer: Buffer) -> InputView:
1523
+ text = buffer.text
1524
+ interaction = self.interaction
1525
+ return InputView(
1526
+ has_interaction=interaction is not None,
1527
+ custom_entry=bool(interaction is not None and interaction.custom_entry),
1528
+ completion_open=buffer.complete_state is not None,
1529
+ slash_line=text.startswith("/") and "\n" not in text,
1530
+ transcript_away=self.transcript_away_from_bottom(),
1531
+ session_waiting=self.sessions.waiting,
1532
+ reviewing=self._reviewing,
1533
+ compacting=self._compacting,
1534
+ running=self.state.running,
1535
+ cancel_sent=self._cancel_sent,
1536
+ mcp_busy=self.mcp_controller.busy,
1537
+ buffer_empty=not text,
1538
+ exiting=self._exiting,
1469
1539
  )
1470
- for notice in snapshot.notices:
1471
- self.state.add_system(notice)
1472
- if not self._reopen_pending_interaction(notify_missing=False):
1473
- self.set_status("Ready", transient=False)
1474
- self._transcript_anchor = None
1475
- self._renderer.clear()
1476
- self.state.usage = dict(self.runner.latest_usage())
1477
- self.application.invalidate()
1478
1540
 
1479
- def exit(self) -> None:
1480
- if self._exiting:
1541
+ def apply_input_action(self, action: InputAction, buffer: Buffer) -> None:
1542
+ """Perform one decision from InputActionRouter. Does not choose another."""
1543
+ if action is InputAction.NONE:
1481
1544
  return
1482
- self._exiting = True
1483
- if self._status_notice_handle is not None:
1484
- self._status_notice_handle.cancel()
1485
- self._status_notice_handle = None
1486
- self._stop_selection_scroll()
1487
- # Stop mouse reports before prompt_toolkit leaves the alternate screen.
1488
- # Otherwise motion generated during shutdown can reach the shell.
1489
- self.application.output.disable_mouse_support()
1490
- self.application.output.flush()
1491
- self.sessions.notify_exit()
1492
- manager = getattr(self.runner, "mcp", None)
1493
- if manager is not None:
1494
- manager.cancel_pending_connections()
1495
- if self.state.running:
1545
+ if action is InputAction.DISMISS_COMPLETION:
1546
+ # Dismiss the menu without cancel_completion restoring its pre-selection document.
1547
+ buffer.complete_state = None
1548
+ self.application.invalidate()
1549
+ return
1550
+ if action is InputAction.CLEAR_SLASH_LINE:
1551
+ self.slash_completer.accepted_text = None
1552
+ buffer.reset()
1553
+ self._pasted_content.clear()
1554
+ self._skill_draft.clear()
1555
+ self.application.invalidate()
1556
+ return
1557
+ if action is InputAction.FOLLOW_TRANSCRIPT:
1558
+ self.follow_transcript()
1559
+ return
1560
+ if action is InputAction.CANCEL_COMPLETION:
1561
+ buffer.cancel_completion()
1562
+ return
1563
+ if action is InputAction.CANCEL_SESSION_WAIT:
1564
+ self.sessions.cancel_wait()
1565
+ return
1566
+ if action is InputAction.CLOSE_REVIEW:
1567
+ self._close_review()
1568
+ return
1569
+ if action is InputAction.EXIT_CUSTOM_ENTRY:
1570
+ assert self.interaction is not None
1571
+ self.interaction.custom_entry = False
1572
+ self.interaction.error = ""
1573
+ buffer.reset()
1574
+ self._pasted_content.clear()
1575
+ self._skill_draft.clear()
1576
+ self.application.invalidate()
1577
+ return
1578
+ if action is InputAction.CANCEL_INTERACTION:
1579
+ self._finish_interaction(cancelled=True)
1580
+ return
1581
+ if action is InputAction.WAIT_FOR_COMPACTION:
1582
+ self.set_status("Wait for compaction to finish")
1583
+ return
1584
+ if action is InputAction.CANCEL_ALREADY_SENT:
1585
+ self.set_status("Cancelling… waiting for the run to stop", transient=False)
1586
+ self.application.invalidate()
1587
+ return
1588
+ if action is InputAction.CANCEL_RUN:
1589
+ restored = self._restore_queued_to_editor(buffer)
1590
+ self._cancel_sent = True
1496
1591
  self.runner.request_cancel()
1497
- self._io_executor.shutdown(wait=False, cancel_futures=True)
1498
- self.application.exit()
1592
+ if restored:
1593
+ self.set_status("Cancelling… · queued messages restored", transient=False)
1594
+ else:
1595
+ self.set_status("Cancelling…", transient=False)
1596
+ self.application.invalidate()
1597
+ return
1598
+ if action is InputAction.MCP_STILL_RUNNING:
1599
+ self.set_status(
1600
+ "MCP is still running in the background; wait for completion or timeout",
1601
+ transient=False,
1602
+ )
1603
+ self.application.invalidate()
1604
+ return
1605
+ if action is InputAction.CLEAR_INPUT:
1606
+ buffer.reset()
1607
+ self._pasted_content.clear()
1608
+ self._skill_draft.clear()
1609
+ self.state.attachments.clear()
1610
+ self._show_input_cleared_hint()
1611
+ return
1612
+ if action is InputAction.REQUEST_EXIT:
1613
+ self.exit()
1614
+ return
1615
+ if action is InputAction.DELETE_AFTER_CURSOR:
1616
+ buffer.delete()
1617
+ return
1618
+ raise AssertionError(f"Unhandled input action: {action}")
1619
+
1620
+ def request_command_exit(self) -> None:
1621
+ """`/exit` uses the same exit action as an empty Ctrl+D."""
1622
+ self.apply_input_action(self._input_actions.decide_exit_command(), self.buffer)
1623
+
1624
+ def exit(self) -> None:
1625
+ """Ask the lifecycle to leave. Resource shutdown happens after the prompt loop."""
1626
+ self.lifecycle.request_exit()
1499
1627
 
1500
1628
  async def _run_blocking(self, func: Any, /, *args: Any, **kwargs: Any) -> Any:
1501
1629
  loop = asyncio.get_running_loop()
@@ -1623,7 +1751,7 @@ class CliApplication:
1623
1751
  self._stop_selection_scroll()
1624
1752
  selected = self._selected_transcript_text()
1625
1753
  if selected:
1626
- asyncio.create_task(self._copy_selection(selected))
1754
+ self.lifecycle.spawn(self._copy_selection(selected), stop=TaskStop.CANCEL, name="clipboard-copy")
1627
1755
  else:
1628
1756
  return
1629
1757
  self.application.invalidate()
@@ -1805,7 +1933,7 @@ class CliApplication:
1805
1933
  return FormattedText(fragments)
1806
1934
 
1807
1935
  def _footer_text(self): # type: ignore[no-untyped-def]
1808
- mode = self.runner.prepared.execution_mode.value
1936
+ mode = self.runner.prepared.execution_mode.value.lower()
1809
1937
  perm = self.runner.permission_mode().value
1810
1938
  pending_permission = self.runner.pending_permission_mode()
1811
1939
  if pending_permission is not None:
@@ -1922,7 +2050,7 @@ class CliApplication:
1922
2050
  if self.interaction is not None:
1923
2051
  return
1924
2052
  self.set_status("Reading clipboard…", transient=False)
1925
- asyncio.create_task(self._paste_clipboard())
2053
+ self.lifecycle.spawn(self._paste_clipboard(), stop=TaskStop.CANCEL, name="clipboard-paste")
1926
2054
 
1927
2055
  @kb.add(Keys.BracketedPaste, eager=True)
1928
2056
  def bracketed_paste(event) -> None: # type: ignore[no-untyped-def]
@@ -2010,78 +2138,24 @@ class CliApplication:
2010
2138
 
2011
2139
  @bind("interrupt")
2012
2140
  def escape(event) -> None: # type: ignore[no-untyped-def]
2013
- if self.interaction is None and event.current_buffer.complete_state is not None:
2014
- # Dismiss the menu without cancel_completion restoring its pre-selection document.
2015
- event.current_buffer.complete_state = None
2016
- self.application.invalidate()
2017
- elif self.interaction is None and event.current_buffer.text.startswith("/") and "\n" not in event.current_buffer.text:
2018
- self.slash_completer.accepted_text = None
2019
- event.current_buffer.reset()
2020
- self._pasted_content.clear()
2021
- self._skill_draft.clear()
2022
- self.application.invalidate()
2023
- elif self.transcript_away_from_bottom():
2024
- self.follow_transcript()
2025
- elif event.current_buffer.complete_state is not None:
2026
- event.current_buffer.cancel_completion()
2027
- elif self.sessions.waiting:
2028
- self.sessions.cancel_wait()
2029
- elif self._reviewing:
2030
- self._close_review()
2031
- elif self.interaction is not None and self.interaction.custom_entry:
2032
- self.interaction.custom_entry = False
2033
- self.interaction.error = ""
2034
- event.current_buffer.reset()
2035
- self._pasted_content.clear()
2036
- self._skill_draft.clear()
2037
- self.application.invalidate()
2038
- elif self.interaction is not None:
2039
- self._finish_interaction(cancelled=True)
2040
- elif self._compacting:
2041
- self.set_status("Wait for compaction to finish")
2042
- elif self.state.running:
2043
- restored = self._restore_queued_to_editor(event.current_buffer)
2044
- if self._cancel_sent:
2045
- self.set_status("Cancelling… waiting for the run to stop", transient=False)
2046
- self.application.invalidate()
2047
- return
2048
- self._cancel_sent = True
2049
- self.runner.request_cancel()
2050
- if restored:
2051
- self.set_status("Cancelling… · queued messages restored", transient=False)
2052
- else:
2053
- self.set_status("Cancelling…", transient=False)
2054
- self.application.invalidate()
2055
-
2056
- elif getattr(self, "_mcp_busy", False):
2057
- self.set_status(
2058
- "MCP is still running in the background; wait for completion or timeout",
2059
- transient=False,
2060
- )
2061
- self.application.invalidate()
2141
+ buffer = event.current_buffer
2142
+ action = self._input_actions.decide_interrupt(self._input_view(buffer))
2143
+ self.apply_input_action(action, buffer)
2062
2144
 
2063
2145
  @bind("clear_or_exit")
2064
2146
  def ctrl_c(event) -> None: # type: ignore[no-untyped-def]
2065
- if self.interaction is not None:
2066
- self._finish_interaction(cancelled=True)
2067
- return
2147
+ buffer = event.current_buffer
2068
2148
  now = time.monotonic()
2069
- if self._last_ctrl_c is not None and now - self._last_ctrl_c < CTRL_C_EXIT_WINDOW_SECONDS:
2070
- self.exit()
2071
- return
2072
- event.current_buffer.reset()
2073
- self._pasted_content.clear()
2074
- self._skill_draft.clear()
2075
- self.state.attachments.clear()
2076
- self._last_ctrl_c = now
2077
- self._show_input_cleared_hint()
2149
+ action = self._input_actions.decide_clear_or_exit(self._input_view(buffer), now)
2150
+ self.apply_input_action(action, buffer)
2151
+ if action is InputAction.CLEAR_INPUT:
2152
+ self._input_actions.arm_clear(now)
2078
2153
 
2079
2154
  @bind("exit")
2080
2155
  def ctrl_d(event) -> None: # type: ignore[no-untyped-def]
2081
- if not event.current_buffer.text:
2082
- self.exit()
2083
- else:
2084
- event.current_buffer.delete()
2156
+ buffer = event.current_buffer
2157
+ action = self._input_actions.decide_ctrl_d(self._input_view(buffer))
2158
+ self.apply_input_action(action, buffer)
2085
2159
 
2086
2160
  @bind("tools_expand")
2087
2161
  def tools_expand(event) -> None: # type: ignore[no-untyped-def]
@@ -2185,7 +2259,7 @@ class CliApplication:
2185
2259
  if (not self._pasted_content.has_blocks and not self._skill_draft.is_present(displayed)
2186
2260
  and text.startswith("/") and "\n" not in text):
2187
2261
  self.buffer.reset(append_to_history=True)
2188
- asyncio.create_task(self._dispatch_command(text))
2262
+ self.lifecycle.spawn(self._dispatch_command(text), stop=TaskStop.CANCEL, name="slash-command")
2189
2263
  return
2190
2264
  pending_interrupt = self.runner.current_interrupt() if not self.state.running else None
2191
2265
  if self.state.running or self.runner.control.pending_message_texts() or pending_interrupt is not None:
@@ -2234,6 +2308,10 @@ class CliApplication:
2234
2308
  resume_call: Any | None = None,
2235
2309
  image_refs: tuple[ImageAttachmentRef, ...] = (),
2236
2310
  ) -> None:
2311
+ # Exit may win the race with a completion callback. Refuse before any
2312
+ # visible state change or a new run reservation.
2313
+ if self.lifecycle.exiting:
2314
+ return
2237
2315
  if resume_call is None:
2238
2316
  self.state.add_user(text, attachments=image_refs)
2239
2317
  self.state.attachments.clear()
@@ -2276,9 +2354,13 @@ class CliApplication:
2276
2354
  if epoch == self._run_epoch:
2277
2355
  self._cancel_sent = False
2278
2356
 
2279
- self._run_task = asyncio.create_task(work())
2357
+ self._run_task = self.lifecycle.spawn(work(), stop=TaskStop.OBSERVE, name="agent-run")
2280
2358
 
2281
2359
  def _start_queued_run(self) -> None:
2360
+ # Do not pop steering or follow-up after exit: those messages are not
2361
+ # consumed, and they are not marked applied.
2362
+ if self.lifecycle.exiting:
2363
+ return
2282
2364
  if self.interaction is not None or self.runner.current_interrupt() is not None:
2283
2365
  return
2284
2366
  text = self.runner.control.pop_steering()
@@ -2356,7 +2438,11 @@ class CliApplication:
2356
2438
  looks_like_image = path.suffix.lower() in {".png", ".jpg", ".jpeg", ".webp", ".gif"}
2357
2439
  if path.is_file() and looks_like_image:
2358
2440
  if self.runner.supports_next_input("image"):
2359
- asyncio.create_task(self._attach_pasted_path(trimmed, payload))
2441
+ self.lifecycle.spawn(
2442
+ self._attach_pasted_path(trimmed, payload),
2443
+ stop=TaskStop.CANCEL,
2444
+ name="attach-image",
2445
+ )
2360
2446
  return
2361
2447
  self.set_status("Image path pasted as text: current or pending model does not support images")
2362
2448
  except (OSError, ClipboardError):
@@ -2636,11 +2722,11 @@ class CliApplication:
2636
2722
  return
2637
2723
  mode_raw = str(interaction.values.get("mode") or "")
2638
2724
  self.interaction = None
2639
- mode = parse_sandbox_mode(mode_raw)
2640
- if mode is None:
2725
+ sandbox_mode = parse_sandbox_mode(mode_raw)
2726
+ if sandbox_mode is None:
2641
2727
  self.state.add_system("No sandbox mode selected", error=True)
2642
2728
  return
2643
- self._request_sandbox_mode(mode)
2729
+ self._request_sandbox_mode(sandbox_mode)
2644
2730
  return
2645
2731
  if interaction.kind == "permission_confirm":
2646
2732
  if cancelled:
@@ -38,7 +38,7 @@ def command_table() -> tuple[Command, ...]:
38
38
  ),
39
39
  Command(
40
40
  "sandbox",
41
- "Filesystem: restricted, or full-access (HIGH RISK)",
41
+ "Filesystem: restricted, full-access (HIGH RISK), or mounts",
42
42
  _sandbox,
43
43
  ),
44
44
  Command(
@@ -116,4 +116,4 @@ async def _mcp(app: "CliApplication", arg: str) -> None:
116
116
 
117
117
 
118
118
  async def _exit(app: "CliApplication", _arg: str) -> None:
119
- app.exit()
119
+ app.request_command_exit()