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): picker Button trigger, session-guarded send, stop-state edges, no-flush on plain Stop (Codex round 12)

cyh87834dacbec4
diff --git a/core/internal_server.py b/core/internal_server.py
index d5b8251cd..19e2c58d1 100644
--- a/core/internal_server.py
+++ b/core/internal_server.py
@@ -90,6 +90,12 @@ def create_app(controller: "Controller") -> FastAPI:
flush_on_cancel: set[str] = set()
app.state.flush_on_cancel = flush_on_cancel
+ # Sessions whose current turn is being stopped by a plain Stop and must NOT
+ # flush, even if the backend interrupt lets the turn settle NORMALLY (no
+ # CancelledError) during the awaited stop — a Stop keeps the queue ("不清空").
+ # Recorded before awaiting the interrupt so the race is covered.
+ stop_no_flush: set[str] = set()
+
async def _noop_chunk(_envelope: dict) -> None:
# Chunks are discarded — the browser renders from ``message.new``.
return None
@@ -136,8 +142,12 @@ def create_app(controller: "Controller") -> FastAPI:
# Don't flush after a Stop (keep the queue) OR after a stream
# timeout (the backend was just interrupted; the user can
# resume). send-now still forces a flush via flush_on_cancel.
- should_flush = (not cancelled and not timed_out) or (session_id in flush_on_cancel)
+ should_flush = (
+ (not cancelled and not timed_out and session_id not in stop_no_flush)
+ or (session_id in flush_on_cancel)
+ )
flush_on_cancel.discard(session_id)
+ stop_no_flush.discard(session_id)
if should_flush:
await _flush_queue(session_id)
@@ -416,12 +426,22 @@ def create_app(controller: "Controller") -> FastAPI:
# the turn STARTED under (captured at dispatch time), not one rebuilt
# from the current session row, so the right backend is interrupted
# even if the Chat header swapped the session's agent / model mid-turn.
+ # Record the no-flush intent BEFORE awaiting the interrupt: if the
+ # backend stop lets the turn settle normally during the await (no
+ # CancelledError), _run_turn's finally would otherwise treat it as a
+ # natural completion and flush the queue — but a plain Stop keeps the
+ # queue (Codex P2).
+ stop_no_flush.add(session_id)
stopped = False
try:
stopped = bool(await controller.command_handler.handle_stop(turn_context))
except Exception:
logger.exception("internal cancel: backend stop failed for session=%s", session_id)
if not stopped:
+ # Stop refused — the turn keeps running, so it isn't being stopped;
+ # drop the no-flush marker so a later natural completion flushes
+ # normally.
+ stop_no_flush.discard(session_id)
# The backend couldn't interrupt this turn (no interruptible active
# session). Don't cancel the waiter — that would fire a false
# ``turn.end``, hide Stop, and let follow-up work start while the turn
diff --git a/ui/src/components/workbench/ChatPage.tsx b/ui/src/components/workbench/ChatPage.tsx
index 70bb91b46..499ead529 100644
--- a/ui/src/components/workbench/ChatPage.tsx
+++ b/ui/src/components/workbench/ChatPage.tsx
@@ -356,6 +356,11 @@ export const ChatPage: React.FC = () => {
body: JSON.stringify({ text }),
});
const body = await response.json().catch(() => null);
+ // If the user switched chats while this POST was in flight, the response
+ // belongs to the previous session — don't append it / mutate working /
+ // error on the chat they moved to (Codex P2). The turn still ran for the
+ // original session; its rows live there.
+ if (sessionId !== sessionIdRef.current) return;
if (!response.ok) {
setWorking(false);
throw new Error(body?.detail ? String(body.detail) : `HTTP ${response.status}`);
@@ -370,8 +375,10 @@ export const ChatPage: React.FC = () => {
// A turn started — optimistically show the user row (echo dedupes by id).
if (body && body.id) appendMessage(body as WorkbenchMessage);
} catch (err: any) {
- setWorking(false);
- setError(err?.message ?? String(err));
+ if (sessionId === sessionIdRef.current) {
+ setWorking(false);
+ setError(err?.message ?? String(err));
+ }
}
},
[sessionId, appendMessage, refreshQueue],
@@ -388,13 +395,22 @@ export const ChatPage: React.FC = () => {
// success the backend is interrupted and the authoritative ``turn.end``
// clears the working state.
if (res && res.ok === false) {
- setError(res.detail ? String(res.detail) : t('chat.stopFailed'));
+ if (res.code === 'not_in_flight') {
+ // The controller has no running turn — our working state was stale
+ // (a missed turn.end). Clear it instead of leaving Stop stuck (Codex P2).
+ setWorking(false);
+ void syncTurnState();
+ } else {
+ // The stop didn't reach the backend (e.g. 503); the turn may still be
+ // live, so keep Stop available + surface the failure.
+ setError(res.detail ? String(res.detail) : t('chat.stopFailed'));
+ }
}
} catch (err: any) {
// The cancel request itself threw (network) — surface it; keep Stop.
setError(err?.message ?? String(err));
}
- }, [api, sessionId, working, t]);
+ }, [api, sessionId, working, t, syncTurnState]);
const removeQueued = useCallback(
async (messageId: string) => {
@@ -420,7 +436,10 @@ export const ChatPage: React.FC = () => {
try {
const res = await api.sendQueuedNow(sessionId, queue[0].id);
if (res && res.ok === false) {
- setWorking(false);
+ // stop_failed: the controller left the ORIGINAL turn running and the
+ // queue intact — keep Stop visible so the user can still interrupt it
+ // (Codex P2). Other failures mean no turn is running → clear working.
+ if (res.code !== 'stop_failed') setWorking(false);
setError(res.detail ? String(res.detail) : t('chat.stopFailed'));
} else if (res && (res as { status?: string }).status === 'empty') {
// Nothing was actually flushed (a stale queue item already gone) — no
@@ -880,9 +899,10 @@ const AgentRoutePicker: React.FC<AgentRoutePickerProps> = ({ session, agents, on
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
- <button
+ <Button
type="button"
- className="ml-auto inline-flex max-w-[62%] items-center gap-1.5 rounded-lg border border-cyan/40 bg-surface-2 px-2.5 py-1.5 text-[12px] transition hover:bg-cyan/[0.06]"
+ variant="outline"
+ className="ml-auto inline-flex h-auto max-w-[62%] items-center justify-start gap-1.5 rounded-lg border-cyan/40 bg-surface-2 px-2.5 py-1.5 text-[12px] font-normal hover:bg-cyan/[0.06]"
>
{backend && (
<span className="inline-flex shrink-0 items-center gap-1 rounded border border-cyan/30 bg-cyan/[0.08] px-1.5 py-0.5 font-mono text-[9px] font-bold uppercase text-cyan">
@@ -904,7 +924,7 @@ const AgentRoutePicker: React.FC<AgentRoutePickerProps> = ({ session, agents, on
</>
)}
<ChevronDown className="size-3 shrink-0 text-muted" />
- </button>
+ </Button>
</PopoverTrigger>
<PopoverContent align="end" className="w-[620px] max-w-[92vw] overflow-hidden p-0">
<div className="grid grid-cols-3 divide-x divide-border">
@@ -939,17 +959,19 @@ const AgentRoutePicker: React.FC<AgentRoutePickerProps> = ({ session, agents, on
))}
</div>
))}
- <button
+ <Button
type="button"
+ variant="ghost"
+ size="sm"
onClick={() => {
setOpen(false);
navigate('/agents');
}}
- className="mt-1 flex items-center gap-1.5 rounded px-2 py-1.5 text-left text-[11px] font-medium text-cyan transition hover:bg-cyan/[0.08]"
+ className="mt-1 h-auto w-full justify-start gap-1.5 rounded px-2 py-1.5 text-[11px] font-medium text-cyan hover:bg-cyan/[0.08] hover:text-cyan"
>
<Plus className="size-3.5" />
{t('chat.picker.newAgent')}
- </button>
+ </Button>
</RouteColumn>
{/* Column 2 — Model (lazy-loaded for the active backend) */}