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.

fix(opencode): propagate prewrite stop intent

cyhea1fd4392c5c
diff --git a/core/native_dispatch_phase.py b/core/native_dispatch_phase.py
index 50f37e1bf..134d042cb 100644
--- a/core/native_dispatch_phase.py
+++ b/core/native_dispatch_phase.py
@@ -9,14 +9,15 @@ DISPATCH_PHASE_KEY = "agent_dispatch_phase"
DISPATCH_EVIDENCE_KEY = "agent_dispatch_evidence"
DISPATCH_PHASE_PREWRITE = "prewrite"
DISPATCH_PHASE_ATTEMPTING = "attempting"
+DISPATCH_PREWRITE_USER_STOP_KEY = "prewrite_user_stop"
def set_dispatch_phase(
context: Any,
phase: str,
*,
- evidence: dict[str, str] | None = None,
-) -> dict[str, str]:
+ evidence: dict[str, Any] | None = None,
+) -> dict[str, Any]:
payload = dict(getattr(context, "platform_specific", None) or {})
current = payload.get(DISPATCH_EVIDENCE_KEY)
if evidence is None:
@@ -43,6 +44,29 @@ def backend_dispatch_attempted(context: Any) -> Optional[bool]:
return None
+def mark_prewrite_user_stop(context: Any) -> None:
+ """Publish user-Stop intent to the adapter before canceling its task."""
+
+ payload = dict(getattr(context, "platform_specific", None) or {})
+ evidence = payload.get(DISPATCH_EVIDENCE_KEY)
+ if not isinstance(evidence, dict):
+ evidence = {}
+ payload[DISPATCH_EVIDENCE_KEY] = evidence
+ context.platform_specific = payload
+ evidence[DISPATCH_PREWRITE_USER_STOP_KEY] = True
+
+
+def prewrite_user_stop_requested(context: Any) -> bool:
+ """Return whether the shared Turn owner canceled this prewrite task for Stop."""
+
+ payload = getattr(context, "platform_specific", None) or {}
+ evidence = payload.get(DISPATCH_EVIDENCE_KEY)
+ return bool(
+ isinstance(evidence, dict)
+ and evidence.get(DISPATCH_PREWRITE_USER_STOP_KEY) is True
+ )
+
+
def mark_backend_dispatch_attempted(context: Any) -> None:
"""Record the boundary immediately before an adapter's native write."""
diff --git a/core/session_turns.py b/core/session_turns.py
index 0ddfd3bc2..edf33d138 100644
--- a/core/session_turns.py
+++ b/core/session_turns.py
@@ -33,7 +33,10 @@ from core.message_context import (
SCHEDULED_DISPATCH_METADATA_APPLIED_KEY,
resolve_turn_sink_key,
)
-from core.native_dispatch_phase import backend_dispatch_attempted
+from core.native_dispatch_phase import (
+ backend_dispatch_attempted,
+ mark_prewrite_user_stop,
+)
from core.run_settlement import (
NON_COMPLETING_TURN_SETTLEMENTS,
SETTLEMENTS_WITHOUT_RESULT,
@@ -3660,6 +3663,7 @@ class SessionTurnManager:
return {"state": "reconciling", "reason": "prewrite_owner_changed"}
projected.cancel_settled_by = SETTLED_BY_STOPPED
+ mark_prewrite_user_stop(projected.context)
projected.task.cancel()
await asyncio.gather(projected.task, return_exceptions=True)
diff --git a/modules/agents/opencode/agent.py b/modules/agents/opencode/agent.py
index 770fcdfe5..4518f543b 100644
--- a/modules/agents/opencode/agent.py
+++ b/modules/agents/opencode/agent.py
@@ -21,7 +21,10 @@ from core.avibe_cloud import avibe_cloud_url_available
from core.backend_failure import emit_backend_failure
from core.message_output import stop_output_for, terminal_output_for
from core.processing_indicator import STOPPED_REACTION_EMOJI
-from core.native_dispatch_phase import mark_backend_dispatch_attempted
+from core.native_dispatch_phase import (
+ mark_backend_dispatch_attempted,
+ prewrite_user_stop_requested,
+)
from core.resource_governance import governor_from_controller
from core.runtime_activation import RuntimeActivationIdentity
from core.runtime_ownership import (
@@ -1326,7 +1329,7 @@ class OpenCodeAgent(OpenCodeMessageProcessorMixin, BaseAgent):
request,
terminal_emoji=(
STOPPED_REACTION_EMOJI
- if self.consume_user_stop_intent(request.base_session_id)
+ if self._claim_user_stop_receipt(request)
else None
),
)
@@ -1464,11 +1467,18 @@ class OpenCodeAgent(OpenCodeMessageProcessorMixin, BaseAgent):
request,
terminal_emoji=(
STOPPED_REACTION_EMOJI
- if self.consume_user_stop_intent(request.base_session_id)
+ if self._claim_user_stop_receipt(request)
else None
),
)
+ def _claim_user_stop_receipt(self, request: AgentRequest) -> bool:
+ """Recognize adapter-local and shared prewrite Stop ownership."""
+
+ return self.consume_user_stop_intent(
+ request.base_session_id
+ ) or prewrite_user_stop_requested(request.context)
+
def additional_steer_targets(self, session_id: str) -> list[ActiveSteerTarget]:
"""Expose restored poll owners that did not pass through AgentService."""
diff --git a/tests/test_opencode_stop_receipt.py b/tests/test_opencode_stop_receipt.py
index f0db66dd5..c9b69311e 100644
--- a/tests/test_opencode_stop_receipt.py
+++ b/tests/test_opencode_stop_receipt.py
@@ -23,6 +23,7 @@ import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from config.v2_sessions import ActivePollInfo
+from core.native_dispatch_phase import mark_prewrite_user_stop
from core.processing_indicator import STOPPED_REACTION_EMOJI, ProcessingIndicatorService
from modules.agents.opencode.agent import OpenCodeAgent
from modules.agents.opencode.poll_loop import OpenCodePollLoop
@@ -235,7 +236,7 @@ class OpenCodeStopIntentTests(unittest.IsolatedAsyncioTestCase):
release_model_hub_overlay_reservation=AsyncMock()
)
agent._remove_ack_reaction = AsyncMock()
- agent._user_stopped_sessions.add(request.base_session_id)
+ mark_prewrite_user_stop(request.context)
await agent._finish_prestart_cancellation(
request,
@@ -331,9 +332,12 @@ class OpenCodeStopReceiptTests(unittest.TestCase):
def test_cancellation_branch_reads_the_intent(self):
source = inspect.getsource(OpenCodeAgent._process_message)
cleanup = inspect.getsource(OpenCodeAgent._finish_prestart_cancellation)
+ claim = inspect.getsource(OpenCodeAgent._claim_user_stop_receipt)
self.assertIn("_finish_prestart_cancellation", source)
- self.assertIn("consume_user_stop_intent", cleanup)
+ self.assertIn("_claim_user_stop_receipt", cleanup)
+ self.assertIn("consume_user_stop_intent", claim)
+ self.assertIn("prewrite_user_stop_requested", claim)
self.assertIn("STOPPED_REACTION_EMOJI", cleanup)
def test_restored_cancellation_branch_reads_the_intent(self):
diff --git a/tests/test_session_delivery_fsm.py b/tests/test_session_delivery_fsm.py
index 1c136d128..7f99067e3 100644
--- a/tests/test_session_delivery_fsm.py
+++ b/tests/test_session_delivery_fsm.py
@@ -16,6 +16,7 @@ from core.services.dispatch import TurnDispatchOutcome
from core.native_dispatch_phase import (
DISPATCH_PHASE_ATTEMPTING,
DISPATCH_PHASE_PREWRITE,
+ prewrite_user_stop_requested,
set_dispatch_phase,
)
from core.run_settlement import (
@@ -3291,13 +3292,20 @@ def test_stop_cancels_a_starting_turn_before_native_write(managers, monkeypatch)
settle_agent_runs_without_result=settle_runs,
)
dispatch_entered = asyncio.Event()
+ stop_intent_seen: list[bool] = []
async def blocked_prewrite_dispatch(_controller, dispatch_context, *_args, **_kwargs):
- set_dispatch_phase(dispatch_context, DISPATCH_PHASE_PREWRITE)
+ dispatch_evidence = set_dispatch_phase(
+ dispatch_context,
+ DISPATCH_PHASE_PREWRITE,
+ )
dispatch_entered.set()
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
+ stop_intent_seen.append(
+ prewrite_user_stop_requested(dispatch_context)
+ )
# OpenCode absorbs cancellation after cleaning up its inner request
# task, so lock that adapter behavior into the shared Stop contract.
return TurnDispatchOutcome(
@@ -3340,6 +3348,7 @@ def test_stop_cancels_a_starting_turn_before_native_write(managers, monkeypatch)
assert "ses_fsm" not in manager.in_flight
manager.controller.command_handler.handle_stop.assert_not_awaited()
manager.controller.emit_agent_message.assert_not_awaited()
+ assert stop_intent_seen == [True]
assert settle_calls == [(["run-stop-prewrite"], SETTLED_BY_STOPPED)]
with engine.connect() as conn:
turn = delivery_store.get_turn(conn, turn_id)