trading_agent.memory.session_store
¶
Keeps a desk's sessions on disk, so a restart does not make the desk forget the day.
A session is one stretch of conversation, normally one India trading day. The store keeps three things in the desk's sessions folder:
sessions/ state.json which session is current, and what each backend needs to resume it 2026-10-19-1.jsonl every finished exchange of that session, one JSON line each summaries/2026-10-19.md the summary written when that day's session ended
Typical usage example:
store = SessionStore(Path('workspace/desks/main/sessions')) state = store.load_state() store.append_exchange(state.session_id, exchange)
Exchange
dataclass
¶
One finished turn: what the desk was told and what it answered.
Attributes:
| Name | Type | Description |
|---|---|---|
trigger_text |
str
|
The trigger's text. |
answer |
str
|
The final answer. |
backend_name |
str
|
The backend that answered. |
finished_at |
str
|
When the turn finished, as an ISO 8601 string in UTC. |
to_json_dictionary
¶
Converts the exchange to a dictionary that json.dumps can write.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The exchange's fields. |
from_json_dictionary
classmethod
¶
Reads an exchange written by to_json_dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Exchange
|
The exchange. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A field is missing. |
Source code in src/trading_agent/memory/session_store.py
RouteState
dataclass
¶
What one backend needs to pick up the current session after a restart.
Attributes:
| Name | Type | Description |
|---|---|---|
exchanges_seen |
int
|
How many of the session's exchanges the backend already knows about. |
runner_state |
dict[str, Any] | None
|
What the backend's turn runner saved, such as Claude's session id, or None when it keeps nothing. |
SessionState
dataclass
¶
Which session is current.
Attributes:
| Name | Type | Description |
|---|---|---|
session_id |
str
|
The session's identifier, such as "2026-10-19-1". |
trading_date |
date
|
The India date the session belongs to. |
routes |
dict[str, RouteState]
|
Each backend's state, keyed by the backend's name. |
to_json_dictionary
¶
Converts the state to a dictionary that json.dumps can write.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The state, with the date as an ISO 8601 string. |
Source code in src/trading_agent/memory/session_store.py
from_json_dictionary
classmethod
¶
Reads a state written by to_json_dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The dictionary. |
required |
Returns:
| Type | Description |
|---|---|
SessionState
|
The state. |
Raises:
| Type | Description |
|---|---|
KeyError
|
A field is missing. |
ValueError
|
The date is not an ISO 8601 date. |
Source code in src/trading_agent/memory/session_store.py
DailySummary
dataclass
¶
The summary written when one day's session ended.
Attributes:
| Name | Type | Description |
|---|---|---|
trading_date |
date
|
The day it summarises. |
text |
str
|
The summary. |
SessionStore
¶
One desk's sessions folder.
Attributes:
| Name | Type | Description |
|---|---|---|
directory |
The sessions folder. |
Describes the folder without touching the disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
The desk's sessions folder. |
required |
date_source
|
Callable[[], date] | None
|
Returns today's India date; None uses the clock. Tests replace it. |
None
|
Source code in src/trading_agent/memory/session_store.py
today
¶
Today's date in India, which decides when a new session starts.
Returns:
| Type | Description |
|---|---|
date
|
The date. |
Source code in src/trading_agent/memory/session_store.py
load_state
¶
Reads which session is current.
A damaged state file is logged and treated as missing, so the desk starts a new session rather than not starting.
Returns:
| Type | Description |
|---|---|
SessionState | None
|
The state, or None when there is none yet. |
Source code in src/trading_agent/memory/session_store.py
save_state
¶
Writes which session is current.
The file is written beside the old one and then renamed over it, so a crash mid-write leaves the old state intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
SessionState
|
The state. |
required |
Source code in src/trading_agent/memory/session_store.py
new_session_id
¶
Picks the next free session identifier for a day and creates its empty log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trading_date
|
date
|
The session's India date. |
required |
Returns:
| Type | Description |
|---|---|
str
|
An identifier such as "2026-10-19-2". |
Source code in src/trading_agent/memory/session_store.py
append_exchange
¶
Adds one finished exchange to the end of a session's log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session. |
required |
exchange
|
Exchange
|
The exchange. |
required |
Source code in src/trading_agent/memory/session_store.py
exchanges
¶
Reads a session's exchanges, oldest first.
A damaged line, such as one cut short by a crash, is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The session. |
required |
Returns:
| Type | Description |
|---|---|
list[Exchange]
|
The exchanges, or an empty list for an unknown session. |
Source code in src/trading_agent/memory/session_store.py
session_ids
¶
Every session that has a log, newest first.
Returns:
| Type | Description |
|---|---|
list[str]
|
The session identifiers. |
Source code in src/trading_agent/memory/session_store.py
summaries
¶
Every daily summary, newest first.
Returns:
| Type | Description |
|---|---|
list[DailySummary]
|
The summaries. |
Source code in src/trading_agent/memory/session_store.py
write_summary
¶
Saves the summary of a day's session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trading_date
|
date
|
The day it summarises. |
required |
text
|
str
|
The summary. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The summary's file. |
Source code in src/trading_agent/memory/session_store.py
latest_summary
¶
Reads the most recent daily summary.
Returns:
| Type | Description |
|---|---|
DailySummary | None
|
The newest summary, or None when no day has been summarised yet. |