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): ignore stale agent projections

Maxvint45f7c7a44c0e
diff --git a/ui/src/lib/useNewSession.test.ts b/ui/src/lib/useNewSession.test.ts
new file mode 100644
index 000000000..462523997
--- /dev/null
+++ b/ui/src/lib/useNewSession.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import type { VibeAgentBrief } from '../context/ApiContext';
+import { createLatestAgentProjectionLoader } from './useNewSession';
+
+const deferred = <T,>() => {
+ let resolve!: (value: T) => void;
+ let reject!: (reason?: unknown) => void;
+ const promise = new Promise<T>((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return { promise, resolve, reject };
+};
+
+const agent = (name: string): VibeAgentBrief => ({
+ id: name,
+ name,
+ description: null,
+ backend: 'codex',
+ model: null,
+ reasoning_effort: null,
+ enabled: true,
+ source: 'user',
+ updated_at: '2026-01-01T00:00:00Z',
+});
+
+type AgentProjection = {
+ ok: boolean;
+ agents: VibeAgentBrief[];
+ default_agent_name: string | null;
+};
+
+describe('createLatestAgentProjectionLoader', () => {
+ it('ignores a pre-revocation response that completes after the refreshed projection', async () => {
+ const stale = deferred<AgentProjection>();
+ const refreshed = deferred<AgentProjection>();
+ const fetchAgents = vi
+ .fn()
+ .mockImplementationOnce(() => stale.promise)
+ .mockImplementationOnce(() => refreshed.promise);
+ const applyProjection = vi.fn();
+ const loader = createLatestAgentProjectionLoader(fetchAgents, applyProjection);
+
+ const initialLoad = loader.load();
+ const authorizationRefresh = loader.load();
+ const authorizedProjection = {
+ ok: true,
+ agents: [agent('still-authorized')],
+ default_agent_name: 'still-authorized',
+ };
+ refreshed.resolve(authorizedProjection);
+ await authorizationRefresh;
+
+ stale.resolve({
+ ok: true,
+ agents: [agent('revoked')],
+ default_agent_name: 'revoked',
+ });
+ await initialLoad;
+
+ expect(applyProjection).toHaveBeenCalledTimes(1);
+ expect(applyProjection).toHaveBeenCalledWith(authorizedProjection);
+ });
+});
diff --git a/ui/src/lib/useNewSession.ts b/ui/src/lib/useNewSession.ts
index e2672d116..59892a2a3 100644
--- a/ui/src/lib/useNewSession.ts
+++ b/ui/src/lib/useNewSession.ts
@@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { useApi } from '../context/ApiContext';
import { useWorkbenchProjectsTree } from '../context/WorkbenchProjectsContext';
-import type { VibeAgentBrief, WorkbenchProject, WorkbenchSessionCreate } from '../context/ApiContext';
+import type { ApiContextType, VibeAgentBrief, WorkbenchProject, WorkbenchSessionCreate } from '../context/ApiContext';
interface UseNewSessionOptions {
/** Re-run the per-open reset on the rising edge — sheets pass their `open`. Default true. */
@@ -53,6 +53,32 @@ const sortByRecent = (list: WorkbenchProject[]) =>
.slice()
.sort((a, b) => (b.last_active_at || b.created_at).localeCompare(a.last_active_at || a.created_at));
+type AgentProjectionResponse = Awaited<ReturnType<ApiContextType['listVibeAgents']>>;
+
+export function createLatestAgentProjectionLoader(
+ fetchAgents: () => Promise<AgentProjectionResponse>,
+ applyProjection: (response: AgentProjectionResponse | null) => void,
+) {
+ let generation = 0;
+ let stopped = false;
+
+ return {
+ load: async () => {
+ const loadGeneration = ++generation;
+ try {
+ const response = await fetchAgents();
+ if (!stopped && loadGeneration === generation) applyProjection(response);
+ } catch {
+ if (!stopped && loadGeneration === generation) applyProjection(null);
+ }
+ },
+ stop: () => {
+ stopped = true;
+ generation += 1;
+ },
+ };
+}
+
// Shared new-session create flow for the desktop Workbench home (`Workbench.tsx`)
// and the mobile NewSessionSheet. A thin layer over the shared projects provider
// (the project LIST + the create itself come from there, so a project/session
@@ -84,12 +110,10 @@ export function useNewSession({ active = true, loadErrorText, createFailedText }
// Agent in an already-open picker.
useEffect(() => {
if (!active) return;
- let cancelled = false;
- const loadAgents = () => {
- void api
- .listVibeAgents({ includeDisabled: false })
- .then((res) => {
- if (cancelled) return;
+ const loader = createLatestAgentProjectionLoader(
+ () => api.listVibeAgents({ includeDisabled: false }),
+ (res) => {
+ if (res) {
setAgents(res.agents);
setDefaultAgentName(res.default_agent_name);
setUserPick((current) => {
@@ -98,21 +122,21 @@ export function useNewSession({ active = true, loadErrorText, createFailedText }
}
return {};
});
- })
- .catch(() => {
- if (!cancelled) {
- setAgents([]);
- setDefaultAgentName(null);
- setUserPick({});
- }
- });
- };
- loadAgents();
+ return;
+ }
+ setAgents([]);
+ setDefaultAgentName(null);
+ setUserPick({});
+ },
+ );
+ void loader.load();
const disconnect = api.connectWorkbenchEvents({
- onAuthorizationChanged: loadAgents,
+ onAuthorizationChanged: () => {
+ void loader.load();
+ },
});
return () => {
- cancelled = true;
+ loader.stop();
disconnect();
};
}, [active, api]);