Background job management for long-running :class:WorkerAgent runs.
Phase 3 of AGENT_REFACTOR_PLAN.md. Some agents — fuzzers, ROM
disassemblers, brute-forcers — take minutes to hours to complete. The
supervisor console needs to spawn these as background tasks, poll their
status, and kill them on demand, all without blocking the REPL.
The :class:AgentJobManager is a thin wrapper around :func:asyncio.
create_task that tracks the lifecycle of every spawned agent run. Each
job entry is exactly the shape the refactor plan specifies::
{job_id: {"task": asyncio.Task, "status": "running|completed|failed",
"output_buffer": str, ...}}
Status transitions:
running — set immediately on spawn.
completed — agent.run() returned normally; output_buffer holds the
final assistant content.
failed — agent.run() raised (including cancellation via
:meth:kill_job); output_buffer holds the error message.
Only the three statuses called out in the plan are used; cancellation is
folded into failed and disambiguated through output_buffer.
AgentJobManager
Tracks background :class:WorkerAgent runs as asyncio tasks.
Source code in wintermute/ai/jobs.py
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 | class AgentJobManager:
"""Tracks background :class:`WorkerAgent` runs as asyncio tasks."""
def __init__(self) -> None:
self._jobs: Dict[str, Dict[str, Any]] = {}
self._lock = asyncio.Lock()
# -- spawn ---------------------------------------------------------------
async def spawn_job(self, agent: WorkerAgent) -> str:
"""Schedule ``agent.run()`` as a background task; return a job id.
Returns immediately; the agent runs concurrently. Use
:meth:`get_status` to poll completion and read the output buffer.
"""
job_id = uuid.uuid4().hex[:12]
task = asyncio.create_task(
self._run_and_record(job_id, agent), name=f"agent-job-{job_id}"
)
async with self._lock:
self._jobs[job_id] = {
"task": task,
"status": "running",
"output_buffer": "",
"agent_name": agent.name,
"started_at": time.time(),
"finished_at": None,
}
return job_id
async def _run_and_record(self, job_id: str, agent: WorkerAgent) -> None:
"""Internal task body. Catches everything so the status dict is
always populated, even on cancellation or hard failures.
"""
try:
output = await agent.run()
except asyncio.CancelledError:
async with self._lock:
if job_id in self._jobs:
self._jobs[job_id]["status"] = "failed"
self._jobs[job_id]["output_buffer"] = (
"Cancelled by kill_job before completion."
)
self._jobs[job_id]["finished_at"] = time.time()
# Re-raise so the Task object's state reflects cancellation
# rather than being silently swallowed (callers awaiting the
# task directly still see CancelledError).
raise
except Exception as exc:
log.exception("Agent job %s raised", job_id)
async with self._lock:
if job_id in self._jobs:
self._jobs[job_id]["status"] = "failed"
self._jobs[job_id]["output_buffer"] = (
f"Agent raised {type(exc).__name__}: {exc}"
)
self._jobs[job_id]["finished_at"] = time.time()
return
async with self._lock:
if job_id in self._jobs:
self._jobs[job_id]["status"] = "completed"
self._jobs[job_id]["output_buffer"] = output
self._jobs[job_id]["finished_at"] = time.time()
# -- introspection -------------------------------------------------------
async def get_status(self, job_id: str) -> Optional[Dict[str, Any]]:
"""Return a snapshot of the job, or ``None`` if unknown.
The returned dict excludes the live :class:`asyncio.Task` handle so
it is safe to serialize / pretty-print from the console.
"""
async with self._lock:
job = self._jobs.get(job_id)
if job is None:
return None
return {
"job_id": job_id,
"status": job["status"],
"agent_name": job.get("agent_name", ""),
"output_buffer": job["output_buffer"],
"started_at": job.get("started_at"),
"finished_at": job.get("finished_at"),
}
async def list_jobs(self) -> List[Dict[str, Any]]:
"""Return a snapshot list of every tracked job."""
async with self._lock:
ids = list(self._jobs.keys())
# Resolve each status outside the lock to avoid holding it across awaits.
out: List[Dict[str, Any]] = []
for jid in ids:
snap = await self.get_status(jid)
if snap is not None:
out.append(snap)
return out
# -- termination ---------------------------------------------------------
async def kill_job(self, job_id: str) -> bool:
"""Cancel a running job. Returns ``True`` iff cancellation was
actually issued (job exists, was still running).
After a successful kill the job's status will read ``"failed"``
with ``output_buffer="Cancelled by kill_job before completion."``,
per the status-set documented at module top.
"""
async with self._lock:
job = self._jobs.get(job_id)
if job is None:
return False
task: asyncio.Task[None] = job["task"]
if task.done():
return False
task.cancel()
try:
await task
except (asyncio.CancelledError, Exception):
# _run_and_record already updated the job record; the
# re-raised CancelledError lands here and is intentionally
# swallowed because the caller wants the kill, not the trace.
pass
return True
async def shutdown(self) -> None:
"""Cancel every running job and await their teardown.
Called by the console on exit so we never leak background agents
past the REPL's lifetime.
"""
async with self._lock:
running = [
(jid, job["task"])
for jid, job in self._jobs.items()
if not job["task"].done()
]
for _, task in running:
task.cancel()
if running:
await asyncio.gather(*(t for _, t in running), return_exceptions=True)
|
get_status(job_id)
async
Return a snapshot of the job, or None if unknown.
The returned dict excludes the live :class:asyncio.Task handle so
it is safe to serialize / pretty-print from the console.
Source code in wintermute/ai/jobs.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 | async def get_status(self, job_id: str) -> Optional[Dict[str, Any]]:
"""Return a snapshot of the job, or ``None`` if unknown.
The returned dict excludes the live :class:`asyncio.Task` handle so
it is safe to serialize / pretty-print from the console.
"""
async with self._lock:
job = self._jobs.get(job_id)
if job is None:
return None
return {
"job_id": job_id,
"status": job["status"],
"agent_name": job.get("agent_name", ""),
"output_buffer": job["output_buffer"],
"started_at": job.get("started_at"),
"finished_at": job.get("finished_at"),
}
|
kill_job(job_id)
async
Cancel a running job. Returns True iff cancellation was
actually issued (job exists, was still running).
After a successful kill the job's status will read "failed"
with output_buffer="Cancelled by kill_job before completion.",
per the status-set documented at module top.
Source code in wintermute/ai/jobs.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | async def kill_job(self, job_id: str) -> bool:
"""Cancel a running job. Returns ``True`` iff cancellation was
actually issued (job exists, was still running).
After a successful kill the job's status will read ``"failed"``
with ``output_buffer="Cancelled by kill_job before completion."``,
per the status-set documented at module top.
"""
async with self._lock:
job = self._jobs.get(job_id)
if job is None:
return False
task: asyncio.Task[None] = job["task"]
if task.done():
return False
task.cancel()
try:
await task
except (asyncio.CancelledError, Exception):
# _run_and_record already updated the job record; the
# re-raised CancelledError lands here and is intentionally
# swallowed because the caller wants the kill, not the trace.
pass
return True
|
list_jobs()
async
Return a snapshot list of every tracked job.
Source code in wintermute/ai/jobs.py
153
154
155
156
157
158
159
160
161
162
163 | async def list_jobs(self) -> List[Dict[str, Any]]:
"""Return a snapshot list of every tracked job."""
async with self._lock:
ids = list(self._jobs.keys())
# Resolve each status outside the lock to avoid holding it across awaits.
out: List[Dict[str, Any]] = []
for jid in ids:
snap = await self.get_status(jid)
if snap is not None:
out.append(snap)
return out
|
shutdown()
async
Cancel every running job and await their teardown.
Called by the console on exit so we never leak background agents
past the REPL's lifetime.
Source code in wintermute/ai/jobs.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 | async def shutdown(self) -> None:
"""Cancel every running job and await their teardown.
Called by the console on exit so we never leak background agents
past the REPL's lifetime.
"""
async with self._lock:
running = [
(jid, job["task"])
for jid, job in self._jobs.items()
if not job["task"].done()
]
for _, task in running:
task.cancel()
if running:
await asyncio.gather(*(t for _, t in running), return_exceptions=True)
|
spawn_job(agent)
async
Schedule agent.run() as a background task; return a job id.
Returns immediately; the agent runs concurrently. Use
:meth:get_status to poll completion and read the output buffer.
Source code in wintermute/ai/jobs.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 | async def spawn_job(self, agent: WorkerAgent) -> str:
"""Schedule ``agent.run()`` as a background task; return a job id.
Returns immediately; the agent runs concurrently. Use
:meth:`get_status` to poll completion and read the output buffer.
"""
job_id = uuid.uuid4().hex[:12]
task = asyncio.create_task(
self._run_and_record(job_id, agent), name=f"agent-job-{job_id}"
)
async with self._lock:
self._jobs[job_id] = {
"task": task,
"status": "running",
"output_buffer": "",
"agent_name": agent.name,
"started_at": time.time(),
"finished_at": None,
}
return job_id
|