OpenProgram Docs

Context

session_context (openprogram/runtime/conversation/session_context.py) installs the per-turn ContextVars and resets them on exit, in reverse order.

@contextmanager
def session_context(
    session_id=None, *,
    agent_id="main", turn_id=None, runtime=None,
    create_runtime_if_none=True,
    id_prefix="session", source="cli",
) -> Iterator[SessionHandle]  # db, session_id, runtime, turn_id, created
SetsValue
_storeSessionNodeWriter(db, sid)
_current_turn_idturn_id, or a new turn_…
_current_runtimeThe given runtime or a new one. Without a usable provider it stays unset and the store is still installed.

A session that does not exist is created here: one of the three creation entry points. _call_id belongs to the Agent method wrapper.

Session boundaries

Caller intentPassBehaviour
Run a new taskNo session_idNew <id_prefix>_… session; the id is on the handle
Continue the same taskThe id from last timeReused, history continues
Start something unrelatedNothing, or a different idA separate session

The call count never decides. A session has no close: exit only resets the ContextVars, and the same id keeps writing later.

Entry points

Entry pointSets its context with
A harnesswith session_context(session_id="research_" + uuid, runtime=rt) around paper_agent
DispatcherTurnBindings.bind, its own per-turn binding
process_runnerIts own set and reset
TestsThe manager in test_session_context.py and others; some fixtures set the variables directly

Why one manager

Last updated · 2026-11-25