Coverage for sources/agentsmgr/renderers/codex.py: 28%

29 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-13 00:43 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Codex CLI renderer implementation. 

22 

23 Provides path resolution and targeting mode validation for Codex CLI. 

24 Codex CLI only supports per-user configuration as of version 0.44.0. 

25''' 

26 

27 

28from . import __ 

29from .base import RENDERERS, ExplicitTargetMode, RendererBase 

30 

31 

32class CodexRenderer( RendererBase ): 

33 ''' Renderer for Codex CLI coder. 

34 

35 Only supports per-user configuration mode. Codex CLI does not 

36 support per-project configuration as of version 0.44.0. Per-user 

37 mode respects CODEX_HOME environment variable with fallback to 

38 configuration overrides and default location. 

39 ''' 

40 

41 name = 'codex' 

42 modes_available = frozenset( ( 'per-user', ) ) 

43 mode_default = 'per-user' 

44 memory_filename = 'AGENTS.md' 

45 

46 def resolve_base_directory( 

47 self, 

48 mode: ExplicitTargetMode, 

49 target: __.Path, 

50 configuration: __.cabc.Mapping[ str, __.typx.Any ], 

51 environment: __.cabc.Mapping[ str, str ], 

52 ) -> __.Path: 

53 ''' Resolves base output directory for Codex CLI. 

54 

55 Only per-user mode is supported. Respects precedence: CODEX_HOME 

56 environment variable, configuration file override (home key), or 

57 default ~/.codex location. Per-project mode raises error with 

58 explanation of Codex CLI limitation. 

59 ''' 

60 self.validate_mode( mode ) 

61 if mode == 'per-user': 

62 return self._resolve_user_directory( configuration, environment ) 

63 reason = ( 

64 "Codex CLI does not support per-project configuration. " 

65 "Only per-user configuration in ~/.codex or $CODEX_HOME " 

66 "is supported as of version 0.44.0." ) 

67 raise __.TargetModeNoSupport( self.name, mode, reason ) 

68 

69 def _resolve_user_directory( 

70 self, 

71 configuration: __.cabc.Mapping[ str, __.typx.Any ], 

72 environment: __.cabc.Mapping[ str, str ], 

73 ) -> __.Path: 

74 ''' Resolves per-user directory following precedence rules. 

75 

76 Precedence order: 

77 1. CODEX_HOME environment variable 

78 2. Configuration file override (home key for this coder) 

79 3. Default ~/.codex location 

80 ''' 

81 if 'CODEX_HOME' in environment: 

82 directory = __.Path( environment[ 'CODEX_HOME' ] ) 

83 return directory.expanduser( ) 

84 coder_configuration = self._extract_coder_configuration( 

85 configuration ) 

86 if 'home' in coder_configuration: 

87 directory = __.Path( coder_configuration[ 'home' ] ) 

88 return directory.expanduser( ) 

89 return __.Path.home( ) / '.codex' 

90 

91 def _extract_coder_configuration( 

92 self, configuration: __.cabc.Mapping[ str, __.typx.Any ] 

93 ) -> __.cabc.Mapping[ str, __.typx.Any ]: 

94 ''' Extracts configuration for this specific coder. 

95 

96 Looks for coder entry in configuration coders array by name. 

97 ''' 

98 coders = configuration.get( 'coders', ( ) ) 

99 for coder in coders: 

100 if coder.get( 'name' ) == self.name: 

101 return coder 

102 return { } 

103 

104 

105RENDERERS[ 'codex' ] = CodexRenderer( )