Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
Revert "feat(cli): download directories as zip archives (#4262)" (#4320)
Zayn Jarvis9b7bb49634e7
diff --git a/crates/ov_cli/src/client.rs b/crates/ov_cli/src/client.rsindex 3d62dba19..16d38b037 100644--- a/crates/ov_cli/src/client.rs+++ b/crates/ov_cli/src/client.rs@@ -503,14 +503,8 @@ impl HttpClient {self.post("/api/v1/system/backend/sync-retry", &body).await}- /// Download file bytes, or a directory as ZIP bytes+ /// Download file as raw bytespub async fn get_bytes(&self, uri: &str) -> Result<Vec<u8>> {- Ok(self.get_bytes_with_type(uri).await?.0)- }-- /// Download bytes together with the response `Content-Type`, which is how- /// the caller tells a raw file apart from a directory served as a ZIP.- pub async fn get_bytes_with_type(&self, uri: &str) -> Result<(Vec<u8>, Option<String>)> {let url = format!("{}/api/v1/content/download", self.base.base_url);let params = vec![("uri".to_string(), uri.to_string()),@@ -538,16 +532,10 @@ impl HttpClient {return Err(crate::base_client::api_error_from_body(&bytes, status));}- let content_type = response- .headers()- .get(reqwest::header::CONTENT_TYPE)- .and_then(|value| value.to_str().ok())- .map(|value| value.to_string());-response.bytes().await- .map(|b| (b.to_vec(), content_type))+ .map(|b| b.to_vec()).map_err(|e| Error::from_reqwest("Failed to read response bytes", e))}diff --git a/crates/ov_cli/src/commands/content.rs b/crates/ov_cli/src/commands/content.rsindex 44245049a..f78b0d534 100644--- a/crates/ov_cli/src/commands/content.rs+++ b/crates/ov_cli/src/commands/content.rs@@ -3,8 +3,8 @@ use crate::error::Result;use crate::output::OutputFormat;use serde_json::{Value, json};use std::collections::BTreeSet;-use std::io::{ErrorKind, Write};-use std::path::{Path, PathBuf};+use std::io::Write;+use std::path::Path;pub async fn read(client: &HttpClient,@@ -88,82 +88,33 @@ pub async fn reindex(Ok(())}-pub async fn get(client: &HttpClient, uri: &str, local_path: Option<&str>) -> Result<()> {- preflight_explicit_download_target(local_path)?;-- // The body has to arrive before the target can be named: a directory URI- // comes back as a ZIP, and that decides the `.zip` suffix.- let (bytes, content_type) = client.get_bytes_with_type(uri).await?;- let is_zip = content_type- .as_deref()- .is_some_and(|value| value.starts_with("application/zip"));-- let path = download_target(uri, local_path, is_zip);- let display = path.display().to_string();+pub async fn get(client: &HttpClient, uri: &str, local_path: &str) -> Result<()> {+ // Check if target path already exists+ let path = Path::new(local_path);if path.exists() {return Err(crate::error::Error::Client(format!(- "File already exists: {display}"+ "File already exists: {}",+ local_path)));}// Ensure parent directory existsif let Some(parent) = path.parent() {- if !parent.as_os_str().is_empty() {- std::fs::create_dir_all(parent)?;- }+ std::fs::create_dir_all(parent)?;}+ // Download file+ let bytes = client.get_bytes(uri).await?;+// Write to local file- let mut file = create_download_target(&path, &display)?;+ let mut file = create_download_target(path, local_path)?;file.write_all(&bytes)?;file.flush()?;- println!("Downloaded {} bytes to {}", bytes.len(), display);+ println!("Downloaded {} bytes to {}", bytes.len(), local_path);Ok(())}-/// Resolve where the downloaded bytes go.-///-/// A target that is an existing directory — or omitted, meaning the current-/// directory — receives a file named after the resource, so downloading a-/// folder lands a real `<name>.zip` instead of an extension-less file carrying-/// ZIP bytes. Any other path is used verbatim, which keeps-/// `ov get <uri> ./explicit-name.zip` working as documented.-fn download_target(uri: &str, local_path: Option<&str>, is_zip: bool) -> PathBuf {- let base = Path::new(local_path.unwrap_or("."));- if local_path.is_some() && !base.is_dir() {- return base.to_path_buf();- }-- let mut name = uri- .trim_end_matches('/')- .rsplit('/')- .find(|segment| !segment.is_empty() && !segment.ends_with(':'))- .unwrap_or("download")- .to_string();- if is_zip && !name.ends_with(".zip") {- name.push_str(".zip");- }- base.join(name)-}--fn preflight_explicit_download_target(local_path: Option<&str>) -> Result<()> {- let Some(local_path) = local_path else {- return Ok(());- };- let path = Path::new(local_path);- if path.is_dir() {- return Ok(());- }- match path.symlink_metadata() {- Ok(_) => Err(crate::error::Error::Client(format!(- "File already exists: {local_path}"- ))),- Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),- Err(error) => Err(error.into()),- }-}-fn create_download_target(path: &Path, local_path: &str) -> Result<std::fs::File> {std::fs::OpenOptions::new().write(true)@@ -285,83 +236,6 @@ mod tests {output}- #[test]- fn download_target_names_archives_inside_a_directory() {- use super::download_target;- use std::path::PathBuf;-- let dir = tempfile::tempdir().expect("tempdir");- let dir_path = dir.path().to_str().expect("utf-8 tempdir");-- // Omitted target -> current directory, named after the resource.- assert_eq!(- download_target("viking://resources/myfolder", None, true),- PathBuf::from("./myfolder.zip")- );- assert_eq!(- download_target("viking://resources/logo.png", None, false),- PathBuf::from("./logo.png")- );- // Existing directory -> the archive lands inside it, not over it.- assert_eq!(- download_target("viking://resources/myfolder", Some(dir_path), true),- dir.path().join("myfolder.zip")- );- // An explicit non-directory path stays verbatim.- assert_eq!(- download_target("viking://resources/myfolder", Some("./explicit.zip"), true),- PathBuf::from("./explicit.zip")- );- // Already-suffixed names are not doubled up.- assert_eq!(- download_target("viking://resources/bundle.zip", None, true),- PathBuf::from("./bundle.zip")- );- // Scope-only URIs still produce a usable name.- assert_eq!(- download_target("viking://", None, true),- PathBuf::from("./download.zip")- );- }-- #[test]- fn preflight_rejects_explicit_existing_file_target() {- let dir = tempfile::tempdir().expect("tempdir should be created");- let path = dir.path().join("result.bin");- std::fs::write(&path, b"existing").expect("fixture should be written");-- let error = super::preflight_explicit_download_target(Some(&path.to_string_lossy()))- .expect_err("explicit existing file targets must fail before download");-- assert!(matches!(error, Error::Client(_)));- }-- #[test]- fn preflight_allows_directory_or_missing_explicit_targets() {- let dir = tempfile::tempdir().expect("tempdir should be created");- let missing_path = dir.path().join("new-result.bin");-- super::preflight_explicit_download_target(None).expect("omitted target is named later");- super::preflight_explicit_download_target(Some(&missing_path.to_string_lossy()))- .expect("missing explicit file target is usable");- super::preflight_explicit_download_target(Some(&dir.path().to_string_lossy()))- .expect("directory target is named after the response type");- }-- #[cfg(unix)]- #[test]- fn preflight_rejects_broken_symlink_target() {- let dir = tempfile::tempdir().expect("tempdir should be created");- let link_path = dir.path().join("broken-link");- std::os::unix::fs::symlink(dir.path().join("missing-target"), &link_path)- .expect("symlink fixture should be created");-- let error = super::preflight_explicit_download_target(Some(&link_path.to_string_lossy()))- .expect_err("existing symlink targets must fail before download");-- assert!(matches!(error, Error::Client(_)));- }-#[test]fn table_output_renders_profiled_scalar_content() {let result = json!({diff --git a/crates/ov_cli/src/error.rs b/crates/ov_cli/src/error.rsindex 65950a42a..d449d60a3 100644--- a/crates/ov_cli/src/error.rs+++ b/crates/ov_cli/src/error.rs@@ -123,7 +123,6 @@ fn code_from_http_status(status: Option<u16>) -> &'static str {Some(404) => "NOT_FOUND",Some(409) => "CONFLICT",Some(412) => "FAILED_PRECONDITION",- Some(413) => "PAYLOAD_TOO_LARGE",Some(429) => "RESOURCE_EXHAUSTED",Some(502 | 503) => "UNAVAILABLE",Some(504) => "DEADLINE_EXCEEDED",diff --git a/crates/ov_cli/src/handlers.rs b/crates/ov_cli/src/handlers.rsindex c61703cf3..d5ac097f3 100644--- a/crates/ov_cli/src/handlers.rs+++ b/crates/ov_cli/src/handlers.rs@@ -1377,9 +1377,9 @@ pub async fn handle_reindex(.await}-pub async fn handle_get(uri: String, local_path: Option<String>, ctx: CliContext) -> Result<()> {+pub async fn handle_get(uri: String, local_path: String, ctx: CliContext) -> Result<()> {let client = ctx.get_client();- commands::content::get(&client, &uri, local_path.as_deref()).await+ commands::content::get(&client, &uri, &local_path).await}pub async fn handle_find(diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rsindex a5895f2c7..7aa6ff826 100644--- a/crates/ov_cli/src/main.rs+++ b/crates/ov_cli/src/main.rs@@ -678,15 +678,14 @@ enum Commands {#[arg(long, default_value = "false")]recursive: bool,},- /// [Data] Download a file, or archive a directory as ZIP, to a local path+ /// [Data] Download file to local path (supports binaries/images)Get {/// Viking URI#[arg(value_name = "uri")]uri: String,- /// Destination directory, or an exact file path. A directory (the- /// default: the current one) receives <name>.zip for a directory URI.+ /// Local path (must not exist yet)#[arg(value_name = "local-path")]- local_path: Option<String>,+ local_path: String,},/// [Data] Run semantic retrievalFind {diff --git a/docs/en/api/01-overview.md b/docs/en/api/01-overview.mdindex 523af51e4..fb9e31373 100644--- a/docs/en/api/01-overview.md+++ b/docs/en/api/01-overview.md@@ -321,7 +321,6 @@ JSON output - error:| `UNAUTHENTICATED` | 401 | Missing or invalid API key || `PERMISSION_DENIED` | 403 | Insufficient permissions || `RESOURCE_EXHAUSTED` | 429 | Rate limit exceeded |-| `PAYLOAD_TOO_LARGE` | 413 | Response body exceeds a hard size limit (not retryable) || `FAILED_PRECONDITION` | 412 | Precondition failed || `CONFLICT` | 409 | Operation conflicts with an in-progress task or existing state || `DEADLINE_EXCEEDED` | 504 | Operation timed out |@@ -376,7 +375,7 @@ This catalog follows the routes actually mounted by the server. Each group headi| GET | `/api/v1/content/read` | Read full content (L2) || GET | `/api/v1/content/abstract` | Read an abstract (L0) || GET | `/api/v1/content/overview` | Read an overview (L1) |-| GET | `/api/v1/content/download` | Download original file bytes, or a directory as a ZIP archive |+| GET | `/api/v1/content/download` | Download original file bytes || POST | `/api/v1/content/write` | Write content and refresh semantic indexes || POST | `/api/v1/content/batch-write` | Apply preconditioned multi-file writes || POST | `/api/v1/content/set_tags` | Set retrieval tags |diff --git a/docs/en/api/12-content.md b/docs/en/api/12-content.mdindex 25f540c1a..89f21e121 100644--- a/docs/en/api/12-content.md+++ b/docs/en/api/12-content.md@@ -433,11 +433,11 @@ The TypeScript and Go SDKs and the CLI do not currently expose batch write direc### download()-Download a file as raw bytes, or a directory as a ZIP archive. File responses use `application/octet-stream`; directory responses use `application/zip` and preserve the downloaded directory as the archive root. Both return the filename through `Content-Disposition`. Directory downloads are limited to 10 MiB of source content, 10 MiB final ZIP size, and 10,000 tree entries; oversized archives return `PAYLOAD_TOO_LARGE` (HTTP `413`). That failure is a property of the directory, not a rate limit, so it is permanent for that URI — do not retry it.+Download a file as raw bytes. This is intended for images, PDFs, and other non-text content. The response uses `application/octet-stream` and returns the filename through `Content-Disposition`.| Parameter | Type | Required | Description ||-----------|------|----------|-------------|-| `uri` | string | Yes | File or directory URI to download |+| `uri` | string | Yes | File URI to download |**HTTP API**@@ -450,10 +450,6 @@ curl --get http://localhost:1933/api/v1/content/download \-H "X-API-Key: your-key" \--data-urlencode "uri=viking://resources/images/logo.png" \--output logo.png--# A directory is returned as a ZIP archive.-# Writes ./project.zip, named after the directory.-ov get viking://resources/project```**Response**@@ -468,7 +464,7 @@ Content-Disposition: attachment; filename*=UTF-8''logo.png<binary body>```-The CLI exposes the same endpoint through `ov get <uri> [local-path]`. When the local path is an existing directory — or omitted, which means the current directory — the download is written inside it under the resource's own name, with a `.zip` suffix for a directory URI. Any other local path is used verbatim, and must not already exist. The CLI never extracts the archive.+The public SDKs and CLI do not currently expose a dedicated raw-byte download method, so this section shows only the HTTP tab.---diff --git a/docs/en/context-compilation/03-knowledge-graph.md b/docs/en/context-compilation/03-knowledge-graph.mdindex 61dd8d629..ff3d56651 100644--- a/docs/en/context-compilation/03-knowledge-graph.md+++ b/docs/en/context-compilation/03-knowledge-graph.md@@ -63,14 +63,24 @@ ov read viking://resources/journal-kg/entities/sun-wukong.mdUnlike the LLM Wiki script, `knowledge_graph.py` reads from a **local directory** (it needs both `entities/` and `relations.jsonl` on disk). So download the output first, then generate the HTML.-Download the whole artifact tree as a ZIP archive, then extract it:+Download the whole artifact tree. `ov get` downloads one file at a time; combine it with `ov ls -r -s` to pull every path:```bash-ov get viking://resources/journal-kg-unzip -o ./journal-kg.zip+SRC="viking://resources/journal-kg"+DST="./journal-kg"+mkdir -p "$DST"+ov ls -r -s "$SRC" | while read -r uri; do+ # only download files (entities/*.md and relations.jsonl), skip directories+ case "$uri" in+ */entities|"$SRC") continue ;;+ esac+ rel="${uri#$SRC/}"+ mkdir -p "$DST/$(dirname "$rel")"+ ov get "$uri" "$DST/$rel"+done```-> `ov get` writes `./journal-kg.zip`, named after the directory. It refuses to overwrite, so remove the old archive (`rm -f ./journal-kg.zip`) before re-downloading. `unzip -o` overwrites the previously extracted `./journal-kg/` instead of prompting.+> `ov get` requires the local target path to not exist yet, so clear the old directory (`rm -rf ./journal-kg`) before re-downloading.Confirm the local layout is correct:diff --git a/docs/zh/api/01-overview.md b/docs/zh/api/01-overview.mdindex 85fb49c23..278039a87 100644--- a/docs/zh/api/01-overview.md+++ b/docs/zh/api/01-overview.md@@ -316,7 +316,6 @@ JSON 输出 - 错误:| `UNAUTHENTICATED` | 401 | 缺少或无效的 API Key || `PERMISSION_DENIED` | 403 | 权限不足 || `RESOURCE_EXHAUSTED` | 429 | 超出速率限制 |-| `PAYLOAD_TOO_LARGE` | 413 | 响应体超出硬性大小限制(不可重试) || `FAILED_PRECONDITION` | 412 | 前置条件不满足 || `CONFLICT` | 409 | 操作与正在进行的任务或已有状态冲突 || `DEADLINE_EXCEEDED` | 504 | 操作超时 |@@ -371,7 +370,7 @@ JSON 输出 - 错误:| GET | `/api/v1/content/read` | 读取完整内容(L2) || GET | `/api/v1/content/abstract` | 读取摘要(L0) || GET | `/api/v1/content/overview` | 读取概览(L1) |-| GET | `/api/v1/content/download` | 下载原始文件字节,或将目录打包为 ZIP 归档 |+| GET | `/api/v1/content/download` | 下载原始文件字节 || POST | `/api/v1/content/write` | 写入内容并刷新语义索引 || POST | `/api/v1/content/batch-write` | 执行带前置条件的多文件写入 || POST | `/api/v1/content/set_tags` | 设置检索标签 |diff --git a/docs/zh/api/12-content.md b/docs/zh/api/12-content.mdindex 7bac00334..21c052ff6 100644--- a/docs/zh/api/12-content.md+++ b/docs/zh/api/12-content.md@@ -433,11 +433,11 @@ TypeScript、Go SDK 和 CLI 当前不直接暴露 batch write。### download()-以原始字节流下载文件,或将目录打包成 ZIP 下载。文件响应使用 `application/octet-stream`;目录响应使用 `application/zip`,并以所下载目录作为压缩包根目录。两者都会通过 `Content-Disposition` 返回文件名。目录下载的源文件总大小、最终 ZIP 大小均限制为 10 MiB,且最多包含 10,000 个目录树条目;超限返回 `PAYLOAD_TOO_LARGE`(HTTP `413`)。该失败取决于目录本身而非速率限制,对同一 URI 是永久性的,不要重试。+以原始字节流下载文件,适用于图片、PDF 和其他非文本内容。响应使用 `application/octet-stream`,并通过 `Content-Disposition` 返回文件名。| 参数 | 类型 | 必填 | 说明 ||------|------|------|------|-| `uri` | string | 是 | 要下载的文件或目录 URI |+| `uri` | string | 是 | 要下载的文件 URI |**HTTP API**@@ -450,10 +450,6 @@ curl --get http://localhost:1933/api/v1/content/download \-H "X-API-Key: your-key" \--data-urlencode "uri=viking://resources/images/logo.png" \--output logo.png--# 目录会以 ZIP 压缩包返回-# 写出 ./project.zip,以目录名命名-ov get viking://resources/project```**响应**@@ -468,7 +464,7 @@ Content-Disposition: attachment; filename*=UTF-8''logo.png<binary body>```-CLI 通过 `ov get <uri> [local-path]` 调用同一接口。当本地路径是已存在的目录、或省略(即当前目录)时,文件会以资源自身的名字写入该目录下,目录 URI 会带上 `.zip` 后缀。其他本地路径按原样使用,且必须尚不存在。CLI 不会自动解压。+公共 SDK 和 CLI 当前没有独立的原始字节下载方法,因此本节只展示 HTTP Tab。---diff --git a/docs/zh/context-compilation/03-knowledge-graph.md b/docs/zh/context-compilation/03-knowledge-graph.mdindex d3c67d666..d2531ca12 100644--- a/docs/zh/context-compilation/03-knowledge-graph.md+++ b/docs/zh/context-compilation/03-knowledge-graph.md@@ -63,14 +63,24 @@ ov read viking://resources/journal-kg/entities/孙悟空.md与 LLM Wiki 的脚本不同,`knowledge_graph.py` 读取的是**本地目录**(需要 `entities/` 和 `relations.jsonl` 都在本地)。所以先把产物拉到本地,再生成 HTML。-先把整棵工件树打包成 ZIP 下载,再解压:+先把整棵工件树下载下来。`ov get` 一次下载一个文件,配合 `ov ls -r -s` 列出全部路径即可批量拉取:```bash-ov get viking://resources/journal-kg-unzip -o ./journal-kg.zip+SRC="viking://resources/journal-kg"+DST="./journal-kg"+mkdir -p "$DST"+ov ls -r -s "$SRC" | while read -r uri; do+ # 只下载文件(entities/*.md 和 relations.jsonl),跳过目录+ case "$uri" in+ */entities|"$SRC") continue ;;+ esac+ rel="${uri#$SRC/}"+ mkdir -p "$DST/$(dirname "$rel")"+ ov get "$uri" "$DST/$rel"+done```-> `ov get` 会写出 `./journal-kg.zip`,以目录名命名。它不会覆盖已有文件,所以重新下载前先删除旧压缩包(`rm -f ./journal-kg.zip`)。`unzip -o` 会直接覆盖上次解压出的 `./journal-kg/`,不会停下来等确认。+> `ov get` 要求本地目标路径尚不存在,所以重新下载前先清掉旧目录(`rm -rf ./journal-kg`)。确认本地目录结构正确:diff --git a/openviking/server/app.py b/openviking/server/app.pyindex d4112ee68..33355abcb 100644--- a/openviking/server/app.py+++ b/openviking/server/app.py@@ -185,7 +185,6 @@ _FRAMEWORK_HTTP_STATUS_TO_ERROR_CODE = {403: "PERMISSION_DENIED",404: "NOT_FOUND",409: "CONFLICT",- 413: "PAYLOAD_TOO_LARGE",422: "INVALID_ARGUMENT",429: "RESOURCE_EXHAUSTED",502: "UNAVAILABLE",diff --git a/openviking/server/models.py b/openviking/server/models.pyindex 5e182e1c7..67c08cf53 100644--- a/openviking/server/models.py+++ b/openviking/server/models.py@@ -36,7 +36,6 @@ ERROR_CODE_TO_HTTP_STATUS = {"PERMISSION_DENIED": 403,"UNAUTHENTICATED": 401,"RESOURCE_EXHAUSTED": 429,- "PAYLOAD_TOO_LARGE": 413,"FAILED_PRECONDITION": 412,"ABORTED": 409,"DEADLINE_EXCEEDED": 504,diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.pyindex 9a8b48044..4912deeb1 100644--- a/openviking/server/routers/content.py+++ b/openviking/server/routers/content.py@@ -2,10 +2,6 @@# SPDX-License-Identifier: AGPL-3.0"""Content endpoints for OpenViking HTTP Server."""-import asyncio-import io-import zipfile-from pathlib import PurePosixPathfrom typing import Literalfrom urllib.parse import quote@@ -32,117 +28,11 @@ from openviking.server.identity import RequestContext, Rolefrom openviking.server.models import Responsefrom openviking.server.telemetry import run_operationfrom openviking.telemetry import TelemetryRequest-from openviking_cli.exceptions import (- InvalidArgumentError,- NotFoundError,- PayloadTooLargeError,- PermissionDeniedError,-)+from openviking_cli.exceptions import InvalidArgumentError, NotFoundError, PermissionDeniedErrorfrom openviking_cli.utils import get_loggerlogger = get_logger(__name__)-_DIRECTORY_ARCHIVE_MAX_BYTES = 10 * 1024 * 1024-_DIRECTORY_ARCHIVE_MAX_ENTRIES = 10_000---def _archive_size_limit_error(uri: str) -> PayloadTooLargeError:- # 413, not 429: the limit is a property of the directory, so retrying the- # same URI can only fail again. A 429 would send clients into backoff-retry.- return PayloadTooLargeError(- f"Directory archive exceeds the {_DIRECTORY_ARCHIVE_MAX_BYTES}-byte download limit: {uri}"- )---def _archive_entry_limit_error(uri: str) -> PayloadTooLargeError:- return PayloadTooLargeError(- f"Directory archive exceeds the {_DIRECTORY_ARCHIVE_MAX_ENTRIES}-entry download limit: {uri}"- )---def _safe_archive_path(root_name: str, rel_path: str = "") -> str:- """Build a portable ZIP member path from server-produced tree entries."""- relative = PurePosixPath(rel_path)- if relative.is_absolute():- raise InvalidArgumentError(f"Unsafe path in directory download: {rel_path}")- parts = [root_name, *relative.parts]- if any(part in {"", ".", ".."} or "\\" in part for part in parts):- raise InvalidArgumentError(f"Unsafe path in directory download: {rel_path or root_name}")- return "/".join(parts)---async def _build_directory_archive(- service, uri: str, stat: dict, ctx: RequestContext-) -> tuple[bytes, str]:- """Pack one visible Viking directory tree into an in-memory ZIP archive.-- The archive is capped at `_DIRECTORY_ARCHIVE_MAX_BYTES`, so it is built in- memory rather than in a temp file: a temp file served through FileResponse- outlives the request whenever the response never reaches its cleanup- callback (a malformed or unsatisfiable Range header returns early, and- cancellation skips it entirely).- """- root_name = str(stat.get("name") or uri.rstrip("/").rsplit("/", 1)[-1] or "download")- root_path = _safe_archive_path(root_name)-- entries = await service.fs.tree(- uri,- ctx=ctx,- output="original",- show_all_hidden=True,- node_limit=_DIRECTORY_ARCHIVE_MAX_ENTRIES + 1,- level_limit=None,- )- if len(entries) > _DIRECTORY_ARCHIVE_MAX_ENTRIES:- raise _archive_entry_limit_error(uri)-- # Reject oversized trees before loading file contents into memory. The- # ZIP itself is checked below as well because headers can push a highly- # fragmented archive over the limit even when its files do not.- declared_total = 0- for entry in entries:- if entry.get("isDir", False):- continue- size = entry.get("size")- if isinstance(size, bool) or not isinstance(size, int) or size < 0:- file_stat = await service.fs.stat(str(entry["uri"]), ctx=ctx)- size = file_stat.get("size")- if isinstance(size, bool) or not isinstance(size, int) or size < 0:- raise InvalidArgumentError(- f"Cannot determine file size for directory download: {entry['uri']}"- )- declared_total += size- if declared_total > _DIRECTORY_ARCHIVE_MAX_BYTES:- raise _archive_size_limit_error(uri)-- buffer = io.BytesIO()- actual_total = 0- with zipfile.ZipFile(- buffer,- mode="w",- compression=zipfile.ZIP_DEFLATED,- allowZip64=True,- ) as archive:- archive.writestr(f"{root_path}/", b"")- for entry in entries:- member_path = _safe_archive_path(root_name, str(entry["rel_path"]))- if entry.get("isDir", False):- archive.writestr(f"{member_path.rstrip('/')}/", b"")- else:- content = await service.fs.read_file_bytes(str(entry["uri"]), ctx=ctx)- actual_total += len(content)- if actual_total > _DIRECTORY_ARCHIVE_MAX_BYTES:- raise _archive_size_limit_error(uri)- await asyncio.to_thread(archive.writestr, member_path, content)- # Bound the buffer while it grows. Per-entry ZIP headers are not- # counted by `actual_total`, so a tree of many empty directories or- # empty files would otherwise only be rejected once fully built.- if buffer.tell() > _DIRECTORY_ARCHIVE_MAX_BYTES:- raise _archive_size_limit_error(uri)- if buffer.tell() > _DIRECTORY_ARCHIVE_MAX_BYTES:- raise _archive_size_limit_error(uri)- return buffer.getvalue(), f"{root_name}.zip"-class WriteContentRequest(BaseModel):"""Request to write, append, or create text content to a file."""@@ -301,27 +191,10 @@ async def download(uri: str = Query(..., description="Viking URI"),_ctx: RequestContext = Depends(get_request_context),):- """Download a file as raw bytes or a directory as a ZIP archive."""+ """Download file as raw bytes (for images, binaries, etc.)."""service = get_service()uri = validate_request_viking_uri(resolve_path_variables(uri), _ctx)try:- stat = await service.fs.stat(uri, ctx=_ctx)- if stat.get("isDir", False):- archive, filename = await _build_directory_archive(service, uri, stat, _ctx)- # Buffering the whole archive is only safe because of the 10 MiB- # cap in _build_directory_archive; directories over it fail with- # 413 rather than downloading. To raise that ceiling, stream from a- # temp file with starlette.responses.FileResponse instead — but- # note that a BackgroundTask cleanup is NOT sufficient on its own:- # starlette skips `background` on the Range-header error paths and- # on cancellation, so the temp archive leaks. Whatever replaces- # this must own its own cleanup (e.g. unlink the file as soon as it- # is opened, and stream from the surviving descriptor).- return FastAPIResponse(- content=archive,- media_type="application/zip",- headers={"Content-Disposition": f"attachment; filename*=UTF-8''{quote(filename)}"},- )content = await service.fs.read_file_bytes(uri, ctx=_ctx)except AGFSNotFoundError:raise NotFoundError(uri, "file")@@ -331,7 +204,14 @@ async def download(raise mapped from eraise- filename = stat.get("name", "download")+ # Try to get filename from stat+ filename = "download"+ try:+ stat = await service.fs.stat(uri, ctx=_ctx)+ if stat and "name" in stat:+ filename = stat["name"]+ except Exception:+ passfilename = quote(filename)return FastAPIResponse(content=content,diff --git a/openviking/utils/exceptions.py b/openviking/utils/exceptions.pyindex 9376cbd90..d3b8444b2 100644--- a/openviking/utils/exceptions.py+++ b/openviking/utils/exceptions.py@@ -10,7 +10,6 @@ HTTP_STATUS_TO_ERROR_CODE = {404: "NOT_FOUND",408: "DEADLINE_EXCEEDED",409: "CONFLICT",- 413: "PAYLOAD_TOO_LARGE",422: "INVALID_ARGUMENT",429: "RESOURCE_EXHAUSTED",500: "UNAVAILABLE",diff --git a/openviking_cli/client/_http_compat.py b/openviking_cli/client/_http_compat.pyindex b8e960bfc..d9fe260b5 100644--- a/openviking_cli/client/_http_compat.py+++ b/openviking_cli/client/_http_compat.py@@ -25,7 +25,6 @@ from openviking_cli.exceptions import (NotFoundError,NotInitializedError,OpenVikingError,- PayloadTooLargeError,PermissionDeniedError,ProcessingError,ResourceExhaustedError,@@ -50,7 +49,6 @@ ERROR_CODE_TO_EXCEPTION = {"UNAUTHENTICATED": UnauthenticatedError,"PERMISSION_DENIED": PermissionDeniedError,"RESOURCE_EXHAUSTED": ResourceExhaustedError,- "PAYLOAD_TOO_LARGE": PayloadTooLargeError,"UNAVAILABLE": UnavailableError,"INTERNAL": InternalError,"DEADLINE_EXCEEDED": DeadlineExceededError,@@ -92,7 +90,6 @@ def _raise_legacy_exception(error: Dict[str, Any]) -> None:if exc_class in (InvalidArgumentError,FailedPreconditionError,- PayloadTooLargeError,ResourceExhaustedError,AbortedError,UnimplementedError,diff --git a/openviking_cli/exceptions.py b/openviking_cli/exceptions.pyindex 87db9fb9f..9305a2c6d 100644--- a/openviking_cli/exceptions.py+++ b/openviking_cli/exceptions.py@@ -136,17 +136,6 @@ class ResourceExhaustedError(OpenVikingError):super().__init__(message, code="RESOURCE_EXHAUSTED", details=details)-class PayloadTooLargeError(OpenVikingError):- """Response or request body exceeds a hard size limit.-- Distinct from ResourceExhaustedError: this failure is permanent for the- given input, so clients must not retry it the way they back off on 429.- """-- def __init__(self, message: str = "Payload too large", details: Optional[dict] = None):- super().__init__(message, code="PAYLOAD_TOO_LARGE", details=details)--class InternalError(OpenVikingError):"""Internal server error."""diff --git a/tests/server/test_api_fs_content_endpoint_suite.py b/tests/server/test_api_fs_content_endpoint_suite.pyindex 42edabc3e..f7c6137bd 100644--- a/tests/server/test_api_fs_content_endpoint_suite.py+++ b/tests/server/test_api_fs_content_endpoint_suite.py@@ -1,16 +1,11 @@# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.# SPDX-License-Identifier: AGPL-3.0-import io-import zipfilefrom types import SimpleNamespaceimport httpx-import pytestfrom openviking.pyagfs.exceptions import AGFSHTTPError-from openviking.server.models import ERROR_CODE_TO_HTTP_STATUS-from openviking.server.routers import content as content_routerdef _assert_error(@@ -169,197 +164,6 @@ async def test_download_returns_attachment_response(client_with_resource):assert "attachment;" in response.headers["content-disposition"]-async def test_download_directory_returns_zip_archive(client_with_resource):- client, uri = client_with_resource- response = await client.get("/api/v1/content/download", params={"uri": uri})-- assert response.status_code == 200- assert response.headers["content-type"] == "application/zip"- assert ".zip" in response.headers["content-disposition"]-- with zipfile.ZipFile(io.BytesIO(response.content)) as archive:- names = archive.namelist()- assert names- root = names[0].rstrip("/")- assert names[0].endswith("/")- assert any(name.startswith(f"{root}/") and not name.endswith("/") for name in names)- assert all(not name.startswith("/") and ".." not in name.split("/") for name in names)---async def test_download_directory_over_limit_returns_payload_too_large(- client_with_resource, monkeypatch-):- client, uri = client_with_resource- monkeypatch.setattr(content_router, "_DIRECTORY_ARCHIVE_MAX_BYTES", 1)-- response = await client.get("/api/v1/content/download", params={"uri": uri})-- _assert_error(response, status_code=413, error_code="PAYLOAD_TOO_LARGE")---async def test_build_directory_archive_preserves_tree_and_empty_directories():- class FakeFS:- async def tree(self, *args, **kwargs):- return [- {"uri": "viking://resources/project/empty", "rel_path": "empty", "isDir": True},- {- "uri": "viking://resources/project/docs/readme.md",- "rel_path": "docs/readme.md",- "isDir": False,- "size": 5,- },- ]-- async def read_file_bytes(self, uri, ctx):- assert uri.endswith("docs/readme.md")- return b"hello"-- service = SimpleNamespace(fs=FakeFS())- payload, filename = await content_router._build_directory_archive(- service,- "viking://resources/project",- {"name": "project", "isDir": True},- SimpleNamespace(),- )- assert filename == "project.zip"- with zipfile.ZipFile(io.BytesIO(payload)) as archive:- assert archive.namelist() == [- "project/",- "project/empty/",- "project/docs/readme.md",- ]- assert archive.read("project/docs/readme.md") == b"hello"---async def test_build_directory_archive_rejects_declared_size_over_limit():- class FakeFS:- async def tree(self, *args, **kwargs):- return [- {- "uri": "viking://resources/project/large.bin",- "rel_path": "large.bin",- "isDir": False,- "size": content_router._DIRECTORY_ARCHIVE_MAX_BYTES + 1,- }- ]-- async def read_file_bytes(self, *args, **kwargs):- raise AssertionError("oversized file must not be read")-- service = SimpleNamespace(fs=FakeFS())- with pytest.raises(Exception, match="exceeds the .* download limit"):- await content_router._build_directory_archive(- service,- "viking://resources/project",- {"name": "project", "isDir": True},- SimpleNamespace(),- )---async def test_build_directory_archive_rejects_too_many_entries_before_reading():- observed = {}-- class FakeFS:- async def tree(self, *args, **kwargs):- observed["node_limit"] = kwargs.get("node_limit")- return [- {- "uri": f"viking://resources/project/d{index}",- "rel_path": f"d{index}",- "isDir": True,- "size": 0,- }- for index in range(content_router._DIRECTORY_ARCHIVE_MAX_ENTRIES + 1)- ]-- async def read_file_bytes(self, *args, **kwargs):- raise AssertionError("entry cap must fail before reading file contents")-- service = SimpleNamespace(fs=FakeFS())- with pytest.raises(Exception, match="entry download limit"):- await content_router._build_directory_archive(- service,- "viking://resources/project",- {"name": "project", "isDir": True},- SimpleNamespace(),- )-- assert observed["node_limit"] == content_router._DIRECTORY_ARCHIVE_MAX_ENTRIES + 1---async def test_build_directory_archive_rejects_zip_over_limit(monkeypatch):- class FakeFS:- async def tree(self, *args, **kwargs):- return [- {- "uri": "viking://resources/project/empty",- "rel_path": "empty",- "isDir": True,- "size": 0,- }- ]-- monkeypatch.setattr(content_router, "_DIRECTORY_ARCHIVE_MAX_BYTES", 1)- service = SimpleNamespace(fs=FakeFS())- with pytest.raises(Exception, match="exceeds the 1-byte download limit"):- await content_router._build_directory_archive(- service,- "viking://resources/project",- {"name": "project", "isDir": True},- SimpleNamespace(),- )---async def test_build_directory_archive_stops_writing_once_over_limit(monkeypatch):- """Header-only entries must not grow the archive buffer past the limit."""- limit = 4096- peak = {"bytes": 0}-- class FakeFS:- async def tree(self, *args, **kwargs):- return [- {- "uri": f"viking://resources/project/d{index}",- "rel_path": f"d{index}",- "isDir": True,- }- for index in range(20000)- ]-- class TrackedBytesIO(io.BytesIO):- def write(self, data):- count = super().write(data)- peak["bytes"] = max(peak["bytes"], self.tell())- return count-- monkeypatch.setattr(content_router, "_DIRECTORY_ARCHIVE_MAX_BYTES", limit)- monkeypatch.setattr(content_router, "io", SimpleNamespace(BytesIO=TrackedBytesIO))-- with pytest.raises(Exception, match="download limit"):- await content_router._build_directory_archive(- SimpleNamespace(fs=FakeFS()),- "viking://resources/project",- {"name": "project", "isDir": True},- SimpleNamespace(),- )-- # Without the in-loop guard this reaches ~1.9 MB for 20k entries.- assert peak["bytes"] < limit * 4---def test_archive_size_limit_error_is_not_retryable():- """Oversize is a property of the directory, so it must be 413, not 429."""- error = content_router._archive_size_limit_error("viking://resources/big")- assert error.code == "PAYLOAD_TOO_LARGE"- assert ERROR_CODE_TO_HTTP_STATUS[error.code] == 413--[email protected]("path", ["../escape", "/absolute", r"..\\escape"])-def test_safe_archive_path_rejects_unsafe_members(path):- with pytest.raises(Exception, match="Unsafe path"):- content_router._safe_archive_path("project", path)--async def test_download_missing_uri_returns_not_found(app, service, monkeypatch):async def fake_read_file_bytes(*args, **kwargs):raise FileNotFoundError("download target missing")