fix(weknora): 上传带上已发现的 X-Tenant-ID,TENANT_REQUIRED 不再误判为去重冲突

平台级 Key 访问 0.9 的租户资源需要 X-Tenant-ID。GET 调用会在 409 后发现并重放,
但 multipart 上传既没有带上这个头、也无法重放请求体,于是每次上传都拿到
409 TENANT_REQUIRED,再被 handler 当成「已存在的同一份文件」去认领,最终
test.upload 连续失败、publication 停在 pending/uploading。

- UploadFile 复用 BeginTask 已发现的 workspace 头;
- 上传的 409 若携带 TENANT_REQUIRED,按普通 409 失败处理,不再返回 ErrConflict;
- test.upload 重试耗尽时把 publication 标为 failed,重试入口不再永远 409。
This commit is contained in:
Zhero & 助理
2026-12-03 18:03:32 +08:00
committed by 姚章浩
parent efa26813dd
commit 5c2ba3e06f
3 changed files with 135 additions and 2 deletions
+20 -2
View File
@@ -232,6 +232,13 @@ func (c *Client) UploadFile(ctx context.Context, request UploadRequest) (Knowled
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-API-Key", c.apiKey)
// A platform-scoped key is refused with 409 TENANT_REQUIRED until it names a
// workspace, and a multipart body cannot be replayed: the workspace has to be
// attached here, from the one BeginTask already discovered. Leaving it off
// turned every upload into a "conflict" that the handler read as a duplicate.
if tenant := c.currentTenant(); tenant != "" {
req.Header.Set("X-Tenant-ID", tenant)
}
resp, err := c.httpClient.Do(req)
if err != nil {
_ = pipeReader.CloseWithError(err)
@@ -245,8 +252,19 @@ func (c *Client) UploadFile(ctx context.Context, request UploadRequest) (Knowled
var response knowledgeResponse
if resp.StatusCode == http.StatusConflict {
if err := c.decodeBody(resp, &response); err != nil {
return Knowledge{}, err
payload, readErr := io.ReadAll(io.LimitReader(resp.Body, c.maxResponseBytes+1))
resp.Body.Close()
if readErr != nil {
return Knowledge{}, errors.New("read WeKnora response")
}
// TENANT_REQUIRED shares the 409 status with a real duplicate. Reading it
// as ErrConflict made the caller adopt a "duplicate" that never existed,
// so it is reported as a bare 409 and dead-letters with a clear reason.
if bytes.Contains(payload, []byte(`"TENANT_REQUIRED"`)) {
return Knowledge{}, &HTTPError{StatusCode: http.StatusConflict}
}
if err := json.Unmarshal(payload, &response); err != nil {
return Knowledge{}, errors.New("decode WeKnora response")
}
return response.Data, ErrConflict
}