# Unified session context

Installed from the command line or the Web, a function should behave the same. Several capabilities read a per-turn session context, and a caller that builds none loses them without an error.

## Without a session (`_store=None`)

| Capability | Failure point | Symptom |
| --- | --- | --- |
| Docstring into prompt | Never reached in `render.py` | Function instructions never reach the model; the Agent degrades into “What would you like to do” |
| DAG persistence | Node append is a no-op | The run is not in session history |
| History from the DAG | `_render_history_messages` returns None | Each exec cannot see earlier steps |
| `ask_user` tracking | Placeholder and finish nodes not written | Questions are not in history |
| Nested `called_by` | `_call_id=None` | Node attribution is lost |

All follow the pattern `store=_store.get(); if store is None: return`.

## What the context is made of

```
SessionDB (default_db / SessionStore)
  └─ ~/.openprogram/projects/<project-id>/chats/<session_id>/
SessionNodeWriter(db, session_id)      append / update / load DAG nodes
ContextVars, set and reset in pairs:
  _store            SessionNodeWriter  deep code writes the DAG, renders docs
  _current_turn_id                     which message a file backup belongs to
  _current_runtime  create_runtime()   Agent method auto-injection
  _call_id          Agent wrapper      called_by attribution
```

A work directory (research output such as literature review, ideas, paper) and a session (the conversation DAG) are independent. Building a session does not touch the work directory.

## Design

One context manager, [`session_context`](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/session/context.html), guarantees paired set and reset. A caller passing nothing still gets a real session, with its id exposed.

```
r1 = run_research("survey agent reliability")        # None: new session
r2 = run_research("now turn it into a paper",
                  session_id=r1.session_id)           # same session, history carries over
r3 = run_research("unrelated: GUI agent benchmark")   # independent session
```

- One call is not one session: continuity is the caller passing the id, as with `openprogram --resume <id>` and the Web client.
- Cleanup belongs to session management (list, delete), not to each call.
- Stage instructions live in the docstring. With the context installed, a command-line run no longer needs them moved into `content`, so no stage function changes.

## Implementation status

| Step | Verified by | State |
| --- | --- | --- |
| Add the manager | Variables set inside, reset after | Done |
| A harness `main.py` wraps `paper_agent` | Captured command-line prompt contains the docstring | Done |
| Dispatcher switches over | Existing dispatcher tests pass | Not done: `TurnBindings.bind` still binds its own |
| `process_runner` and tests converge | Subprocess DAG tracking and fixtures still work | Not done: `process_runner` still sets its own |
