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(ui): Show Page toggle round-2 review (atomicity, busy, bridge, i18n)

cyh2515cf1fc86c
diff --git a/core/show_pages.py b/core/show_pages.py
index a4e982dc1..0a8a3da15 100644
--- a/core/show_pages.py
+++ b/core/show_pages.py
@@ -245,6 +245,54 @@ class ShowPageStore:
)
return page
+ def ensure_active(self, session_id: str) -> tuple[ShowPage, bool]:
+ """Atomically ensure a page for a NON-archived session; return (page, created).
+
+ The existing-row check, the archived check and the insert all run in ONE
+ transaction so (a) a concurrent archive can't race the create (TOCTOU),
+ and (b) ``created`` is derived from the insert itself — concurrent
+ first-ensures don't both report "created" (which would otherwise double-
+ send the visualize prompt or collide on the unique key). An existing page
+ is returned untouched (archive already took it offline).
+ """
+ session_id = validate_session_id(session_id)
+ now = _utc_now_iso()
+ with self.engine.begin() as conn:
+ existing = (
+ conn.execute(select(show_pages).where(show_pages.c.session_id == session_id).limit(1))
+ .mappings()
+ .first()
+ )
+ if existing is not None:
+ return _page_from_row(existing), False
+ status = conn.execute(
+ select(agent_sessions.c.status).where(agent_sessions.c.id == session_id)
+ ).scalar_one_or_none()
+ if status == "archived":
+ raise ShowPageError(
+ "Cannot create a Show Page for an archived session.",
+ code="session_archived",
+ )
+ result = conn.execute(
+ insert(show_pages)
+ .prefix_with("OR IGNORE")
+ .values(
+ session_id=session_id,
+ visibility=VISIBILITY_PRIVATE,
+ share_id=None,
+ offline_at=None,
+ created_at=now,
+ updated_at=now,
+ )
+ )
+ created = bool(result.rowcount and result.rowcount > 0)
+ row = (
+ conn.execute(select(show_pages).where(show_pages.c.session_id == session_id).limit(1))
+ .mappings()
+ .first()
+ )
+ return _page_from_row(row), created
+
def _is_archived(self, session_id: str) -> bool:
with self.engine.connect() as conn:
status = conn.execute(
diff --git a/ui/src/components/workbench/ChatPage.tsx b/ui/src/components/workbench/ChatPage.tsx
index cf43139cd..a72fcaa9c 100644
--- a/ui/src/components/workbench/ChatPage.tsx
+++ b/ui/src/components/workbench/ChatPage.tsx
@@ -108,32 +108,37 @@ export const ChatPage: React.FC = () => {
{ disabled: !sessionId },
);
- // Publish this chat's composer to the ComposerBridge while it's mounted, so
- // the sidebar's "reference this session" action can insert a #<session>
- // mention into the open chat's input. Null target (no sessionId) = no chat
- // open, which hides that sidebar action.
- const insertSessionReference = useCallback(
- (refSessionId: string, title?: string | null) =>
- composerRef.current?.insertSessionReference(refSessionId, title),
- [],
- );
- const composerTarget = useMemo<ComposerInsertTarget | null>(
- () => (sessionId ? { sessionId, insertSessionReference } : null),
- [sessionId, insertSessionReference],
- );
- useRegisterComposerTarget(composerTarget);
-
// Show Page toggle: swap the chat surface (transcript + composer, NOT the
- // header bar) for this session's Show Page in an iframe, and back. Resets to
- // chat view on session change so a freshly-opened chat starts in chat mode.
+ // header bar) for this session's Show Page in an iframe, and back. Declared
+ // before the composer bridge target, which depends on showPageMode.
const [showPageMode, setShowPageMode] = useState(false);
const [showPageBusy, setShowPageBusy] = useState(false);
const [showPageUrl, setShowPageUrl] = useState<string | null>(null);
useEffect(() => {
+ // ChatPage is reused across :sessionId — clear all show-page state so the
+ // next chat starts in chat view with a live (not stuck-busy) toggle.
setShowPageMode(false);
setShowPageUrl(null);
+ setShowPageBusy(false);
}, [sessionId]);
+ // Publish this chat's composer to the ComposerBridge so the sidebar's
+ // "reference this session" action can insert a #<session> mention into the
+ // open chat's input.
+ const insertSessionReference = useCallback(
+ (refSessionId: string, title?: string | null) =>
+ composerRef.current?.insertSessionReference(refSessionId, title),
+ [],
+ );
+ // Null target hides that sidebar action — when no chat is open (no sessionId)
+ // OR while the Show Page iframe has replaced the composer (it's unmounted, so
+ // an insert would silently no-op).
+ const composerTarget = useMemo<ComposerInsertTarget | null>(
+ () => (sessionId && !showPageMode ? { sessionId, insertSessionReference } : null),
+ [sessionId, showPageMode, insertSessionReference],
+ );
+ useRegisterComposerTarget(composerTarget);
+
// Back returns to the page the user came from, not a hardcoded inbox.
// location.key === 'default' means /chat was the first history entry (deep
// link / refresh) with nothing to pop back to — fall back to the inbox then.
@@ -757,16 +762,19 @@ export const ChatPage: React.FC = () => {
);
// Only a freshly-created page needs the agent to build the visualization.
if (res.existed === false) {
- void sendMessage('Please visualize this session as a Show Page.');
+ void sendMessage(t('chat.showPage.prompt'));
}
setShowPageMode(true);
}
} catch {
// apiFetch already surfaced a toast; stay in chat view.
} finally {
- if (sessionIdRef.current === sid) setShowPageBusy(false);
+ // Always clear — the in-flight request is done regardless of which chat is
+ // now mounted (ChatPage is reused across sessions; a guarded clear would
+ // strand the shared busy flag on a session the user switched to).
+ setShowPageBusy(false);
}
- }, [sessionId, showPageMode, api, sendMessage]);
+ }, [sessionId, showPageMode, api, sendMessage, t]);
// A quick-reply click sends the chosen label as a normal user turn, tagged with
// the agent message it answers so the group can lock + highlight the choice on
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index 30c3e12a0..69ebca198 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -1758,7 +1758,8 @@
"showPage": {
"open": "View Show Page",
"backToChat": "Back to chat",
- "title": "Show Page"
+ "title": "Show Page",
+ "prompt": "Please visualize this session as a Show Page."
},
"titlePlaceholder": "Session title",
"pickAgent": "Pick an agent",
diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.json
index decce4147..1cfa5253c 100644
--- a/ui/src/i18n/zh.json
+++ b/ui/src/i18n/zh.json
@@ -1757,7 +1757,8 @@
"showPage": {
"open": "查看 Show Page",
"backToChat": "返回对话",
- "title": "Show Page"
+ "title": "Show Page",
+ "prompt": "Please visualize this session as a Show Page."
},
"titlePlaceholder": "会话标题",
"pickAgent": "选择 Agent",
diff --git a/vibe/api.py b/vibe/api.py
index 16e9835cc..8d9f1a5be 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -1008,26 +1008,20 @@ def ensure_show_page(session_id: str) -> dict:
workbench can ALSO send the "visualize this session" prompt only on first
creation. Mirrors the CLI ``vibe show path`` (ensure + return).
"""
- from core.show_pages import ShowPageError, ShowPageStore, show_page_payload
+ from core.show_pages import ShowPageStore, show_page_payload
config = V2Config.load()
store = ShowPageStore()
try:
- existed = store.get(session_id) is not None
- # Don't materialize a fresh page row for an archived (terminal) session —
- # /show/<id>/ serves private pages without re-checking archived state, so
- # creating one would resurrect it. Mirrors update_visibility's guard. An
- # already-existing page is returned as-is (archive took it offline).
- if not existed and store._is_archived(session_id):
- raise ShowPageError(
- "Cannot create a Show Page for an archived session.",
- code="session_archived",
- )
- page = store.ensure(session_id)
+ # Atomic: refuses to create a page for an archived session and reports
+ # whether IT created the row (so the UI only prompts the agent on a real
+ # first creation, not a concurrent ensure). Raises ShowPageError for an
+ # archived session — the route maps it to a 4xx.
+ page, created = store.ensure_active(session_id)
payload = show_page_payload(page, config=config)
finally:
store.close()
- return {"ok": True, "existed": existed, **_apply_session_meta([payload])[0]}
+ return {"ok": True, "existed": not created, **_apply_session_meta([payload])[0]}
def rotate_show_page_share(session_id: str) -> dict: