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): lock pending fork backends

cyhe7c75e0745b7
diff --git a/storage/workbench_sessions_service.py b/storage/workbench_sessions_service.py
index 90d26208a..c4270c03e 100644
--- a/storage/workbench_sessions_service.py
+++ b/storage/workbench_sessions_service.py
@@ -363,8 +363,11 @@ def update_session(
# after a crash is reset to ``idle`` on startup). Before the first turn
# nothing is strandable — a fresh session may carry a project-default
# backend (see ``create_session``) and the user can still re-route it to ANY
- # backend or clear back to the default. Within the same backend,
- # agent/model/effort changes stay allowed for the session's whole life.
+ # backend or clear back to the default. A pending fork is the exception: it
+ # has no native id yet, but its saved source-native id belongs to exactly
+ # one backend, so the fork target must stay on that source backend until the
+ # native fork binds. Within the same backend, agent/model/effort changes
+ # stay allowed for the session's whole life.
# Legacy agent-less rows whose native predates the bind-time backend
# backfill keep the old empty -> concrete "initial pin" escape (while idle)
# — the row doesn't know which backend owns its native, and locking them
@@ -372,9 +375,16 @@ def update_session(
backend_changes = agent_backend is not _UNSET and str(agent_backend or "") != str(
existing.agent_backend or ""
)
+ existing_metadata = _load_metadata(existing.metadata_json)
+ pending_fork = bool(
+ existing_metadata.get("created_via") == "session_fork"
+ and not str(existing.native_session_id or "")
+ and str(existing_metadata.get("fork_source_backend") or "")
+ )
if backend_changes and (
(str(existing.native_session_id or "") and str(existing.agent_backend or ""))
or str(existing.agent_status or "") == "running"
+ or pending_fork
):
raise SessionBackendLockedError(
session_id=session_id,
@@ -386,11 +396,10 @@ def update_session(
if title is not _UNSET:
cleaned = str(title or "").strip()
values["title"] = cleaned or None
- metadata = _load_metadata(existing.metadata_json)
# "user" (Web UI / human) or "agent" (vibe session update) — both deliberate.
- metadata["title_source"] = str(title_source or "user")
- metadata["title_user_modified_at"] = values["updated_at"]
- values["metadata_json"] = _dumps_metadata(metadata)
+ existing_metadata["title_source"] = str(title_source or "user")
+ existing_metadata["title_user_modified_at"] = values["updated_at"]
+ values["metadata_json"] = _dumps_metadata(existing_metadata)
if agent_id is not _UNSET:
values["agent_id"] = agent_id or None
if agent_name is not _UNSET:
diff --git a/tests/test_core_services_sessions.py b/tests/test_core_services_sessions.py
index 0349f218b..c5de049eb 100644
--- a/tests/test_core_services_sessions.py
+++ b/tests/test_core_services_sessions.py
@@ -451,6 +451,38 @@ def test_update_session_backend_is_free_until_native_bind(isolated_state):
assert not sessions_service.get_session(conn, sid)["agent_backend"]
+def test_update_session_pending_fork_locks_backend_until_native_bind(isolated_state):
+ """A fork target has no native id yet, but its pending fork metadata points
+ at a source native session owned by one backend. Cross-backend changes would
+ make the first turn fall back to a fresh native session, so the backend is
+ locked until the fork binds. Same-backend agent/model overrides stay allowed.
+ """
+
+ engine = create_sqlite_engine()
+ with engine.begin() as conn:
+ scope_id = _seed_avibe_scope(conn)
+ sid = sessions_service.create_session(
+ conn,
+ scope_id=scope_id,
+ agent_backend="claude",
+ agent_name="claude",
+ metadata={
+ "created_via": "session_fork",
+ "fork_source_session_id": "source-session",
+ "fork_source_native_session_id": "source-native",
+ "fork_source_backend": "claude",
+ },
+ )["id"]
+
+ sessions_service.update_session(conn, sid, agent_backend="claude", agent_name="claude-pro", model="opus")
+ assert sessions_service.get_session(conn, sid)["agent_name"] == "claude-pro"
+
+ with pytest.raises(sessions_service.SessionBackendLockedError):
+ sessions_service.update_session(conn, sid, agent_backend="codex", agent_name="codex")
+ with pytest.raises(sessions_service.SessionBackendLockedError):
+ sessions_service.update_session(conn, sid, agent_backend=None, agent_name=None)
+
+
def test_update_session_locks_backend_once_native_exists(isolated_state):
"""Once the first turn bound a native conversation the backend is pinned for
life — the native can only be resumed by the backend that created it.
diff --git a/tests/test_ui_session_stream.py b/tests/test_ui_session_stream.py
index 7f7512aae..136aec761 100644
--- a/tests/test_ui_session_stream.py
+++ b/tests/test_ui_session_stream.py
@@ -229,6 +229,41 @@ def test_fork_session_rejects_unbound_source_session(isolated_state, tmp_path):
assert response.get_json()["code"] == "session_not_bound"
+def test_patch_rejects_backend_switch_for_pending_fork(isolated_state, tmp_path):
+ from sqlalchemy import update
+
+ from storage.db import create_sqlite_engine
+ from storage.models import agent_sessions
+ from vibe.ui_server import app
+
+ _, session_id = _make_session(tmp_path)
+ engine = create_sqlite_engine()
+ with engine.begin() as conn:
+ conn.execute(
+ update(agent_sessions)
+ .where(agent_sessions.c.id == session_id)
+ .values(native_session_id="native-source-1")
+ )
+
+ client = app.test_client()
+ headers = csrf_headers(client)
+ fork_response = client.post(f"/api/sessions/{session_id}/fork", json={}, headers=headers)
+ assert fork_response.status_code == 201
+ forked_id = fork_response.get_json()["id"]
+
+ response = client.patch(
+ f"/api/sessions/{forked_id}",
+ json={"agent_backend": "codex", "agent_name": "codex"},
+ headers=headers,
+ )
+
+ assert response.status_code == 409
+ body = response.get_json()
+ assert body["code"] == "backend_locked"
+ assert body["current_backend"] == "claude"
+ assert body["requested_backend"] == "codex"
+
+
def test_chat_bootstrap_returns_first_screen_payload(isolated_state, tmp_path):
from storage import messages_service
from storage.db import create_sqlite_engine
diff --git a/ui/src/components/workbench/WorkbenchSidebar.tsx b/ui/src/components/workbench/WorkbenchSidebar.tsx
index 6bb61f0b7..d18b81e66 100644
--- a/ui/src/components/workbench/WorkbenchSidebar.tsx
+++ b/ui/src/components/workbench/WorkbenchSidebar.tsx
@@ -267,7 +267,7 @@ const SessionRow: React.FC<{
}
const displayName = session.title?.trim() || t('workbench.untitledSession');
- const canFork = !forking;
+ const canFork = !!session.native_session_id && !forking;
return (
<>
<Popover open={menuOpen} onOpenChange={setMenuOpen}>
@@ -328,6 +328,7 @@ const SessionRow: React.FC<{
<button
type="button"
disabled={!canFork}
+ title={!session.native_session_id ? t('workbench.sessionForkUnavailable') : undefined}
onClick={async () => {
if (!canFork) return;
setMenuOpen(false);
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index a3cda2610..b0e290ebe 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -584,6 +584,7 @@
"sessionRenamePlaceholder": "Session name",
"sessionReference": "Reference this session",
"sessionFork": "Fork session",
+ "sessionForkUnavailable": "This session has no native agent session to fork yet.",
"sessionArchive": "Archive session",
"archiveSession": {
"title": "Archive this session permanently?",
diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.json
index f5762540c..7d166011d 100644
--- a/ui/src/i18n/zh.json
+++ b/ui/src/i18n/zh.json
@@ -584,6 +584,7 @@
"sessionRenamePlaceholder": "会话名称",
"sessionReference": "引用该会话",
"sessionFork": "Fork 会话",
+ "sessionForkUnavailable": "此会话还没有可 fork 的原生 Agent 会话。",
"sessionArchive": "归档会话",
"archiveSession": {
"title": "永久归档此会话?",