forked from LangQi99/NeoFish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_manager.py
More file actions
374 lines (312 loc) · 11.6 KB
/
background_manager.py
File metadata and controls
374 lines (312 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
"""
background_manager.py - Async background task execution for NeoFish agent.
Run commands in background using asyncio tasks. A notification queue is drained
before each agent step to deliver results.
Timeline:
Agent ----[spawn A]----[spawn B]----[other work]----
| |
v v
[A runs] [B runs] (parallel)
| |
+-- notification queue --> [results injected]
"""
import os
import signal
import asyncio
import contextlib
import uuid
from typing import Optional, Dict, List, Any
from pathlib import Path
import time
from message_center import MessageCenter
# Default timeout for background tasks
BG_TASK_TIMEOUT = int(os.getenv("BG_TASK_TIMEOUT", "300")) # 5 minutes default
WORKDIR = Path(os.getenv("WORKDIR", "./workspace")).resolve()
class BackgroundManager:
"""Manages background task execution using asyncio."""
def __init__(self, workdir: Path = None, default_timeout: int = None):
"""
Initialize background manager.
Args:
workdir: Working directory for commands
default_timeout: Default timeout in seconds
"""
self.workdir = (workdir or WORKDIR).resolve()
self.default_timeout = default_timeout or BG_TASK_TIMEOUT
self.tasks: Dict[str, Dict[str, Any]] = {} # task_id -> task info
self._notification_queue: List[Dict[str, Any]] = []
self._lock = asyncio.Lock()
async def run(
self,
command: str,
timeout: int = None,
session_id: Optional[str] = None,
) -> str:
"""
Start a background async task.
Args:
command: Shell command to execute
timeout: Timeout in seconds (uses default if not specified)
Returns:
Task ID and status message
"""
task_id = str(uuid.uuid4())[:8]
use_timeout = timeout or self.default_timeout
# Initialize task record
self.tasks[task_id] = {
"status": "running",
"result": None,
"command": command,
"start_time": time.time(),
"timeout": use_timeout,
"session_id": session_id,
"cancel_requested": False,
"process": None,
"runner_task": None,
}
# Create asyncio task
runner_task = asyncio.create_task(
self._execute(task_id, command, use_timeout, session_id)
)
self.tasks[task_id]["runner_task"] = runner_task
return f"Background task {task_id} started: {command[:80]}"
async def _terminate_process(self, process, grace_period: float = 3.0) -> None:
"""Terminate a background subprocess and its child processes if possible."""
if process.returncode is not None:
return
try:
if os.name == "posix":
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
else:
process.terminate()
except ProcessLookupError:
return
try:
await asyncio.wait_for(process.wait(), timeout=grace_period)
return
except asyncio.TimeoutError:
pass
except ProcessLookupError:
return
try:
if os.name == "posix":
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
else:
process.kill()
except ProcessLookupError:
return
with contextlib.suppress(Exception):
await process.wait()
async def _execute(
self,
task_id: str,
command: str,
timeout: int,
session_id: Optional[str] = None,
):
"""
Execute a command in background (asyncio task target).
Args:
task_id: Task identifier
command: Shell command
timeout: Timeout in seconds
"""
process = None
try:
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=str(self.workdir),
env=os.environ.copy(),
start_new_session=(os.name == "posix"),
)
if task_id in self.tasks:
self.tasks[task_id]["process"] = process
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(),
timeout=timeout
)
output = (stdout.decode() + stderr.decode()).strip()
status = "completed"
except asyncio.TimeoutError:
if self.tasks.get(task_id, {}).get("cancel_requested"):
if process and process.returncode is None:
await self._terminate_process(process)
output = "Task cancelled by user"
status = "cancelled"
else:
await self._terminate_process(process, grace_period=0.5)
output = f"Error: Timeout ({timeout}s)"
status = "timeout"
except asyncio.CancelledError:
if process and process.returncode is None:
await self._terminate_process(process)
output = "Task cancelled by user"
status = "cancelled"
except Exception as e:
output = f"Error: {str(e)}"
status = "error"
# Update task record
if task_id in self.tasks:
task = self.tasks[task_id]
if task.get("cancel_requested"):
status = "cancelled"
output = output or "Task cancelled by user"
task["status"] = status
task["result"] = output or "(no output)"
task["end_time"] = time.time()
task["process"] = None
# Add to notification queue
async with self._lock:
self._notification_queue.append({
"task_id": task_id,
"status": status,
"command": command[:80],
"result": (output or "(no output)")[:500],
"elapsed": time.time() - self.tasks[task_id].get("start_time", time.time()),
"session_id": session_id,
})
if session_id:
center = MessageCenter(session_id)
await center.publish(
"background_task_update",
{
"task_id": task_id,
"status": status,
"command": command[:80],
"result": (output or "(no output)")[:500],
"elapsed": time.time()
- self.tasks[task_id].get("start_time", time.time()),
"message": (
f"[bg:{task_id}] {status} ({time.time() - self.tasks[task_id].get('start_time', time.time()):.1f}s): "
f"{command[:50]}"
),
},
)
async def check(self, task_id: Optional[str] = None) -> str:
"""
Check status of background tasks.
Args:
task_id: Specific task ID, or None to list all
Returns:
Task status information
"""
if task_id:
t = self.tasks.get(task_id)
if not t:
return f"Error: Unknown task {task_id}"
elapsed = time.time() - t.get("start_time", time.time())
result = t.get("result") or "(still running)"
return (
f"[{t['status']}] {t['command'][:60]}\n"
f"Elapsed: {elapsed:.1f}s\n"
f"Result: {result[:1000]}"
)
# List all tasks
if not self.tasks:
return "No background tasks."
lines = []
for tid, t in self.tasks.items():
elapsed = time.time() - t.get("start_time", time.time())
lines.append(f"{tid}: [{t['status']}] {t['command'][:50]} ({elapsed:.0f}s)")
return "\n".join(lines)
async def drain_notifications(
self, session_id: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
Return and clear all pending completion notifications.
Returns:
List of notification dictionaries
"""
async with self._lock:
if session_id is None:
notifs = list(self._notification_queue)
self._notification_queue.clear()
return notifs
matching = []
remaining = []
for notif in self._notification_queue:
if notif.get("session_id") == session_id:
matching.append(notif)
else:
remaining.append(notif)
self._notification_queue = remaining
notifs = matching
return notifs
def format_notifications(self, notifs: List[Dict[str, Any]]) -> str:
"""
Format notifications for injection into messages.
Args:
notifs: List of notification dicts
Returns:
Formatted string
"""
if not notifs:
return ""
lines = []
for n in notifs:
lines.append(
f"[bg:{n['task_id']}] {n['status']} ({n.get('elapsed', 0):.1f}s): "
f"{n['command'][:50]}\n Result: {n['result'][:200]}"
)
return "\n".join(lines)
async def cancel(self, task_id: str) -> str:
"""
Cancel a running background task.
Args:
task_id: Task ID to cancel
Returns:
Status message
"""
t = self.tasks.get(task_id)
if not t:
return f"Error: Unknown task {task_id}"
if t["status"] != "running":
return f"Task {task_id} is not running (status: {t['status']})"
t["cancel_requested"] = True
process = t.get("process")
runner_task = t.get("runner_task")
if process and process.returncode is None:
await self._terminate_process(process)
elif runner_task and not runner_task.done():
runner_task.cancel()
if runner_task and not runner_task.done():
with contextlib.suppress(asyncio.CancelledError):
await runner_task
t["status"] = "cancelled"
t["result"] = "Task cancelled by user"
t["end_time"] = time.time()
return f"Task {task_id} cancelled."
async def cancel_by_session(self, session_id: str) -> int:
"""Cancel all running background tasks for a specific session."""
task_ids = [
task_id
for task_id, task in self.tasks.items()
if task.get("session_id") == session_id and task.get("status") == "running"
]
for task_id in task_ids:
await self.cancel(task_id)
return len(task_ids)
async def cleanup_completed(self, max_age: int = 3600) -> int:
"""
Remove old completed/timeout/error tasks.
Args:
max_age: Maximum age in seconds for completed tasks
Returns:
Number of tasks removed
"""
now = time.time()
to_remove = []
for tid, t in self.tasks.items():
if t["status"] in ("completed", "timeout", "error", "cancelled"):
end_time = t.get("end_time", now)
if now - end_time > max_age:
to_remove.append(tid)
for tid in to_remove:
del self.tasks[tid]
return len(to_remove)
# Default instance
background_manager = BackgroundManager()