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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(chat): accept terminal stop races (#1166)
Call Whitec6a421503f85
diff --git a/core/session_turns.py b/core/session_turns.pyindex dc89748f7..713ef5a23 100644--- a/core/session_turns.py+++ b/core/session_turns.py@@ -5075,12 +5075,12 @@ class SessionTurnManager:)if result.state in {"waiting_terminal", "interrupt_waiting"}:return {"ok": True, "session_id": session_id, "status": "cancel_requested"}- if result.state == "settled" and result.reason == "not_active":+ if result.state == "settled":return {"ok": True,"session_id": session_id,"status": "stale_released",- "reason": "not_active",+ "reason": result.reason or "already_terminal",}return {"ok": False,diff --git a/tests/scenarios/harness_failure_recovery/catalog.yaml b/tests/scenarios/harness_failure_recovery/catalog.yamlindex 10f3a8313..401bb47d7 100644--- a/tests/scenarios/harness_failure_recovery/catalog.yaml+++ b/tests/scenarios/harness_failure_recovery/catalog.yaml@@ -3430,6 +3430,18 @@ scenarios:# A refused Stop leaves both owners intact, and an idle backlog starts directly# without a post-submit call racing and interrupting the newly started head.test: tests/test_internal_server.py::test_agent_run_send_now_interrupts_then_dispatches_the_fifo_head+ - id: HFR-431+ name: a terminal Turn racing a successful Stop receipt remains a successful cancel+ status: covered+ kind: recovery+ layer: contract+ backend: shared+ # A backend may emit its terminal result synchronously while handling Stop.+ # The exact durable Turn can therefore settle before the interrupt owner+ # records its accepted receipt. That terminal snapshot is stronger evidence+ # than the late receipt and must clear stale UI working state, not surface a+ # false transport failure banner. A genuinely unresolved Stop remains 409.+ test: tests/test_internal_server.py::test_cancel_accepts_terminal_settlement_that_races_the_stop_receiptmanual_evidence:- local Incus backend process death after prompt acceptance- local Incus controller restart with persisted queued successordiff --git a/tests/test_internal_server.py b/tests/test_internal_server.pyindex fd57c585b..58422da9d 100644--- a/tests/test_internal_server.py+++ b/tests/test_internal_server.py@@ -32,7 +32,7 @@ from sqlalchemy import selectsys.path.insert(0, str(Path(__file__).resolve().parents[1]))from core import internal_server, session_turns-from core.run_settlement import SETTLED_BY_TERMINAL_RESULT+from core.run_settlement import SETTLED_BY_STOPPED, SETTLED_BY_TERMINAL_RESULTfrom core.services.dispatch import (SOURCE_HUMAN,SOURCE_SCHEDULED,@@ -2372,6 +2372,68 @@ def test_cancel_releases_stale_turn_when_backend_not_active(tmp_path, monkeypatcassert notices == [True]+def test_cancel_accepts_terminal_settlement_that_races_the_stop_receipt(+ tmp_path,+ monkeypatch,+):+ """HFR-431: a successful Stop may emit terminal proof before its receipt lands."""++ monkeypatch.setenv("AVIBE_HOME", str(tmp_path))+ _engine, session, turn_id = _create_active_test_turn(+ tmp_path,+ native_id="proj_terminal_stop_race",+ )+ session_id = session["id"]+ controller = _build_controller_double()+ app = internal_server.create_app(controller)+ transport = httpx.ASGITransport(app=app)++ async def _go():+ task = asyncio.create_task(asyncio.sleep(60))+ context = MessageContext(+ user_id="U",+ channel_id="C",+ platform="avibe",+ platform_specific={"agent_session_id": session_id},+ )+ app.state.in_flight_dispatches[session_id] = session_turns.Turn(+ task=task,+ context=context,+ logical_turn_id=turn_id,+ )++ async def _stop_and_settle(_context):+ terminal = controller.session_turns._terminalize_durable_turn(+ turn_id,+ "canceled",+ settled_by=SETTLED_BY_STOPPED,+ evidence_kind="test_stop_terminal",+ )+ assert terminal["changed"] is True+ return True++ controller.command_handler.handle_stop = AsyncMock(+ side_effect=_stop_and_settle+ )+ async with httpx.AsyncClient(+ transport=transport,+ base_url="http://testserver",+ ) as client:+ response = await client.post(f"/internal/cancel/{session_id}")+ task.cancel()+ await asyncio.gather(task, return_exceptions=True)+ return response++ response = asyncio.run(_go())+ assert response.status_code == 200+ assert response.json() == {+ "ok": True,+ "session_id": session_id,+ "status": "stale_released",+ "reason": "already_terminal",+ }++def test_cancel_waits_for_stale_dispatch_cleanup_before_releasing(tmp_path, monkeypatch):monkeypatch.setenv("AVIBE_HOME", str(tmp_path))_engine, session, turn_id = _create_active_test_turn(