@@ -3,11 +3,17 @@ import { describe, it, beforeEach, afterEach, mock } from "node:test";
33
44import {
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" ;
1117import { formatElapsed } from "./ui/agentSessionUtils.ts" ;
1218
1319const 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+
12331304describe ( "formatElapsed" , ( ) => {
12341305 it ( "renders sub-10s as whole seconds" , ( ) => {
12351306 assert . equal ( formatElapsed ( 0 ) , "0s" ) ;
0 commit comments