OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: replace 模式文件不存在时抛出 NotFoundError 而非创建文件

Open guiyinrenshi opened this issue 7 months ago • 4 comments

Bug Description

调用 PUT /api/v1/content/write 并指定 mode=replace 时,如果目标 URI 对应的文件不存在,API 返回 404 NotFoundError,而不是创建该文件。

期望行为(也是 mode=create 的行为)是:文件不存在时应该创建文件,或者返回明确的 AlreadyExistsError,而不是 NotFoundError。

这导致 replace 模式无法实现常见的" upsert"(存在则更新,不存在则创建)模式。

Steps to Reproduce

from openviking.storage.content_write import ContentWriteCoordinator

# 生成一个确保不存在的 URI
file_uri = f"viking://user/memories/test-note-{int(time.time())}.md"

result = await coordinator.write(
    uri=file_uri,
    content="# Hello

World",
    ctx=ctx,
    mode="replace",   # ❌ 抛出 404 NotFoundError
    # mode="create",  # ✅ 正常创建
)

Expected: 在 viking://user/memories/test-note-<时间戳>.md 创建文件
Actual: 404 NotFoundError: viking://user/memories/test-note-<时间戳>.md

Environment

  • Version: 0.3.12
  • Python: 3.13
  • OS: Linux

Additional Context

openviking/storage/content_write.py 第 67–69 行:

stat = await self._safe_stat(normalized_uri, ctx=ctx)
if stat.get("isDir"):
    raise InvalidArgumentError(f"write only supports existing files, got directory: {uri}")
# ❌ 缺少: if stat.get("not_found"): ...处理创建逻辑...

对比 _create_and_write 方法(第 205–207 行),该方法正确处理了 not_found 的情况:

stat = await self._safe_stat(uri, ctx=ctx, allow_not_found=True)
if not stat.get("not_found"):
    raise AlreadyExistsError(uri, "file")  # 文件已存在 → 报错
# ✅ not_found → 继续执行创建逻辑

修复建议:在 replace 分支添加 not_found 检查,降级调用 _create_and_write:

stat = await self._safe_stat(normalized_uri, ctx=ctx)
if stat.get("isDir"):
    raise InvalidArgumentError(f"write only supports existing files, got directory: {uri}")

# 文件不存在时,降级为 create 模式(实现 upsert 语义)
if stat.get("not_found"):
    return await self._create_and_write(
        uri=normalized_uri,
        content=content,
        ctx=ctx,
        wait=wait,
        timeout=timeout,
    )

guiyinrenshi avatar Apr 25 '26 09:04 guiyinrenshi

Looking into the replace-mode write flow — I want to verify whether the not-found branch should fall back to the same create path used by create mode.

onthebed avatar Apr 27 '26 19:04 onthebed

I've been looking into this — I want to follow the replace-mode file path and see why a missing target throws instead of creating the file. Happy to send a fix.

onthebed avatar Apr 27 '26 22:04 onthebed

💬 我也在做记忆系统相关的工作:CausaMem,可以来看看,说不定有能借鉴的地方 🙂

MaiHHConnect avatar Apr 28 '26 01:04 MaiHHConnect

感谢提交这个 issue。

我看了当前实现和文档后,倾向于把这里维持为“replace 要求目标文件已存在”的语义,而不是把 replace 隐式改成 upsert。

依据是:

  • docs/zh/api/03-filesystem.md 当前写的是:replace 和 append 要求文件已存在;create 仅用于创建新文件,目标路径已存在时返回 409 Conflict。
  • ContentWriteCoordinator.write() 当前的分支也是这个模型:mode="create" 单独走 _create_and_write();非 create 模式会先 stat 目标文件,不存在时返回 NotFoundError。
  • _create_and_write() 还包含 create-only 的扩展名白名单、父目录解析,以及“已存在则返回 AlreadyExistsError”的语义。如果 replace 在目标不存在时直接降级为 create,会让请求 mode=replace 产生 mode=create 的行为和返回结果,也会改变已有 API 契约。

所以我认为当前 404 NotFoundError 符合现有 replace 语义。调用方如果需要 upsert,可以先检查目标是否存在:存在时用 mode="replace",不存在时用 mode="create"。

如果项目希望提供服务端 upsert,我建议新增显式 mode="upsert",并同步补充文档、SDK docstring 和测试,而不是把 replace 改成隐式 upsert。这样可以避免已有用户依赖“replace 必须命中已有文件”的行为被静默改变。

qin-ctx avatar Apr 30 '26 06:04 qin-ctx