From 85775c56e5feaf2bc66519547e88d09196c564b5 Mon Sep 17 00:00:00 2001 From: Meng Jianwen Date: Fri, 27 Mar 2026 19:11:10 +0800 Subject: [PATCH] fix(storage): prevent silent data loss in VikingFS.append_file() When reading existing file content fails (e.g. AGFS timeout, connection error, decryption failure), the broad `except Exception: pass` silently discards the error and sets existing content to empty string. The subsequent write then overwrites the entire file with only the new content, losing all previous data. Only suppress FileNotFoundError (HTTP 404) which is expected on first write. Let other AGFS errors propagate so the caller sees the failure instead of silently losing data. --- openviking/storage/viking_fs.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/openviking/storage/viking_fs.py b/openviking/storage/viking_fs.py index 98c505b94..902df7758 100644 --- a/openviking/storage/viking_fs.py +++ b/openviking/storage/viking_fs.py @@ -23,6 +23,7 @@ from pathlib import PurePath from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from openviking.pyagfs.exceptions import AGFSClientError, AGFSHTTPError from openviking.server.identity import RequestContext, Role from openviking.telemetry import get_current_telemetry from openviking.utils.time_utils import format_simplified, get_current_timestamp, parse_iso_datetime @@ -1667,8 +1668,11 @@ async def append_file( existing_bytes = self._handle_agfs_read(self.agfs.read(path)) existing_bytes = await self._decrypt_content(existing_bytes, ctx=ctx) existing = self._decode_bytes(existing_bytes) - except Exception: - pass + except AGFSHTTPError as e: + if e.status_code != 404: + raise + except AGFSClientError: + raise await self._ensure_parent_dirs(path) final_content = (existing + content).encode("utf-8")