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
| Sets | Value |
|---|---|
_store | SessionNodeWriter(db, sid) |
_current_turn_id | turn_id, or a new turn_… |
_current_runtime | The 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 intent | Pass | Behaviour |
|---|---|---|
| Run a new task | No session_id | New <id_prefix>_… session; the id is on the handle |
| Continue the same task | The id from last time | Reused, history continues |
| Start something unrelated | Nothing, or a different id | A 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 point | Sets its context with |
|---|---|
| A harness | with session_context(session_id="research_" + uuid, runtime=rt) around paper_agent |
| Dispatcher | TurnBindings.bind, its own per-turn binding |
process_runner | Its own set and reset |
| Tests | The manager in test_session_context.py and others; some fixtures set the variables directly |
Last updated · 2026-11-25