Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

feat(agent-core): JobHost reports why a job was stopped (#2342)

Call White9cc43fa89b0b
diff --git a/core/agent_core/agent/jobs.py b/core/agent_core/agent/jobs.py
index 192125182..5d5b86f53 100644
--- a/core/agent_core/agent/jobs.py
+++ b/core/agent_core/agent/jobs.py
@@ -71,10 +71,13 @@ class TrackingJobHost:
def output_path(self, job_id: str) -> str:
return self.host.output_path(job_id)
- async def kill(self, job_id: str) -> None:
- await self.host.kill(job_id)
+ async def kill(self, job_id: str, *, reason: str = "killed") -> None:
+ await self.host.kill(job_id, reason=reason)
self._foreground.pop(job_id, None)
+ def stop_reason(self, job_id: str) -> Optional[str]:
+ return self.host.stop_reason(job_id)
+
async def hand_over(self, job_id: str) -> str:
task = asyncio.create_task(self.host.hand_over(job_id))
try:
@@ -86,14 +89,16 @@ class TrackingJobHost:
self._foreground.pop(job_id, None)
return watch_id
- async def kill_foreground(self, session_id: str, *, tool_call_id: Optional[str] = None) -> None:
+ async def kill_foreground(
+ self, session_id: str, *, tool_call_id: Optional[str] = None, reason: str = "killed"
+ ) -> None:
failures = []
for job_id, (owner, call) in list(self._foreground.items()):
if owner != session_id or (tool_call_id is not None and call != tool_call_id):
continue
try:
if self.status(job_id).state == "running":
- await self.kill(job_id)
+ await self.kill(job_id, reason=reason)
else:
self._foreground.pop(job_id, None)
except (Exception, asyncio.CancelledError) as error:
diff --git a/core/agent_core/agent/loop.py b/core/agent_core/agent/loop.py
index 42b7125a7..3182a303a 100644
--- a/core/agent_core/agent/loop.py
+++ b/core/agent_core/agent/loop.py
@@ -381,7 +381,8 @@ class Agent:
# Sweep on every terminal path, including failed tool cleanup and
# cleanup hooks. Handed-over handles are no longer foreground.
try:
- await self.jobs.kill_foreground(self.session_id)
+ reason = "aborted" if self._outcome.reason == "aborted" or self._ctx.cancel.cancelled else "killed"
+ await self.jobs.kill_foreground(self.session_id, reason=reason)
except Exception as error:
self._outcome.foreground_leaked()
if self._consumer_closed:
@@ -709,4 +710,8 @@ class Agent:
await asyncio.gather(*(task for task in (update, execution) if task is not None), return_exceptions=True)
# Release failure is supplementary to a tool's cancellation or
# result. The final sweep alone arbitrates a still-leaked process.
- await self._cleanup(lambda: self.jobs.kill_foreground(self.session_id, tool_call_id=call.id))
+ # A user abort is recorded as such, so bash and recovery report "Command aborted".
+ release_reason = "aborted" if self._ctx.cancel.cancelled else "killed"
+ await self._cleanup(
+ lambda: self.jobs.kill_foreground(self.session_id, tool_call_id=call.id, reason=release_reason)
+ )
diff --git a/core/agent_core/tools/base.py b/core/agent_core/tools/base.py
index d17c23a73..5abe3c637 100644
--- a/core/agent_core/tools/base.py
+++ b/core/agent_core/tools/base.py
@@ -92,7 +92,16 @@ class JobHost(Protocol):
"""Raw output bytes after ``since`` and the new offset; callers normalize."""
...
- async def kill(self, job_id: str) -> None: ...
+ async def kill(self, job_id: str, *, reason: str = "killed") -> None:
+ """Terminate the job's process tree, recording ``reason`` unless a stop reason was already recorded."""
+ ...
+
+ def stop_reason(self, job_id: str) -> Optional[str]:
+ """Why the job was stopped (``timeout``, ``aborted``, ``wrapper_error``, ``killed``), or ``None``.
+
+ The first recorded reason wins; callers report a job's end from this, never from elapsed time.
+ """
+ ...
async def hand_over(self, job_id: str) -> str:
"""Give the running job to a once Watch and return the Watch id."""
diff --git a/tests/agent_core/agent/test_loop.py b/tests/agent_core/agent/test_loop.py
index c25e7e708..b945c7f9d 100644
--- a/tests/agent_core/agent/test_loop.py
+++ b/tests/agent_core/agent/test_loop.py
@@ -562,6 +562,7 @@ async def test_abort_kills_foreground_through_JobHost_but_preserves_handed_over_
events = await asyncio.wait_for(run, 1)
assert events[-1].reason == "aborted"
assert host.killed == ["job_2"]
+ assert agent.jobs.stop_reason("job_2") == "aborted" # bash reports the end from the recorded reason
assert host.status("job_1").state == "running"
assert [row.message.tool_call_id for row in await agent.store.load("session") if row.kind == "tool_result"] == [
"bg"
@@ -1135,11 +1136,11 @@ async def test_failed_foreground_kill_is_reported_and_does_not_skip_other_handle
super().__init__()
self.attempts = []
- async def kill(self, job_id):
+ async def kill(self, job_id, *, reason="killed"):
self.attempts.append(job_id)
if job_id == "job_2":
raise OSError("kill denied")
- await super().kill(job_id)
+ await super().kill(job_id, reason=reason)
host = Host()
provider = ScriptedProvider([[Done(assistant(calls=[ToolCallBlock("a", "bash")]))], [Done(assistant())]])
diff --git a/tests/agent_core/agent/test_outcomes.py b/tests/agent_core/agent/test_outcomes.py
index 07a941e44..72c235dbe 100644
--- a/tests/agent_core/agent/test_outcomes.py
+++ b/tests/agent_core/agent/test_outcomes.py
@@ -146,7 +146,7 @@ async def test_primary_outcome_cross_cleanup_keeps_reason_rows_and_event_order(
agent = agent_for(provider, store=store, hooks=[Observe()], tools=[tool])
class UnkillableHost(FakeJobHost):
- async def kill(self, job_id):
+ async def kill(self, job_id, *, reason="killed"):
raise OSError("kill failed")
if cleanup == "kill":
diff --git a/tests/agent_core/fakes.py b/tests/agent_core/fakes.py
index 1be732ab8..233c156a7 100644
--- a/tests/agent_core/fakes.py
+++ b/tests/agent_core/fakes.py
@@ -148,6 +148,7 @@ class FakeJobHost:
self.outputs: dict[str, bytes] = {}
self.starts: list[dict] = []
self.killed: list[str] = []
+ self.stop_reasons: dict[str, str] = {}
self.watches: dict[str, str] = {}
self.changed = asyncio.Event()
@@ -184,11 +185,15 @@ class FakeJobHost:
def output_path(self, job_id) -> str:
return f"/test-owned/jobs/{job_id}/output.log"
- async def kill(self, job_id) -> None:
+ async def kill(self, job_id, *, reason="killed") -> None:
self.killed.append(job_id)
+ self.stop_reasons.setdefault(job_id, reason) # the first recorded reason wins
self.states[job_id] = JobStatus("gone")
self.changed.set()
+ def stop_reason(self, job_id):
+ return self.stop_reasons.get(job_id)
+
async def hand_over(self, job_id) -> str:
watch_id = f"watch_{job_id}"
self.watches[job_id] = watch_id