Skip to content

Commit d335623

Browse files
authored
Refactor repo structure to prepare addition of a Rust recorder (#10)
We've prepared the repo to start working on a second Rust-based recorder. We've added a POC Rust module and CI scripts to verify that it builds correctly
2 parents 98e776e + 43090ec commit d335623

File tree

19 files changed

+429
-50
lines changed

19 files changed

+429
-50
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@ on:
77
branches: [main]
88

99
jobs:
10-
tests:
11-
name: Tests on ${{ matrix.os }}
12-
runs-on: ${{ matrix.os }}
13-
strategy:
14-
matrix:
15-
os: [ubuntu-latest, macos-latest, windows-latest]
16-
steps:
17-
- uses: actions/checkout@v4
18-
- name: Install make on Windows
19-
if: runner.os == 'Windows'
20-
run: choco install make -y
21-
- uses: actions/setup-python@v5
22-
with:
23-
python-version: '3.x'
24-
- name: Run tests
25-
shell: bash
26-
run: make test
27-
2810
nix-tests:
2911
runs-on: ubuntu-latest
3012
steps:
3113
- uses: actions/checkout@v4
3214
- uses: cachix/install-nix-action@v27
3315
with:
34-
nix_path: nixpkgs=channel:nixos-24.05
16+
nix_path: nixpkgs=channel:nixos-25.05
3517
extra_nix_config: |
3618
experimental-features = nix-command flakes
3719
- name: Run tests via Nix
38-
run: nix develop --command make test
20+
run: nix develop --command just test
21+
22+
rust-tests:
23+
name: Rust module test on ${{ matrix.os }} (Python ${{ matrix.python-version }})
24+
runs-on: ${{ matrix.os }}
25+
strategy:
26+
matrix:
27+
os: [ubuntu-latest, macos-latest, windows-latest]
28+
python-version: ["10", "11", "12", "13"]
29+
steps:
30+
- uses: actions/checkout@v4
31+
- uses: actions/setup-python@v5
32+
with:
33+
python-version: 3.${{ matrix.python-version }}
34+
- uses: astral-sh/setup-uv@v4
35+
- uses: messense/maturin-action@v1
36+
with:
37+
command: build
38+
args: --interpreter python3.${{ matrix.python-version }} -m crates/codetracer-python-recorder/Cargo.toml --release
39+
- name: Install and test built wheel with uv (pytest)
40+
shell: bash
41+
run: |
42+
v=${{matrix.python-version}}
43+
file=(crates/codetracer-python-recorder/target/wheels/*.whl)
44+
file="${file[0]}"
45+
uv run -p python3.$v --with "${file}" --with pytest -- python -m pytest crates/codetracer-python-recorder/test -q

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
.direnv/
22
**/__pycache__/
3+
.aider*
4+
.venv/
5+
**/target/
6+
build

AGENTS.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
# Instructions for Codex
22

3-
To run the test suite, execute:
3+
This repository contains two related projects:
4+
5+
- codetracer-pure-python-recorder — the original pure-Python tracer.
6+
- codetracer-python-recorder — a Rust-backed Python module built with PyO3 and maturin.
7+
8+
To run the Python test suite for the pure-Python tracer, execute:
49

510
```
6-
make test
11+
just test
712
```
813

914
The tester executes a number of sample programs in `tests/programs` and compares their outputs to the fixtures in `tests/fixtures`.
1015

16+
To build and locally develop-install the Rust-backed module:
17+
18+
```
19+
just build-rust
20+
# or:
21+
maturin develop -m crates/codetracer-python-recorder/Cargo.toml
22+
```
23+
1124
# Code quality guidelines
1225

1326
- Strive to achieve high code quality.
@@ -33,4 +46,4 @@ https://www.conventionalcommits.org/en/v1.0.0/
3346

3447
In the remaining lines, provide a short description of the implemented functionality.
3548
Provide sufficient details for the justification of each design decision if multiple
36-
approaches were considered.
49+
approaches were considered.

Justfile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Development helpers for the monorepo
2+
3+
# Python versions used for multi-version testing/building with uv
4+
PY_VERSIONS := "3.10 3.11 3.12 3.13"
5+
PY_SHORT_VERSIONS := "10 11 12 13"
6+
# Print toolchain versions to verify the dev environment
7+
env:
8+
python3 --version
9+
cargo --version
10+
rustc --version
11+
maturin --version
12+
13+
# Create a local virtualenv for Python tooling
14+
venv:
15+
test -d .venv || python3 -m venv .venv
16+
17+
# Build and develop-install the Rust-backed Python module
18+
build-rust:
19+
test -d .venv || python3 -m venv .venv
20+
VIRTUAL_ENV=.venv maturin develop -m crates/codetracer-python-recorder/Cargo.toml
21+
22+
# Smoke test the Rust module after build
23+
smoke-rust:
24+
.venv/bin/python -m pip install -U pip pytest
25+
.venv/bin/python -m pytest crates/codetracer-python-recorder/test -q
26+
27+
# Run the Python test suite for the pure-Python recorder
28+
test:
29+
python3 -m unittest discover -v
30+
31+
# Run the test suite across multiple Python versions using uv
32+
test-uv-all:
33+
uv python install {{PY_VERSIONS}}
34+
for v in {{PY_VERSIONS}}; do uv run -p "$v" -m unittest discover -v; done
35+
36+
# Build wheels for all target Python versions with maturin
37+
build-rust-uv-all:
38+
for v in {{PY_VERSIONS}}; do \
39+
maturin build --interpreter "python$v" -m crates/codetracer-python-recorder/Cargo.toml --release; \
40+
done
41+
42+
# Smoke the built Rust wheels across versions using uv
43+
smoke-rust-uv-all:
44+
for v in {{PY_SHORT_VERSIONS}}; do \
45+
file=(crates/codetracer-python-recorder/target/wheels/codetracer_python_recorder-*-cp3$v-cp3$v-*.whl); \
46+
file="${file[0]}"; \
47+
uv run -p "python3.$v" --with "${file}" --with pytest -- python -m pytest crates/codetracer-python-recorder/test -q; \
48+
done

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
# This Makefile is deprecated. Use 'just test' instead.
12
.PHONY: test
2-
33
test:
4-
python3 -m unittest discover -v
4+
@echo "Deprecated: Use 'just test' instead." && false

README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
1-
## codetracer-python-recorder
1+
## CodeTracer Recorders (Monorepo)
22

3-
An unfinished prototype of a recorder of Python programs that produces [CodeTracer](https://github.com/metacraft-labs/CodeTracer) traces.
3+
This repository now hosts two related projects:
44

5-
> [!WARNING]
6-
> Currently it is in a very early phase: we're welcoming contribution and discussion!
5+
- codetracer-pure-python-recorder — the existing pure-Python prototype that records [CodeTracer](https://github.com/metacraft-labs/CodeTracer) traces using sys.settrace.
6+
- codetracer-python-recorder — a new, Rust-backed Python extension module (PyO3) intended to provide a faster and more featureful recorder.
77

8+
> [!WARNING]
9+
> Both projects are early-stage prototypes. Contributions and discussion are welcome!
810
9-
### Usage
11+
### codetracer-pure-python-recorder
1012

11-
Install the package with `pip` or `uv`:
13+
Install from PyPI:
1214

1315
```bash
14-
pip install codetracer-python-recorder
16+
pip install codetracer-pure-python-recorder
1517
```
1618

17-
Then invoke the recorder as a command line tool:
19+
CLI usage:
1820

1921
```bash
2022
codetracer-record <path to python file>
2123
# produces several trace json files in the current directory
2224
# or in the folder of `$CODETRACER_DB_TRACE_PATH` if such an env var is defined
2325
```
2426

25-
During development you can also run it directly with
27+
During development you can also run it directly:
2628

2729
```bash
28-
python trace.py <path to python file>
30+
python src/trace.py <path to python file>
2931
# produces several trace json files in the current directory
3032
# or in the folder of `$CODETRACER_DB_TRACE_PATH` if such an env var is defined
3133
```
3234

33-
however you probably want to use it in combination with CodeTracer, which would be released soon.
35+
### codetracer-python-recorder (Rust-backed)
36+
37+
A separate Python module implemented in Rust with PyO3 and built via maturin lives under:
38+
crates/codetracer-python-recorder/
39+
40+
Basic workflow:
41+
42+
- Build/dev install the Rust module:
43+
- maturin develop -m crates/codetracer-python-recorder/Cargo.toml
44+
- Use in Python:
45+
- from codetracer_python_recorder import hello
46+
- hello()
3447

35-
## Future directions
48+
### Future directions
3649

3750
The current Python support is an unfinished prototype. We can finish it. In the future, it may be expanded to function in a way to similar to the more complete implementations, e.g. [Noir](https://github.com/blocksense-network/noir/tree/blocksense/tooling/tracer).
3851

3952
Currently it's very similar to our [Ruby tracer](https://github.com/metacraft-labs/ct-ruby-tracer)
4053

41-
### Current approach: sys.settrace API
54+
#### Current approach: sys.settrace API
4255

4356
Currently we're using the sys.settrace API: https://docs.python.org/3/library/sys.html#sys.settrace .
4457
This is very flexible and can function with probably multiple Python versions out of the box.
@@ -49,15 +62,15 @@ However, this is limited:
4962

5063
For other languages, we've used a more deeply integrated approach: patching the interpreter or VM itself (e.g. Noir).
5164

52-
### Patching the VM
65+
#### Patching the VM
5366

5467
This can be a good approach for Python as well: it can let us record more precisely subvalues, assignments and subexpressions and to let
5568
some CodeTracer features work in a deeper/better way.
5669

5770
One usually needs to add additional logic to places where new opcodes/lines are being ran, and to call entries/exits. Additionally
5871
tracking assignments can be a great addition, but it really depends on the interpreter internals.
5972

60-
### Filtering
73+
#### Filtering
6174

6275
It would be useful to have a way to record in detail only certain periods of the program, or certain functions or modules:
6376
we plan on expanding the [trace format](https://github.com/metacraft-labs/runtime_tracing/) and CodeTracer' support, so that this is possible. It would let one be able to record interesting

0 commit comments

Comments
 (0)