§3 Traversal
Scope
This chapter defines how a conforming engine moves through a Graph.
Traversal behavior is normative.
Engine State
At minimum, an engine maintains:
current: the active node IDhistory: stack of previously visited node IDsindex: mapping from node IDs to node positions
A node may include:
traversal.nextoverridetraversal.branch-pointwith options
Operation: Next
Next advances from the current node using deterministic precedence.
Algorithm
- Let
nodebe the current node. - If
node.traversal.branch-pointexists,NextMUST NOT auto-advance. The engine remains atnodeuntil a validChooseorBackoperation. - If
node.traversal.nextexists:- validate target node ID
- push
node.idontohistory - set
currenttotraversal.next - return
- Otherwise, resolve implicit sequence:
- find the next node in array order (
nodes[i+1]) - if present, push
node.idtohistoryand move there - if absent, the graph is complete and traversal ends
- find the next node in array order (
Precedence
branch-point gate -> traversal.next -> nodes[i+1] -> completeOperation: Choose
Choose selects an option at a branch point.
Preconditions
- current node has
traversal.branch-point - selected key or option label maps to exactly one option
Algorithm
- Resolve selected option from presenter input.
- Let
targetbe optiontarget. - Validate
targetexists. - Push current node ID to
history. - Set
currenttotarget.
Choose is invalid outside a branch-point node.
Operation: Goto
Goto jumps to any node ID explicitly requested by the presenter.
Algorithm
- Validate destination node ID exists.
- Push current node ID to
history. - Set
currentto destination node ID.
Goto bypasses branch-point gating because it is an explicit navigation command.
Operation: Back
Back returns to the previous node from history.
Algorithm
- If
historyis empty, remain at current node. - Otherwise pop top ID from
history. - Set
currentto popped node ID.
Back MUST NOT push a new history entry during the same operation.
History Invariants
A conforming engine MUST satisfy all invariants:
ChooseandGotopush exactly one history entry on success.- Successful
Nextpushes exactly one history entry when it moves. Backpops one entry and pushes none.- Failed operations MUST NOT mutate history.
- History entries are node IDs, not array indices.
Branch-Point Rendering Rules
When current node has a branch point:
- Render node content normally.
- Render branch prompt and options.
- Accept
ChooseandBack. NextSHOULD either no-op or show branch selection guidance.
Graph Patterns
These patterns are valid compositions of the same core traversal rules.
Linear Sequence
graph LR A[Node 1] --> B[Node 2] --> C[Node 3] --> D[Node 4]
Uses implicit nodes[i+1] progression.
Branch and Rejoin
graph TD Q[Question] -->|Choose A| A[Branch A] Q -->|Choose B| B[Branch B] A -->|next| R[Resume] B -->|next| R R --> N[Continue]
Implementation: branch options target branch nodes, and branch termini set
traversal.next to the same resume node.
Hub and Spoke
graph TD H[Hub] -->|Choose 1| S1[Spoke 1] H -->|Choose 2| S2[Spoke 2] H -->|Choose Done| D[Done] S1 -->|next| H S2 -->|next| H
Implementation: spoke nodes explicitly return to Hub via traversal.next.
Open World
graph TD A[Room A] -->|Choose| B[Room B] A -->|Choose| C[Room C] B -->|Choose| A B -->|Choose| D[Room D] C -->|Choose| A C -->|Choose| D
Implementation: dense branch-point graph with no required convergence.
Error Handling
A conforming engine MUST reject or safely handle:
- unknown branch option target IDs
- unknown
traversal.nexttarget IDs - duplicate node IDs
- malformed branch-point options
Recommended behavior is fail-fast validation before presentation starts.
Conformance Checklist
An engine conforms to traversal semantics when it:
- implements
Next,Choose,Goto,Back - enforces branch-point gating for implicit
Next - resolves
traversal.nextbefore implicit sequence - preserves history invariants
- validates node targets before navigation