.agents/references/session-persistence.md
Use this reference for changes to client-managed sessions, session input callbacks, per-turn persistence, retries, rewind, compaction replacement, or session backend implementations.
Read Conversation state ownership first when server-managed continuation is also involved. A client-managed session is a history store; it is not a second owner for a server-managed conversation.
get_items(limit=N) returns the latest N items in chronological order.add_items() appends one logical batch. Backends should make the batch atomic so partial turns are not visible after failure.pop_item() removes the current tail item and is used only for guarded rollback of items the current run can prove it owns.clear_session() clears the session boundary; compaction decorators that replace history must provide stronger restore behavior around destructive replacement.Third-party implementations target the Session protocol. Internal base classes and backend-specific metadata are not the compatibility contract unless explicitly documented.
get_items(limit=N) argument overrides the backend's default session limit. Return the latest N items in chronological order, with a deterministic tie-breaker when timestamps can collide.get_items() and pop_item() behavior that isolates the bad record and continues safely.prepare_input_with_session() returns two different values: the normalized input for the next model request and the subset of new-turn items that should be appended to the session.
session_input_callback deep-copies, reorders, filters, duplicates, or reconstructs items.session_step_items retains the complete unfiltered sequence for history and observability._current_turn_persisted_item_count tracks which generated items have already been saved during streaming, retry, or resume. Count items after conversion and persistence filtering, not from the unsanitized source list.RunState after resume.Retry cleanup is ownership-sensitive and best effort.
RunContextWrapper objects through retry rewind or compaction storage paths unless the session API explicitly owns that runtime context.store behavior; do not combine a local replay with a server-owned history chain.RunState rules rather than bypassing them as backend-only data.src/agents/memory/session.pysrc/agents/memory/session_settings.pysrc/agents/memory/sqlite_session.pysrc/agents/extensions/memory/src/agents/run_internal/session_persistence.pysrc/agents/run_internal/items.pysrc/agents/run_internal/run_steps.pytests/memory/tests/extensions/memory/tests/test_agent_runner.pytests/test_agent_runner_streamed.py