fix(worker): 单次任务固定同一 WeKnora 连接

TargetIdentity 与业务调用原先各自 Resolve:身份核验通过后,上传/轮询/验收/
移除的后续调用仍可能在下一次解析时切到新实例。现在 BeginTask 只解析一次,
用同一连接读取对象租户并把该不可变连接钉进返回 context;后续上传、列库恢复、
查询、chunk 读取、删除与删除后重查全部复用它。

TaskConnector 优先于 IdentityProvider;仅实现 TargetIdentity 的窄假客户端仍可
工作,生产路径不会走双解析。新增假端点测试断言一次处理只解析一次、绑定后改
当前实例仍全部打到原实例;四个 handler 的 TaskConnector 假客户端断言所有业务
调用携带同一连接令牌,覆盖上传恢复和删除重查。
This commit is contained in:
姚章浩
2026-11-19 15:33:40 +08:00
parent 1f5bf72aec
commit 6ae5150fdf
10 changed files with 424 additions and 50 deletions
+25 -7
View File
@@ -6,24 +6,42 @@ import (
"knowledge-governance/internal/platform/weknora"
)
// IdentityProvider is implemented by the production WeKnora access client. Unit
// fakes may omit it; a handler must then treat stored identities as unverifiable
// and never write/delete/adopt an object across instances.
// TaskConnector is implemented by the production client. BeginTask resolves the
// endpoint/key/binding exactly once, verifies the object's owning tenant through
// that connection and returns a context that pins the same immutable connection
// for every later call of this job (including recovery and read-back).
type TaskConnector interface {
BeginTask(context.Context, string) (context.Context, weknora.InstanceIdentity, error)
}
// IdentityProvider remains for narrow fakes/tests that only expose identity.
type IdentityProvider interface {
TargetIdentity(context.Context, string) (weknora.InstanceIdentity, error)
}
func resolveIdentity(ctx context.Context, client any, knowledgeBaseID string) (weknora.InstanceIdentity, bool, error) {
// resolveIdentity prefers BeginTask so upload/poll/validate/remove pass the
// pinned context to every follow-up call. Returning the original ctx for
// identity-only fakes keeps the unit seam simple; production never takes that
// path.
func resolveIdentity(ctx context.Context, client any, knowledgeBaseID string) (context.Context, weknora.InstanceIdentity, bool, error) {
if connector, ok := client.(TaskConnector); ok {
bound, identity, err := connector.BeginTask(ctx, knowledgeBaseID)
if err != nil {
return ctx, weknora.InstanceIdentity{}, true, err
}
identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL)
return bound, identity, true, nil
}
provider, ok := client.(IdentityProvider)
if !ok {
return weknora.InstanceIdentity{}, false, nil
return ctx, weknora.InstanceIdentity{}, false, nil
}
identity, err := provider.TargetIdentity(ctx, knowledgeBaseID)
if err != nil {
return weknora.InstanceIdentity{}, true, err
return ctx, weknora.InstanceIdentity{}, true, err
}
identity.BaseURL = weknora.NormalizeInstanceURL(identity.BaseURL)
return identity, true, nil
return ctx, identity, true, nil
}
// identityMatches requires BOTH the instance address and the owning tenant to