Diff of /docs/ops/CodeOps.md [000000] .. [3af7d7]

Switch to unified view

a b/docs/ops/CodeOps.md
1
---
2
hide:
3
  - navigation
4
  - toc
5
---
6
7
<img src='../VPE.png' height="50" width="50"></img>
8
9
# <font color=black>CodeOps</font>
10
> <font color=black>ℹ️</font><br>
11
> **Date** 2024-11-02<br>
12
> **Version** 1.0<br>
13
14
1. [Overview](#overview)
15
2. [GitHub Repo Types](#github-repo-types)
16
3. [GitHub templates for PRs, features and bugs](#github-templates-for-prs-features-and-bugs)
17
4. [Coding practices](#coding-practices)
18
5. [Testing locally](#testing-locally)
19
6. [Keywords in commit messages](#keywords-in-commit-messages)
20
7. [Resources](#resources)
21
22
<hr>
23
24
## Overview
25
Welcome to Team VPE’s CodeOps document!
26
27
This document serves as a guide to our team's approach to managing code. Your insights and feedback are highly encouraged 😊. Please provide feedback via [GitHub Issues](https://github.com/VirtualPatientEngine/AIAgents4Pharma/issues). Thanks 😊.
28
29
This guide will cover the following topics relevant for our CodeOps:.
30
31
🗸 **GitHub repo organization**: About the organization of the repository on GitHub.<br>
32
🗸 **GitHub templates**: Explains the predefined templates that are provided in a standardized format to provide details on proposed changes, new features, or bugs.<br>
33
🗸 **Coding Practices**: Outlines the coding standards that we strive to follow to ensure the quality, maintainability, and consistency of our codebase.<br>
34
🗸 **Testing locally**: Steps to run unit tests locally before committing your code, thereby reducing workflow failures on GitHub Actions prior to commit.<br>
35
🗸 **Keywords in Commit Messages**: Specific keywords in commit messages that can trigger release on GitHub Actions.<br>
36
🗸 **Resources for Further Reading**: Additional reading material.<br>
37
38
## GitHub Repo Organization
39
This repository is intended to be public facing, encouraging easy collaboration, and sharing within the wider community. It follows a modular structure that allows developers to focus on individual AI Agent modules or compose a complete application using all or some of the available AI Agents. Our folder structure is as follows:
40
41
| Folder or File | Description |
42
| -------------- | ----------- |
43
| .gitignore | Path to files and folders to be ignored |
44
| .github/ | Workflows for continuous integration (CI) and templates (Bug/Feature/PR) |
45
| app/ | Where the application code sits |
46
| app/\<frontend or backend\>/src/ | Code related to the client web UI or server backend, respectively |
47
| app/\<frontend or backend\>/tests/ | All pytests for the frontend or backend |
48
| app/\<frontend or backend\>/docs/ | All documentation for the frontend or backend |
49
| app/\<frontend or backend\>/pyproject.toml | List all the packages required for the front-end or backend |
50
| app/\<frontend or backend\>/LICENSE | If differing for the frontend or backend compared to the LICENSE of the repo |
51
| app/\<frontend or backend\>/README.md | Description of the frontend or backend |
52
| agents/ | Where the AI agents code sits |
53
| agents/\<agent\>/src/ |Code related to a particular <agent> |
54
| agents/\<agent\>/src/models/ | All code that is specific to defining the decision making of a particular \<agent\> |
55
| agents/\<agent\>/src/tools/ | All code that is specific to defining functionality of tools available to a particular \<agent\> |
56
| agents/\<agent\>/src/prompts/ | Prompts that are specific to a particular \<agent\> |
57
| agents/\<agent\>/tests/ | All pytests for a particular \<agent\> |
58
| agents/\<agent\>/docs/ | All documentation for a particular \<agent\> |
59
| agents/\<agent\>/examples/ | Notebooks exemplifying how to use a particular \<agent\> |
60
| agents/\<agent\>/pyproject.toml | Python installation script for a particular \<agent\> |
61
| agents/\<agent\>/LICENSE | If differing for a particular \<agent\> compared to the LICENSE of the repo |
62
| agents/\<agent\>/README.md | Description of a particular \<agent\> |
63
| docs/ | Where you write .md files for MkDocs for the repository website |
64
| env/ | Dockerfiles or scripts for setting up a virtual environment for development or deployment |
65
| node_modules | Packages required by sematic-release (do not modify/delete) |
66
| pyproject.toml | Build script for the repository following [PEP 518](https://peps.python.org/pep-0518/) |
67
| LICENSE | You know what it means |
68
| README.md | Description of your repo |
69
| CONTRIBUTING.md | Contributing guidelines |
70
| *.yml Files | that come with semantic-release and MkDocs |
71
| *.json | They are pre-configured |
72
| *.js | Modify them based on your need |
73
74
## GitHub templates for PRs, features, and bugs
75
We have created three essential templates: the Pull Request (PR) template, the Feature Request template, and the Bug Report template (all present in the .github/ folder). Each serves a distinct purpose in streamlining our workflow and ensuring effective communication among team members and contributors.
76
77
1. The **PR template** serves as a structured guide for anyone submitting a pull request. It outlines essential details such as the purpose of the changes, any associated issues or feature requests, testing instructions, and any necessary documentation updates. By adhering to this template, contributors provide comprehensive context, making the review process smoother and more efficient.
78
2. The **Feature Request** template offers a standardized format for suggesting new functionalities or enhancements to our projects. It prompts users to describe the desired feature, its rationale, and any potential challenges or considerations.
79
3. The **Bug Report** template assists users in reporting issues or bugs encountered within our projects. It encourages clear and concise descriptions of the problem, including steps to reproduce, expected behavior, and any relevant screenshots or error messages.
80
81
Upon opening an issue on GitHub, users are prompted to select the appropriate template based on their specific needs—whether it's a bug report, or a feature request. Likewise, when initiating a pull request, the PR template automatically loads, guiding contributors through the necessary steps to ensure thorough documentation and review of their proposed changes.
82
83
## Coding practices
84
### Branching model
85
1. [GitHub Flow](https://docs.github.com/en/get-started/using-github/github-flow)
86
2. How to use branches:
87
    1. Tagged branches (on VPE account) for releases.
88
    2. Main branch (on VPE account) to start all Feature/Fix branches (on private account).
89
    3. Merge of Feature/Fix branches (on private account) into Main (on VPE account) following a successful PR.
90
    4. Tag the Main (on VPE accounts) for a new release.
91
92
### Unit Tests
93
1. Each class/method should have a [unit test](https://en.wikipedia.org/wiki/Unit_testing)
94
2. The tests must cover at least the following:
95
    1. Unit testing: see [PyTest](https://docs.pytest.org/en/7.4.x/)
96
    2. Linting: see [Pylint](https://pypi.org/project/pylint/)
97
    3. Code coverage: see [Coverage](https://coverage.readthedocs.io/en/7.3.2/)
98
    4. Works on Linux, Windows, and Mac machines
99
100
*NOTE: All tests must be written in the tests/ folder (this is where pytest will search by default)*
101
102
*Pro-tips:
103
  - Use the GitHub co-pilot to write docstrings (though not always accurate).
104
  - Install PyLint on VS code to spot the linting errors on the fly.*
105
106
### PR policies
107
1. Number of approving reviewers on a PR: >= 1
108
2. Passing unit testing (pytest)
109
3. Passing linting (pylint)
110
4. Passing coverage (coverage)
111
112
### Documentation of classes, methods, and APIs
113
Use [MkDocs](https://www.mkdocs.org/). Refer to the DevOps guide for more details.
114
115
### Best practices in Python
116
1. Choose your preferred Python version, but ensure your repository's environment passes tests on Windows, macOS, and Linux (you should be able to test that via GitHub actions).
117
2. Coding style -> Numpy style ([Style guide](https://numpydoc.readthedocs.io/en/latest/format.html)) for Python using Flake8
118
3. Use modules and packages (add __init__.py)
119
4. One Class per script
120
5. Separate packages for utilities/helper functions
121
6. Import the module, not the function (call the function by accessing the module in the code)
122
123
```
124
# bad practice
125
import module1.pkg
126
result = pkg()
127
```
128
129
```
130
# good practice
131
import module1
132
result = module1.pkg()
133
# You always know where the function is coming from
134
# Avoids polluting the global name space
135
```
136
137
7. Readable code >> efficient code
138
8. Use type hinting whenever possible
139
140
```
141
def greeting(name: str) - > str:
142
  return "Hello "+name
143
```
144
145
9. Use List comprehension whenever possible (but don't forget the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle))
146
147
```
148
list = [x*2 for x in array if x > 1]
149
```
150
151
10. Docstring for methods and classes
152
153
```
154
def sum(a: int, b: int) -> int:
155
    """
156
    Function to return sum of 2 integers
157
158
    Args:
159
        a: first number
160
        b: second number
161
162
    Returns:
163
        int: sum of 2 integers
164
    """
165
    return (a + b)
166
```
167
168
*Pro-tip: Use co-pilot to automatically write a docstring for the methods/classes (though not always accurate)*
169
170
11. Examples: Jupyter notebook with the following 
171
    1. Clear API calls to the sources of all data to run the analysis.
172
    2. Record of all analyses and figure generation routines.
173
    3. Documentation of the analysis so that another team member could reproduce the results in your absence.
174
175
## Testing locally
176
To streamline our development process and save time, we've implemented a CI/CD pipeline that includes automated testing through GitHub Actions (see the DevOps document for details). Essentially, each time code is pushed to GitHub, a TESTS workflow is triggered to test the code automatically. However, running these tests on GitHub Actions can be time-consuming. To optimize efficiency and catch issues early, **it's recommended to run the tests locally before committing changes to GitHub**. This involves executing pytest, pylint, and coverage tests (which are the core of the TESTS workflow) locally to ensure code quality and test coverage meet our standards. Below are the commands to execute these tests locally:
177
178
### pytest:
179
**Job**: test scripts in the tests/ folder<br>
180
**Passing-criteria**: pass all the tests<br>
181
182
```
183
pytest
184
```
185
186
*Note: Running pytest without any options can sometimes execute all the python files, including unintended ones. To avoid this, you can specify the folders you want to test. For example, running*
187
188
```
189
pytest tests/
190
```
191
192
*will execute pytest only on the tests/ folder. It is important to ensure that pytest is run on at least the app/ and agents/ folders. Additionally, if you choose to run pytest on specific folders while testing locally, you must also do the same in the workflow files for GitHub actions (see the section on Automated workflows in the DevOps guide).*
193
194
### pylint:
195
**Action**: lint all *.py scripts in the specified folder<br>
196
**Passing-criteria**: code rating must be **10.00/10.00**<br>
197
198
```
199
pylint app/
200
pylint agents/
201
```
202
203
*Note: If you want to disable a particular warning, use the disable option in pylint.
204
For example, running*
205
206
```
207
pylint --disable=R0801,W0613 app/
208
pylint --disable=R0801,W0613 agents/
209
```
210
211
*will ignore the warnings with codes [R0801](https://pylint.readthedocs.io/en/stable/user_guide/messages/refactor/duplicate-code.html) and [W0613](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unused-argument.html). Choose to disable warnings wisely. Additionally, if you choose to disable a warning while testing locally, you must also disable it in the workflow files for GitHub Actions (see the section on Automated workflows in the DevOps guide). We have already disabled a few warnings. Please look at the [tests.yml](https://github.com/VirtualPatientEngine/AIAgents4Pharma/main/.github/workflows/tests.yml) to know the warnings we have currently disabled.*
212
213
### coverage:
214
**Job**: makes sure every method is called at least once in the tests/ folder<br>
215
**Passing-criteria**: 100% score<br>
216
217
```
218
coverage run –m pytest agents app
219
coverage report –m
220
```
221
222
*Note: Lines to be excluded should be specified in the file .coveragerc*
223
224
### MkDocs:
225
**Job**: Hosts the documentation locally<br>
226
**Passing-criteria**: Manual assessment<br>
227
228
```
229
mkdocs serve
230
```
231
232
*NOTE: Please refer to the "Unit Tests" subsection within the "Coding Practices" section for further details.*
233
234
## Keywords in commit messages
235
We use [semantic-release](https://github.com/semantic-release/semantic-release) software to automate the process of versioning and releasing code on GitHub. It operates by analyzing commit messages to determine the type of changes made in the codebase. Following the Semantic Versioning convention, commonly known as SemVer, version numbers are structured as MAJOR.MINOR.PATCH. The MAJOR version is incremented for incompatible changes in the code, MINOR for feature additions, and PATCH for backward-compatible bug fixes. This automated approach ensures that version increments are consistent and meaningful, aiding developers and users in understanding the impact of updates.
236
237
**Job: Bump up the release (MAJOR.MINOR.PATCH) based on the commit message**
238
239
###  “feat:” 
240
will bump up the minor version (MINOR)
241
242
```
243
git commit –m “feat: add a new feature”
244
```
245
246
### “fix:”
247
will bump up the patch version (PATCH)
248
249
```
250
git commit –m “fix: fix bug”
251
```
252
253
### “feat:” or “fix:” followed by “BREAKING CHANGE:” 
254
will bump up the major version (MAJOR)
255
256
```
257
git commit –m “feat: add a new feature
258
BREAKING CHANGE: update several features”
259
```
260
261
#### Notes:
262
1. The first line of the commit message must begin with the keyword “feat” or “fix” and the last line with "BREAKING_CHANGE" to prompt a major version bump.
263
2. Write the commit message on GitHub when approving the merge between the 'develop' branch (on VPE account) and the 'main' branch (on VPE account), this ensures that the 'release.yml' will run using GitHub Actions. For all other cases, this commit message will not trigger any actions."
264
3. **The obligation of bumping up the major version lies with the reviewer.**
265
266
### “chore”
267
triggers no action.
268
269
```
270
git commit –m “chore: add new example in the folder
271
```
272
273
*Please note it is mandatory to specify keywords when merging a pull request (e.g.: merging develop into main) on GitHub that results in a release or when pushing code to GitHub that will lead to a release. In most cases the release workflow activation is linked to the keywords mentioned. Leaf through the section on ‘How to open and close a Pull request on GitHub’ in the DevOps guide to know more.*
274
275
## Resources
276
- Outline of working with GitHub for collaborative projects [GitHub flow](https://docs.github.com/en/get-started/quickstart/github-flow).
277
Read more about: i. [GitHub Actions](https://docs.github.com/en/actions/using-workflows) workflows and ii. [Semantic versioning](https://semver.org/)