Metadata-Version: 2.4
Name: klein-bt
Version: 0.9.0
Summary: Lightweight CLI telemetry dashboard for BehaviorTree.CPP v4 over ZeroMQ.
Author-email: Johan Ubbink <25775970+johanubbink@users.noreply.github.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/johanubbink/klein-bt
Project-URL: Issues, https://github.com/johanubbink/klein-bt/issues
Project-URL: Changelog, https://github.com/johanubbink/klein-bt/blob/main/CHANGELOG.md
Keywords: behaviortree,behaviortree.cpp,btcpp,behavior-tree,behavior-trees,groot,groot2,ros2,nav2,robotics,zeromq,telemetry,monitoring,debugger,dashboard,visualization
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Topic :: Software Development :: Debuggers
Classifier: Topic :: Software Development :: Embedded Systems
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pyzmq>=25
Requires-Dist: websockets>=14
Requires-Dist: msgpack>=1.0
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: playwright; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: complexipy; extra == "dev"
Requires-Dist: lizard; extra == "dev"
Dynamic: license-file

# klein

klein lets you watch a running BehaviorTree.CPP v4 tree live in your browser.
You see which nodes are running, succeeding or failing, plus the current
blackboard values, as the robot ticks.

![klein's Tree view: a CrossDoor tree paused on PickLock's failure, the Blackboard on the left, the inspector with PickLock's activity strip and facts on the right, and the bar's strips below](https://raw.githubusercontent.com/johanubbink/klein-bt/main/assets/klein-tree.png)

## Install

Install it from [PyPI](https://pypi.org/project/klein-bt/) with
[uv](https://docs.astral.sh/uv/):

```bash
uv tool install klein-bt
```

uv fetches a newer Python by itself if yours is older than the 3.10 klein
needs. [pipx](https://pipx.pypa.io) works too:

```bash
pipx install klein-bt
```

This gives you two commands: `klein-bt` (the dashboard) and `klein-bt-mock`
(a fake robot for trying it out).

## Run it

```bash
klein-bt                                          # robot on 127.0.0.1:1667
klein-bt --robot-host 10.0.0.5 --robot-port 1667  # robot somewhere else
klein-bt --port 8080 --no-browser                 # don't open a browser
klein-bt --host 0.0.0.0                           # let other machines open it
klein-bt --open-log run.btlog                         # a saved recording, no robot
```

| Flag              | Default     | Meaning                              |
| ----------------- | ----------- | ------------------------------------ |
| `--robot-host`    | `127.0.0.1` | IP address of the robot              |
| `--robot-port`    | `1667`      | Groot2 publisher port on the robot   |
| `--host`          | `127.0.0.1` | interface the dashboard is served on; `0.0.0.0` opens it to the network |
| `--port`          | `8080`      | port the dashboard is served on      |
| `--no-browser`    | off         | don't open the browser automatically |
| `--record-buffer` | `10m`       | how much recent history to record (`90s`, `30m`, `1h`); `0` turns recording off |
| `--open-log FILE` |             | show a saved `.btlog` (and `FILE.bb.jsonl` beside it) instead of a robot |

klein opens `http://localhost:8080` for you. You can start it before the robot
is up; it keeps retrying until the robot appears.

By default only this machine can open the dashboard. To watch from another
computer (klein on the robot, the browser on your laptop), pass
`--host 0.0.0.0` and open `http://<this machine's address>:8080` there.
There is no password: anyone who can reach that port sees the live tree and
blackboard values and can download the recording. Do this only on a network
you trust, or use an SSH tunnel instead
(`ssh -L 8080:localhost:8080 robot`, then `http://localhost:8080`).

## Using the dashboard

- Each card's outline and mark show its status: green Success, amber
  Running (the mark spins while live), red Failure, grey Idle.
- Scroll to pan (Shift+scroll sideways), Ctrl+scroll or pinch to zoom, and
  drag to pan. Double-click a node, or click the `−` under it, to fold or
  unfold its children; a folded node shows `+N` for what it hides. A tree
  that fits opens unfolded, a bigger one with its subtrees folded. The
  edges to the running nodes light up.
- Click a node to select it: the inspector on the right shows its path,
  its activity over the window (a click on it goes to that moment) and its
  ports, with the blackboard value each `{key}` holds at the moment shown. Click a blackboard key to select it instead: the inspector
  shows its value, its board and the nodes that write and read it. `[` and
  `]` step to the selection's previous and next change. Click the
  background, or press Esc, to clear the selection. Press `/` to search
  for a node or key by name.
- Press `R` to see the whole tree again, or `F` to jump to whatever is running.
  `V` folds the tree down to the part that ran in the zoomed-in strip's window.
- The rail on the left switches between the **Tree** and the **Timeline**
  (or press `1` and `2`); the selection and the moment you look at stay.
  Its `?` lists every key, and its settings choose the theme (Auto, Light,
  Dark) and a vertical or horizontal tree.
- The **Blackboard** panel shows every subtree's values. A value that just
  changed gets a thin blue edge that fades; a key that changes all the time
  (a position, a counter) gets a steady dim edge instead. Hover a board to
  find its node in the tree. See
  [docs/blackboards.md](https://github.com/johanubbink/klein-bt/blob/main/docs/blackboards.md) for how values are shown.
- Hover a key to see which nodes use it: writers outlined solid, readers
  dashed. Hover a node to ring its keys.
- Drag the side panel's right edge to make it wider or narrower, or fold it
  away by clicking the Tree icon in the rail again (or its header's button, or
  double-clicking the edge) for more room. klein remembers both.

If the robot loads a different tree, the dashboard switches to it by itself.

## Record, rewind and replay

klein records everything the robot does (BehaviorTree.CPP 4.3.3 or newer),
blackboard included, and keeps the last 10 minutes. Change that with
`--record-buffer` (`30m`, `1h`, or `0` to turn recording off). Don't log
from Groot2 against the same robot at the same time: each would get only
some of the transitions.

![klein's Timeline view: the Log and the tree map on top, each node's and key's activity over the window below, and the bar](https://raw.githubusercontent.com/johanubbink/klein-bt/main/assets/klein-timeline.png)

The bar along the bottom shows the recording, in both views:

- The buttons on the left go to the oldest moment kept, step to the
  previous or next failure (red) or transition, and play at 1×. While live,
  the play button reads pause: press it to freeze the tree and the
  blackboard right where they are.
- Beside them, **● Live** says you are live. In the past **Go live** takes
  its place and takes you back; the view gets a thin blue frame and a flag
  on the cursor gives its time, counting up from when klein started
  recording (`2:21.5`; hover it for the time of day). The robot gone reads
  **Offline**; an opened file shows **Go to end**, or **● End** at its end.
- `− 30 s +` sets the window: how much the zoomed-in strip, the Timeline,
  the Log and the inspector show (type a length, or step it).
- The strip to the right shows the whole recording (scrolling once it is
  longer than eight windows): its waveform shows how busy the robot was,
  red pins are failures, hatched stretches are outages, and its box is the
  window. Click or drag it to go to that moment; hover it for the time.
- The keys work wherever the focus is (except in a text field): ←/→ step,
  Shift+←/→ step through failures, Space plays or pauses, Home goes to the
  oldest moment kept, End goes live, Esc steps back one thing at a time,
  and R fits the tree or, in the Timeline view, the Timeline. `?` lists
  them all.

The recorder sits in the rail, above `?`: a dot and how much is kept
(`2:27`, `1.0 h`; hover it for the rest), and **Save**, which downloads one
`.zip` with a `.btlog` per tree run (Groot2 opens these) and its blackboard
in a `.bb.jsonl` beside it.

The **Timeline** view (`2`) shows the recording in full:

- **Timeline**, along the bottom: each node's running time as bars, so
  every retry shows, and each blackboard key's value under its board's
  node. Drag the playhead to go back in time. The inspector's button opens
  it on the selected node.
- **Log**, top left: every transition in the window. Click a row to select
  its node and see the tree and blackboard at that moment.
- The **tree map**, top right: every node's status at that moment; click a
  dot to select it. The search field above it filters the rows and the Log
  to the nodes and subtrees you name.
- Drag the splitters to share the room; klein remembers them.

`klein-bt --open-log FILE.btlog` shows a saved recording without a robot, in the
same Log and Timeline. It opens paused at its start, ready to play, and End
takes you to its end. Unzip a saved `.zip` first; the `.bb.jsonl` beside the
file brings its blackboard along.

## Set up your robot

klein talks to the Groot2 publisher that comes with BehaviorTree.CPP v4. If
Groot2 can connect to your robot, klein can too. If not, add a publisher after
you create the tree:

```cpp
#include "behaviortree_cpp/loggers/groot2_publisher.h"

auto tree = factory.createTreeFromFile("my_tree.xml");
BT::Groot2Publisher publisher(tree, 1667);
```

The publisher also uses the next port up (1668), so leave that one free.

## Try it without a robot

```bash
klein-bt-mock --port 1777        # in one terminal
klein-bt --robot-port 1777       # in another
```

The mock runs a robot barista: 76 nodes, seven subtrees, orders that fail now
and then. `--tree crossdoor` publishes BehaviorTree.CPP's t11 CrossDoor tree
instead, `--tree nested` a tree with a subtree inside a subtree, and
`--switch-every 200` swaps between two trees every 20 seconds or so.

## Docs

- [docs/blackboards.md](https://github.com/johanubbink/klein-bt/blob/main/docs/blackboards.md): reading blackboard values, and
  adding a renderer for your own message types.
- [docs/architecture.md](https://github.com/johanubbink/klein-bt/blob/main/docs/architecture.md): how klein works inside.
- [docs/protocol.md](https://github.com/johanubbink/klein-bt/blob/main/docs/protocol.md): the Groot2 wire protocol klein speaks.
- [docs/testing.md](https://github.com/johanubbink/klein-bt/blob/main/docs/testing.md): the test tiers and harness.

## License

klein is released under the MIT License — see [LICENSE](https://github.com/johanubbink/klein-bt/blob/main/LICENSE).

The dashboard bundles [D3.js](https://d3js.org) v7 (ISC License, © Mike
Bostock), served locally so it works on air-gapped networks.

## Disclaimer

klein is an independent tool. It is not affiliated with, endorsed by, or
sponsored by the BehaviorTree.CPP project or the authors of Groot / Groot2.
"BehaviorTree.CPP", "Groot", and "Groot2" are the property of their respective
owners. klein interoperates over the Groot2 publisher wire protocol that is part
of the open-source, MIT-licensed BehaviorTree.CPP library; it contains no code
copied from those projects.
