main
md 75 lines 4.74 KB
Rendered Raw
1 # persist_chat.py DOX
2
3 ## Purpose
4
5 - Own the `persist_chat.py` helper module.
6 - This module persists and reloads chat contexts and message artifacts.
7 - Keep this file-level DOX profile synchronized with `persist_chat.py` because this directory is intentionally flat.
8
9 ## Ownership
10
11 - `persist_chat.py` owns the runtime implementation.
12 - `persist_chat.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation.
13 - Top-level functions:
14 - `_fallback_datetime_iso() -> str`
15 - `_parse_persisted_datetime(value: str | None) -> datetime`
16 - `get_chat_folder_path(ctxid: str)`: Get the folder path for any context (chat or task).
17 - `get_chat_msg_files_folder(ctxid: str)`
18 - `save_tmp_chat(context: AgentContext)`: Save context to the chats folder
19 - `save_tmp_chats()`: Save all contexts to the chats folder
20 - `load_tmp_chats()`: Load all contexts from the chats folder
21 - `_get_chat_file_path(ctxid: str)`
22 - `_convert_v080_chats()`
23 - `load_json_chats(jsons: list[str])`: Load contexts from JSON strings
24 - `export_json_chat(context: AgentContext)`: Export context as JSON string
25 - `remove_chat(ctxid)`: Remove a chat or task context
26 - `remove_msg_files(ctxid)`: Remove all message files for a chat or task context
27 - `mark_chat_saved(context: AgentContext) -> None`
28 - `saved_chat_ids() -> set[str]`
29 - `_serialize_context(context: AgentContext)`
30 - `_serialize_agent(agent: Agent)`
31 - `_serialize_log(log: Log)`
32 - `_deserialize_context(data)`
33 - `_deserialize_agent_config(agent_data: dict[str, Any], fallback_config: AgentConfig) -> AgentConfig`
34 - `_deserialize_agents(agents: list[dict[str, Any]], config: AgentConfig, context: AgentContext) -> Agent`
35 - `_deserialize_log(data: dict[str, Any]) -> 'Log'`
36 - `_safe_json_serialize(obj, **kwargs)`
37 - Notable constants/configuration names: `CHATS_FOLDER`, `LOG_SIZE`, `CHAT_FILE_NAME`, `SAVED_CHAT_CONTEXT_DATA_KEY`.
38
39 ## Runtime Contracts
40
41 - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
42 - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
43 - Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, settings/state persistence, scheduler state.
44 - Imported dependency areas include: `agent`, `collections`, `datetime`, `helpers`, `helpers.localization`, `helpers.log`, `initialize`, `json`, `typing`, `uuid`.
45 - Serialized chats store `agent_profile` both at the context level for the main chat and on each serialized agent so subordinate profiles survive server restart.
46 - Deserialization must rebuild each agent with its serialized profile when present, falling back to the context profile for older chat files.
47 - Chat loading skips directories that do not contain `chat.json`; malformed existing chat files still report load errors.
48 - Chat saves write and fsync a same-directory temporary file, atomically replace `chat.json`, and fsync the directory so an interrupted save cannot truncate the previous chat.
49 - Contexts are marked with private `SAVED_CHAT_CONTEXT_DATA_KEY` only after a successful save or load from disk so snapshot code can detect deleted chat files without hiding fresh unsaved chats.
50 - Chat and message-file removal reject empty or whitespace-only context IDs before provider or filesystem deletion begins.
51
52 ## Key Concepts
53
54 - Important called helpers/classes observed in the source: `datetime.fromtimestamp.isoformat`, `datetime.fromisoformat`, `files.get_abs_path`, `_get_chat_file_path`, `files.make_dirs`, `_serialize_context`, `_safe_json_serialize`, `files.write_file`, `_convert_v080_chats`, `files.list_files`, `get_chat_folder_path`, `files.delete_dir`, `get_chat_msg_files_folder`, `agent.history.serialize`, `initialize_agent`, `_deserialize_log`, `AgentContext`, `_deserialize_agent_config`, `_deserialize_agents`, `Log`, `log.set_initial_progress`.
55 - Keep request/response, tool, or helper semantics documented here at the same time as source changes.
56
57 ## Work Guidance
58
59 - Preserve public helper APIs used by core code and plugins unless every caller is updated.
60 - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded.
61 - Prefer adding cohesive helper functions here only when behavior is reused across modules.
62
63 ## Verification
64
65 - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers.
66 - Related tests observed by source search:
67 - `tests/test_api_chat_lifetime.py`
68 - `tests/test_browser_agent_regressions.py`
69 - `tests/test_persist_chat_log_ids.py`
70 - `tests/test_subagent_profiles.py`
71 - `tests/test_tool_action_contracts.py`
72
73 ## Child DOX Index
74
75 No child DOX files.