Skip to content

Commit ea6db4f

Browse files
committed
Milestone 5 - Step 6
codetracer-python-recorder/src/runtime/mod.rs: codetracer-python-recorder/src/runtime/tracer/io.rs: codetracer-python-recorder/src/runtime/tracer/mod.rs: design-docs/adr/0001-file-level-single-responsibility.md: design-docs/codetracer-architecture-refactor-implementation-plan.status.md: design-docs/file-level-srp-refactor-plan.md: design-docs/value-capture.md: Signed-off-by: Tzanko Matev <[email protected]>
1 parent ca58b35 commit ea6db4f

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

codetracer-python-recorder/src/runtime/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
//! Runtime tracer facade translating sys.monitoring callbacks into `runtime_tracing` records.
1+
//! Runtime tracing facade wiring sys.monitoring callbacks into dedicated collaborators.
2+
//!
3+
//! The [`tracer`] module hosts lifecycle, IO, filtering, and event pipelines and re-exports
4+
//! [`RuntimeTracer`] so callers can keep importing it from `crate::runtime`.
25
36
mod activation;
47
mod frame_inspector;
@@ -10,7 +13,5 @@ pub mod tracer;
1013
mod value_capture;
1114
mod value_encoder;
1215

13-
#[allow(unused_imports)]
14-
pub use line_snapshots::{FrameId, LineSnapshotStore};
1516
pub use output_paths::TraceOutputPaths;
1617
pub use tracer::RuntimeTracer;

codetracer-python-recorder/src/runtime/tracer/io.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,37 @@ use std::sync::Arc;
1515
use std::thread::ThreadId;
1616

