Skip to content

Commit cf048ac

Browse files
author
Michael Feth
committed
fix(desktop): fix live-path notify gap and surface circuit badge in the real Agents view
An adversarial review of the circuit-breaker badge (890cfb6) found two issues, both confirmed by independently tracing the actual code: - processLiveObserverEvents gated notifyListeners() solely on appendAgentEvents' return value, discarding applyCircuitEvent's own changed flag. appendAgentEvents' per-agent eviction floor and applyCircuitEvent's per-slot ordering gate compare against different reference points, so a circuit-only change could be committed to the store without ever notifying useAgentCircuitStatus subscribers. OR the two signals together, matching every other ingestion path in the file. Added a regression test reproducing the divergence via a raw- journal dedup collision (cheaper to construct than real eviction). - The badge was wired into ManagedAgentRow/AgentGroupRows, but that component tree has no import anywhere outside itself — it is not mounted on the app's actual Agents route. The real reachable screen (AgentsScreen -> AgentsView -> UnifiedAgentsSection, rendering AgentPersonaCard/StandaloneAgentCard) had no circuit-status wiring at all, so the feature had no effect in the running app. Added the same badge to both card components' existing statusBadge slot, taking priority over the "Configuration missing" badge when both apply. Left ManagedAgentRow's badge in place (still covered by its own tests) rather than deciding unilaterally whether that component tree is dead code or a future integration point. Signed-off-by: Michael Feth <[email protected]>
1 parent 890cfb6 commit cf048ac

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎desktop/src/features/agents/observerRelayStore.circuitStatus.test.mjs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, it, beforeEach, afterEach } from "node:test";
44
import {
55
getAgentCircuitStatus,
66
resetAgentObserverStore,
7+
subscribeAgentObserverStore,
78
_testProcessLiveObserverEvents,
89
} from "./observerRelayStore.ts";
910

@@ -207,6 +208,56 @@ describe("getAgentCircuitStatus", () => {
207208
);
208209
});
209210

