@@ -65,6 +65,10 @@ import {
6565} from "./remoteRegistration" ;
6666import type { AgentActivityProps } from "../../widgets/AgentActivity" ;
6767import { publishAgentActivityWidget } from "./agentLiveActivity" ;
68+ import {
69+ agentActivityTimeline ,
70+ AGENT_ACTIVITY_FRESHNESS_MS ,
71+ } from "../../widgets/agentActivityTimeline" ;
6872import * as Notifications from "expo-notifications" ;
6973import { Platform } from "react-native" ;
7074import {
@@ -2639,7 +2643,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
26392643 appStateMock . currentState = "background" ;
26402644 for ( const listener of appStateMock . listeners ) listener ( "background" ) ;
26412645 const publishedBeforeBackground = vi . mocked ( publishAgentActivityWidget ) . mock . calls . length ;
2642- expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ?. [ 0 ] ?. isStale ) . toBe ( true ) ;
2646+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ?. [ 0 ] ?. isStale ) . toBe ( false ) ;
26432647 setLiveShell ( "ready" ) ;
26442648 expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( publishedBeforeBackground ) ;
26452649 appStateMock . currentState = "active" ;
@@ -2655,7 +2659,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
26552659 } ) ;
26562660
26572661 it . effect (
2658- "marks retained observations stale when a background relay read cannot validate them " ,
2662+ "keeps the original deadline when a background relay read cannot validate retained observations " ,
26592663 ( ) => {
26602664 addLiveEnvironment ( ) ;
26612665 setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "clerk-token-user-a" ) , "user-a" ) ;
@@ -2670,7 +2674,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
26702674 expect ( publishAgentActivityWidget ) . toHaveBeenLastCalledWith (
26712675 expect . objectContaining ( {
26722676 activeCount : 1 ,
2673- isStale : true ,
2677+ isStale : false ,
26742678 activities : [ expect . objectContaining ( { threadTitle : "Live task" } ) ] ,
26752679 } ) ,
26762680 ) ;
@@ -2714,6 +2718,206 @@ describe("makeRelayDeviceRegistrationRequest", () => {
27142718 } ) . pipe ( Effect . provide ( snapshotRelayLayer ( ( ) => Effect . succeed ( { aggregate : null } ) ) ) ) ;
27152719 } ) ;
27162720
2721+ it . effect (
2722+ "does not renew disconnected work from a fresh read of an older relay observation" ,
2723+ ( ) =>
2724+ Effect . gen ( function * ( ) {
2725+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2726+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2727+ addLiveEnvironment ( ) ;
2728+ setLiveShell ( "running" ) ;
2729+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2730+ backgroundRuntime . pending . length = 0 ;
2731+ const layer = snapshotRelayLayer ( ( ) => Effect . succeed ( { aggregate : null } ) ) ;
2732+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2733+ const deadline = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] . expiresAt ;
2734+
2735+ clock . mockReturnValue ( now + 60_000 ) ;
2736+ setLiveShell ( "running" , "cached" ) ;
2737+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2738+ const retained = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2739+ expect ( retained ) . toMatchObject ( { isStale : true , expiresAt : deadline } ) ;
2740+ expect ( retained . activities [ 0 ] ?. threadTitle ) . toBe ( "Live task" ) ;
2741+
2742+ appStateMock . currentState = "background" ;
2743+ for ( const listener of appStateMock . listeners ) listener ( "background" ) ;
2744+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ) . toMatchObject ( {
2745+ isStale : true ,
2746+ expiresAt : deadline ,
2747+ } ) ;
2748+ } ) ,
2749+ ) ;
2750+
2751+ it ( "renews a confirmed live shell but not an unrelated atom recomputation" , ( ) => {
2752+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2753+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2754+ addLiveEnvironment ( ) ;
2755+ setLiveShell ( "running" ) ;
2756+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2757+ const original = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2758+ expect ( original . expiresAt ) . toBe ( now + AGENT_ACTIVITY_FRESHNESS_MS ) ;
2759+ const publications = vi . mocked ( publishAgentActivityWidget ) . mock . calls . length ;
2760+
2761+ clock . mockReturnValue ( now + 60_000 ) ;
2762+ const catalog = appAtomRegistry . get ( environmentCatalog . catalogValueAtom ) ;
2763+ setTestAtom ( environmentCatalog . catalogValueAtom , { ...catalog } ) ;
2764+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( publications ) ;
2765+
2766+ setLiveShell ( "running" ) ;
2767+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] . expiresAt ) . toBe (
2768+ now + 60_000 + AGENT_ACTIVITY_FRESHNESS_MS ,
2769+ ) ;
2770+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( publications + 1 ) ;
2771+ } ) ;
2772+
2773+ it . effect ( "renews unchanged confirmed relay content, but not a failed read" , ( ) =>
2774+ Effect . gen ( function * ( ) {
2775+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2776+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2777+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2778+ backgroundRuntime . pending . length = 0 ;
2779+ const layer = snapshotRelayLayer ( ) ;
2780+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2781+ const original = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2782+ expect ( original . expiresAt ) . toBe ( now + AGENT_ACTIVITY_FRESHNESS_MS ) ;
2783+
2784+ clock . mockReturnValue ( now + 60_000 ) ;
2785+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2786+ const renewed = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2787+ expect ( renewed . activities ) . toEqual ( original . activities ) ;
2788+ expect ( renewed . expiresAt ) . toBe ( now + 60_000 + AGENT_ACTIVITY_FRESHNESS_MS ) ;
2789+ const publications = vi . mocked ( publishAgentActivityWidget ) . mock . calls . length ;
2790+
2791+ clock . mockReturnValue ( now + 120_000 ) ;
2792+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2793+ Effect . provide (
2794+ snapshotRelayLayer ( ( ) =>
2795+ Effect . fail (
2796+ new ManagedRelay . ManagedRelayRequestFailedError ( {
2797+ action : "read relay agent activity snapshot" ,
2798+ transportFailed : true ,
2799+ cause : new Error ( "offline" ) ,
2800+ } ) ,
2801+ ) ,
2802+ ) ,
2803+ ) ,
2804+ ) ;
2805+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( publications ) ;
2806+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] . expiresAt ) . toBe (
2807+ renewed . expiresAt ,
2808+ ) ;
2809+ } ) ,
2810+ ) ;
2811+
2812+ it . effect ( "does not renew expiration when an older unchanged relay read finishes last" , ( ) =>
2813+ Effect . gen ( function * ( ) {
2814+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2815+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2816+ const started = yield * Deferred . make < void > ( ) ;
2817+ const finish = yield * Deferred . make < void > ( ) ;
2818+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2819+ backgroundRuntime . pending . length = 0 ;
2820+ const older = yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2821+ Effect . provide (
2822+ snapshotRelayLayer ( ( ) =>
2823+ Deferred . succeed ( started , undefined ) . pipe (
2824+ Effect . andThen ( Deferred . await ( finish ) ) ,
2825+ Effect . as ( activeAgentActivitySnapshot ) ,
2826+ ) ,
2827+ ) ,
2828+ ) ,
2829+ Effect . forkChild ,
2830+ ) ;
2831+ yield * Deferred . await ( started ) ;
2832+ clock . mockReturnValue ( now + 60_000 ) ;
2833+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2834+ Effect . provide ( snapshotRelayLayer ( ) ) ,
2835+ ) ;
2836+ const publications = vi . mocked ( publishAgentActivityWidget ) . mock . calls . length ;
2837+ clock . mockReturnValue ( now + 120_000 ) ;
2838+ yield * Deferred . succeed ( finish , undefined ) ;
2839+ yield * Fiber . join ( older ) ;
2840+ expect ( publishAgentActivityWidget ) . toHaveBeenCalledTimes ( publications ) ;
2841+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] . expiresAt ) . toBe (
2842+ now + 60_000 + AGENT_ACTIVITY_FRESHNESS_MS ,
2843+ ) ;
2844+ } ) . pipe ( Effect . scoped ) ,
2845+ ) ;
2846+
2847+ it . effect ( "keeps a slow read's original deadline instead of freshening it on arrival" , ( ) =>
2848+ Effect . gen ( function * ( ) {
2849+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2850+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2851+ const started = yield * Deferred . make < void > ( ) ;
2852+ const finish = yield * Deferred . make < void > ( ) ;
2853+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2854+ backgroundRuntime . pending . length = 0 ;
2855+ const refresh = yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe (
2856+ Effect . provide (
2857+ snapshotRelayLayer ( ( ) =>
2858+ Deferred . succeed ( started , undefined ) . pipe (
2859+ Effect . andThen ( Deferred . await ( finish ) ) ,
2860+ Effect . as ( activeAgentActivitySnapshot ) ,
2861+ ) ,
2862+ ) ,
2863+ ) ,
2864+ Effect . forkChild ,
2865+ ) ;
2866+ yield * Deferred . await ( started ) ;
2867+ clock . mockReturnValue ( now + AGENT_ACTIVITY_FRESHNESS_MS + 1 ) ;
2868+ yield * Deferred . succeed ( finish , undefined ) ;
2869+ yield * Fiber . join ( refresh ) ;
2870+ const publication = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2871+ expect ( publication . expiresAt ) . toBe ( now + AGENT_ACTIVITY_FRESHNESS_MS ) ;
2872+ expect ( agentActivityTimeline ( publication , Date . now ( ) ) ) . toEqual ( [
2873+ {
2874+ date : new Date ( Date . now ( ) ) ,
2875+ props : expect . objectContaining ( { isExpired : true , activeCount : null } ) ,
2876+ } ,
2877+ ] ) ;
2878+ } ) . pipe ( Effect . scoped ) ,
2879+ ) ;
2880+
2881+ it . effect ( "renews unchanged live observations and preserves their deadline on background" , ( ) =>
2882+ Effect . gen ( function * ( ) {
2883+ const now = Date . parse ( "2026-10-02T12:00:00.000Z" ) ;
2884+ const clock = vi . spyOn ( Date , "now" ) . mockReturnValue ( now ) ;
2885+ addLiveEnvironment ( ) ;
2886+ setLiveShell ( "running" ) ;
2887+ setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "token-a" ) , "user-a" ) ;
2888+ backgroundRuntime . pending . length = 0 ;
2889+ const layer = snapshotRelayLayer ( ( { excludedEnvironmentIds } ) =>
2890+ Effect . succeed ( { aggregate : null , excludedEnvironmentIds : excludedEnvironmentIds ?? [ ] } ) ,
2891+ ) ;
2892+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2893+ const original = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2894+
2895+ clock . mockReturnValue ( now + 60_000 ) ;
2896+ setLiveShell ( "running" ) ;
2897+ yield * refreshActiveLiveActivityRemoteRegistration ( ) . pipe ( Effect . provide ( layer ) ) ;
2898+ const renewed = vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ;
2899+ expect ( renewed . activities . map ( ( { updatedAt : _updatedAt , ...row } ) => row ) ) . toEqual (
2900+ original . activities . map ( ( { updatedAt : _updatedAt , ...row } ) => row ) ,
2901+ ) ;
2902+ expect ( renewed . expiresAt ) . toBe ( now + 60_000 + AGENT_ACTIVITY_FRESHNESS_MS ) ;
2903+
2904+ clock . mockReturnValue ( now + 120_000 ) ;
2905+ appStateMock . currentState = "background" ;
2906+ for ( const listener of appStateMock . listeners ) listener ( "background" ) ;
2907+ expect ( vi . mocked ( publishAgentActivityWidget ) . mock . lastCall ! [ 0 ] ) . toMatchObject ( {
2908+ isStale : false ,
2909+ expiresAt : renewed . expiresAt ,
2910+ } ) ;
2911+ const timeline = agentActivityTimeline ( renewed , Date . now ( ) ) ;
2912+ expect ( timeline [ 0 ] ?. props . isExpired ) . toBeUndefined ( ) ;
2913+ expect ( timeline [ 1 ] ?. date . getTime ( ) ) . toBe ( renewed . expiresAt ) ;
2914+ expect ( timeline [ 1 ] ?. props . activities [ 0 ] ) . toMatchObject ( {
2915+ phase : "stale" ,
2916+ status : "Out of date" ,
2917+ } ) ;
2918+ } ) ,
2919+ ) ;
2920+
27172921 it . effect ( "detaches the shell publisher on sign-out and provider release" , ( ) => {
27182922 addLiveEnvironment ( ) ;
27192923 setAgentAwarenessRelayTokenProvider ( ( ) => Promise . resolve ( "clerk-token-user-a" ) , "user-a" ) ;
0 commit comments