1717
/// Coordinates installation, flushing, and teardown of the IO capture pipeline.
18-
pub struct IoCoordinator {
18+
pub(crate) struct IoCoordinator {
1919
snapshots: Arc<LineSnapshotStore>,
2020
pipeline: Option<IoCapturePipeline>,
2121
}
2222

2323
impl IoCoordinator {
2424
/// Create a coordinator with a fresh snapshot store and no active pipeline.
25-
pub fn new() -> Self {
25+
pub(crate) fn new() -> Self {
2626
Self {
2727
snapshots: Arc::new(LineSnapshotStore::new()),
2828
pipeline: None,
2929
}
3030
}
3131

3232
/// Expose the shared snapshot store for collaborators (tests, IO capture).
33-
pub fn snapshot_store(&self) -> Arc<LineSnapshotStore> {
33+
pub(crate) fn snapshot_store(&self) -> Arc<LineSnapshotStore> {
3434
Arc::clone(&self.snapshots)
3535
}
3636

3737
/// Install the IO capture pipeline using the provided settings.
38-
pub fn install(&mut self, py: Python<'_>, settings: IoCaptureSettings) -> PyResult<()> {
38+
pub(crate) fn install(
39+
&mut self,
40+
py: Python<'_>,
41+
settings: IoCaptureSettings,
42+
) -> PyResult<()> {
3943
self.pipeline = IoCapturePipeline::install(py, Arc::clone(&self.snapshots), settings)?;
4044
Ok(())
4145
}
4246

4347
/// Flush buffered output for the active thread before emitting a step event.
44-
pub fn flush_before_step(
48+
pub(crate) fn flush_before_step(
4549
&self,
4650
thread_id: ThreadId,
4751
writer: &mut NonStreamingTraceWriter,
@@ -55,7 +59,7 @@ impl IoCoordinator {
5559
}
5660

5761
/// Flush every buffered chunk regardless of thread affinity.
58-
pub fn flush_all(&self, writer: &mut NonStreamingTraceWriter) -> bool {
62+
pub(crate) fn flush_all(&self, writer: &mut NonStreamingTraceWriter) -> bool {
5963
let Some(pipeline) = self.pipeline.as_ref() else {
6064
return false;
6165
};
@@ -65,7 +69,11 @@ impl IoCoordinator {
6569
}
6670

6771
/// Drain remaining chunks and uninstall the capture pipeline.
68-
pub fn teardown(&mut self, py: Python<'_>, writer: &mut NonStreamingTraceWriter) -> bool {
72+
pub(crate) fn teardown(
73+
&mut self,
74+
py: Python<'_>,
75+
writer: &mut NonStreamingTraceWriter,
76+
) -> bool {
6977
let Some(mut pipeline) = self.pipeline.take() else {
7078
return false;
7179
};
@@ -87,12 +95,12 @@ impl IoCoordinator {
8795
}
8896

8997
/// Clear the snapshot cache once tracing concludes.
90-
pub fn clear_snapshots(&self) {
98+
pub(crate) fn clear_snapshots(&self) {
9199
self.snapshots.clear();
92100
}
93101

94102
/// Record the latest frame snapshot for the active thread.
95-
pub fn record_snapshot(
103+
pub(crate) fn record_snapshot(
96104
&self,
97105
thread_id: ThreadId,
98106
path_id: PathId,
@@ -192,7 +200,7 @@ impl IoCoordinator {
192200
}
193201

194202
/// Translate chunk flags into telemetry labels.
195-
pub fn flag_labels(flags: IoChunkFlags) -> Vec<&'static str> {
203+
fn flag_labels(flags: IoChunkFlags) -> Vec<&'static str> {
196204
let mut labels = Vec::new();
197205
if flags.contains(IoChunkFlags::NEWLINE_TERMINATED) {
198206
labels.push("newline");

codetracer-python-recorder/src/runtime/tracer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Collaborators for the runtime tracer lifecycle, IO coordination, filtering, and event handling.
2+
//!
3+
//! Re-exports [`RuntimeTracer`] so downstream callers continue using `crate::runtime::RuntimeTracer`.
24
35
pub mod events;
46
pub mod filtering;

design-docs/adr/0001-file-level-single-responsibility.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
The codetracer Python recorder crate has evolved quickly and several source files now mix unrelated concerns:
1111
- [`src/lib.rs`](../../codetracer-python-recorder/src/lib.rs) hosts PyO3 module wiring, global logging setup, tracing session state, and filesystem validation in one place.
12-
- [`src/runtime_tracer.rs`](../../codetracer-python-recorder/src/runtime_tracer.rs) interleaves activation gating, writer lifecycle control, PyFrame helpers, and Python value encoding logic, making it challenging to test or extend any portion independently.
12+
- [`src/runtime/tracer/runtime_tracer.rs`](../../codetracer-python-recorder/src/runtime/tracer/runtime_tracer.rs) (formerly `src/runtime_tracer.rs`) interleaves activation gating, writer lifecycle control, PyFrame helpers, and Python value encoding logic, making it challenging to test or extend any portion independently.
1313
- [`src/tracer.rs`](../../codetracer-python-recorder/src/tracer.rs) combines sys.monitoring shim code with the `Tracer` trait, callback registration, and global caches.
1414
- [`codetracer_python_recorder/api.py`](../../codetracer-python-recorder/codetracer_python_recorder/api.py) mixes format constants, backend interaction, context manager ergonomics, and environment based auto-start side effects.
1515

@@ -43,7 +43,7 @@ These changes are mechanical reorganisations—no behavioural changes are expect
4343
2. **Preserve APIs.** When moving functions, re-export them from their new module so that existing callers (Rust and Python) compile without modification in the same PR.
4444
3. **Add Focused Tests.** Whenever a helper is extracted (e.g., value encoding), add or migrate unit tests that cover its edge cases.
4545
4. **Document Moves.** Update doc comments and module-level docs to reflect the new structure. Remove outdated TODOs or convert them into follow-up issues.
46-
5. **Coordinate on Shared Types.** When splitting `runtime_tracer.rs`, agree on ownership for shared structs (e.g., `RuntimeTracer` remains in `runtime/mod.rs`). Use `pub(crate)` to keep internals encapsulated.
46+
5. **Coordinate on Shared Types.** When evolving the `runtime::tracer` modules, agree on ownership for shared structs (e.g., `RuntimeTracer` remains re-exported from `runtime/mod.rs`). Use `pub(crate)` to keep internals encapsulated.
4747
6. **Python Imports.** After splitting the Python modules, ensure `__all__` in `__init__.py` continues to export the public API. Use relative imports to avoid accidental circular dependencies.
4848
7. **Parallel Work.** Follow the sequencing from `design-docs/file-level-srp-refactor-plan.md` to know when tasks can proceed in parallel.
4949

design-docs/codetracer-architecture-refactor-implementation-plan.status.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
- ✅ Milestone 5 Step 3: introduced `runtime::tracer::filtering::FilterCoordinator` to own scope resolution, skip caching, telemetry stats, and metadata wiring. `RuntimeTracer` now delegates trace decisions and summary emission, while tests continue to validate skip behaviour and metadata shape with unchanged expectations.
8585
- ✅ Milestone 5 Step 4: carved lifecycle orchestration into `runtime::tracer::lifecycle::LifecycleController`, covering activation gating, writer initialisation/finalisation, policy enforcement, failure cleanup, and trace id scoping. Added focused unit tests for the controller and re-ran `just test` (nextest + pytest) to verify no behavioural drift.
8686
- ✅ Milestone 5 Step 5: shifted event handling into `runtime::tracer::events`, relocating the `Tracer` trait implementation alongside failure-injection helpers and telemetry wiring. `RuntimeTracer` now exposes a slim collaborator API (`mark_event`, `flush_io_before_step`, `ensure_function_id`), while tests import the trait explicitly. `just test` (nextest + pytest) confirms the callbacks behave identically after the split.
87+
- ✅ Milestone 5 Step 6: harmonised the tracer module facade by tightening `IoCoordinator` visibility, pruning unused re-exports, documenting the `runtime::tracer` layout, and updating design docs that referenced the legacy `runtime_tracer.rs` path. `just test` (Rust nextest + Python pytest) verified the cleanup.
8788

8889

8990
### Planned Extraction Order (Milestone 4)
@@ -114,5 +115,5 @@
114115
5. **Tests:** After each move, update unit tests in `trace_filter` modules and dependent integration tests (`session/bootstrap.rs` tests, `runtime` tests). Targeted command: `just test` (covers Rust + Python suites).
115116

116117
## Next Actions
117-
1. Kick off Milestone 5 Step 6 by harmonising the new tracer submodules (facade re-exports, docs, dead code sweep) ahead of integration cleanup.
118+
1. Scope the Milestone 6 integration/cleanup tasks (CI configs, packaging metadata, doc updates) now that the runtime tracer refactor is complete.
118119
2. Track stakeholder feedback and spin out follow-up issues if new risks surface.

design-docs/file-level-srp-refactor-plan.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
## Current State Observations
99
- `src/lib.rs` is responsible for PyO3 module registration, lifecycle management for tracing sessions, global logging initialisation, and runtime format selection, which mixes unrelated concerns in one file.
10-
- `src/runtime_tracer.rs` couples trace lifecycle control, activation toggling, and Python value encoding in a single module, making it difficult to unit test or substitute individual pieces.
10+
- `src/runtime/tracer/runtime_tracer.rs` (previously the monolithic `runtime_tracer.rs`) couples trace lifecycle control, activation toggling, and Python value encoding in a single module, making it difficult to unit test or substitute individual pieces.
1111
- `src/tracer.rs` combines the `Tracer` trait definition, sys.monitoring shims, callback registration utilities, and thread-safe storage, meaning small changes can ripple through unrelated logic.
1212
- `codetracer_python_recorder/api.py` interleaves environment based auto-start, context-manager ergonomics, backend state management, and format constants, leaving no clearly isolated entry-point for CLI or library callers.
1313

design-docs/value-capture.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ result = builtins_test([5, 3, 7])
336336
# General Rules
337337

338338
* This spec is for `/codetracer-python-recorder` project and NOT for `/codetracer-pure-python-recorder`
339-
* Code and tests should be added to `/codetracer-python-recorder/src/runtime_tracer.rs`
339+
* Code and tests should be added under `/codetracer-python-recorder/src/runtime/tracer/` (primarily `runtime_tracer.rs` and its collaborators)
340340
* Performance is important. Avoid using Python modules and functions and prefer PyO3 methods including the FFI API.
341341
* If you want to run Python do it like so `uv run python` This will set up the right venv. Similarly for running tests `uv run pytest`.
342342
* After every code change you need to run `just dev` to make sure that you are testing the new code. Otherwise some tests might run against the old code

0 commit comments

Comments
 (0)