211+
it("notifies subscribers on a circuit change even when appendAgentEvents rejects the raw event as a dedup collision", () => {
212+
// appendAgentEvents dedups purely on (timestamp, seq), independent of
213+
// kind — so a circuit_open event that happens to share its (timestamp,
214+
// seq) with an already-appended, unrelated event is treated as a
215+
// duplicate by the raw journal (appendAgentEvents returns false) even
216+
// though it is the first-ever circuit event for its own slot, so
217+
// applyCircuitEvent legitimately applies it (returns true). Regression
218+
// guard for the case where only appendAgentEvents' return value gated
219+
// notifyListeners(): the circuit state would update silently with no
220+
// subscriber ever told, so useAgentCircuitStatus consumers (the
221+
// suspended-agent badge) would not re-render until unrelated traffic
222+
// happened to touch the same agent.
223+
_testProcessLiveObserverEvents(AGENT, [
224+
makeEvent({
225+
seq: 7,
226+
timestamp: "2024-01-01T00:00:00Z",
227+
kind: "acp_read",
228+
agentIndex: null,
229+
channelId: "chan-uuid-1",
230+
payload: {},
231+
}),
232+
]);
233+
234+
let notifyCount = 0;
235+
const unsubscribe = subscribeAgentObserverStore(() => {
236+
notifyCount += 1;
237+
});
238+
try {
239+
_testProcessLiveObserverEvents(AGENT, [
240+
makeEvent({
241+
seq: 7,
242+
timestamp: "2024-01-01T00:00:00Z",
243+
kind: "circuit_open",
244+
agentIndex: 2,
245+
channelId: "chan-uuid-2",
246+
payload: { error: "slot 2 opened" },
247+
}),
248+
]);
249+
} finally {
250+
unsubscribe();
251+
}
252+
253+
assert.equal(
254+
notifyCount,
255+
1,
256+
"a circuit-only state change must still notify useSyncExternalStore subscribers",
257+
);
258+
assert.equal(getAgentCircuitStatus(AGENT).isOpen, true);
259+
});
260+
210261
it("resetAgentObserverStore clears circuit state back to isOpen:false", () => {
211262
_testProcessLiveObserverEvents(AGENT, [
212263
makeEvent({

‎desktop/src/features/agents/observerRelayStore.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,20 @@ function processLiveObserverEvents(
427427
// callbacks. Those callbacks historically observed their triggering frame
428428
// in the raw/transcript stores; batching must preserve that visibility while
429429
// deferring only the global external-store publication.
430-
const observerChanged = appendAgentEvents(agentPubkey, events);
430+
let observerChanged = appendAgentEvents(agentPubkey, events);
431431

432432
for (const parsed of events) {
433-
applyCircuitEvent(agentPubkey, parsed);
433+
// appendAgentEvents' per-agent eviction floor and applyCircuitEvent's
434+
// per-slot ordering gate compare against different reference points, so a
435+
// circuit event can be rejected by one and accepted by the other (e.g. a
436+
// late-delivered frame after a relay reconnect, at/before the agent's
437+
// floor but the first event ever seen for its slot). OR the two signals
438+
// so a circuit-only change still notifies subscribers — matches every
439+
// other ingestion path in this file (ingestArchivedObserverEvents,
440+
// injectObserverEventsForE2E, syncAgentObserverEvents).
441+
if (applyCircuitEvent(agentPubkey, parsed)) {
442+
observerChanged = true;
443+
}
434444

435445
// Track the latest-live-session-id per (agent, channel) on the live path.
436446
// Only set when the parsed event carries both a sessionId and channelId,

‎desktop/src/features/agents/ui/UnifiedAgentsSection.tsx‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { resolveAgentCardModelLabel } from "@/features/agents/lib/agentCardModel
99
import { friendlyAgentLastError } from "@/features/agents/lib/friendlyAgentLastError";
1010
import { isManagedAgentActive } from "@/features/agents/lib/managedAgentControlActions";
1111
import { pickProfileAgent } from "@/features/agents/lib/pickProfileAgent";
12+
import { useAgentCircuitStatus } from "@/features/agents/observerRelayStore";
1213
import { useUserProfileQuery } from "@/features/profile/hooks";
1314
import type { AgentPersona, ManagedAgent } from "@/shared/api/types";
1415
import type { ProfilePanelOpenOptions } from "@/shared/context/ProfilePanelContext";
@@ -258,6 +259,7 @@ function AgentPersonaCard({
258259
});
259260
const isActive = agent ? isManagedAgentActive(agent) : false;
260261
const profileQuery = useUserProfileQuery(agent?.pubkey);
262+
const circuitStatus = useAgentCircuitStatus(agent?.pubkey);
261263
const avatarUrl = agent
262264
? resolveAgentCardAvatarUrl(profileQuery.data?.avatarUrl, persona.avatarUrl)
263265
: persona.avatarUrl;
@@ -322,7 +324,19 @@ function AgentPersonaCard({
322324
onOpenPersonaProfile(persona);
323325
}}
324326
statusBadge={
325-
agent?.personaOrphaned ? (
327+
circuitStatus.isOpen ? (
328+
<Badge
329+
className="gap-1"
330+
data-testid={
331+
agent ? `managed-agent-circuit-open-${agent.pubkey}` : undefined
332+
}
333+
title={circuitStatus.message ?? undefined}
334+
variant="destructive"
335+
>
336+
<AlertTriangle className="h-3 w-3" />
337+
Suspended — repeated crashes
338+
</Badge>
339+
) : agent?.personaOrphaned ? (
326340
<Badge className="gap-1" variant="warning">
327341
<AlertTriangle className="h-3 w-3" />
328342
Configuration missing
@@ -355,6 +369,7 @@ function StandaloneAgentCard({
355369
}) {
356370
const title = agent.name;
357371
const profileQuery = useUserProfileQuery(agent.pubkey);
372+
const circuitStatus = useAgentCircuitStatus(agent.pubkey);
358373
const friendlyError = friendlyAgentLastError(
359374
agent.lastError,
360375
agent.lastErrorCode,
@@ -402,7 +417,17 @@ function StandaloneAgentCard({
402417
);
403418
}}
404419
statusBadge={
405-
agent.personaOrphaned ? (
420+
circuitStatus.isOpen ? (
421+
<Badge
422+
className="gap-1"
423+
data-testid={`managed-agent-circuit-open-${agent.pubkey}`}
424+
title={circuitStatus.message ?? undefined}
425+
variant="destructive"
426+
>
427+
<AlertTriangle className="h-3 w-3" />
428+
Suspended — repeated crashes
429+
</Badge>
430+
) : agent.personaOrphaned ? (
406431
<Badge className="gap-1" variant="warning">
407432
<AlertTriangle className="h-3 w-3" />
408433
Configuration missing

0 commit comments

Comments
 (0)