Skip to content

bug(resource): add_resource fails with 'file exists' due to trailing slash and missing account context #846

@qin-ctx

Description

@qin-ctx

Bug Description

add_resource 在以下两种场景下会报 file exists 错误导致资源添加失败:

  1. to 参数带尾部斜杠:调用 add_resource(to="viking://resources/xxx/") 时,100% 首次添加即失败。
  2. 非默认账户下重复添加同名资源:第二次添加同名资源时必定失败(如连续两次 ov add-resource file.md)。

Steps to Reproduce

场景 1:

client.add_resource(path="file.md", to="viking://resources/target/")
# → Error: file exists

场景 2:

ov add-resource /path/to/file.md   # 成功
ov add-resource /path/to/file.md   # 失败: file exists

Expected Behavior

  • 场景 1:to 参数带不带尾部斜杠应行为一致,资源正常写入。
  • 场景 2:重复添加同名资源时应自动生成唯一名称(如 resource_1)或走增量更新路径。

Actual Behavior

两种场景均报错:

AGFSClientError: failed to rename: rename /path/temp/metadata /path/resources/target: file exists

Root Cause

Bug 1(尾部斜杠): resource_processor.py Phase 3.5 中计算 parent_uri 时,对带尾部 / 的 URI 做 rsplit("/", 1) 会将目标目录本身当作父目录。随后 mkdir(parent_uri) 预创建了 mv 的目标路径,导致 mv 失败。

Bug 2(账户上下文缺失): Phase 3.5 中 _resolve_unique_uri 调用位于 bind_request_context(ctx) 作用域之外,内部 stat 使用了默认账户(default)而非实际账户(如 coral),导致唯一性检查查询了错误的路径,未发现已有资源,返回了冲突的原始 URI。

Environment

  • OS: macOS
  • Python: 3.12
  • OpenViking: main branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions