Skip to content

Commit 1f5ba5b

Browse files
tellahonpub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w
andauthored
fix(desktop): sync derived active-turn liveness in channel activity path (#1492)
Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co>
1 parent e3008b3 commit 1f5ba5b

27 files changed

Lines changed: 1475 additions & 141 deletions

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default defineConfig({
3636
"**/profile-active-turn.spec.ts",
3737
"**/config-bridge-screenshots.spec.ts",
3838
"**/observer-feed-screenshots.spec.ts",
39+
"**/activity-scope-label-screenshots.spec.ts",
3940
"**/local-archive-screenshots.spec.ts",
4041
"**/agent-readiness-screenshots.spec.ts",
4142
"**/file-attachment.spec.ts",

‎desktop/scripts/check-file-sizes.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ const overrides = new Map([
172172
// test. Load-bearing feature growth, queued to split publishSplitSlots path
173173
// into readStateManagerSplit.ts.
174174
["src/features/channels/readState/readStateManager.ts", 1030],
175+
// review feedback on #1492 restored the two-line load-bearing comment
176+
// documenting why `lastMessageAt` must not be an `activeReadAt` fallback
177+
// (reply-inclusive; would clear unread state early). The file was already
178+
// at the 1000 ceiling; comment-only overage, not code growth. Queued to
179+
// split with the rest of this list.
180+
["src/features/channels/ui/ChannelScreen.tsx", 1002],
175181
// Shared UI was added to this guard after splitting globals/markdown so
176182
// large shared renderers cannot grow further while follow-up splits land.
177183
// +33 for config-nudge detect-and-render + author-auth gate (normalizePubkey guard).

‎desktop/src/app/AppShell.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { PreventSleepProvider } from "@/features/agents/usePreventSleep";
3939
import { requestOpenCreateAgent } from "@/features/agents/openCreateAgentEvent";
4040
import { useAgentsDataRefresh } from "@/features/agents/lib/useAgentsDataRefresh";
4141
import { usePersonaSync } from "@/features/agents/lib/usePersonaSync";
42+
import { useAgentObserverIngestion } from "@/features/agents/useAgentObserverIngestion";
4243
import {
4344
usePresenceSession,
4445
usePresenceSubscription,
@@ -150,6 +151,14 @@ export function AppShell() {
150151
);
151152
usePersonaSync(identityQuery.data?.pubkey);
152153
useAgentsDataRefresh();
154+
// Owner-global observer ingestion: receives + decrypts agent observer
155+
// frames and keeps derived active-turn liveness in sync app-wide, so no
156+
// individual screen/panel has to mount its own bridge for ingestion.
157+
// Intentionally mounted without a `startupReady`/identity guard: before
158+
// `currentPubkey` resolves the hook ingests managed agents only, and
159+
// relay-owned agents join automatically once identity arrives. Adding a
160+
// guard here would drop managed-agent coverage during startup.
161+
useAgentObserverIngestion();
153162
useArchiveSync();
154163
useObserverArchiveSeed(identityQuery.data?.pubkey);
155164
useAgentMetricArchiveSeed(identityQuery.data?.pubkey);

‎desktop/src/app/navigation/useAppNavigation.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export function useAppNavigation() {
145145
(
146146
channelId: string,
147147
options?: {
148+
/** Open the agent activity pane for this agent pubkey on arrival. */
149+
agentSession?: string;
148150
messageId?: string;
149151
replace?: boolean;
150152
threadRootId?: string | null;
@@ -156,12 +158,17 @@ export function useAppNavigation() {
156158
params: {
157159
channelId,
158160
},
159-
search: options?.messageId
160-
? {
161-
messageId: options.messageId,
162-
threadRootId: options.threadRootId ?? undefined,
163-
}
164-
: {},
161+
search: {
162+
...(options?.messageId
163+
? {
164+
messageId: options.messageId,
165+
threadRootId: options.threadRootId ?? undefined,
166+
}
167+
: {}),
168+
...(options?.agentSession
169+
? { agentSession: options.agentSession }
170+
: {}),
171+
},
165172
},
166173
{
167174
replace: options?.replace,

‎desktop/src/features/agents/activeAgentTurnsStore.test.mjs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { describe, it, beforeEach, afterEach, mock } from "node:test";
33

44
import {
55
syncAgentTurnsFromEvents,
6+
syncActiveAgentTurnsFromObserver,
67
getActiveTurnsForAgent,
78
getActiveTurnsByChannel,
89
resetActiveAgentTurnsStore,
910
subscribeActiveAgentTurns,
1011
} from "./activeAgentTurnsStore.ts";
12+
import {
13+
injectObserverEventsForE2E,
14+
getAgentObserverSnapshot,
15+
resetAgentObserverStore,
16+
} from "./observerRelayStore.ts";
1117
import { formatElapsed } from "./ui/agentSessionUtils.ts";
1218

1319
const AGENT =
@@ -1230,6 +1236,71 @@ describe("activeAgentTurnsStore", () => {
12301236
});
12311237
});
12321238

1239+
/**
1240+
* Regression: raw observer ingestion and derived active-turn liveness must
1241+
* stay in sync. The channel activity path previously mounted only the observer
1242+
* bridge, so a raw `turn_completed` could be visible in the activity feed
1243+
* while the derived liveness indicator kept spinning. This drives events
1244+
* through the real observer store (appendAgentEvent path) and the same sync
1245+
* the bridge hook runs, asserting derived liveness clears with the raw feed.
1246+
*/
1247+
describe("observer → active-turns bridge sync", () => {
1248+
const bridgeAgents = [{ pubkey: AGENT, status: "deployed" }];
1249+
1250+
beforeEach(() => {
1251+
resetActiveAgentTurnsStore();
1252+
resetAgentObserverStore();
1253+
});
1254+
1255+
afterEach(() => {
1256+
resetAgentObserverStore();
1257+
});
1258+
1259+
it("clears derived liveness when raw turn_completed arrives", () => {
1260+
injectObserverEventsForE2E(AGENT, [
1261+
makeEvent({ seq: 1, kind: "turn_started" }),
1262+
]);
1263+
syncActiveAgentTurnsFromObserver(bridgeAgents);
1264+
assert.ok(
1265+
channelIdsOf(getActiveTurnsForAgent(AGENT)).has("chan-1"),
1266+
"turn_started must surface an active turn",
1267+
);
1268+
1269+
injectObserverEventsForE2E(AGENT, [
1270+
makeEvent({
1271+
seq: 2,
1272+
kind: "turn_completed",
1273+
timestamp: "2024-01-01T00:00:05Z",
1274+
}),
1275+
]);
1276+
syncActiveAgentTurnsFromObserver(bridgeAgents);
1277+
1278+
const rawEvents = getAgentObserverSnapshot(AGENT, true).events;
1279+
assert.equal(
1280+
rawEvents.at(-1)?.kind,
1281+
"turn_completed",
1282+
"raw feed must contain the completion event",
1283+
);
1284+
assert.equal(
1285+
getActiveTurnsForAgent(AGENT).length,
1286+
0,
1287+
"derived liveness must clear when the raw feed shows turn_completed",
1288+
);
1289+
});
1290+
1291+
it("skips agents that are neither running nor deployed", () => {
1292+
injectObserverEventsForE2E(AGENT, [
1293+
makeEvent({ seq: 1, kind: "turn_started" }),
1294+
]);
1295+
syncActiveAgentTurnsFromObserver([{ pubkey: AGENT, status: "stopped" }]);
1296+
assert.equal(
1297+
getActiveTurnsForAgent(AGENT).length,
1298+
0,
1299+
"inactive agents must not populate the active-turns store",
1300+
);
1301+
});
1302+
});
1303+
12331304
describe("formatElapsed", () => {
12341305
it("renders sub-10s as whole seconds", () => {
12351306
assert.equal(formatElapsed(0), "0s");

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,21 @@ export function useActiveAgentTurnsByChannel(): ActiveChannelTurnSummary[] {
527527
);
528528
}
529529

530+
/**
531+
* Sync every running/deployed agent's observer events into the active-turns
532+
* store. Extracted from the bridge hook so a regression can drive the exact
533+
* observer→derived-liveness path without a React renderer.
534+
*/
535+
export function syncActiveAgentTurnsFromObserver(
536+
agents: readonly { pubkey: string; status: string }[],
537+
) {
538+
for (const agent of agents) {
539+
if (agent.status !== "running" && agent.status !== "deployed") continue;
540+
const snapshot = getAgentObserverSnapshot(agent.pubkey, true);
541+
syncAgentTurnsFromEvents(agent.pubkey, snapshot.events);
542+
}
543+
}
544+
530545
/**
531546
* Bridge hook: processes observer events into the active-turns store.
532547
* Should be called by a parent component that has access to the observer events.
@@ -536,11 +551,7 @@ export function useActiveAgentTurnsBridge(
536551
) {
537552
React.useEffect(() => {
538553
function syncAll() {
539-
for (const agent of agents) {
540-
if (agent.status !== "running" && agent.status !== "deployed") continue;
541-
const snapshot = getAgentObserverSnapshot(agent.pubkey, true);
542-
syncAgentTurnsFromEvents(agent.pubkey, snapshot.events);
543-
}
554+
syncActiveAgentTurnsFromObserver(agents);
544555
}
545556

546557
syncAll();

0 commit comments

Comments
 (0)