mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-07-09 17:08:29 +00:00
Skip persisted tool-result files for BACKGROUND contexts, remove ephemeral direct parallel worker chat folders during cleanup, and ignore chat directories without chat.json during startup loading. Add focused regressions for background output persistence, parallel worker cleanup, and orphan directory loading.
4.1 KiB
4.1 KiB
persist_chat.py DOX
Purpose
- Own the
persist_chat.pyhelper module. - This module persists and reloads chat contexts and message artifacts.
- Keep this file-level DOX profile synchronized with
persist_chat.pybecause this directory is intentionally flat.
Ownership
persist_chat.pyowns the runtime implementation.persist_chat.py.dox.mdowns durable notes about responsibilities, contracts, side effects, and verification for that implementation.- Top-level functions:
_fallback_datetime_iso() -> str_parse_persisted_datetime(value: str | None) -> datetimeget_chat_folder_path(ctxid: str): Get the folder path for any context (chat or task).get_chat_msg_files_folder(ctxid: str)save_tmp_chat(context: AgentContext): Save context to the chats foldersave_tmp_chats(): Save all contexts to the chats folderload_tmp_chats(): Load all contexts from the chats folder_get_chat_file_path(ctxid: str)_convert_v080_chats()load_json_chats(jsons: list[str]): Load contexts from JSON stringsexport_json_chat(context: AgentContext): Export context as JSON stringremove_chat(ctxid): Remove a chat or task contextremove_msg_files(ctxid): Remove all message files for a chat or task context_serialize_context(context: AgentContext)_serialize_agent(agent: Agent)_serialize_log(log: Log)_deserialize_context(data)_deserialize_agent_config(agent_data: dict[str, Any], fallback_config: AgentConfig) -> AgentConfig_deserialize_agents(agents: list[dict[str, Any]], config: AgentConfig, context: AgentContext) -> Agent_deserialize_log(data: dict[str, Any]) -> 'Log'_safe_json_serialize(obj, **kwargs)- Notable constants/configuration names:
CHATS_FOLDER,LOG_SIZE,CHAT_FILE_NAME.
Runtime Contracts
- Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
- Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
- Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, settings/state persistence, scheduler state.
- Imported dependency areas include:
agent,collections,datetime,helpers,helpers.localization,helpers.log,initialize,json,typing,uuid. - Serialized chats store
agent_profileboth at the context level for the main chat and on each serialized agent so subordinate profiles survive server restart. - Deserialization must rebuild each agent with its serialized profile when present, falling back to the context profile for older chat files.
- Chat loading skips directories that do not contain
chat.json; malformed existing chat files still report load errors.
Key Concepts
- 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. - Keep request/response, tool, or helper semantics documented here at the same time as source changes.
Work Guidance
- Preserve public helper APIs used by core code and plugins unless every caller is updated.
- Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded.
- Prefer adding cohesive helper functions here only when behavior is reused across modules.
Verification
- Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers.
- Related tests observed by source search:
tests/test_api_chat_lifetime.pytests/test_browser_agent_regressions.pytests/test_persist_chat_log_ids.pytests/test_subagent_profiles.pytests/test_tool_action_contracts.py
Child DOX Index
No child DOX files.