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(workbench): round-21 — P1 Claude/Codex bind to reserved session + P2 guard patch by route

cyh3bf44856c7ce
diff --git a/modules/agents/base.py b/modules/agents/base.py
index 81b9138b9..b00c7e92b 100644
--- a/modules/agents/base.py
+++ b/modules/agents/base.py
@@ -126,6 +126,52 @@ class BaseAgent(ABC):
request.context.platform_specific = payload
return agent_session_id
+ def _bind_reserved_workbench_session(
+ self,
+ context: Any,
+ native_session_id: Any,
+ *,
+ working_path: Optional[str] = None,
+ ) -> Optional[str]:
+ """Bind the backend-native id to the RESERVED workbench session row, by id.
+
+ An avibe turn stamps ``platform_specific['agent_session_target']['id']``
+ with the open Chat session's ``agent_sessions`` PK. Claude/Codex must bind
+ the native session to THAT row (like OpenCode's ``bind_agent_session_by_id``)
+ instead of letting the generic ``bind_agent_session`` mint a fresh row and
+ overwrite ``agent_session_id`` — otherwise ``persist_agent_message`` would
+ publish ``message.new`` under the new hidden id and the reply would never
+ reach the open Chat page (Codex P1). Returns the reserved id when this is an
+ avibe turn (so the caller skips its normal binder), else ``None``.
+ """
+ payload = getattr(context, "platform_specific", None) or {}
+ target = payload.get("agent_session_target")
+ reserved_id = ""
+ if isinstance(target, dict) and target.get("id"):
+ reserved_id = str(target["id"]).strip()
+ if not reserved_id:
+ return None
+ sessions = getattr(self, "sessions", None)
+ bind_by_id = getattr(sessions, "bind_agent_session_by_id", None)
+ bound: Optional[str] = None
+ if callable(bind_by_id):
+ try:
+ bound = bind_by_id(
+ session_id=reserved_id,
+ native_session_id=native_session_id,
+ workdir=working_path,
+ )
+ except Exception:
+ logger.debug("bind_agent_session_by_id failed; keeping reserved id", exc_info=True)
+ # Keep ``agent_session_id`` pinned to the reserved workbench row even if the
+ # by-id bind couldn't record the native id — the publish target is what
+ # matters for the open Chat page.
+ new_id = bound or reserved_id
+ new_payload = dict(payload)
+ new_payload["agent_session_id"] = new_id
+ context.platform_specific = new_payload
+ return new_id
+
def bind_agent_session_id(
self,
request: AgentRequest,
@@ -135,6 +181,13 @@ class BaseAgent(ABC):
) -> Optional[str]:
"""Bind a backend-native session id to the existing Vibe session row."""
anchor = session_anchor or request.base_session_id
+ # avibe: bind to the reserved workbench row by id (mirrors OpenCode) so the
+ # reply publishes under the open Chat session, not a new hidden row (P1).
+ reserved = self._bind_reserved_workbench_session(
+ request.context, native_session_id, working_path=getattr(request, "working_path", None)
+ )
+ if reserved:
+ return reserved
sessions = getattr(self, "sessions", None)
binder = getattr(sessions, "bind_agent_session", None)
if callable(binder):
diff --git a/modules/agents/claude_agent.py b/modules/agents/claude_agent.py
index 4826b1297..6d4027f21 100644
--- a/modules/agents/claude_agent.py
+++ b/modules/agents/claude_agent.py
@@ -787,6 +787,13 @@ class ClaudeAgent(BaseAgent):
):
session_id = message.data.get("session_id")
if session_id:
+ # avibe: bind the native id to the RESERVED workbench session row so
+ # the reply publishes under the open Chat session, not a freshly
+ # minted hidden row (Codex P1). Mirrors OpenCode / the Codex base
+ # path; falls through to the normal binder for IM turns.
+ reserved = self._bind_reserved_workbench_session(context, session_id)
+ if reserved:
+ return session_id
binder = getattr(self.session_handler, "bind_agent_session_id", None)
if callable(binder):
agent_session_id = binder(
diff --git a/tests/test_claude_agent_sessions.py b/tests/test_claude_agent_sessions.py
index 3ade78e36..5a2bea543 100644
--- a/tests/test_claude_agent_sessions.py
+++ b/tests/test_claude_agent_sessions.py
@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMock
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+from modules.agents.base import BaseAgent
from modules.agents.claude_agent import ClaudeAgent
@@ -850,5 +851,47 @@ class AdoptPendingTurnTokenTests(unittest.TestCase):
self.assertEqual(ctx.platform_specific["turn_token"], "T1")
+class BindReservedWorkbenchSessionTests(unittest.TestCase):
+ """``BaseAgent._bind_reserved_workbench_session`` keeps Claude/Codex avibe
+ replies attributed to the OPEN Chat session (the reserved workbench row),
+ instead of a freshly-minted hidden row, so ``message.new`` reaches the page
+ (Codex P1)."""
+
+ @staticmethod
+ def _ctx(target_id):
+ spec = {"agent_session_id": "from_build"}
+ if target_id:
+ spec["agent_session_target"] = {"id": target_id}
+ return SimpleNamespace(platform_specific=spec)
+
+ def test_avibe_turn_binds_by_reserved_id_and_pins_agent_session_id(self):
+ calls = {}
+
+ def bind_by_id(*, session_id, native_session_id, workdir=None):
+ calls.update(session_id=session_id, native=native_session_id, workdir=workdir)
+ return session_id # the reserved row exists → rowcount 1
+
+ fake_self = SimpleNamespace(sessions=SimpleNamespace(bind_agent_session_by_id=bind_by_id))
+ ctx = self._ctx("ses_workbench")
+ ret = BaseAgent._bind_reserved_workbench_session(fake_self, ctx, "claude-native-123", working_path="/tmp/x")
+ self.assertEqual(ret, "ses_workbench")
+ self.assertEqual(ctx.platform_specific["agent_session_id"], "ses_workbench")
+ self.assertEqual(calls, {"session_id": "ses_workbench", "native": "claude-native-123", "workdir": "/tmp/x"})
+
+ def test_im_turn_without_target_falls_through(self):
+ fake_self = SimpleNamespace(sessions=SimpleNamespace(bind_agent_session_by_id=lambda **k: None))
+ ctx = self._ctx(None)
+ self.assertIsNone(BaseAgent._bind_reserved_workbench_session(fake_self, ctx, "native"))
+ # untouched → caller runs its normal binder
+ self.assertEqual(ctx.platform_specific["agent_session_id"], "from_build")
+
+ def test_pins_reserved_id_even_without_bind_by_id_support(self):
+ fake_self = SimpleNamespace(sessions=SimpleNamespace()) # no bind_agent_session_by_id
+ ctx = self._ctx("ses_wb")
+ ret = BaseAgent._bind_reserved_workbench_session(fake_self, ctx, "native")
+ self.assertEqual(ret, "ses_wb")
+ self.assertEqual(ctx.platform_specific["agent_session_id"], "ses_wb")
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/ui/src/components/workbench/ChatPage.tsx b/ui/src/components/workbench/ChatPage.tsx
index e565ed94d..c2295ac81 100644
--- a/ui/src/components/workbench/ChatPage.tsx
+++ b/ui/src/components/workbench/ChatPage.tsx
@@ -571,11 +571,18 @@ export const ChatPage: React.FC = () => {
const patch = useCallback(
async (changes: Partial<WorkbenchSession>) => {
if (!session) return;
+ const patchedId = session.id;
try {
const updated = await api.updateSession(session.id, changes as any);
+ // Drop a stale response after a chat switch: if the user navigated to a
+ // different chat (this ChatPage instance is reused) before the PATCH
+ // resolved, installing A's session into B would show A's title/picker on
+ // B and make later edits patch the wrong session.id (Codex P2). Mirrors
+ // the sessionIdRef guards on send/cancel.
+ if (patchedId !== sessionIdRef.current) return;
setSession(updated);
} catch (err: any) {
- setError(err?.message ?? String(err));
+ if (patchedId === sessionIdRef.current) setError(err?.message ?? String(err));
}
},
[api, session],