> ## Documentation Index
> Fetch the complete documentation index at: https://usefused.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# harnest.session

> Public Python signatures, fields, and methods.

[All Python modules](https://usefused.com/docs/harnest/reference/api/overview.md) · [Import and compatibility guidance](https://usefused.com/docs/harnest/build/python-imports.md)

## Exports

| Name | Kind |
| - | - |
| [`ADKSessionStorage`](#adksessionstorage) | Class |
| [`InMemorySessionStore`](#inmemorysessionstore) | Class |
| [`SessionLease`](#sessionlease) | Class |
| [`SessionStore`](#sessionstore) | Class |

## ADKSessionStorage

Deployment-owned ADK session backend configuration.

```python theme={null}
ADKSessionStorage(uri: str, database_kwargs: Mapping[str, Any] = ...) -> None
```

Fields (`—` means no declared default):

| Field | Type | Default |
| - | - | - |
| `uri` | `str` | `—` |
| `database_kwargs` | `Mapping[str, Any]` | `dict()` |

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `uri` | `str` | Required |
> | `database_kwargs` | `Mapping[str, Any]` | `...` |
>
> ```python theme={null}
> from harnest.session import ADKSessionStorage
>
> storage = ADKSessionStorage(uri=...)
> ```

### ADKSessionStorage.create\_service

```python theme={null}
ADKSessionStorage.create_service(base_directory: str) -> Any
```

Create the native ADK session service for a compiled application.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `base_directory` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import ADKSessionStorage
>
> storage: ADKSessionStorage
> result = storage.create_service(base_directory=...)
> ```

## InMemorySessionStore

Development store; inject a durable SessionStore in production.

```python theme={null}
InMemorySessionStore() -> None
```

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store = InMemorySessionStore()
> ```

### InMemorySessionStore.start

```python theme={null}
async InMemorySessionStore.start() -> None
```

Satisfy the application-owned store lifecycle without external I/O.

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> await store.start()
> ```

### InMemorySessionStore.create

```python theme={null}
async InMemorySessionStore.create(*, session_id: str, user_id: str, state: Mapping[str, Any]) -> SessionRecord
```

Create one process-local tenant-scoped session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
> | `state` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> record = await store.create(session_id=..., user_id=..., state=...)
> ```

### InMemorySessionStore.get

```python theme={null}
async InMemorySessionStore.get(*, session_id: str, user_id: str) -> SessionRecord | None
```

Read one process-local tenant-scoped session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> record = await store.get(session_id=..., user_id=...)
> ```

### InMemorySessionStore.list

```python theme={null}
async InMemorySessionStore.list(*, user_id: str, after: str | None = None, limit: int | None = None) -> Sequence[SessionRecord]
```

List an ordered keyset page without materializing unrelated users.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `user_id` | `str` | Required |
> | `after` | `str \| None` | `None` |
> | `limit` | `int \| None` | `None` |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> result = await store.list(user_id=...)
> ```

### InMemorySessionStore.update

```python theme={null}
async InMemorySessionStore.update(*, session_id: str, user_id: str, state_delta: Mapping[str, Any]) -> SessionRecord | None
```

Merge a state delta into a process-local session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
> | `state_delta` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> record = await store.update(session_id=..., user_id=..., state_delta=...)
> ```

### InMemorySessionStore.delete

```python theme={null}
async InMemorySessionStore.delete(*, session_id: str, user_id: str) -> bool
```

Delete one process-local session after its active lease exits.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> result = await store.delete(session_id=..., user_id=...)
> ```

### InMemorySessionStore.acquire

```python theme={null}
InMemorySessionStore.acquire(*, session_id: str, user_id: str) -> AsyncIterator[SessionLease]
```

Hold exclusive access to one process-local session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> async with store.acquire(session_id=..., user_id=...):
>     ...
> ```

### InMemorySessionStore.close

```python theme={null}
async InMemorySessionStore.close() -> None
```

Discard all process-local session state.

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import InMemorySessionStore
>
> store: InMemorySessionStore
> await store.close()
> ```

## SessionLease

Exclusive session access held across one graph execution.

```python theme={null}
SessionLease(*args, **kwargs)
```

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import SessionLease
>
> class MySessionLease(SessionLease):
>     ...
> ```

### SessionLease.record

Return the current session snapshot held by this lease.

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import SessionLease
>
> lease: SessionLease
> value = lease.record
> ```

### SessionLease.patch\_state

```python theme={null}
async SessionLease.patch_state(delta: Mapping[str, Any]) -> SessionRecord
```

Merge a state delta and return the updated session snapshot.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `delta` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import SessionLease
>
> lease: SessionLease
> record = await lease.patch_state(delta=...)
> ```

### SessionLease.replace\_state

```python theme={null}
async SessionLease.replace_state(state: Mapping[str, Any]) -> SessionRecord
```

Replace framework-owned state and return the updated session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `state` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import SessionLease
>
> lease: SessionLease
> record = await lease.replace_state(state=...)
> ```

### SessionLease.replace\_application\_data

```python theme={null}
async SessionLease.replace_application_data(data: Mapping[str, Any]) -> SessionRecord
```

Replace application-owned data and return the updated session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `data` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import SessionLease
>
> lease: SessionLease
> record = await lease.replace_application_data(data=...)
> ```

## SessionStore

Persistent session state with tenant scoping and execution leases.

Production implementations should use set-based listing, durable writes,
and a distributed lease. Durable mutations are also responsible for the
repository's privacy-safe OTEL audit contract after commit.

```python theme={null}
SessionStore(*args, **kwargs)
```

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> class MySessionStore(SessionStore):
>     ...
> ```

### SessionStore.start

```python theme={null}
async SessionStore.start() -> None
```

Initialize resources required by the session store.

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> await store.start()
> ```

### SessionStore.create

```python theme={null}
async SessionStore.create(*, session_id: str, user_id: str, state: Mapping[str, Any]) -> SessionRecord
```

Create one tenant-scoped session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
> | `state` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> record = await store.create(session_id=..., user_id=..., state=...)
> ```

### SessionStore.get

```python theme={null}
async SessionStore.get(*, session_id: str, user_id: str) -> SessionRecord | None
```

Read one tenant-scoped session when it exists.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> record = await store.get(session_id=..., user_id=...)
> ```

### SessionStore.list

```python theme={null}
async SessionStore.list(*, user_id: str, after: str | None = None, limit: int | None = None) -> Sequence[SessionRecord]
```

Return an ordered, optionally paginated session list for one user.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `user_id` | `str` | Required |
> | `after` | `str \| None` | `None` |
> | `limit` | `int \| None` | `None` |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> result = await store.list(user_id=...)
> ```

### SessionStore.update

```python theme={null}
async SessionStore.update(*, session_id: str, user_id: str, state_delta: Mapping[str, Any]) -> SessionRecord | None
```

Merge a state delta into an existing tenant-scoped session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
> | `state_delta` | `Mapping[str, Any]` | Required |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> record = await store.update(session_id=..., user_id=..., state_delta=...)
> ```

### SessionStore.delete

```python theme={null}
async SessionStore.delete(*, session_id: str, user_id: str) -> bool
```

Delete one tenant-scoped session and report whether it existed.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> result = await store.delete(session_id=..., user_id=...)
> ```

### SessionStore.acquire

```python theme={null}
SessionStore.acquire(*, session_id: str, user_id: str) -> AsyncContextManager[SessionLease]
```

Acquire exclusive execution access to one tenant-scoped session.

> [accordion] Parameters and example
>
> | Parameter | Type | Default |
> | - | - | - |
> | `session_id` | `str` | Required |
> | `user_id` | `str` | Required |
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> async with store.acquire(session_id=..., user_id=...):
>     ...
> ```

### SessionStore.close

```python theme={null}
async SessionStore.close() -> None
```

Release resources owned by the session store.

> [accordion] Example
>
> ```python theme={null}
> from harnest.session import SessionStore
>
> store: SessionStore
> await store.close()
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.