| name | bfs-traverse |
| description | Demonstrates recursive self-invocation with a depth limit. Performs a breadth-first (level-order) traversal of the decision tree in tree.json by invoking ITSELF once per level — each call expands the current frontier and recurses on the next, stopping at a maximum depth. Takes an optional max depth. |
| argument-hint | [max-depth] |
BFS Traverse (demo) — recursive self-invocation
This skill demonstrates recursion via self-invocation: the skill invokes
itself, once per level, to walk a decision tree breadth-first. Each call
processes one BFS frontier (all nodes at the current depth), then recurses on
the next frontier. A maximum depth is the base case that stops the recursion.
- The tree lives in
tree.json (root id + nodes map of id → {label, children}).
- The traversal order is recorded in
bfs.log so the level-by-level walk is
observable.
- The "call argument" is the recursion's state: the current depth, the max depth,
and the frontier (the queue of node ids at this level).
Argument formats
This same skill is both the entry point and the recursive step, so it accepts two
shapes of $ARGUMENTS:
- Initial call (from the user): an optional max depth, e.g.
3, or empty.
Default max depth = 3. The root level is depth 0.
- Recursive self-call:
depth=<d> max=<m> frontier=<id,id,...>
(always contains the literal frontier=).
What to do
-
Detect the call type from $ARGUMENTS:
-
If it contains frontier=, this is a recursive call. Parse d (depth),
m (max), and the comma-separated frontier ids.
-
Otherwise it is the initial call. Set m = the first integer in the
argument, or 3 if none. Set d = 0 and frontier = [root] (read root
from tree.json). Create (overwrite) bfs.log with the header:
=== BFS over tree.json (max depth m=<m>) ===
-
Visit the current frontier. Read each id's label from tree.json and
append one line to bfs.log, indented by depth for a tree-like view:
depth <d>: <id>:<label> <id>:<label> ...
(Indent the line with 2 * d leading spaces.) Show this line to the user too —
this is the level BFS is currently visiting.
-
Compute the next frontier: concatenate, in order, the children of every
node in the current frontier (skip ids missing from nodes).
-
Base case — decide whether to stop:
- If
d >= m (reached the depth limit) or the next frontier is empty
(no more nodes), do not recurse. Instead:
- read back the full
bfs.log,
- report the complete visiting order (all ids in BFS order) and the deepest
level reached, and
- state that the recursion stopped because of the depth limit or because the
tree was exhausted.
Then stop.
-
Recurse — the self-invocation: otherwise, invoke the bfs-traverse
skill again (yourself), passing:
depth=<d+1> max=<m> frontier=<comma-separated next frontier ids>
Do not do the next level's work inline — hand it to the recursive call.
Always go one level per invocation. The only way to descend is to invoke yourself.