datawhalechina/hello-agents · error · FileWriteError

无法保存会话记录:{e}

Error message

无法保存会话记录:{e}

What it means

FileWriteError raised by FileManager.save_session when writing a session transcript to BASE_DIR/<domain>/sessions/session_<date>_<time>.md fails. Besides missing parent directories, the timestamped filename uses minute resolution, so two sessions saved within the same minute silently overwrite each other rather than erroring — the write itself only fails on missing dirs or OS-level errors.

Source

Thrown at Co-creation-projects/Yixiang-Wu-LearningAgent/core/file_manager.py:95

    def save_session(self, domain: str, session_content: str) -> Path:
        """
        保存单次学习会话记录

        Args:
            domain: 领域名称
            session_content: 会话内容

        Returns:
            保存的文件路径
        """
        date = datetime.now().strftime("%Y-%m-%d")
        time = datetime.now().strftime("%H-%M")
        session_path = self.BASE_DIR / domain / "sessions" / f"session_{date}_{time}.md"

        try:
            session_path.write_text(session_content, encoding="utf-8")
        except Exception as e:
            raise FileWriteError(f"无法保存会话记录:{e}")

        return session_path

    def read_plan(self, domain: str) -> str:
        """
        读取学习计划

        Args:
            domain: 领域名称

        Returns:
            计划内容

        Raises:
            FileNotFoundError: 如果计划不存在
        """
        plan_path = self.BASE_DIR / domain / "plan.md"
        if not plan_path.exists():

View on GitHub (pinned to 606a07d341)

Solutions

  1. mkdir parents before writing: session_path.parent.mkdir(parents=True, exist_ok=True)
  2. Add seconds + a random suffix to the filename (e.g. %H-%M-%S plus uuid4 hex) to avoid same-minute collisions
  3. Verify write permissions on the sessions directory in deployment
  4. Use `raise ... from e` to keep the cause

Example fix

# before
session_path = self.BASE_DIR / domain / "sessions" / f"session_{date}_{time}.md"
try:
    session_path.write_text(session_content, encoding="utf-8")
except Exception as e:
    raise FileWriteError(f"无法保存会话记录:{e}")

# after
stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
session_path = self.BASE_DIR / domain / "sessions" / f"session_{stamp}_{uuid4().hex[:6]}.md"
try:
    session_path.parent.mkdir(parents=True, exist_ok=True)
    session_path.write_text(session_content, encoding="utf-8")
except OSError as e:
    raise FileWriteError(f"无法保存会话记录 {session_path}: {e}") from e
Defensive patterns

Strategy: validation

Validate before calling

def session_dir_ready(base_dir: Path, domain: str) -> bool:
    d = base_dir / domain / "sessions"
    return d.is_dir() and os.access(d, os.W_OK)

Try / catch

try:
    path = fm.save_session(domain, content)
except FileWriteError as e:
    # sessions are append-only history: log and continue rather than crash the chat
    logger.exception("session persist failed for %s", domain)

Prevention

When it happens

Trigger: Calling save_session when BASE_DIR/<domain>/sessions/ does not exist; permission failure on the sessions directory; disk full; BASE_DIR on a filesystem mounted read-only (e.g. squashed container layer).

Common situations: First session saved for a fresh domain; containerized deployment writing to an unmounted path; two rapid sessions in one minute overwriting each other (data loss, not exception).

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/9c7457e1c9558735. Report an issue: GitHub.