Skip to content

Add live visualization for behavior trees - #58

Merged
Sollimann merged 24 commits into
Sollimann:mainfrom
kmolan:main
May 14, 2026
Merged

Add live visualization for behavior trees#58
Sollimann merged 24 commits into
Sollimann:mainfrom
kmolan:main

Conversation

@kmolan

@kmolan kmolan commented May 7, 2026

Copy link
Copy Markdown
Collaborator
  1. Live web visualizer: Attach via BT::with_telemetry(port) (default 127.0.0.1) or BT::with_telemetry_at(addr, port) — opens an embedded HTML page at http://{addr}:{port}/ that renders the tree over a WebSocket. Rationale for choosing websockets, along with advantages I wrote here: Add view/read only graphical interface for inspecting tree state in real-time #55 (comment)

  2. Zero-overhead when off: Whole feature is hidden behind visualize

To see a live demo:

  1. cargo run --bin visualizer_smoke
  2. On your local web browser, open http://127.0.0.1:8910/

I also added a whole bunch of tests for prevent regression. Also, a huge chunk of this PR is me adding a tracer to all bt nodes. While there is graphviz and json serialization support, without an actual tracer it would be impossible to know the live state of each node (running/pass/fail/idle) at every tick. Existing code only gave static structure of the tree, not the live state of the entire tree along with every single node's status at each tick.

Disclaimer: Used claude code to generate documentation and the unit tests.

Recording.2026-05-07.223944.mp4

Comment thread bonsai/src/bt.rs
@Sollimann
Sollimann requested review from andriyDev, kaphula and matthewashton-k and removed request for andriyDev and kaphula May 8, 2026 08:46
Comment thread bonsai/src/telemetry.rs

@Sollimann Sollimann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try to find some time to review during the weekend. Its a big PR, so I'll have to do it batches. I pinged a few other reviewers as well

@kaphula kaphula left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't participate at this time. Thanks.

Comment thread bonsai/Cargo.toml Outdated
Comment thread bonsai/src/bt_telemetry.rs Outdated
Comment thread bonsai/src/bt.rs
Comment thread bonsai/src/index.html
Comment on lines +5 to +6
<title>bonsai-bt visualizer</title>
<script src="https://d3js.org/d3.v7.min.js"></script>

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be a problem, but this grabs d3.js from a CDN which means the visualizer server requires egress (internet access) or else you'll get a 404.

I think one way around it is by downloading the file locally and referencing following way

pub const VISUALIZER_D3: &str = include_str!("d3.v7.min.js");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess its fine for now, but might be something we need to address in future if someone complains

Comment thread bonsai/src/visualizer_server.rs Outdated
@kmolan

kmolan commented May 9, 2026

Copy link
Copy Markdown
Collaborator Author

@Sollimann I will address all PR comments once you've had the time to go through it all as the first pass. Let me know. In the meantime, I saw you comment here, and whipped up some changes to the html file, shall I push? Any refinements to the UI/color scheme/etc? #55 (comment)

Recording.2026-05-09.002941.mp4

@Sollimann

Sollimann commented May 10, 2026

Copy link
Copy Markdown
Owner

Very cool, looks great! Yes, please push changes👍 how does the color animations look e.g if ticks run at 20Hz? Also, is the animations able to keep up at that rate? Will try to take do a second wave of reviews later today.

@kmolan

kmolan commented May 10, 2026

Copy link
Copy Markdown
Collaborator Author

Will push in a second! Attaching what it looks like for 20Hz - it is able to keep up (although I doubt the human looking at it will be able to make sense of it haha). I also cranked it up all the way to 100Hz, where I change tree status every tick. Framework is able to handle the load no problem 👍

Recording.2026-05-10.150150.mp4

@Sollimann Sollimann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few more comments. General opinion is that implementation looks solid and test coverage is good 👍 I think you need to rebase as there has been a change to main today, also you need to install and run pre-commit

Let me know when you have addressed comments, and I'll take another look

Comment thread examples/src/visualizer_smoke/main.rs
Comment on lines +103 to +108
/// Like [`with_telemetry`](Self::with_telemetry), but lets you pick the
/// bind address. Pass `"0.0.0.0"` to listen on every interface (so peers
/// on the LAN can connect), `"127.0.0.1"` for loopback only, or any
/// specific interface IP. The address is parsed by `TcpListener::bind`,
/// so hostnames and IPv6 literals (e.g. `"::1"`) also work.
///

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localhost debug server is fine for most cases. Debugging on a separate machine can be achieved with ssh port portwarding 👍

Comment thread bonsai/src/status.rs Outdated
Comment thread bonsai/src/telemetry.rs
@kmolan

kmolan commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

@Sollimann I addressed all the comments, rebased onto main, and builds are passing now.

@Sollimann Sollimann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its getting closer now 🚀 I just noticed a couple of things when doing a final round of reviews. Please take a look

Also, once merged, to get some publicity around your latest enhancement of the crate you can post an announcement in following subreddits Rust, rust_gamedev, robotics, gamedev etc - include the gif. Up to you, but I think people will be excited to see this :)

Comment thread bonsai/src/telemetry.rs
use Behavior::*;
match b {
Action(a) => ("Action", Some(format!("{a:?}"))),
Wait(t) => ("Wait", Some(format!("Wait({t:.2}s)"))),

@Sollimann Sollimann May 14, 2026

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not for this PR, just pointing out for future

if you're up for the task, I think this porting Wait to take in std::time::Duration instead of seconds in float is something we should #52

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. While the actual fix is non-trivial, it may break backwards compatibility, how do you feel about that?
Wither way I might address in future PR.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the fix is non-trivial. Its not great introducing breaking changes, but the sooner the better. Also, if any wants to use this crate for production scenarios having Wait only accept seconds in floating point is not a great interface I think. I'll have to look at the implementation to fully jugde whether we want to publish it or not

Comment thread bonsai/src/visualizer_server.rs Outdated
Comment thread bonsai/src/visualizer_server.rs Outdated
@kmolan

kmolan commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

@Sollimann

Also, once merged, to get some publicity around your latest enhancement of the crate you can post an announcement in following subreddits Rust, rust_gamedev, robotics, gamedev etc - include the gif

Do you have a template you generally follow for bonsai announcements? Also, what about tagging new release/pushing to crates.io, etc. ?

@Sollimann

Sollimann commented May 14, 2026

Copy link
Copy Markdown
Owner

Do you have a template you generally follow for bonsai announcements? Also, what about tagging new release/pushing to crates.io, etc. ?

Nope, no template. Just explain the new feature and link to the repo

on second question - I'll make sure to release it now right after merge

@Sollimann Sollimann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks good. Great work on this one! 🚀 I'll make sure to publish it

@Sollimann
Sollimann merged commit ce44276 into Sollimann:main May 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants