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): mark running at turn acceptance + bypass read cache on native refresh

cyh71c26f4f85da
diff --git a/core/session_turns.py b/core/session_turns.py
index f9b655685..75b14d43a 100644
--- a/core/session_turns.py
+++ b/core/session_turns.py
@@ -317,6 +317,16 @@ class SessionTurnManager:
task = asyncio.create_task(_runner(), name="internal-dispatch-async")
if isinstance(session_id, str) and session_id:
self.in_flight[session_id] = Turn(task=task, context=context)
+ # Make the DB row authoritative at ACCEPTANCE, not at dispatch start:
+ # ``update_session``'s backend lock re-checks ``agent_status`` inside
+ # its UPDATE predicate, so writing ``running`` synchronously here —
+ # before the loop can start the dispatch task — closes the startup
+ # window where a cross-backend PATCH could land while the row still
+ # read idle (and would then be silently undone by the bind-time
+ # backfill). The inbound chokepoint's own ``running`` write becomes a
+ # no-op; every terminal path still settles the status (outbound
+ # chokepoint / cancel / startup recovery).
+ self.controller.set_agent_status(session_id, "running")
bus.publish("turn.start", {"session_id": session_id})
async def flush_queue(self, session_id: str) -> bool:
diff --git a/tests/test_controller_agent_status.py b/tests/test_controller_agent_status.py
index 447ad850d..9608628c3 100644
--- a/tests/test_controller_agent_status.py
+++ b/tests/test_controller_agent_status.py
@@ -82,3 +82,45 @@ def test_inbound_skips_non_avibe_turn():
# IM turn carries no workbench session id → the dot is never touched.
assert calls == []
+
+
+def test_run_marks_running_at_acceptance_before_dispatch(monkeypatch, tmp_path):
+ """The status flips ``running`` synchronously when the turn is ACCEPTED
+ (in_flight registration), not when dispatch later starts: update_session's
+ backend lock re-checks ``agent_status`` inside its UPDATE predicate, so the
+ accept-time write closes the startup window where a cross-backend PATCH
+ could land while the row still read idle."""
+
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ from storage.importer import ensure_sqlite_state
+
+ ensure_sqlite_state()
+
+ import core.session_turns as session_turns_module
+ from core.session_turns import SessionTurnManager
+
+ calls: list = []
+ dispatched: list = []
+ controller = SimpleNamespace(
+ _session_id_from_context=staticmethod(Controller._session_id_from_context).__func__,
+ set_agent_status=lambda sid, status: calls.append((sid, status)),
+ )
+ mgr = SessionTurnManager(controller)
+
+ async def _dispatch(controller_arg, context, text, **kwargs):
+ dispatched.append(text)
+
+ monkeypatch.setattr(session_turns_module, "dispatch_turn", _dispatch)
+
+ async def _exercise():
+ await mgr._run("ses-accept", _ctx("ses-accept"), "hi")
+ # _run returns right after acceptance; the dispatch task hasn't run yet
+ # (single-threaded loop) — the running mark must already be recorded.
+ assert ("ses-accept", "running") in calls
+ assert dispatched == []
+ turn = mgr.in_flight.get("ses-accept")
+ assert turn is not None
+ await turn.task
+
+ asyncio.run(_exercise())
+ assert dispatched == ["hi"]
diff --git a/ui/src/components/workbench/ChatPage.tsx b/ui/src/components/workbench/ChatPage.tsx
index 9a05c53ae..50391d43a 100644
--- a/ui/src/components/workbench/ChatPage.tsx
+++ b/ui/src/components/workbench/ChatPage.tsx
@@ -199,7 +199,10 @@ export const ChatPage: React.FC = () => {
const id = sessionIdRef.current;
if (!id || hasNativeRef.current) return;
try {
- const row = await api.getSession(id);
+ // cache:false — an earlier refresh (page open / reconnect) may have
+ // cached the still-native-less row; a quick turn ending inside the read
+ // cache's TTL would reuse it and leave the picker unlocked.
+ const row = await api.getSession(id, { cache: false });
setSession((prev) => (prev && prev.id === row.id && row.id === sessionIdRef.current ? row : prev));
} catch {
// Best-effort: the next recovery point retries.
diff --git a/ui/src/context/ApiContext.tsx b/ui/src/context/ApiContext.tsx
index a0d8c9a9e..9fbd5b400 100644
--- a/ui/src/context/ApiContext.tsx
+++ b/ui/src/context/ApiContext.tsx
@@ -174,7 +174,7 @@ export type ApiContextType = {
) => Promise<{ ok: boolean; backends: GlobalPromptFile[] }>;
listSessions: (params?: { projectId?: string; status?: 'active' | 'archived' | 'all'; limit?: number; beforeId?: string; q?: string; cache?: boolean }) => Promise<{ sessions: WorkbenchSession[]; next_before_id: string | null }>;
createSession: (payload: WorkbenchSessionCreate) => Promise<WorkbenchSession>;
- getSession: (sessionId: string) => Promise<WorkbenchSession>;
+ getSession: (sessionId: string, params?: { cache?: boolean }) => Promise<WorkbenchSession>;
getSessionBootstrap: (sessionId: string) => Promise<WorkbenchSessionBootstrap>;
updateSession: (sessionId: string, payload: Partial<WorkbenchSessionUpdate>) => Promise<WorkbenchSession>;
archiveSession: (sessionId: string) => Promise<WorkbenchSession>;
@@ -1672,7 +1672,10 @@ export const ApiProvider: React.FC<{ children: React.ReactNode }> = ({ children
return params?.cache === false ? getJson(path) : getCachedJson(path);
},
createSession: (payload) => postJson('/api/sessions', payload),
- getSession: (sessionId) => getCachedJson(`/api/sessions/${encodeURIComponent(sessionId)}`),
+ getSession: (sessionId, params) =>
+ params?.cache === false
+ ? getJson(`/api/sessions/${encodeURIComponent(sessionId)}`)
+ : getCachedJson(`/api/sessions/${encodeURIComponent(sessionId)}`),
getSessionBootstrap: (sessionId) =>
getJson(`/api/sessions/${encodeURIComponent(sessionId)}/bootstrap`),
updateSession: async (sessionId, payload) => {