fleetdeck 0.3.0__py3-none-any.whl
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.
- fleetdeck/__init__.py +3 -0
- fleetdeck/__main__.py +232 -0
- fleetdeck/accounts.py +115 -0
- fleetdeck/adapters/__init__.py +0 -0
- fleetdeck/adapters/base.py +71 -0
- fleetdeck/adapters/claude_cli.py +187 -0
- fleetdeck/adapters/codex_cli.py +362 -0
- fleetdeck/adapters/demo.py +331 -0
- fleetdeck/config.py +323 -0
- fleetdeck/db.py +511 -0
- fleetdeck/gitinfo.py +169 -0
- fleetdeck/models.py +178 -0
- fleetdeck/paths.py +64 -0
- fleetdeck/proc.py +92 -0
- fleetdeck/redact.py +51 -0
- fleetdeck/services/__init__.py +0 -0
- fleetdeck/services/dispatcher.py +382 -0
- fleetdeck/services/fleet.py +65 -0
- fleetdeck/services/monitor.py +253 -0
- fleetdeck/services/notifier.py +71 -0
- fleetdeck/status.py +97 -0
- fleetdeck/terminal.py +235 -0
- fleetdeck/termrender.py +129 -0
- fleetdeck/ui/__init__.py +0 -0
- fleetdeck/ui/app.py +1295 -0
- fleetdeck/ui/app.tcss +263 -0
- fleetdeck/ui/commands.py +63 -0
- fleetdeck/ui/screens/__init__.py +0 -0
- fleetdeck/ui/screens/modals.py +623 -0
- fleetdeck/ui/style.py +150 -0
- fleetdeck/ui/views.py +230 -0
- fleetdeck/ui/widgets/__init__.py +0 -0
- fleetdeck/ui/widgets/agent_table.py +185 -0
- fleetdeck/ui/widgets/bottom.py +27 -0
- fleetdeck/ui/widgets/details.py +295 -0
- fleetdeck/ui/widgets/header.py +87 -0
- fleetdeck/ui/widgets/sidebar.py +227 -0
- fleetdeck/ui/widgets/splitter.py +66 -0
- fleetdeck/ui/widgets/terminal.py +119 -0
- fleetdeck-0.3.0.dist-info/METADATA +245 -0
- fleetdeck-0.3.0.dist-info/RECORD +44 -0
- fleetdeck-0.3.0.dist-info/WHEEL +4 -0
- fleetdeck-0.3.0.dist-info/entry_points.txt +2 -0
- fleetdeck-0.3.0.dist-info/licenses/LICENSE +21 -0
fleetdeck/ui/app.py
ADDED
|
@@ -0,0 +1,1295 @@
|
|
|
1
|
+
"""FleetDeck — main Textual application."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import functools
|
|
6
|
+
import os
|
|
7
|
+
import subprocess
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from textual import on, work
|
|
12
|
+
from textual.actions import SkipAction
|
|
13
|
+
from textual.app import App, ComposeResult
|
|
14
|
+
from textual.binding import Binding
|
|
15
|
+
from textual.containers import Horizontal, Vertical
|
|
16
|
+
from textual.css.query import NoMatches
|
|
17
|
+
from textual.widgets import Button, ContentSwitcher, DataTable, Footer, Input, TextArea
|
|
18
|
+
|
|
19
|
+
from .. import gitinfo
|
|
20
|
+
from ..adapters.base import Backend, BackendError, CommandResult
|
|
21
|
+
from ..config import DEFAULT_KEYBINDINGS, Settings, save_settings
|
|
22
|
+
from ..db import Store
|
|
23
|
+
from ..gitinfo import GitCache
|
|
24
|
+
from ..models import Agent, AgentStatus, Project, Task, TaskStatus
|
|
25
|
+
from ..proc import kill_all, run
|
|
26
|
+
from ..redact import redact
|
|
27
|
+
from ..terminal import TerminalManager
|
|
28
|
+
from ..termrender import render as render_terminal
|
|
29
|
+
from ..services.dispatcher import Dispatcher
|
|
30
|
+
from ..services.fleet import Fleet
|
|
31
|
+
from ..services.monitor import Monitor, Snapshot, poll_due
|
|
32
|
+
from ..services.notifier import Notifier, notices_for
|
|
33
|
+
from .commands import CenterCommands
|
|
34
|
+
from .screens.modals import (
|
|
35
|
+
AgentForm, AgentRequest, ConfirmModal, FollowUpForm, HelpScreen, ProjectForm,
|
|
36
|
+
ProjectRequest,
|
|
37
|
+
TemplatesModal, TextViewer,
|
|
38
|
+
)
|
|
39
|
+
from .style import Styler
|
|
40
|
+
from .views import ProjectsView, SessionsView, SettingsView, TasksView
|
|
41
|
+
from .widgets.agent_table import AgentTable
|
|
42
|
+
from .widgets.bottom import ActivityFeed
|
|
43
|
+
from .widgets.details import TABS, AgentDetails, TestRun
|
|
44
|
+
from .widgets.header import CenterHeader
|
|
45
|
+
from .widgets.sidebar import ALL, ProjectSidebar, Scope
|
|
46
|
+
from .widgets.splitter import Splitter
|
|
47
|
+
from .widgets.terminal import TerminalView
|
|
48
|
+
|
|
49
|
+
VIEWS = ("dashboard", "projects", "sessions", "tasks", "settings")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _while_alive(handler):
|
|
53
|
+
"""Skip a UI handler once shutdown has begun.
|
|
54
|
+
|
|
55
|
+
Textual can still deliver queued events (row highlighted, tab activated,
|
|
56
|
+
focus…) while it is removing widgets; a handler that queries the DOM then
|
|
57
|
+
raises NoMatches and the app exits with a traceback.
|
|
58
|
+
"""
|
|
59
|
+
@functools.wraps(handler)
|
|
60
|
+
def wrapper(self, *args, **kwargs):
|
|
61
|
+
if not self._ui_alive():
|
|
62
|
+
return None
|
|
63
|
+
return handler(self, *args, **kwargs)
|
|
64
|
+
return wrapper
|
|
65
|
+
|
|
66
|
+
_DESCRIPTIONS = {
|
|
67
|
+
"new_agent": ("New agent", True), "add_project": ("Add project", True),
|
|
68
|
+
"attach": ("Attach", False), "preview": ("Preview", False), "logs": ("Logs", True),
|
|
69
|
+
"diff": ("Diff", True), "stop": ("Stop", True), "restart": ("Restart", False),
|
|
70
|
+
"search": ("Filter", True), "pin": ("Pin", False), "new_task": ("Queue task", True),
|
|
71
|
+
"run_tests": ("Tests", False), "toggle_dispatch": ("Dispatch", False),
|
|
72
|
+
"follow_up": ("Follow up", True), "terminal": ("Terminal", False),
|
|
73
|
+
"details_wider": ("Wider", False), "details_narrower": ("Narrower", False),
|
|
74
|
+
"sidebar_wider": ("Sidebar wider", False), "sidebar_narrower": ("Sidebar narrower", False),
|
|
75
|
+
"zoom_details": ("Zoom", False), "switch_account": ("Account", False),
|
|
76
|
+
"refresh": ("Refresh", False), "view_dashboard": ("Dashboard", False),
|
|
77
|
+
"view_projects": ("Projects", False), "view_sessions": ("Sessions", False),
|
|
78
|
+
"view_tasks": ("Tasks", False), "view_settings": ("Settings", False),
|
|
79
|
+
"help": ("Help", True), "quit": ("Quit", True),
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
PANELS = ("sidebar", "center", "details")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _bindings() -> list[Binding]:
|
|
87
|
+
out = [
|
|
88
|
+
# ←/→ move between the dashboard panels; they fall through (SkipAction)
|
|
89
|
+
# to text inputs, the terminal and the details tab bar.
|
|
90
|
+
Binding("left", "panel(-1)", show=False, priority=True),
|
|
91
|
+
Binding("right", "panel(1)", show=False, priority=True),
|
|
92
|
+
]
|
|
93
|
+
for action, keys in DEFAULT_KEYBINDINGS.items():
|
|
94
|
+
desc, show = _DESCRIPTIONS[action]
|
|
95
|
+
out.append(Binding(keys, action, desc, show=show, id=action,
|
|
96
|
+
key_display={"question_mark": "?", "enter": "⏎"}.get(keys)))
|
|
97
|
+
return out
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class CommandCenterApp(App):
|
|
101
|
+
CSS_PATH = "app.tcss"
|
|
102
|
+
TITLE = "FleetDeck"
|
|
103
|
+
COMMANDS = App.COMMANDS | {CenterCommands}
|
|
104
|
+
BINDINGS = _bindings()
|
|
105
|
+
|
|
106
|
+
def __init__(self, settings: Settings, store: Store, backend: "Backend | Fleet", *,
|
|
107
|
+
config_path: Path, demo: bool = False) -> None:
|
|
108
|
+
self.settings = settings
|
|
109
|
+
self.store = store
|
|
110
|
+
multi = isinstance(backend, Fleet)
|
|
111
|
+
self.fleet = backend if multi else Fleet.single(backend)
|
|
112
|
+
self.backend = self.fleet.active.backend # active account (compatibility)
|
|
113
|
+
self.config_path = config_path
|
|
114
|
+
self.demo = demo
|
|
115
|
+
self.styler = Styler(settings.theme)
|
|
116
|
+
self.git = GitCache(ttl=settings.git_refresh_interval)
|
|
117
|
+
self.monitor = Monitor(
|
|
118
|
+
self.backend,
|
|
119
|
+
projects=lambda: self.projects,
|
|
120
|
+
launches=store.launches,
|
|
121
|
+
environments=self._monitor_envs,
|
|
122
|
+
git=self.git,
|
|
123
|
+
include_interactive=lambda: self.settings.show_interactive_sessions,
|
|
124
|
+
members=(lambda: list(self.fleet)) if multi else None,
|
|
125
|
+
)
|
|
126
|
+
self.dispatcher = Dispatcher(store, self.fleet, settings)
|
|
127
|
+
self.notifier = Notifier(settings.notifications)
|
|
128
|
+
self.snapshot = Snapshot()
|
|
129
|
+
self.projects: list[Project] = store.list_projects()
|
|
130
|
+
self.tasks: list[Task] = []
|
|
131
|
+
self.scope: Scope = ALL
|
|
132
|
+
self.filter_text = ""
|
|
133
|
+
self.test_runs: dict[str, TestRun] = {}
|
|
134
|
+
self._polling = False
|
|
135
|
+
self._last_git = 0.0
|
|
136
|
+
self._last_logs = 0.0
|
|
137
|
+
self._last_error: str | None = None
|
|
138
|
+
self._poll_timer = None
|
|
139
|
+
# NB: never name this `_closing`: Textual's MessagePump uses that flag and
|
|
140
|
+
# post_message() drops every message while it's True (that broke quit).
|
|
141
|
+
self._shutting_down = False
|
|
142
|
+
self._focused = True # terminal focus (AppFocus/AppBlur, if the terminal reports it)
|
|
143
|
+
self._last_poll = 0.0
|
|
144
|
+
self.terminals = TerminalManager()
|
|
145
|
+
self.account_status: dict[str, object] = {} # name -> accounts.AccountStatus
|
|
146
|
+
super().__init__()
|
|
147
|
+
|
|
148
|
+
# ------------------------------------------------------------ layout
|
|
149
|
+
def compose(self) -> ComposeResult:
|
|
150
|
+
yield CenterHeader(id="header")
|
|
151
|
+
with ContentSwitcher(id="views", initial="dashboard"):
|
|
152
|
+
with Horizontal(id="dashboard"):
|
|
153
|
+
yield ProjectSidebar(id="sidebar")
|
|
154
|
+
yield Splitter(id="split-left")
|
|
155
|
+
with Vertical(id="center"):
|
|
156
|
+
yield Input(placeholder="Filter agents by name, project, branch, task… "
|
|
157
|
+
"(f)", id="agent-filter")
|
|
158
|
+
yield AgentTable(id="agents")
|
|
159
|
+
yield Splitter(id="split-right")
|
|
160
|
+
yield AgentDetails(id="details")
|
|
161
|
+
yield ProjectsView(id="projects")
|
|
162
|
+
yield SessionsView(id="sessions")
|
|
163
|
+
yield TasksView(id="tasks")
|
|
164
|
+
yield SettingsView(self.settings, str(self.config_path), id="settings")
|
|
165
|
+
yield ActivityFeed(id="activity")
|
|
166
|
+
yield Footer()
|
|
167
|
+
|
|
168
|
+
def on_mount(self) -> None:
|
|
169
|
+
self.set_keymap(dict(self.settings.keybindings))
|
|
170
|
+
self._set_panel_titles()
|
|
171
|
+
self.query_one("#agents", AgentTable).set_show_account(len(self.fleet) > 1)
|
|
172
|
+
self._update_account_chip()
|
|
173
|
+
self._zoomed = False
|
|
174
|
+
if self.settings.sidebar_width:
|
|
175
|
+
self._set_panel_width("sidebar", self.settings.sidebar_width, save=False)
|
|
176
|
+
if self.settings.details_width:
|
|
177
|
+
self._set_panel_width("details", self.settings.details_width, save=False)
|
|
178
|
+
try:
|
|
179
|
+
self.theme = self.settings.theme
|
|
180
|
+
except Exception: # noqa: BLE001 — unknown theme name in config
|
|
181
|
+
self.theme = "catppuccin-mocha"
|
|
182
|
+
self.styler.set_theme(self.theme)
|
|
183
|
+
self.theme_changed_signal.subscribe(self, self._on_theme_changed)
|
|
184
|
+
header = self.query_one(CenterHeader)
|
|
185
|
+
header.demo = self.demo
|
|
186
|
+
for item in self.store.recent_activity(30):
|
|
187
|
+
self.query_one(ActivityFeed).add(item)
|
|
188
|
+
for err in self.settings.load_errors:
|
|
189
|
+
self.log_activity("warning", f"config: {err}")
|
|
190
|
+
self._get_version()
|
|
191
|
+
self.query_one("#agents", AgentTable).focus()
|
|
192
|
+
self._render_all()
|
|
193
|
+
self._start_polling()
|
|
194
|
+
if not self.projects and not self.demo:
|
|
195
|
+
self.log_activity("info", "No projects yet — press p to register a directory.")
|
|
196
|
+
|
|
197
|
+
def on_resize(self, event) -> None:
|
|
198
|
+
width = event.size.width
|
|
199
|
+
# details panel needs ~40 cols; keep at least ~60 for the agent table
|
|
200
|
+
self.screen.set_class(width < 130, "-narrow")
|
|
201
|
+
self.screen.set_class(width < 90, "-very-narrow")
|
|
202
|
+
|
|
203
|
+
def _start_polling(self) -> None:
|
|
204
|
+
if self._poll_timer is not None:
|
|
205
|
+
self._poll_timer.stop()
|
|
206
|
+
self._poll_timer = self.set_interval(self.settings.refresh_interval, self._poll_tick)
|
|
207
|
+
self.poll_now()
|
|
208
|
+
|
|
209
|
+
def _is_idle(self) -> bool:
|
|
210
|
+
if not self._focused:
|
|
211
|
+
return True
|
|
212
|
+
if any(a.status.is_active for a in self.snapshot.agents):
|
|
213
|
+
return False
|
|
214
|
+
waiting = (TaskStatus.DISPATCHING, TaskStatus.RUNNING, TaskStatus.BLOCKED)
|
|
215
|
+
if any(t.status in waiting for t in self.tasks):
|
|
216
|
+
return False
|
|
217
|
+
has_queued = any(t.status is TaskStatus.QUEUED for t in self.tasks)
|
|
218
|
+
return not (has_queued and not self.settings.dispatch_paused)
|
|
219
|
+
|
|
220
|
+
@_while_alive
|
|
221
|
+
def _poll_tick(self) -> None:
|
|
222
|
+
if poll_due(time.time(), self._last_poll, base=self.settings.refresh_interval,
|
|
223
|
+
idle_interval=self.settings.idle_refresh_interval, idle=self._is_idle()):
|
|
224
|
+
self.poll_now()
|
|
225
|
+
|
|
226
|
+
@_while_alive
|
|
227
|
+
def on_app_focus(self) -> None:
|
|
228
|
+
self._focused = True
|
|
229
|
+
self.poll_now() # catch up immediately when the user comes back
|
|
230
|
+
|
|
231
|
+
def on_app_blur(self) -> None:
|
|
232
|
+
self._focused = False
|
|
233
|
+
|
|
234
|
+
@work(group="meta", exclusive=True)
|
|
235
|
+
async def _get_version(self) -> None:
|
|
236
|
+
# resolve account environments (Keychain tokens) before the first poll
|
|
237
|
+
try:
|
|
238
|
+
await self.fleet.resolve_envs()
|
|
239
|
+
except Exception as exc: # noqa: BLE001 — never block startup on a token lookup
|
|
240
|
+
self.log_activity("warning", f"accounts: {exc}")
|
|
241
|
+
labels = {"claude": "Claude Code", "codex": "Codex"}
|
|
242
|
+
parts, seen = [], set()
|
|
243
|
+
for m in self.fleet:
|
|
244
|
+
if m.provider in seen:
|
|
245
|
+
continue
|
|
246
|
+
seen.add(m.provider)
|
|
247
|
+
try:
|
|
248
|
+
v = await m.backend.version()
|
|
249
|
+
except BackendError as exc:
|
|
250
|
+
self.log_activity("error", str(exc))
|
|
251
|
+
v = None
|
|
252
|
+
parts.append(f"{labels.get(m.provider, m.provider)} {v or '?'}")
|
|
253
|
+
if self._ui_alive():
|
|
254
|
+
self.query_one(CenterHeader).backend = (
|
|
255
|
+
"simulated agents" if self.backend.is_demo else " · ".join(parts))
|
|
256
|
+
self.poll_now()
|
|
257
|
+
await self._probe_accounts()
|
|
258
|
+
|
|
259
|
+
async def _probe_accounts(self) -> None:
|
|
260
|
+
from ..accounts import AccountStatus, probe
|
|
261
|
+
for m in self.fleet:
|
|
262
|
+
if getattr(m.backend, "is_demo", False):
|
|
263
|
+
self.account_status[m.name] = AccountStatus(
|
|
264
|
+
m.account, True, label="demo account", detail="simulated")
|
|
265
|
+
continue
|
|
266
|
+
try:
|
|
267
|
+
self.account_status[m.name] = await probe(
|
|
268
|
+
m.account, env=m.env, claude_binary=self.settings.claude_binary,
|
|
269
|
+
codex_binary=self.settings.codex_binary)
|
|
270
|
+
except Exception as exc: # noqa: BLE001 — status is informational only
|
|
271
|
+
self.account_status[m.name] = AccountStatus(m.account, None, error=str(exc))
|
|
272
|
+
|
|
273
|
+
# ---------------------------------------------------------- accounts
|
|
274
|
+
def _accounts_meta(self) -> list[tuple[str, str, str]]:
|
|
275
|
+
return [(m.name, m.account.icon, m.provider) for m in self.fleet]
|
|
276
|
+
|
|
277
|
+
def _update_account_chip(self) -> None:
|
|
278
|
+
if not self._ui_alive():
|
|
279
|
+
return
|
|
280
|
+
header = self.query_one(CenterHeader)
|
|
281
|
+
active = self.fleet.active
|
|
282
|
+
header.account = active.name if len(self.fleet) > 1 else ""
|
|
283
|
+
header.account_provider = active.provider
|
|
284
|
+
|
|
285
|
+
def action_switch_account(self) -> None:
|
|
286
|
+
from .screens.modals import AccountRow, AccountSwitcher
|
|
287
|
+
rows = []
|
|
288
|
+
for m in self.fleet:
|
|
289
|
+
st = self.account_status.get(m.name)
|
|
290
|
+
rows.append(AccountRow(
|
|
291
|
+
m.name, m.provider, m.account.icon, m.account.home,
|
|
292
|
+
getattr(st, "logged_in", None), getattr(st, "label", "") or "",
|
|
293
|
+
getattr(st, "detail", "") or getattr(st, "error", "") or ""))
|
|
294
|
+
|
|
295
|
+
def done(name: str | None) -> None:
|
|
296
|
+
if not name or name == self.fleet.active_name:
|
|
297
|
+
return
|
|
298
|
+
self.fleet.active_name = name
|
|
299
|
+
self.backend = self.fleet.active.backend
|
|
300
|
+
self.settings.active_account = name
|
|
301
|
+
if not self.demo:
|
|
302
|
+
try:
|
|
303
|
+
save_settings(self.settings, self.config_path)
|
|
304
|
+
except OSError:
|
|
305
|
+
pass
|
|
306
|
+
self._update_account_chip()
|
|
307
|
+
m = self.fleet.active
|
|
308
|
+
self.log_activity("info", f"Active account: {m.account.icon} {name} "
|
|
309
|
+
f"({'Claude Code' if m.provider == 'claude' else 'Codex'})")
|
|
310
|
+
self.notify(f"New agents will use {m.account.icon} {name}", timeout=3)
|
|
311
|
+
|
|
312
|
+
self.push_screen(AccountSwitcher(rows, self.fleet.active_name), done)
|
|
313
|
+
|
|
314
|
+
@_while_alive
|
|
315
|
+
def _on_theme_changed(self, theme) -> None:
|
|
316
|
+
self.styler.set_theme(theme.name)
|
|
317
|
+
self.settings.theme = theme.name
|
|
318
|
+
self._render_all()
|
|
319
|
+
|
|
320
|
+
def _monitor_envs(self) -> list[dict[str, str]]:
|
|
321
|
+
names = {"default"} | {p.environment for p in self.projects}
|
|
322
|
+
return [self.settings.env_for(n) for n in sorted(names)]
|
|
323
|
+
|
|
324
|
+
# ----------------------------------------------------------- polling
|
|
325
|
+
def poll_now(self) -> None:
|
|
326
|
+
if not self._polling:
|
|
327
|
+
self._polling = True # set before the worker starts: no double polls
|
|
328
|
+
self._last_poll = time.time()
|
|
329
|
+
self._poll_cycle()
|
|
330
|
+
|
|
331
|
+
@work(group="poll", exclusive=False)
|
|
332
|
+
async def _poll_cycle(self) -> None:
|
|
333
|
+
try:
|
|
334
|
+
snap = await self.monitor.poll()
|
|
335
|
+
if not self._ui_alive():
|
|
336
|
+
return
|
|
337
|
+
try:
|
|
338
|
+
events = await self.dispatcher.tick(snap)
|
|
339
|
+
except Exception as exc: # noqa: BLE001 — dispatcher bugs must not stop polling
|
|
340
|
+
events = []
|
|
341
|
+
self.log_activity("error", f"dispatcher: {exc!r}")
|
|
342
|
+
for ev in events:
|
|
343
|
+
self.log_activity(ev.level, ev.message, ev.project_id)
|
|
344
|
+
if not self._ui_alive():
|
|
345
|
+
return
|
|
346
|
+
self._handle_snapshot(snap)
|
|
347
|
+
now = time.time()
|
|
348
|
+
if now - self._last_git >= self.settings.git_refresh_interval:
|
|
349
|
+
self._last_git = now
|
|
350
|
+
self._refresh_git()
|
|
351
|
+
if now - self._last_logs >= self.settings.log_refresh_interval:
|
|
352
|
+
self._last_logs = now
|
|
353
|
+
self._refresh_selected_logs()
|
|
354
|
+
finally:
|
|
355
|
+
self._polling = False
|
|
356
|
+
|
|
357
|
+
def _handle_snapshot(self, snap: Snapshot) -> None:
|
|
358
|
+
self.tasks = self.store.list_tasks()
|
|
359
|
+
by_id = {t.id: t for t in self.tasks}
|
|
360
|
+
by_agent = {t.agent_id: t for t in self.tasks if t.agent_id}
|
|
361
|
+
by_name = {t.agent_name: t for t in self.tasks if t.agent_name}
|
|
362
|
+
for a in snap.agents:
|
|
363
|
+
task = by_id.get(a.task_id) if a.task_id else None
|
|
364
|
+
task = task or by_agent.get(a.id) or by_name.get(a.name)
|
|
365
|
+
if task is not None:
|
|
366
|
+
a.task_title, a.task_id = task.title, task.id
|
|
367
|
+
header = self.query_one(CenterHeader)
|
|
368
|
+
header.healthy = snap.ok
|
|
369
|
+
if snap.errors:
|
|
370
|
+
msg = snap.errors[0]
|
|
371
|
+
if msg != self._last_error:
|
|
372
|
+
self.log_activity("error", f"claude agents: {msg}")
|
|
373
|
+
self._last_error = msg
|
|
374
|
+
else:
|
|
375
|
+
self._last_error = None
|
|
376
|
+
self.snapshot = snap
|
|
377
|
+
notices = notices_for(snap.transitions)
|
|
378
|
+
prefs = self.settings.notifications
|
|
379
|
+
for n in notices:
|
|
380
|
+
self.log_activity(n.level, f"{n.title} {n.message}", n.project_id)
|
|
381
|
+
if prefs.toast:
|
|
382
|
+
self.notify(n.message, title=n.title,
|
|
383
|
+
severity="error" if n.level == "error" else
|
|
384
|
+
"warning" if n.level == "warning" else "information")
|
|
385
|
+
if notices and prefs.bell:
|
|
386
|
+
self.bell()
|
|
387
|
+
self.notifier.external(notices)
|
|
388
|
+
self._render_all()
|
|
389
|
+
|
|
390
|
+
@work(group="git", exclusive=True)
|
|
391
|
+
async def _refresh_git(self) -> None:
|
|
392
|
+
await self.monitor.refresh_git(self.snapshot.agents)
|
|
393
|
+
self._render_all()
|
|
394
|
+
|
|
395
|
+
@work(group="logs", exclusive=True)
|
|
396
|
+
async def _refresh_selected_logs(self, force: bool = False) -> None:
|
|
397
|
+
if not self._ui_alive():
|
|
398
|
+
return
|
|
399
|
+
details = self.query_one(AgentDetails)
|
|
400
|
+
if self.current_view != "dashboard" or (details.active_tab != "tab-logs" and not force):
|
|
401
|
+
return
|
|
402
|
+
agent = self.selected_agent
|
|
403
|
+
if agent is None:
|
|
404
|
+
return
|
|
405
|
+
if not agent.controllable:
|
|
406
|
+
details.set_logs(agent.key, "Logs are available for background sessions only "
|
|
407
|
+
"(`claude logs <id>`). This session runs in another terminal.")
|
|
408
|
+
return
|
|
409
|
+
try:
|
|
410
|
+
res = await self.fleet.for_agent(agent).backend.logs(agent.id or "",
|
|
411
|
+
self._env_for(agent))
|
|
412
|
+
except Exception as exc: # noqa: BLE001 — show the error instead of crashing
|
|
413
|
+
res = CommandResult(False, f"could not read logs: {exc}")
|
|
414
|
+
if not self._ui_alive():
|
|
415
|
+
return
|
|
416
|
+
details.set_logs(agent.key, res.output, tail=self.settings.log_lines)
|
|
417
|
+
|
|
418
|
+
# ---------------------------------------------------------- rendering
|
|
419
|
+
@property
|
|
420
|
+
def current_view(self) -> str:
|
|
421
|
+
return self.query_one("#views", ContentSwitcher).current or "dashboard"
|
|
422
|
+
|
|
423
|
+
def _visible_agents(self) -> list[Agent]:
|
|
424
|
+
projects = {p.id: p for p in self.projects}
|
|
425
|
+
q = self.filter_text.lower()
|
|
426
|
+
out = []
|
|
427
|
+
for a in self.snapshot.agents:
|
|
428
|
+
if not self.scope.matches(a, projects):
|
|
429
|
+
continue
|
|
430
|
+
if q and not any(q in (v or "").lower() for v in (
|
|
431
|
+
a.display_name, a.project_name, a.branch, a.task_title, a.status.label,
|
|
432
|
+
a.cwd, a.id, a.waiting_for)):
|
|
433
|
+
continue
|
|
434
|
+
out.append(a)
|
|
435
|
+
return out
|
|
436
|
+
|
|
437
|
+
def _ui_alive(self) -> bool:
|
|
438
|
+
"""False once shutdown starts: late worker results must not touch widgets."""
|
|
439
|
+
if self._shutting_down or not self.is_running or not self.screen_stack:
|
|
440
|
+
return False
|
|
441
|
+
try:
|
|
442
|
+
self.screen_stack[0].query_one(CenterHeader)
|
|
443
|
+
except NoMatches:
|
|
444
|
+
return False
|
|
445
|
+
return True
|
|
446
|
+
|
|
447
|
+
def _render_all(self) -> None:
|
|
448
|
+
if not self._ui_alive():
|
|
449
|
+
return
|
|
450
|
+
self.projects = self.store.list_projects()
|
|
451
|
+
agents = self.snapshot.agents
|
|
452
|
+
header = self.query_one(CenterHeader)
|
|
453
|
+
header.active = sum(1 for a in agents if a.status.is_active)
|
|
454
|
+
header.attention = sum(1 for a in agents
|
|
455
|
+
if a.status in (AgentStatus.PERMISSION, AgentStatus.WAITING))
|
|
456
|
+
header.total_agents = len(agents)
|
|
457
|
+
header.projects = len(self.projects)
|
|
458
|
+
header.queued = sum(1 for t in self.tasks if t.status is TaskStatus.QUEUED)
|
|
459
|
+
header.paused = self.settings.dispatch_paused
|
|
460
|
+
view = self.current_view
|
|
461
|
+
if view == "dashboard":
|
|
462
|
+
visible = self._visible_agents()
|
|
463
|
+
self.query_one(ProjectSidebar).update_data(self.projects, agents,
|
|
464
|
+
self._accounts_meta())
|
|
465
|
+
self.query_one("#agents", AgentTable).update_agents(visible)
|
|
466
|
+
center = self.query_one("#center")
|
|
467
|
+
center.border_title = (f"Agents {len(visible)}" if len(visible) == len(agents)
|
|
468
|
+
else f"Agents {len(visible)}/{len(agents)}")
|
|
469
|
+
self._update_details()
|
|
470
|
+
elif view == "projects":
|
|
471
|
+
self.query_one(ProjectsView).update_data(self.projects, agents, self.tasks)
|
|
472
|
+
elif view == "sessions":
|
|
473
|
+
self.query_one("#sessions-table", AgentTable).update_agents(agents)
|
|
474
|
+
elif view == "tasks":
|
|
475
|
+
self.query_one(TasksView).update_data(
|
|
476
|
+
self.tasks, {p.id: p for p in self.projects},
|
|
477
|
+
paused=self.settings.dispatch_paused,
|
|
478
|
+
max_agents=self.settings.max_concurrent_agents)
|
|
479
|
+
|
|
480
|
+
def _set_panel_titles(self) -> None:
|
|
481
|
+
self.query_one("#sidebar").border_title = "Projects"
|
|
482
|
+
self.query_one("#center").border_title = "Agents"
|
|
483
|
+
self.query_one("#center").border_subtitle = "⏎ attach · m follow up · f filter"
|
|
484
|
+
self.query_one("#details").border_title = "Details"
|
|
485
|
+
self.query_one("#activity").border_title = "Activity"
|
|
486
|
+
|
|
487
|
+
def _terminal_target(self) -> tuple[str | None, Project | None]:
|
|
488
|
+
agent = self.selected_agent
|
|
489
|
+
if agent is not None and os.path.isdir(agent.cwd):
|
|
490
|
+
return agent.cwd, self._project(agent.project_id)
|
|
491
|
+
project = self.selected_project
|
|
492
|
+
if project is not None and os.path.isdir(project.path):
|
|
493
|
+
return project.path, project
|
|
494
|
+
return None, None
|
|
495
|
+
|
|
496
|
+
@_while_alive
|
|
497
|
+
def _sync_terminal(self, start: bool = False) -> None:
|
|
498
|
+
"""Point the Terminal tab at the selected agent's directory. Shells start
|
|
499
|
+
lazily: only while the tab is visible (or when ``start`` is set)."""
|
|
500
|
+
details = self.query_one(AgentDetails)
|
|
501
|
+
if details.active_tab != "tab-terminal" and not start:
|
|
502
|
+
return
|
|
503
|
+
cwd, project = self._terminal_target()
|
|
504
|
+
view = details.terminal
|
|
505
|
+
if cwd is None:
|
|
506
|
+
from rich.text import Text
|
|
507
|
+
view.attach(None, Text("\n Select an agent or a project to open a shell in its "
|
|
508
|
+
"directory.", style="dim"))
|
|
509
|
+
details.set_terminal_bar(None, None, False)
|
|
510
|
+
return
|
|
511
|
+
env = {}
|
|
512
|
+
if project is not None:
|
|
513
|
+
env = {**self.settings.env_for(project.environment), **project.env}
|
|
514
|
+
size = view.size
|
|
515
|
+
try:
|
|
516
|
+
session = self.terminals.open(cwd, env=env, cols=size.width or 80,
|
|
517
|
+
rows=size.height or 24)
|
|
518
|
+
except OSError as exc:
|
|
519
|
+
self.notify(f"Couldn't start a shell: {exc}", severity="error")
|
|
520
|
+
return
|
|
521
|
+
if view.session is not session:
|
|
522
|
+
view.attach(session)
|
|
523
|
+
details.set_terminal_bar(cwd, session.shell, session.alive)
|
|
524
|
+
|
|
525
|
+
@on(TerminalView.Leave)
|
|
526
|
+
def _terminal_leave(self) -> None:
|
|
527
|
+
if getattr(self, "_zoomed", False):
|
|
528
|
+
# the agent list is hidden while zoomed: land on the tab bar, where
|
|
529
|
+
# "\" restores the layout
|
|
530
|
+
self.query_one("#details ContentTabs").focus()
|
|
531
|
+
else:
|
|
532
|
+
self.query_one("#agents", AgentTable).focus()
|
|
533
|
+
self._sync_terminal()
|
|
534
|
+
|
|
535
|
+
@on(TerminalView.Restart)
|
|
536
|
+
def _terminal_restart(self) -> None:
|
|
537
|
+
cwd, _ = self._terminal_target()
|
|
538
|
+
if cwd and (s := self.terminals.get(cwd)) is not None and not s.alive:
|
|
539
|
+
self.terminals.sessions.pop(cwd, None)
|
|
540
|
+
self._sync_terminal(start=True)
|
|
541
|
+
|
|
542
|
+
def action_terminal(self) -> None:
|
|
543
|
+
if self.current_view != "dashboard":
|
|
544
|
+
self._switch("dashboard")
|
|
545
|
+
details = self.query_one(AgentDetails)
|
|
546
|
+
details.show_tab("tab-terminal")
|
|
547
|
+
self._sync_terminal(start=True)
|
|
548
|
+
self.call_after_refresh(details.terminal.focus)
|
|
549
|
+
|
|
550
|
+
# ------------------------------------------------------------ sizing
|
|
551
|
+
MIN_WIDTH = {"sidebar": 18, "details": 32}
|
|
552
|
+
MIN_CENTER = 50 # must match #center's min-width in app.tcss
|
|
553
|
+
SPLITTERS = 2
|
|
554
|
+
|
|
555
|
+
def _set_panel_width(self, panel: str, width: int, *, save: bool = True) -> int:
|
|
556
|
+
"""Resize the sidebar or details panel, keeping the agent list usable."""
|
|
557
|
+
other = "details" if panel == "sidebar" else "sidebar"
|
|
558
|
+
other_w = self.query_one(f"#{other}").outer_size.width if \
|
|
559
|
+
self.query_one(f"#{other}").display else 0
|
|
560
|
+
screen_w = self.size.width or 160
|
|
561
|
+
upper = max(self.MIN_WIDTH[panel],
|
|
562
|
+
screen_w - other_w - self.MIN_CENTER - self.SPLITTERS)
|
|
563
|
+
width = max(self.MIN_WIDTH[panel], min(int(width), upper))
|
|
564
|
+
widget = self.query_one(f"#{panel}")
|
|
565
|
+
widget.styles.width = width
|
|
566
|
+
# inline limits must be explicit: clearing them would re-apply the
|
|
567
|
+
# stylesheet's max-width (64) and silently cap the panel
|
|
568
|
+
widget.styles.max_width = width
|
|
569
|
+
widget.styles.min_width = self.MIN_WIDTH[panel]
|
|
570
|
+
setattr(self.settings, f"{panel}_width", width)
|
|
571
|
+
if save and not self.demo:
|
|
572
|
+
try:
|
|
573
|
+
save_settings(self.settings, self.config_path)
|
|
574
|
+
except OSError:
|
|
575
|
+
pass
|
|
576
|
+
return width
|
|
577
|
+
|
|
578
|
+
@on(Splitter.Dragged)
|
|
579
|
+
def _splitter_dragged(self, event: Splitter.Dragged) -> None:
|
|
580
|
+
if event.splitter.id == "split-left":
|
|
581
|
+
sidebar = self.query_one("#sidebar")
|
|
582
|
+
self._set_panel_width("sidebar", event.screen_x - sidebar.region.x, save=event.done)
|
|
583
|
+
else:
|
|
584
|
+
details = self.query_one("#details")
|
|
585
|
+
self._set_panel_width("details", details.region.right - event.screen_x - 1,
|
|
586
|
+
save=event.done)
|
|
587
|
+
|
|
588
|
+
def _nudge(self, panel: str, delta: int) -> None:
|
|
589
|
+
if self.current_view != "dashboard" or self._zoomed:
|
|
590
|
+
return
|
|
591
|
+
widget = self.query_one(f"#{panel}")
|
|
592
|
+
if not widget.display:
|
|
593
|
+
return
|
|
594
|
+
self._set_panel_width(panel, widget.outer_size.width + delta)
|
|
595
|
+
|
|
596
|
+
def action_details_wider(self) -> None:
|
|
597
|
+
self._nudge("details", 4)
|
|
598
|
+
|
|
599
|
+
def action_details_narrower(self) -> None:
|
|
600
|
+
self._nudge("details", -4)
|
|
601
|
+
|
|
602
|
+
def action_sidebar_wider(self) -> None:
|
|
603
|
+
self._nudge("sidebar", 2)
|
|
604
|
+
|
|
605
|
+
def action_sidebar_narrower(self) -> None:
|
|
606
|
+
self._nudge("sidebar", -2)
|
|
607
|
+
|
|
608
|
+
def action_zoom_details(self) -> None:
|
|
609
|
+
"""Toggle Details at full width (great for the terminal and logs)."""
|
|
610
|
+
if self.current_view != "dashboard":
|
|
611
|
+
self._switch("dashboard")
|
|
612
|
+
details = self.query_one("#details")
|
|
613
|
+
hide = ("#sidebar", "#center", "#split-left", "#split-right")
|
|
614
|
+
self._zoomed = not self._zoomed
|
|
615
|
+
if self._zoomed:
|
|
616
|
+
for sel in hide:
|
|
617
|
+
self.query_one(sel).styles.display = "none"
|
|
618
|
+
details.styles.display = "block" # even on narrow screens
|
|
619
|
+
details.styles.width = "1fr"
|
|
620
|
+
details.styles.max_width = "100%"
|
|
621
|
+
details.border_subtitle = "\\ restore (ctrl+] first, from the terminal)"
|
|
622
|
+
target = (details.query_one("#terminal") if details.active_tab == "tab-terminal"
|
|
623
|
+
else details.query_one("ContentTabs"))
|
|
624
|
+
self.call_after_refresh(target.focus) # the focused list is now hidden
|
|
625
|
+
else:
|
|
626
|
+
for sel in (*hide, "#details"):
|
|
627
|
+
self.query_one(sel).styles.clear_rule("display") # back to CSS rules
|
|
628
|
+
details.border_subtitle = ""
|
|
629
|
+
self.call_after_refresh(self.query_one("#agents").focus)
|
|
630
|
+
if self.settings.details_width:
|
|
631
|
+
self._set_panel_width("details", self.settings.details_width, save=False)
|
|
632
|
+
else:
|
|
633
|
+
for rule in ("width", "max_width", "min_width"):
|
|
634
|
+
details.styles.clear_rule(rule)
|
|
635
|
+
self.call_after_refresh(details.refresh, layout=True)
|
|
636
|
+
|
|
637
|
+
# ------------------------------------------------------------ panels
|
|
638
|
+
def _panel_of(self, widget) -> str | None:
|
|
639
|
+
node = widget
|
|
640
|
+
while node is not None:
|
|
641
|
+
if getattr(node, "id", None) in PANELS:
|
|
642
|
+
return node.id
|
|
643
|
+
node = node.parent
|
|
644
|
+
return None
|
|
645
|
+
|
|
646
|
+
def _focus_panel(self, panel: str) -> None:
|
|
647
|
+
if panel == "sidebar":
|
|
648
|
+
self.query_one(ProjectSidebar).tree.focus()
|
|
649
|
+
elif panel == "center":
|
|
650
|
+
self.query_one("#agents", AgentTable).focus()
|
|
651
|
+
else:
|
|
652
|
+
self.query_one("#details ContentTabs").focus()
|
|
653
|
+
|
|
654
|
+
def action_panel(self, direction: int) -> None:
|
|
655
|
+
"""←/→ between Projects · Agents · Details (dashboard only)."""
|
|
656
|
+
if self.screen is not self.screen_stack[0] or self.current_view != "dashboard":
|
|
657
|
+
raise SkipAction()
|
|
658
|
+
focused = self.focused
|
|
659
|
+
if isinstance(focused, (Input, TextArea, TerminalView)):
|
|
660
|
+
raise SkipAction() # editing / shell: arrows belong to the widget
|
|
661
|
+
panel = self._panel_of(focused) if focused is not None else None
|
|
662
|
+
if panel is None:
|
|
663
|
+
self._focus_panel("center")
|
|
664
|
+
return
|
|
665
|
+
if panel == "details":
|
|
666
|
+
on_tabs = type(focused).__name__ == "ContentTabs"
|
|
667
|
+
first = self.query_one(AgentDetails).active_tab == TABS[0][0]
|
|
668
|
+
if direction > 0 or (on_tabs and not first):
|
|
669
|
+
raise SkipAction() # let the tab bar switch tabs
|
|
670
|
+
order = [p for p in PANELS if self.query_one(f"#{p}").display]
|
|
671
|
+
if panel not in order:
|
|
672
|
+
raise SkipAction()
|
|
673
|
+
index = order.index(panel) + direction
|
|
674
|
+
if not 0 <= index < len(order):
|
|
675
|
+
raise SkipAction()
|
|
676
|
+
self._focus_panel(order[index])
|
|
677
|
+
|
|
678
|
+
@_while_alive
|
|
679
|
+
def _update_details(self) -> None:
|
|
680
|
+
agent = self.selected_agent
|
|
681
|
+
details = self.query_one(AgentDetails)
|
|
682
|
+
if agent is None:
|
|
683
|
+
details.show(None, None, None, None)
|
|
684
|
+
else:
|
|
685
|
+
task = next((t for t in self.tasks if t.id == agent.task_id), None)
|
|
686
|
+
project = self._project(agent.project_id)
|
|
687
|
+
details.show(agent, task, self.git.peek(agent.cwd), self.test_runs.get(agent.cwd),
|
|
688
|
+
project.path if project else None)
|
|
689
|
+
self._sync_terminal() # follows the selection while the Terminal tab is open
|
|
690
|
+
|
|
691
|
+
# -------------------------------------------------------- selections
|
|
692
|
+
@property
|
|
693
|
+
def selected_agent(self) -> Agent | None:
|
|
694
|
+
view = self.current_view
|
|
695
|
+
if view == "sessions":
|
|
696
|
+
return self.query_one("#sessions-table", AgentTable).selected
|
|
697
|
+
if view == "dashboard":
|
|
698
|
+
return self.query_one("#agents", AgentTable).selected
|
|
699
|
+
return None
|
|
700
|
+
|
|
701
|
+
def _project(self, project_id: int | None) -> Project | None:
|
|
702
|
+
if project_id is None:
|
|
703
|
+
return None
|
|
704
|
+
return next((p for p in self.projects if p.id == project_id), None)
|
|
705
|
+
|
|
706
|
+
@property
|
|
707
|
+
def selected_project(self) -> Project | None:
|
|
708
|
+
view = self.current_view
|
|
709
|
+
if view == "projects":
|
|
710
|
+
return self._project(self.query_one(ProjectsView).selected_project_id)
|
|
711
|
+
if view == "dashboard":
|
|
712
|
+
sidebar = self.query_one(ProjectSidebar)
|
|
713
|
+
if self.focused is sidebar.tree or self.focused is sidebar.search_input:
|
|
714
|
+
pid = sidebar.selected_project_id
|
|
715
|
+
if pid is not None:
|
|
716
|
+
return self._project(pid)
|
|
717
|
+
agent = self.selected_agent
|
|
718
|
+
if agent is not None and agent.project_id is not None:
|
|
719
|
+
return self._project(agent.project_id)
|
|
720
|
+
if self.scope.kind == "project":
|
|
721
|
+
return self._project(self.scope.project_id)
|
|
722
|
+
return None
|
|
723
|
+
|
|
724
|
+
def _env_for(self, agent: Agent) -> dict[str, str]:
|
|
725
|
+
"""Environment for commands about this agent: its project's settings
|
|
726
|
+
plus its own account's (config dir, token), which win."""
|
|
727
|
+
project = self._project(agent.project_id)
|
|
728
|
+
base = self.settings.env_for(project.environment if project else "default")
|
|
729
|
+
if project is not None:
|
|
730
|
+
base.update(project.env)
|
|
731
|
+
return {**base, **self.fleet.for_agent(agent).env}
|
|
732
|
+
|
|
733
|
+
def _require_agent(self, *, controllable: bool = True) -> Agent | None:
|
|
734
|
+
agent = self.selected_agent
|
|
735
|
+
if agent is None:
|
|
736
|
+
self.notify("Select an agent first.", severity="warning")
|
|
737
|
+
return None
|
|
738
|
+
if controllable and not agent.controllable:
|
|
739
|
+
self.notify("This is an interactive session running in another terminal; "
|
|
740
|
+
"only background sessions can be controlled from here.",
|
|
741
|
+
severity="warning")
|
|
742
|
+
return None
|
|
743
|
+
return agent
|
|
744
|
+
|
|
745
|
+
def log_activity(self, level: str, message: str, project_id: int | None = None) -> None:
|
|
746
|
+
item = self.store.log(level, message, project_id)
|
|
747
|
+
if self._ui_alive():
|
|
748
|
+
self.screen_stack[0].query_one(ActivityFeed).add(item)
|
|
749
|
+
|
|
750
|
+
def focus_project(self, project_id: int) -> None:
|
|
751
|
+
self.action_view_dashboard()
|
|
752
|
+
sidebar = self.query_one(ProjectSidebar)
|
|
753
|
+
self.scope = Scope("project", project_id)
|
|
754
|
+
sidebar.select_scope(self.scope)
|
|
755
|
+
self._render_all()
|
|
756
|
+
|
|
757
|
+
def focus_agent(self, key: str) -> None:
|
|
758
|
+
self.scope = ALL
|
|
759
|
+
self.filter_text = ""
|
|
760
|
+
self.query_one("#agent-filter", Input).value = ""
|
|
761
|
+
self.action_view_dashboard()
|
|
762
|
+
table = self.query_one("#agents", AgentTable)
|
|
763
|
+
self._render_all()
|
|
764
|
+
keys = [a.key for a in self._visible_agents()]
|
|
765
|
+
if key in keys:
|
|
766
|
+
table.move_cursor(row=keys.index(key))
|
|
767
|
+
table.focus()
|
|
768
|
+
|
|
769
|
+
# ------------------------------------------------------------ events
|
|
770
|
+
@on(ProjectSidebar.ScopeChanged)
|
|
771
|
+
@_while_alive
|
|
772
|
+
def _scope_changed(self, event: ProjectSidebar.ScopeChanged) -> None:
|
|
773
|
+
self.scope = event.scope
|
|
774
|
+
self._render_all()
|
|
775
|
+
|
|
776
|
+
@on(Input.Changed, "#agent-filter")
|
|
777
|
+
@_while_alive
|
|
778
|
+
def _filter_changed(self, event: Input.Changed) -> None:
|
|
779
|
+
self.filter_text = event.value.strip()
|
|
780
|
+
self._render_all()
|
|
781
|
+
|
|
782
|
+
@on(Input.Submitted, "#agent-filter")
|
|
783
|
+
@_while_alive
|
|
784
|
+
def _filter_submitted(self) -> None:
|
|
785
|
+
self.query_one("#agents", AgentTable).focus()
|
|
786
|
+
|
|
787
|
+
@on(DataTable.RowHighlighted, "#agents")
|
|
788
|
+
@_while_alive
|
|
789
|
+
def _agent_highlighted(self) -> None:
|
|
790
|
+
self._update_details()
|
|
791
|
+
self._refresh_selected_logs()
|
|
792
|
+
|
|
793
|
+
@on(AgentTable.AttachRequested)
|
|
794
|
+
@_while_alive
|
|
795
|
+
def _agent_attach_requested(self, event: AgentTable.AttachRequested) -> None:
|
|
796
|
+
# Enter on the table (only while Enter is the attach key) or double-click.
|
|
797
|
+
# A single click never attaches: it only moves the cursor.
|
|
798
|
+
if event.via_keyboard and "enter" not in \
|
|
799
|
+
self.settings.keybindings.get("attach", "enter").split(","):
|
|
800
|
+
return
|
|
801
|
+
self.action_attach()
|
|
802
|
+
|
|
803
|
+
@_while_alive
|
|
804
|
+
def on_tabbed_content_tab_activated(self, event) -> None:
|
|
805
|
+
pane = getattr(event.pane, "id", None)
|
|
806
|
+
if pane == "tab-logs":
|
|
807
|
+
self._refresh_selected_logs(force=True)
|
|
808
|
+
elif pane == "tab-terminal":
|
|
809
|
+
self._sync_terminal(start=True)
|
|
810
|
+
|
|
811
|
+
@on(Button.Pressed, "#settings-save")
|
|
812
|
+
def _save_settings(self) -> None:
|
|
813
|
+
old_interval = self.settings.refresh_interval
|
|
814
|
+
s = self.query_one(SettingsView).collect()
|
|
815
|
+
self.theme = s.theme
|
|
816
|
+
self.notifier.prefs = s.notifications
|
|
817
|
+
if self.demo:
|
|
818
|
+
if s.refresh_interval != old_interval:
|
|
819
|
+
self._start_polling()
|
|
820
|
+
self.notify("Demo mode: settings applied for this session only.")
|
|
821
|
+
self._render_all()
|
|
822
|
+
return
|
|
823
|
+
try:
|
|
824
|
+
path = save_settings(s, self.config_path)
|
|
825
|
+
except OSError as exc:
|
|
826
|
+
self.notify(f"Could not save settings: {exc}", severity="error")
|
|
827
|
+
return
|
|
828
|
+
if s.refresh_interval != old_interval:
|
|
829
|
+
self._start_polling()
|
|
830
|
+
self.notify(f"Settings saved to {path}")
|
|
831
|
+
self._render_all()
|
|
832
|
+
|
|
833
|
+
# =========================================================== actions
|
|
834
|
+
# views
|
|
835
|
+
def _switch(self, view: str) -> None:
|
|
836
|
+
self.query_one("#views", ContentSwitcher).current = view
|
|
837
|
+
self._render_all()
|
|
838
|
+
target = {
|
|
839
|
+
"dashboard": "#agents", "projects": "#projects-table",
|
|
840
|
+
"sessions": "#sessions-table", "tasks": "#tasks-table", "settings": "#set-theme",
|
|
841
|
+
}[view]
|
|
842
|
+
self.query_one(target).focus()
|
|
843
|
+
|
|
844
|
+
def action_view_dashboard(self) -> None:
|
|
845
|
+
self._switch("dashboard")
|
|
846
|
+
|
|
847
|
+
def action_view_projects(self) -> None:
|
|
848
|
+
self._switch("projects")
|
|
849
|
+
|
|
850
|
+
def action_view_sessions(self) -> None:
|
|
851
|
+
self._switch("sessions")
|
|
852
|
+
|
|
853
|
+
def action_view_tasks(self) -> None:
|
|
854
|
+
self._switch("tasks")
|
|
855
|
+
|
|
856
|
+
def action_view_settings(self) -> None:
|
|
857
|
+
self.query_one(SettingsView).sync_from_settings()
|
|
858
|
+
self._switch("settings")
|
|
859
|
+
|
|
860
|
+
def action_help(self) -> None:
|
|
861
|
+
self.push_screen(HelpScreen(self.settings.keybindings))
|
|
862
|
+
|
|
863
|
+
def action_refresh(self) -> None:
|
|
864
|
+
self.git.invalidate()
|
|
865
|
+
self._last_git = 0.0
|
|
866
|
+
self._last_logs = 0.0
|
|
867
|
+
self.poll_now()
|
|
868
|
+
self.notify("Refreshing…", timeout=1.5)
|
|
869
|
+
|
|
870
|
+
def action_search(self) -> None:
|
|
871
|
+
if self.current_view != "dashboard":
|
|
872
|
+
self._switch("dashboard")
|
|
873
|
+
self.query_one("#agent-filter", Input).focus()
|
|
874
|
+
|
|
875
|
+
async def action_quit(self) -> None:
|
|
876
|
+
self._shutting_down = True
|
|
877
|
+
self.terminals.close_all()
|
|
878
|
+
kill_all()
|
|
879
|
+
self.exit()
|
|
880
|
+
|
|
881
|
+
# projects
|
|
882
|
+
def action_add_project(self) -> None:
|
|
883
|
+
def done(req: ProjectRequest | None) -> None:
|
|
884
|
+
if req is None:
|
|
885
|
+
return
|
|
886
|
+
try:
|
|
887
|
+
p = self.store.add_project(
|
|
888
|
+
req.name, req.path, req.group, environment=req.environment,
|
|
889
|
+
always_worktree=req.always_worktree, test_command=req.test_command,
|
|
890
|
+
instructions=req.instructions, account=req.account)
|
|
891
|
+
except Exception as exc: # noqa: BLE001 — e.g. UNIQUE constraint
|
|
892
|
+
self.notify(f"Could not add project: {exc}", severity="error")
|
|
893
|
+
return
|
|
894
|
+
self.log_activity("success", f"Added project {p.name} ({p.path})", p.id)
|
|
895
|
+
self._render_all()
|
|
896
|
+
self.poll_now()
|
|
897
|
+
|
|
898
|
+
self.push_screen(ProjectForm(list(self.settings.environments),
|
|
899
|
+
accounts=self._accounts_meta(),
|
|
900
|
+
existing_paths={p.path for p in self.projects}), done)
|
|
901
|
+
|
|
902
|
+
def action_edit_project(self) -> None:
|
|
903
|
+
project = self.selected_project
|
|
904
|
+
if project is None:
|
|
905
|
+
self.notify("Select a project first.", severity="warning")
|
|
906
|
+
return
|
|
907
|
+
|
|
908
|
+
def done(req: ProjectRequest | None) -> None:
|
|
909
|
+
if req is None:
|
|
910
|
+
return
|
|
911
|
+
self.store.update_project(
|
|
912
|
+
project.id, name=req.name, group=req.group, environment=req.environment,
|
|
913
|
+
always_worktree=req.always_worktree, test_command=req.test_command,
|
|
914
|
+
instructions=req.instructions, account=req.account)
|
|
915
|
+
self.log_activity("info", f"Updated project {req.name}", project.id)
|
|
916
|
+
self._render_all()
|
|
917
|
+
|
|
918
|
+
self.push_screen(ProjectForm(list(self.settings.environments), project,
|
|
919
|
+
accounts=self._accounts_meta()), done)
|
|
920
|
+
|
|
921
|
+
def action_remove_project(self) -> None:
|
|
922
|
+
project = self.selected_project
|
|
923
|
+
if project is None:
|
|
924
|
+
self.notify("Select a project first.", severity="warning")
|
|
925
|
+
return
|
|
926
|
+
n_tasks = sum(1 for t in self.tasks if t.project_id == project.id)
|
|
927
|
+
|
|
928
|
+
def done(ok: bool | None) -> None:
|
|
929
|
+
if ok:
|
|
930
|
+
self.store.remove_project(project.id)
|
|
931
|
+
self.log_activity("warning", f"Removed project {project.name}")
|
|
932
|
+
if self.scope.project_id == project.id:
|
|
933
|
+
self.scope = ALL
|
|
934
|
+
self._render_all()
|
|
935
|
+
|
|
936
|
+
self.push_screen(ConfirmModal(
|
|
937
|
+
f"Remove {project.name}?",
|
|
938
|
+
f"Unregisters {project.path} from the command center and deletes its "
|
|
939
|
+
f"{n_tasks} queued/historical task(s).\nNo files are touched and running "
|
|
940
|
+
"agents keep running.", "Remove"), done)
|
|
941
|
+
|
|
942
|
+
def action_pin(self) -> None:
|
|
943
|
+
project = self.selected_project
|
|
944
|
+
if project is None:
|
|
945
|
+
self.notify("Select a project first.", severity="warning")
|
|
946
|
+
return
|
|
947
|
+
self.store.update_project(project.id, pinned=not project.pinned)
|
|
948
|
+
self.notify(f"{'Unpinned' if project.pinned else 'Pinned'} {project.name}")
|
|
949
|
+
self._render_all()
|
|
950
|
+
|
|
951
|
+
# agents
|
|
952
|
+
def _open_agent_form(self, queue_only: bool) -> None:
|
|
953
|
+
if not self.projects:
|
|
954
|
+
self.notify("Register a project first.", severity="warning")
|
|
955
|
+
self.action_add_project()
|
|
956
|
+
return
|
|
957
|
+
project = self.selected_project
|
|
958
|
+
|
|
959
|
+
def done(req: AgentRequest | None) -> None:
|
|
960
|
+
if req is not None:
|
|
961
|
+
self._submit_agent(req)
|
|
962
|
+
|
|
963
|
+
names = {p.id: p.name for p in self.projects}
|
|
964
|
+
open_tasks = [
|
|
965
|
+
(f"#{t.id} {t.title[:40]} · {names.get(t.project_id, '?')} ({t.status.value})", t.id)
|
|
966
|
+
for t in self.store.list_tasks()
|
|
967
|
+
if not t.status.is_terminal or t.status is TaskStatus.COMPLETED
|
|
968
|
+
][:200]
|
|
969
|
+
self.push_screen(AgentForm(
|
|
970
|
+
self.projects, project_id=project.id if project else None,
|
|
971
|
+
templates=self.store.list_templates(), queue_only=queue_only,
|
|
972
|
+
default_model=self.settings.default_model,
|
|
973
|
+
default_mode=self.settings.default_permission_mode,
|
|
974
|
+
open_tasks=open_tasks, accounts=self._accounts_meta(),
|
|
975
|
+
account=(project.account if project and project.account in self.fleet.members
|
|
976
|
+
else self.fleet.active_name)), done)
|
|
977
|
+
|
|
978
|
+
def action_new_agent(self) -> None:
|
|
979
|
+
self._open_agent_form(queue_only=False)
|
|
980
|
+
|
|
981
|
+
def action_new_task(self) -> None:
|
|
982
|
+
self._open_agent_form(queue_only=True)
|
|
983
|
+
|
|
984
|
+
@work(group="launch", exclusive=False)
|
|
985
|
+
async def _submit_agent(self, req: AgentRequest) -> None:
|
|
986
|
+
project = self._project(req.project_id)
|
|
987
|
+
if project is None:
|
|
988
|
+
return
|
|
989
|
+
task = self.store.add_task(
|
|
990
|
+
project.id, req.title, req.prompt, priority=req.priority, model=req.model,
|
|
991
|
+
permission_mode=req.permission_mode, use_worktree=req.use_worktree,
|
|
992
|
+
depends_on=req.depends_on, account=req.account)
|
|
993
|
+
prereq = self.store.get_task(req.depends_on) if req.depends_on else None
|
|
994
|
+
if prereq is not None and prereq.status is not TaskStatus.COMPLETED:
|
|
995
|
+
self.log_activity("info", f"Queued task #{task.id} {task.title!r} to run after "
|
|
996
|
+
f"#{prereq.id}", project.id)
|
|
997
|
+
self.notify(f"Queued: starts when task #{prereq.id} completes.")
|
|
998
|
+
self.poll_now()
|
|
999
|
+
return
|
|
1000
|
+
await self._start_or_queue(task, project, queue_only=req.queue_only)
|
|
1001
|
+
|
|
1002
|
+
async def _start_or_queue(self, task: Task, project: Project, *, queue_only: bool) -> None:
|
|
1003
|
+
"""Launch now if capacity allows (explicit user action), else queue."""
|
|
1004
|
+
if queue_only:
|
|
1005
|
+
self.log_activity("info", f"Queued task #{task.id} {task.title!r}", project.id)
|
|
1006
|
+
if self.settings.dispatch_paused:
|
|
1007
|
+
self.notify("Queued. Dispatch is paused (z to resume).")
|
|
1008
|
+
self.poll_now()
|
|
1009
|
+
return
|
|
1010
|
+
if not self.snapshot.ok:
|
|
1011
|
+
self.log_activity("warning", f"Session list unavailable; queued task #{task.id} "
|
|
1012
|
+
"until capacity can be checked", project.id)
|
|
1013
|
+
self.notify("Can't verify capacity right now — queued instead.",
|
|
1014
|
+
severity="warning")
|
|
1015
|
+
return
|
|
1016
|
+
if self.dispatcher.free_slots(self.snapshot) <= 0:
|
|
1017
|
+
self.log_activity(
|
|
1018
|
+
"warning",
|
|
1019
|
+
f"At capacity ({self.settings.max_concurrent_agents} active); queued task "
|
|
1020
|
+
f"#{task.id} instead of launching", project.id)
|
|
1021
|
+
self.notify("At max concurrent agents — queued instead.", severity="warning")
|
|
1022
|
+
return
|
|
1023
|
+
if task.is_follow_up and not self.dispatcher.follow_up_ready(task, self.snapshot):
|
|
1024
|
+
self.notify("That agent is busy; the follow-up starts when it finishes.")
|
|
1025
|
+
self.poll_now()
|
|
1026
|
+
return
|
|
1027
|
+
self.notify("Sending follow-up…" if task.is_follow_up
|
|
1028
|
+
else f"Launching agent in {project.name}…", timeout=2)
|
|
1029
|
+
ev = await self.dispatcher.launch(task, project, self.snapshot)
|
|
1030
|
+
self.log_activity(ev.level, ev.message, ev.project_id)
|
|
1031
|
+
if ev.level == "error":
|
|
1032
|
+
self.notify(redact(ev.message), severity="error", timeout=8)
|
|
1033
|
+
self.poll_now()
|
|
1034
|
+
|
|
1035
|
+
def action_follow_up(self) -> None:
|
|
1036
|
+
agent = self._require_agent()
|
|
1037
|
+
if agent is None:
|
|
1038
|
+
return
|
|
1039
|
+
if agent.status.is_active:
|
|
1040
|
+
self.notify(f"{agent.display_name} is {agent.status.label.lower()}. Attach (Enter) "
|
|
1041
|
+
"to talk to a running session.", severity="warning")
|
|
1042
|
+
return
|
|
1043
|
+
if not agent.session_id:
|
|
1044
|
+
self.notify("Claude Code didn't report a session id for this agent.",
|
|
1045
|
+
severity="warning")
|
|
1046
|
+
return
|
|
1047
|
+
project = self._project(agent.project_id)
|
|
1048
|
+
if project is None:
|
|
1049
|
+
self.notify("Register this agent's project (p) to send follow-ups.",
|
|
1050
|
+
severity="warning")
|
|
1051
|
+
return
|
|
1052
|
+
last = next((t for t in self.tasks if t.id == agent.task_id), None)
|
|
1053
|
+
|
|
1054
|
+
def done(prompt: str | None) -> None:
|
|
1055
|
+
if prompt:
|
|
1056
|
+
self._submit_follow_up(agent, project, prompt)
|
|
1057
|
+
|
|
1058
|
+
self.push_screen(FollowUpForm(agent.display_name, project.name,
|
|
1059
|
+
last.title if last else None,
|
|
1060
|
+
will_stop=agent.pid is not None), done)
|
|
1061
|
+
|
|
1062
|
+
@work(group="launch", exclusive=False)
|
|
1063
|
+
async def _submit_follow_up(self, agent: Agent, project: Project, prompt: str) -> None:
|
|
1064
|
+
first = prompt.splitlines()[0]
|
|
1065
|
+
task = self.store.add_task(
|
|
1066
|
+
project.id, f"Follow-up: {first[:50]}", prompt, priority=1,
|
|
1067
|
+
resume_session=agent.session_id, resume_agent=agent.id, account=agent.account)
|
|
1068
|
+
await self._start_or_queue(task, project, queue_only=False)
|
|
1069
|
+
|
|
1070
|
+
def action_attach(self) -> None:
|
|
1071
|
+
agent = self._require_agent()
|
|
1072
|
+
if agent is None:
|
|
1073
|
+
return
|
|
1074
|
+
argv = self.fleet.for_agent(agent).backend.attach_argv(agent.id or "")
|
|
1075
|
+
if argv is None:
|
|
1076
|
+
self.notify("Attach isn't available for simulated demo agents.",
|
|
1077
|
+
severity="warning")
|
|
1078
|
+
return
|
|
1079
|
+
env = {**os.environ, **self._env_for(agent)}
|
|
1080
|
+
with self.suspend():
|
|
1081
|
+
print(f"\n Attaching to {agent.display_name} — press ← on an empty prompt or "
|
|
1082
|
+
"Ctrl+Z to return.\n", flush=True)
|
|
1083
|
+
try:
|
|
1084
|
+
subprocess.run(argv, env=env, check=False)
|
|
1085
|
+
except OSError as exc:
|
|
1086
|
+
print(f"attach failed: {exc}")
|
|
1087
|
+
self.log_activity("info", f"Detached from {agent.display_name}", agent.project_id)
|
|
1088
|
+
self.poll_now()
|
|
1089
|
+
|
|
1090
|
+
def action_preview(self) -> None:
|
|
1091
|
+
if self.current_view != "dashboard":
|
|
1092
|
+
self.action_logs()
|
|
1093
|
+
return
|
|
1094
|
+
if self.selected_agent is None:
|
|
1095
|
+
self.notify("Select an agent first.", severity="warning")
|
|
1096
|
+
return
|
|
1097
|
+
self.query_one(AgentDetails).show_tab("tab-logs")
|
|
1098
|
+
self._refresh_selected_logs(force=True)
|
|
1099
|
+
|
|
1100
|
+
def action_logs(self) -> None:
|
|
1101
|
+
agent = self._require_agent()
|
|
1102
|
+
if agent is None:
|
|
1103
|
+
return
|
|
1104
|
+
env = self._env_for(agent)
|
|
1105
|
+
|
|
1106
|
+
async def load() -> str:
|
|
1107
|
+
res = await self.fleet.for_agent(agent).backend.logs(agent.id or "", env)
|
|
1108
|
+
return render_terminal(res.output, tail=5000)
|
|
1109
|
+
|
|
1110
|
+
self.push_screen(TextViewer(f"Logs · {agent.display_name}", load,
|
|
1111
|
+
live=self.settings.log_refresh_interval))
|
|
1112
|
+
|
|
1113
|
+
def action_diff(self) -> None:
|
|
1114
|
+
agent = self.selected_agent
|
|
1115
|
+
cwd = agent.cwd if agent else (self.selected_project.path
|
|
1116
|
+
if self.selected_project else None)
|
|
1117
|
+
if not cwd or not os.path.isdir(cwd):
|
|
1118
|
+
self.notify("Select an agent or project with a local directory.",
|
|
1119
|
+
severity="warning")
|
|
1120
|
+
return
|
|
1121
|
+
|
|
1122
|
+
async def load() -> str:
|
|
1123
|
+
return redact(await gitinfo.diff(cwd))
|
|
1124
|
+
|
|
1125
|
+
self.push_screen(TextViewer(f"Diff · {cwd}", load, syntax="diff"))
|
|
1126
|
+
|
|
1127
|
+
def _confirm_agent_command(self, verb: str, title: str, body: str, danger: bool) -> None:
|
|
1128
|
+
agent = self._require_agent()
|
|
1129
|
+
if agent is None:
|
|
1130
|
+
return
|
|
1131
|
+
|
|
1132
|
+
def done(ok: bool | None) -> None:
|
|
1133
|
+
if ok:
|
|
1134
|
+
self._run_agent_command(verb, agent)
|
|
1135
|
+
|
|
1136
|
+
self.push_screen(ConfirmModal(title.format(name=agent.display_name), body,
|
|
1137
|
+
verb.capitalize(), danger=danger), done)
|
|
1138
|
+
|
|
1139
|
+
def action_stop(self) -> None:
|
|
1140
|
+
self._confirm_agent_command(
|
|
1141
|
+
"stop", "Stop {name}?",
|
|
1142
|
+
"Runs `claude stop`. The conversation is kept and can be resumed or "
|
|
1143
|
+
"restarted later.", True)
|
|
1144
|
+
|
|
1145
|
+
def action_restart(self) -> None:
|
|
1146
|
+
self._confirm_agent_command(
|
|
1147
|
+
"respawn", "Restart {name}?",
|
|
1148
|
+
"Runs `claude respawn`: restarts the session and resumes its saved "
|
|
1149
|
+
"conversation.", False)
|
|
1150
|
+
|
|
1151
|
+
def action_remove_session(self) -> None:
|
|
1152
|
+
self._confirm_agent_command(
|
|
1153
|
+
"remove", "Remove {name} from the list?",
|
|
1154
|
+
"Runs `claude rm`. Claude Code refuses if its worktree has uncommitted changes "
|
|
1155
|
+
"or unpushed commits; the transcript stays available via `claude --resume`.",
|
|
1156
|
+
True)
|
|
1157
|
+
|
|
1158
|
+
@work(group="agent-cmd", exclusive=False)
|
|
1159
|
+
async def _run_agent_command(self, verb: str, agent: Agent) -> None:
|
|
1160
|
+
backend = self.fleet.for_agent(agent).backend
|
|
1161
|
+
fn = {"stop": backend.stop, "respawn": backend.respawn,
|
|
1162
|
+
"remove": backend.remove}[verb]
|
|
1163
|
+
try:
|
|
1164
|
+
res = await fn(agent.id or "", self._env_for(agent))
|
|
1165
|
+
except Exception as exc: # noqa: BLE001 — a failed command must not crash the UI
|
|
1166
|
+
res = CommandResult(False, str(exc))
|
|
1167
|
+
level = "success" if res.ok else "error"
|
|
1168
|
+
self.log_activity(level, f"{verb} {agent.display_name}: {redact(res.message)}",
|
|
1169
|
+
agent.project_id)
|
|
1170
|
+
if not res.ok:
|
|
1171
|
+
self.notify(redact(res.output.strip()) or f"{verb} failed", severity="error",
|
|
1172
|
+
timeout=10)
|
|
1173
|
+
self.poll_now()
|
|
1174
|
+
|
|
1175
|
+
def action_run_tests(self) -> None:
|
|
1176
|
+
agent = self.selected_agent
|
|
1177
|
+
project = self._project(agent.project_id) if agent else self.selected_project
|
|
1178
|
+
if project is None:
|
|
1179
|
+
self.notify("Select an agent in a registered project.", severity="warning")
|
|
1180
|
+
return
|
|
1181
|
+
if not project.test_command:
|
|
1182
|
+
self.notify(f"No test command for {project.name}. Set one with e in Projects.",
|
|
1183
|
+
severity="warning")
|
|
1184
|
+
return
|
|
1185
|
+
cwd = agent.cwd if agent and os.path.isdir(agent.cwd) else project.path
|
|
1186
|
+
self._run_tests(project, cwd)
|
|
1187
|
+
|
|
1188
|
+
@work(group="tests", exclusive=False)
|
|
1189
|
+
async def _run_tests(self, project: Project, cwd: str) -> None:
|
|
1190
|
+
self.log_activity("info", f"Running tests for {project.name}: {project.test_command}",
|
|
1191
|
+
project.id)
|
|
1192
|
+
if self.current_view == "dashboard":
|
|
1193
|
+
self.query_one(AgentDetails).show_tab("tab-tests")
|
|
1194
|
+
started = time.time()
|
|
1195
|
+
env = {**self.settings.env_for(project.environment), **project.env}
|
|
1196
|
+
if not os.path.isdir(cwd):
|
|
1197
|
+
code, out, err = 127, "", f"directory does not exist: {cwd}"
|
|
1198
|
+
else:
|
|
1199
|
+
try:
|
|
1200
|
+
code, out, err = await run(["/bin/sh", "-c", project.test_command], cwd=cwd,
|
|
1201
|
+
env=env, timeout=1800)
|
|
1202
|
+
except Exception as exc: # noqa: BLE001 — record as a failed run
|
|
1203
|
+
code, out, err = 1, "", f"could not run tests: {exc}"
|
|
1204
|
+
result = TestRun(project.test_command, cwd, code, out + err, started, time.time())
|
|
1205
|
+
self.test_runs[cwd] = result
|
|
1206
|
+
self.log_activity("success" if result.passed else "error",
|
|
1207
|
+
f"Tests {'passed' if result.passed else f'failed (exit {code})'} "
|
|
1208
|
+
f"for {project.name}", project.id)
|
|
1209
|
+
self._render_all()
|
|
1210
|
+
|
|
1211
|
+
# tasks
|
|
1212
|
+
def _selected_task(self) -> Task | None:
|
|
1213
|
+
if self.current_view != "tasks":
|
|
1214
|
+
return None
|
|
1215
|
+
tid = self.query_one(TasksView).selected_task_id
|
|
1216
|
+
return self.store.get_task(tid) if tid is not None else None
|
|
1217
|
+
|
|
1218
|
+
def action_toggle_dispatch(self) -> None:
|
|
1219
|
+
self.dispatcher.set_paused(not self.settings.dispatch_paused)
|
|
1220
|
+
state = "paused" if self.settings.dispatch_paused else "resumed"
|
|
1221
|
+
self.log_activity("warning" if self.settings.dispatch_paused else "info",
|
|
1222
|
+
f"Task dispatch {state}")
|
|
1223
|
+
if not self.demo: # demo state must not leak into the real config
|
|
1224
|
+
try:
|
|
1225
|
+
save_settings(self.settings, self.config_path)
|
|
1226
|
+
except OSError:
|
|
1227
|
+
pass
|
|
1228
|
+
self.query_one(SettingsView).sync_from_settings()
|
|
1229
|
+
self._render_all()
|
|
1230
|
+
|
|
1231
|
+
def action_retry_task(self) -> None:
|
|
1232
|
+
task = self._selected_task()
|
|
1233
|
+
if task is None:
|
|
1234
|
+
return
|
|
1235
|
+
if not task.status.is_terminal:
|
|
1236
|
+
self.notify("Only finished, failed or cancelled tasks can be retried.",
|
|
1237
|
+
severity="warning")
|
|
1238
|
+
return
|
|
1239
|
+
self.dispatcher.retry(task.id)
|
|
1240
|
+
self.log_activity("info", f"Re-queued task #{task.id}", task.project_id)
|
|
1241
|
+
self._render_all()
|
|
1242
|
+
self.poll_now()
|
|
1243
|
+
|
|
1244
|
+
def action_cancel_task(self) -> None:
|
|
1245
|
+
task = self._selected_task()
|
|
1246
|
+
if task is None:
|
|
1247
|
+
return
|
|
1248
|
+
if task.status is TaskStatus.QUEUED:
|
|
1249
|
+
self.dispatcher.cancel_queued(task.id)
|
|
1250
|
+
self.log_activity("info", f"Cancelled queued task #{task.id}", task.project_id)
|
|
1251
|
+
self._render_all()
|
|
1252
|
+
return
|
|
1253
|
+
agent = self.snapshot.find(task.agent_id, task.agent_name)
|
|
1254
|
+
if agent is None or not agent.controllable:
|
|
1255
|
+
self.notify("Task has no running session to stop.", severity="warning")
|
|
1256
|
+
return
|
|
1257
|
+
|
|
1258
|
+
def done(ok: bool | None) -> None:
|
|
1259
|
+
if ok:
|
|
1260
|
+
self._run_agent_command("stop", agent)
|
|
1261
|
+
|
|
1262
|
+
self.push_screen(ConfirmModal(f"Stop task #{task.id}?",
|
|
1263
|
+
f"Stops session {agent.display_name} (`claude stop`).",
|
|
1264
|
+
"Stop"), done)
|
|
1265
|
+
|
|
1266
|
+
def action_delete_task(self) -> None:
|
|
1267
|
+
task = self._selected_task()
|
|
1268
|
+
if task is None:
|
|
1269
|
+
return
|
|
1270
|
+
if task.status in (TaskStatus.RUNNING, TaskStatus.BLOCKED, TaskStatus.DISPATCHING):
|
|
1271
|
+
self.notify("Stop the task (c) before deleting it.", severity="warning")
|
|
1272
|
+
return
|
|
1273
|
+
|
|
1274
|
+
def done(ok: bool | None) -> None:
|
|
1275
|
+
if ok:
|
|
1276
|
+
self.store.delete_task(task.id)
|
|
1277
|
+
self._render_all()
|
|
1278
|
+
|
|
1279
|
+
self.push_screen(ConfirmModal(f"Delete task #{task.id}?", task.title, "Delete"), done)
|
|
1280
|
+
|
|
1281
|
+
def action_task_priority(self, delta: int) -> None:
|
|
1282
|
+
task = self._selected_task()
|
|
1283
|
+
if task is None:
|
|
1284
|
+
return
|
|
1285
|
+
self.store.update_task(task.id, priority=max(1, min(4, task.priority + delta)))
|
|
1286
|
+
self._render_all()
|
|
1287
|
+
|
|
1288
|
+
def action_templates(self) -> None:
|
|
1289
|
+
self.push_screen(TemplatesModal(self.store))
|
|
1290
|
+
|
|
1291
|
+
# ---------------------------------------------------------- shutdown
|
|
1292
|
+
async def on_unmount(self) -> None:
|
|
1293
|
+
self._shutting_down = True
|
|
1294
|
+
self.terminals.close_all()
|
|
1295
|
+
kill_all()
|