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

34 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-03 23:00 +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''' 

25 

26 

27from . import __ 

28from .base import RENDERERS as _RENDERERS 

29from .base import ExplicitTargetMode as _ExplicitTargetMode 

30from .base import RendererBase as _RendererBase 

31from .base import extract_coder_configuration as _extract_coder_configuration 

32 

33 

34class CodexRenderer( _RendererBase ): 

35 ''' Renderer for Codex CLI coder. 

36 

37 Supports both per-user and per-project configuration modes. 

38 

39 Per-project mode stores configuration under 

40 `.auxiliary/configuration/coders/codex/` and exposes it via a 

41 `.codex` symlink at project root (created by population logic). 

42 

43 Per-user mode respects CODEX_HOME environment variable with fallback 

44 to configuration overrides and default location. 

45 ''' 

46 

47 name = 'codex' 

48 modes_available = frozenset( ( 'per-user', 'per-project' ) ) 

49 mode_default = 'per-project' 

50 memory_filename = 'AGENTS.md' 

51 item_types_available = frozenset( ( 'skills', ) ) 

52 

53 def get_template_flavor( self, item_type: str ) -> str: 

54 ''' Determines template flavor for Codex CLI. 

55 

56 Codex uses same markdown format as Claude for all item types, 

57 so always returns 'claude' flavor. 

58 ''' 

59 return 'claude' 

60 

61 def provide_project_symlinks( 

62 self, target: __.Path 

63 ) -> __.cabc.Sequence[ tuple[ __.Path, __.Path ] ]: 

64 ''' Provides symlinks required for Codex CLI in per-project mode. 

65 

66 Codex expects project configuration under `.codex/`. We keep the 

67 canonical configuration under `.auxiliary/configuration/` and 

68 expose it via the standard coder directory symlink. 

69 ''' 

70 return super( ).provide_project_symlinks( target ) 

71 

72 def resolve_base_directory( 

73 self, 

74 mode: _ExplicitTargetMode, 

75 target: __.Path, 

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

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

78 ) -> __.Path: 

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

80 

81 Per-project: `.auxiliary/configuration/coders/codex/` 

82 Per-user: respects precedence: CODEX_HOME environment variable, 

83 configuration file override (home key), or default `~/.codex`. 

84 ''' 

85 self.validate_mode( mode ) 

86 if mode == 'per-project': 

87 return target / ".auxiliary/configuration/coders/codex" 

88 if mode == 'per-user': 88 ↛ 90line 88 didn't jump to line 90 because the condition on line 88 was always true

89 return self._resolve_user_directory( configuration, environment ) 

90 raise __.TargetModeNoSupport( self.name, mode ) 

91 

92 def _resolve_user_directory( 

93 self, 

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

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

96 ) -> __.Path: 

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

98 

99 Precedence order: 

100 1. CODEX_HOME environment variable 

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

102 3. Default ~/.codex location 

103 ''' 

104 if 'CODEX_HOME' in environment: 

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

106 return directory.expanduser( ) 

107 coder_configuration = self._extract_coder_configuration( 

108 configuration ) 

109 if 'home' in coder_configuration: 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true

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

111 return directory.expanduser( ) 

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

113 

114 def _extract_coder_configuration( 

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

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

117 ''' Extracts configuration for this specific coder. 

118 

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

120 ''' 

121 return _extract_coder_configuration( configuration, self.name ) 

122 

123 

124_RENDERERS[ 'codex' ] = CodexRenderer( )