-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add new feature related to debugging and tracing #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dc545dc
feat: add execution trace system to zapcode-core
TheUncharted 299c10c
feat: expose execution trace in JS, Python, and WASM bindings
TheUncharted d53a543
test: add execution trace tests
TheUncharted 124eec9
feat: add autoFix and execution trace to zapcode-ai packages
TheUncharted a65b8e5
refactor: reorganize examples by language and topic
TheUncharted eb94d59
docs: document autoFix, execution trace, and debug logging in README
TheUncharted 312c7b6
chore: update Cargo.lock
TheUncharted 01e29a8
style: fix cargo fmt formatting in trace code
TheUncharted ccfc2ee
fix: update CI paths after examples directory reorganization
TheUncharted cdbc6ee
fix: address CodeRabbit review findings
TheUncharted File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| //! Execution trace for debugging and observability. | ||
| //! | ||
| //! Captures a tree of spans covering parse → compile → execute → tool calls. | ||
| //! The trace is lightweight and always collected (sub-microsecond overhead). | ||
| //! | ||
| //! The `TraceSpan` shape is designed to map cleanly to OpenTelemetry spans | ||
| //! for future export to Jaeger, Langfuse, Datadog, etc. | ||
|
|
||
| use std::time::{Instant, SystemTime, UNIX_EPOCH}; | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| /// A single span in the execution trace. | ||
| /// | ||
| /// Shaped to be OTel-compatible: each span has a name, timestamps, | ||
| /// status, key-value attributes, and children. | ||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct TraceSpan { | ||
| /// Span name (e.g. "parse", "compile", "execute", "tool_call", "suspend"). | ||
| pub name: String, | ||
| /// When the span started (ms since Unix epoch). | ||
| pub start_time_ms: u64, | ||
| /// When the span ended (ms since Unix epoch). 0 if still open. | ||
| pub end_time_ms: u64, | ||
| /// Duration in microseconds. | ||
| pub duration_us: u64, | ||
| /// "ok" or "error". | ||
| pub status: TraceStatus, | ||
| /// Structured attributes. Keys use `zapcode.*` namespace. | ||
| pub attributes: Vec<(String, String)>, | ||
| /// Child spans. | ||
| pub children: Vec<TraceSpan>, | ||
| } | ||
|
|
||
| /// Span status. | ||
| #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
| pub enum TraceStatus { | ||
| Ok, | ||
| Error, | ||
| } | ||
|
|
||
| /// Builder for constructing trace spans with proper timing. | ||
| pub(crate) struct SpanBuilder { | ||
| name: String, | ||
| start_wall: u64, | ||
| start_instant: Instant, | ||
| attributes: Vec<(String, String)>, | ||
| children: Vec<TraceSpan>, | ||
| } | ||
|
|
||
| impl SpanBuilder { | ||
| pub fn new(name: &str) -> Self { | ||
| Self { | ||
| name: name.to_string(), | ||
| start_wall: now_ms(), | ||
| start_instant: Instant::now(), | ||
| attributes: Vec::new(), | ||
| children: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| pub fn attr(mut self, key: &str, value: impl ToString) -> Self { | ||
| self.attributes.push((key.to_string(), value.to_string())); | ||
| self | ||
| } | ||
|
|
||
| pub fn set_attr(&mut self, key: &str, value: impl ToString) { | ||
| self.attributes.push((key.to_string(), value.to_string())); | ||
| } | ||
|
|
||
| pub fn add_child(&mut self, child: TraceSpan) { | ||
| self.children.push(child); | ||
| } | ||
|
|
||
| pub fn finish(self, status: TraceStatus) -> TraceSpan { | ||
| let elapsed = self.start_instant.elapsed(); | ||
| TraceSpan { | ||
| name: self.name, | ||
| start_time_ms: self.start_wall, | ||
| end_time_ms: self.start_wall + elapsed.as_millis() as u64, | ||
| duration_us: elapsed.as_micros() as u64, | ||
| status, | ||
| attributes: self.attributes, | ||
| children: self.children, | ||
| } | ||
| } | ||
|
|
||
| pub fn finish_ok(self) -> TraceSpan { | ||
| self.finish(TraceStatus::Ok) | ||
| } | ||
|
|
||
| pub fn finish_error(self, error: &str) -> TraceSpan { | ||
| self.attr("zapcode.error", error).finish(TraceStatus::Error) | ||
| } | ||
| } | ||
|
|
||
| fn now_ms() -> u64 { | ||
| SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .unwrap_or_default() | ||
| .as_millis() as u64 | ||
| } | ||
|
|
||
| /// Execution trace covering a full run (parse + compile + execute). | ||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub struct ExecutionTrace { | ||
| pub root: TraceSpan, | ||
| } | ||
|
|
||
| impl ExecutionTrace { | ||
| /// Pretty-print the trace as a tree. | ||
| pub fn print(&self) { | ||
| print_span(&self.root, 0, true); | ||
| } | ||
|
|
||
| /// Format the trace as a tree string. | ||
| pub fn to_string_pretty(&self) -> String { | ||
| let mut buf = String::new(); | ||
| format_span(&self.root, 0, true, &mut buf); | ||
| buf | ||
| } | ||
| } | ||
|
|
||
| fn format_duration(us: u64) -> String { | ||
| if us < 1000 { | ||
| format!("{}µs", us) | ||
| } else if us < 1_000_000 { | ||
| format!("{:.1}ms", us as f64 / 1000.0) | ||
| } else { | ||
| format!("{:.2}s", us as f64 / 1_000_000.0) | ||
| } | ||
| } | ||
|
|
||
| fn format_span(span: &TraceSpan, depth: usize, is_last: bool, buf: &mut String) { | ||
| let icon = match span.status { | ||
| TraceStatus::Ok => "✓", | ||
| TraceStatus::Error => "✗", | ||
| }; | ||
| let duration = format_duration(span.duration_us); | ||
|
|
||
| // Build prefix | ||
| let prefix = if depth == 0 { | ||
| String::new() | ||
| } else { | ||
| let connector = if is_last { "└─ " } else { "├─ " }; | ||
| let indent = "│ ".repeat(depth - 1); | ||
| format!("{}{}", indent, connector) | ||
| }; | ||
|
|
||
| buf.push_str(&format!("{}{} {} ({})", prefix, icon, span.name, duration)); | ||
|
|
||
| // Show key attributes inline | ||
| for (k, v) in &span.attributes { | ||
| if k == "zapcode.error" { | ||
| buf.push_str(&format!(" error=\"{}\"", v)); | ||
| } else if k == "zapcode.tool.name" { | ||
| buf.push_str(&format!(" {}", v)); | ||
| } else if k == "zapcode.tool.args" { | ||
| buf.push_str(&format!("({})", v)); | ||
| } else if k == "zapcode.tool.result" { | ||
| let display = if v.len() > 60 { &v[..57] } else { v }; | ||
| buf.push_str(&format!(" → {}", display)); | ||
| if v.len() > 60 { | ||
| buf.push_str("..."); | ||
| } | ||
| } | ||
| } | ||
| buf.push('\n'); | ||
|
|
||
| for (i, child) in span.children.iter().enumerate() { | ||
| format_span(child, depth + 1, i == span.children.len() - 1, buf); | ||
| } | ||
| } | ||
|
|
||
| fn print_span(span: &TraceSpan, depth: usize, is_last: bool) { | ||
| let mut buf = String::new(); | ||
| format_span(span, depth, is_last, &mut buf); | ||
| print!("{}", buf); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TheUncharted/zapcode
Length of output: 81
🏁 Script executed:
head -n 70 CONTRIBUTING.md | tail -n 20Repository: TheUncharted/zapcode
Length of output: 1138
🏁 Script executed:
Repository: TheUncharted/zapcode
Length of output: 663
🏁 Script executed:
Repository: TheUncharted/zapcode
Length of output: 181
🌐 Web query:
maturin develop virtualenv requirements documentation💡 Result:
maturin. [1]maturin developto install into the current environment:maturin developbuilds your Rust crate and installs it directly into the currently active virtualenv (skipping wheel generation). This is the standard workflow for fast local iteration. [1][2][3]docs/requirements.txt(i.e., Python packages needed to build the docs). [4]maturinas a requirement (with a pinned version) and set it as thebuild-backendper PEP 517. [1]Sources: [1] Maturin User Guide – Tutorial, [2] Maturin User Guide – Local Development, [3] PyPI maturin (current release page), [4] Maturin User Guide – Sphinx Integration.
🌐 Web query:
maturin installation requirements Python binding💡 Result:
Maturin (Python binding / Rust-extension builds): installation requirements
rustc/cargoinstalled (for PyO3, the minimum Rust version is 1.83). [3]pipx install maturin(orpip install maturin). [1][3]Optional / scenario-specific dependencies
zig: optional, used as a linker to simplify cross-compiling and help with manylinux compliance. [1]patchelf: optional, used to repair wheels that link to other shared libraries (install viamaturin[patchelf]). [1]cffibindings: ensurecffiis installed (maturin may auto-install it only in some virtualenv cases; otherwise you mustpip install cffi). [2]Sources: [1] Maturin Installation docs, [2] Maturin Bindings docs, [3] PyO3 “Getting started / Installation” docs.
Document the Python env/bootstrap step before
maturin develop.maturin developinstalls into the current virtualenv, andmaturinitself must be installed separately. A fresh contributor will hit a failure before the example runs if neither is set up. Please add the venv creation and maturin installation step.♻️ Suggested doc update
📝 Committable suggestion
🤖 Prompt for AI Agents