Skip to main content
A token session is megan-tk pursuing one objective across an ordered chain of milestones. You open it once, then drive it each control cycle. It hands back a Decision: continue or adapt, and the milestone its attention is on. This is how Megan does finding ways around obstacles — when the frontier is blocked, it routes you to adapt (act here) instead of letting a frozen plan push into the wall. There are two ways to drive a session:

Opening a session

session() returns a MeganSession. It’s a context manager — prefer with so the session is always closed on the server:

The milestone path — step

Each cycle, tell the token which milestone indices are now satisfied and which new obstacles block a milestone. It returns the decision.
Obstacles accept either a dict or a (id, milestone_index) tuple — both normalise to the same request:
reached and every obstacle’s milestone are indices into the milestone list you opened the session with. An out-of-range index is rejected by the server (HTTP 422 → MeganTKError).

Acting on the decision

The whole point is the adapt branch — that’s megan-tk telling you the current plan won’t carry the frontier and your change-doer should intervene here:

The perception path — perceive

For grounded navigation you can let the token reason about distance instead of discrete “reached” flags. Open the session with one shrinking ring radius per milestone, then report raw position/goal/obstacles.
Perception obstacles take a dict or an (id, position) tuple:
ring_radii must have exactly one radius per milestone and must shrink as milestones advance (each ring tighter than the last). Calling perceive on a session opened without ring_radii is a 422 — use step there instead.

Inspecting and closing

The server holds the live token object (its knowledge graph accumulates across steps), so close sessions you’re done with. The with form does this for you; if you manage a session by hand, call close() in a finally.

A full milestone loop

Next

Disturbance anticipators

Learn a periodic disturbance and protect ahead of it.

SDK reference

Every method, argument, and return type.